diff --git a/commonspace/object/tree/objecttree/change.go b/commonspace/object/tree/objecttree/change.go index 2e5a3008..1f7dc442 100644 --- a/commonspace/object/tree/objecttree/change.go +++ b/commonspace/object/tree/objecttree/change.go @@ -19,18 +19,17 @@ type Change struct { AclHeadId string Id string SnapshotId string - IsSnapshot bool Timestamp int64 ReadKeyId string Identity crypto.PubKey Data []byte Model interface{} + Signature []byte // iterator helpers visited bool branchesFinished bool - - Signature []byte + IsSnapshot bool } func NewChange(id string, identity crypto.PubKey, ch *treechangeproto.TreeChange, signature []byte) *Change { diff --git a/net/peer/context.go b/net/peer/context.go index 89626276..d759ad01 100644 --- a/net/peer/context.go +++ b/net/peer/context.go @@ -3,6 +3,7 @@ package peer import ( "context" "errors" + "github.com/anytypeio/any-sync/util/crypto" "github.com/libp2p/go-libp2p/core/sec" "storj.io/drpc/drpcctx" ) @@ -43,6 +44,14 @@ func CtxIdentity(ctx context.Context) ([]byte, error) { return nil, ErrIdentityNotFoundInContext } +// CtxPubKey returns identity unmarshalled from proto in crypto.PubKey model +func CtxPubKey(ctx context.Context) (crypto.PubKey, error) { + if identity, ok := ctx.Value(contextKeyIdentity).([]byte); ok { + return crypto.UnmarshalEd25519PublicKeyProto(identity) + } + return nil, ErrIdentityNotFoundInContext +} + // CtxWithIdentity sets identity in the context func CtxWithIdentity(ctx context.Context, identity []byte) context.Context { return context.WithValue(ctx, contextKeyIdentity, identity) diff --git a/net/secureservice/credential.go b/net/secureservice/credential.go index 7b4dadf6..82fde3a3 100644 --- a/net/secureservice/credential.go +++ b/net/secureservice/credential.go @@ -38,8 +38,10 @@ func (p *peerSignVerifier) MakeCredentials(sc sec.SecureConn) *handshakeproto.Cr if err != nil { log.Warn("can't sign identity credentials", zap.Error(err)) } + // this will actually be called only once + marshalled, _ := p.account.SignKey.GetPublic().Marshall() msg := &handshakeproto.PayloadSignedPeerIds{ - Identity: p.account.Identity, + Identity: marshalled, Sign: sign, } payload, _ := msg.Marshal() @@ -57,7 +59,7 @@ func (p *peerSignVerifier) CheckCredential(sc sec.SecureConn, cred *handshakepro if err = msg.Unmarshal(cred.Payload); err != nil { return nil, handshake.ErrUnexpectedPayload } - pubKey, err := crypto.NewSigningEd25519PubKeyFromBytes(msg.Identity) + pubKey, err := crypto.UnmarshalEd25519PublicKeyProto(msg.Identity) if err != nil { return nil, handshake.ErrInvalidCredentials } diff --git a/net/secureservice/credential_test.go b/net/secureservice/credential_test.go index d384af5f..1bab8ac9 100644 --- a/net/secureservice/credential_test.go +++ b/net/secureservice/credential_test.go @@ -17,6 +17,8 @@ import ( func TestPeerSignVerifier_CheckCredential(t *testing.T) { a1 := newTestAccData(t) a2 := newTestAccData(t) + identity1, _ := a1.SignKey.GetPublic().Marshall() + identity2, _ := a2.SignKey.GetPublic().Marshall() cc1 := newPeerSignVerifier(a1) cc2 := newPeerSignVerifier(a2) @@ -28,11 +30,11 @@ func TestPeerSignVerifier_CheckCredential(t *testing.T) { cr2 := cc2.MakeCredentials(c2) id1, err := cc1.CheckCredential(c1, cr2) assert.NoError(t, err) - assert.Equal(t, a2.Identity, id1) + assert.Equal(t, identity2, id1) id2, err := cc2.CheckCredential(c2, cr1) assert.NoError(t, err) - assert.Equal(t, a1.Identity, id2) + assert.Equal(t, identity1, id2) _, err = cc1.CheckCredential(c1, cr1) assert.EqualError(t, err, handshake.ErrInvalidCredentials.Error()) diff --git a/net/secureservice/secureservice_test.go b/net/secureservice/secureservice_test.go index 2807ef84..b1dc7e6a 100644 --- a/net/secureservice/secureservice_test.go +++ b/net/secureservice/secureservice_test.go @@ -46,8 +46,9 @@ func TestHandshake(t *testing.T) { require.NoError(t, err) accId, err := peer.CtxIdentity(res.ctx) require.NoError(t, err) + marshalledId, _ := nc.GetAccountService(1).Account().SignKey.GetPublic().Marshall() assert.Equal(t, nc.GetAccountService(1).Account().PeerId, peerId) - assert.Equal(t, nc.GetAccountService(1).Account().Identity, accId) + assert.Equal(t, marshalledId, accId) } func newFixture(t *testing.T, nc *testnodeconf.Config, acc accountservice.Service) *fixture { diff --git a/nodeconf/config.go b/nodeconf/config.go index 17093019..5a60315b 100644 --- a/nodeconf/config.go +++ b/nodeconf/config.go @@ -15,10 +15,9 @@ type configGetter interface { } type NodeConfig struct { - PeerId string `yaml:"peerId"` - Addresses []string `yaml:"address"` - EncryptionKey string `yaml:"encryptionPubKey,omitempty"` - Types []NodeType `yaml:"types,omitempty"` + PeerId string `yaml:"peerId"` + Addresses []string `yaml:"address"` + Types []NodeType `yaml:"types,omitempty"` } func (n NodeConfig) HasType(t NodeType) bool { diff --git a/nodeconf/service.go b/nodeconf/service.go index 234f22f7..ad66fc5a 100644 --- a/nodeconf/service.go +++ b/nodeconf/service.go @@ -5,9 +5,6 @@ import ( "github.com/anytypeio/any-sync/app" "github.com/anytypeio/any-sync/app/logger" "github.com/anytypeio/any-sync/util/crypto" - "github.com/anytypeio/any-sync/util/keys" - "github.com/anytypeio/any-sync/util/keys/asymmetric/encryptionkey" - "github.com/anytypeio/any-sync/util/keys/asymmetric/signingkey" "github.com/anytypeio/go-chash" "github.com/libp2p/go-libp2p/core/peer" ) @@ -37,10 +34,9 @@ type service struct { } type Node struct { - Addresses []string - PeerId string - SigningKey signingkey.PubKey - EncryptionKey encryptionkey.PubKey + Addresses []string + PeerId string + SigningKey crypto.PubKey } func (n *Node) Id() string { @@ -127,18 +123,9 @@ func nodeFromConfigNode(n NodeConfig) (*Node, error) { return nil, err } - encPubKey, err := keys.DecodeKeyFromString( - n.EncryptionKey, - encryptionkey.NewEncryptionRsaPubKeyFromBytes, - nil) - if err != nil { - return nil, err - } - return &Node{ - Addresses: n.Addresses, - PeerId: n.PeerId, - SigningKey: sigPubKey, - EncryptionKey: encPubKey, + Addresses: n.Addresses, + PeerId: n.PeerId, + SigningKey: sigPubKey, }, nil } diff --git a/testutil/accounttest/accountservice.go b/testutil/accounttest/accountservice.go index 42d21a26..57986340 100644 --- a/testutil/accounttest/accountservice.go +++ b/testutil/accounttest/accountservice.go @@ -6,8 +6,6 @@ import ( "github.com/anytypeio/any-sync/commonspace/object/accountdata" "github.com/anytypeio/any-sync/nodeconf" "github.com/anytypeio/any-sync/util/crypto" - "github.com/anytypeio/any-sync/util/keys" - "github.com/anytypeio/any-sync/util/keys/asymmetric/encryptionkey" "github.com/anytypeio/any-sync/util/peer" ) @@ -20,19 +18,10 @@ func (s *AccountTestService) Init(a *app.App) (err error) { if s.acc != nil { return } - encKey, _, err := encryptionkey.GenerateRandomRSAKeyPair(2048) - if err != nil { - return - } - signKey, _, err := crypto.GenerateRandomEd25519KeyPair() if err != nil { return } - ident, err := signKey.GetPublic().Raw() - if err != nil { - return - } peerKey, _, err := crypto.GenerateRandomEd25519KeyPair() if err != nil { @@ -44,11 +33,9 @@ func (s *AccountTestService) Init(a *app.App) (err error) { return err } s.acc = &accountdata.AccountKeys{ - Identity: ident, - PeerKey: peerKey, - SignKey: signKey, - EncKey: encKey, - PeerId: peerId.String(), + PeerKey: peerKey, + SignKey: signKey, + PeerId: peerId.String(), } return nil } @@ -62,14 +49,9 @@ func (s *AccountTestService) Account() *accountdata.AccountKeys { } func (s *AccountTestService) NodeConf(addrs []string) nodeconf.NodeConfig { - encEk, err := keys.EncodeKeyToString(s.acc.EncKey.GetPublic()) - if err != nil { - panic(err) - } return nodeconf.NodeConfig{ - PeerId: s.acc.PeerId, - Addresses: addrs, - EncryptionKey: encEk, - Types: []nodeconf.NodeType{nodeconf.NodeTypeTree}, + PeerId: s.acc.PeerId, + Addresses: addrs, + Types: []nodeconf.NodeType{nodeconf.NodeTypeTree}, } } diff --git a/util/crypto/ed25519.go b/util/crypto/ed25519.go index 778d2ff0..faa64a40 100644 --- a/util/crypto/ed25519.go +++ b/util/crypto/ed25519.go @@ -10,6 +10,7 @@ import ( "github.com/anytypeio/any-sync/util/crypto/cryptoproto" "github.com/anytypeio/any-sync/util/strkey" "github.com/gogo/protobuf/proto" + "github.com/libp2p/go-libp2p/core/crypto" "io" "sync" ) @@ -125,6 +126,12 @@ func (k *Ed25519PrivKey) Decrypt(msg []byte) ([]byte, error) { return DecryptX25519(k.privCurve, k.pubCurve, msg) } +// LibP2P converts the key to libp2p format +func (k *Ed25519PrivKey) LibP2P() (crypto.PrivKey, error) { + return crypto.UnmarshalEd25519PrivateKey(k.privKey) +} + +// String returns string representation of key func (k *Ed25519PubKey) String() string { res, _ := strkey.Encode(strkey.AccountAddressVersionByte, k.pubKey) return res @@ -165,6 +172,7 @@ func (k *Ed25519PubKey) Verify(data []byte, sig []byte) (bool, error) { return ed25519.Verify(k.pubKey, data, sig), nil } +// Marshall marshalls the key into proto func (k *Ed25519PubKey) Marshall() ([]byte, error) { k.marshallOnce.Do(func() { msg := &cryptoproto.Key{ @@ -176,6 +184,11 @@ func (k *Ed25519PubKey) Marshall() ([]byte, error) { return k.marshalled, k.marshallErr } +// LibP2P converts the key to libp2p format +func (k *Ed25519PubKey) LibP2P() (crypto.PubKey, error) { + return crypto.UnmarshalEd25519PublicKey(k.pubKey) +} + // UnmarshalEd25519PublicKey returns a public key from input bytes. func UnmarshalEd25519PublicKey(data []byte) (PubKey, error) { if len(data) != 32 { diff --git a/util/crypto/key.go b/util/crypto/key.go index 93642ad4..969a773f 100644 --- a/util/crypto/key.go +++ b/util/crypto/key.go @@ -3,6 +3,7 @@ package crypto import ( "crypto/subtle" "errors" + "github.com/libp2p/go-libp2p/core/crypto" ) var ErrIncorrectKeyType = errors.New("incorrect key type") @@ -26,6 +27,8 @@ type PrivKey interface { Sign([]byte) ([]byte, error) // GetPublic returns the associated public key GetPublic() PubKey + // LibP2P returns libp2p model + LibP2P() (crypto.PrivKey, error) } // PubKey is the public key used to verify the signatures and decrypt messages @@ -42,6 +45,8 @@ type PubKey interface { Storage() []byte // String returns string representation String() string + // LibP2P returns libp2p model + LibP2P() (crypto.PubKey, error) } type SymKey interface { diff --git a/util/peer/peer.go b/util/peer/peer.go index 9e77f299..ee6a32cd 100644 --- a/util/peer/peer.go +++ b/util/peer/peer.go @@ -1,12 +1,12 @@ package peer import ( - "github.com/anytypeio/any-sync/util/keys/asymmetric/signingkey" + utilcrypto "github.com/anytypeio/any-sync/util/crypto" "github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/peer" ) -func IdFromSigningPubKey(pubKey signingkey.PubKey) (peer.ID, error) { +func IdFromSigningPubKey(pubKey utilcrypto.PubKey) (peer.ID, error) { rawSigning, err := pubKey.Raw() if err != nil { return "", err