refactor: migrate core import to dappco.re/go/core
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
b85319ae6b
commit
69464fe503
4 changed files with 25 additions and 21 deletions
|
|
@ -6,15 +6,18 @@ import (
|
|||
goio "io"
|
||||
"strings"
|
||||
|
||||
core "forge.lthn.ai/core/go-log"
|
||||
|
||||
framework "dappco.re/go/core"
|
||||
"github.com/ProtonMail/go-crypto/openpgp"
|
||||
"github.com/ProtonMail/go-crypto/openpgp/armor"
|
||||
"github.com/ProtonMail/go-crypto/openpgp/packet"
|
||||
|
||||
coreerr "forge.lthn.ai/core/go-log"
|
||||
framework "forge.lthn.ai/core/go/pkg/core"
|
||||
framework "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// Service implements the framework.Crypt interface using OpenPGP.
|
||||
// Service provides OpenPGP cryptographic operations.
|
||||
type Service struct {
|
||||
core *framework.Core
|
||||
}
|
||||
|
|
@ -59,19 +62,20 @@ func (s *Service) CreateKeyPair(name, passphrase string) (string, error) {
|
|||
return "", coreerr.E("openpgp.CreateKeyPair", "failed to create armor encoder", err)
|
||||
}
|
||||
|
||||
// Manual serialization to avoid panic from re-signing encrypted keys
|
||||
err = s.serializeEntity(w, entity)
|
||||
// Manual serialisation to avoid panic from re-signing encrypted keys
|
||||
err = serializeEntity(w, entity)
|
||||
if err != nil {
|
||||
w.Close()
|
||||
return "", coreerr.E("openpgp.CreateKeyPair", "failed to serialize private key", err)
|
||||
<<<<<<< HEAD
|
||||
return "", coreerr.E("openpgp.CreateKeyPair", "failed to serialise private key", err)
|
||||
}
|
||||
w.Close()
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// serializeEntity manually serializes an OpenPGP entity to avoid re-signing.
|
||||
func (s *Service) serializeEntity(w goio.Writer, e *openpgp.Entity) error {
|
||||
// serializeEntity manually serialises an OpenPGP entity to avoid re-signing.
|
||||
func serializeEntity(w goio.Writer, e *openpgp.Entity) error {
|
||||
err := e.PrivateKey.Serialize(w)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -188,6 +192,3 @@ func (s *Service) HandleIPCEvents(c *framework.Core, msg framework.Message) erro
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ensure Service implements framework.Crypt.
|
||||
var _ framework.Crypt = (*Service)(nil)
|
||||
|
|
|
|||
|
|
@ -4,40 +4,40 @@ import (
|
|||
"bytes"
|
||||
"testing"
|
||||
|
||||
framework "forge.lthn.ai/core/go/pkg/core"
|
||||
framework "dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCreateKeyPair(t *testing.T) {
|
||||
c, _ := framework.New()
|
||||
c := framework.New()
|
||||
s := &Service{core: c}
|
||||
|
||||
privKey, err := s.CreateKeyPair("test user", "password123")
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, privKey)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, privKey)
|
||||
assert.Contains(t, privKey, "-----BEGIN PGP PRIVATE KEY BLOCK-----")
|
||||
}
|
||||
|
||||
func TestEncryptDecrypt(t *testing.T) {
|
||||
c, _ := framework.New()
|
||||
c := framework.New()
|
||||
s := &Service{core: c}
|
||||
|
||||
passphrase := "secret"
|
||||
privKey, err := s.CreateKeyPair("test user", passphrase)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
// In this simple test, the public key is also in the armored private key string
|
||||
// (openpgp.ReadArmoredKeyRing reads both)
|
||||
// ReadArmoredKeyRing extracts public keys from armored private key blocks
|
||||
publicKey := privKey
|
||||
|
||||
data := "hello openpgp"
|
||||
var buf bytes.Buffer
|
||||
armored, err := s.EncryptPGP(&buf, publicKey, data)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, armored)
|
||||
assert.NotEmpty(t, buf.String())
|
||||
|
||||
decrypted, err := s.DecryptPGP(privKey, armored, passphrase)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, data, decrypted)
|
||||
}
|
||||
|
|
|
|||
3
go.mod
3
go.mod
|
|
@ -3,8 +3,8 @@ module forge.lthn.ai/core/go-crypt
|
|||
go 1.26.0
|
||||
|
||||
require (
|
||||
dappco.re/go/core v0.4.7
|
||||
forge.lthn.ai/core/cli v0.3.7
|
||||
forge.lthn.ai/core/go v0.3.3
|
||||
forge.lthn.ai/core/go-i18n v0.1.7
|
||||
forge.lthn.ai/core/go-io v0.1.7
|
||||
forge.lthn.ai/core/go-log v0.0.4
|
||||
|
|
@ -15,6 +15,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
forge.lthn.ai/core/go v0.3.3 // indirect
|
||||
forge.lthn.ai/core/go-inference v0.1.7 // indirect
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/charmbracelet/bubbletea v1.3.10 // indirect
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -1,3 +1,5 @@
|
|||
dappco.re/go/core v0.4.7 h1:KmIA/2lo6rl1NMtLrKqCWfMlUqpDZYH3q0/d10dTtGA=
|
||||
dappco.re/go/core v0.4.7/go.mod h1:f2/tBZ3+3IqDrg2F5F598llv0nmb/4gJVCFzM5geE4A=
|
||||
forge.lthn.ai/core/cli v0.3.7 h1:1GrbaGg0wDGHr6+klSbbGyN/9sSbHvFbdySJznymhwg=
|
||||
forge.lthn.ai/core/cli v0.3.7/go.mod h1:DBUppJkA9P45ZFGgI2B8VXw1rAZxamHoI/KG7fRvTNs=
|
||||
forge.lthn.ai/core/go v0.3.3 h1:kYYZ2nRYy0/Be3cyuLJspRjLqTMxpckVyhb/7Sw2gd0=
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue