From 06222e33ffd1831352391f602a9b28901ea4e22d Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Mon, 27 Mar 2023 17:04:59 +0200 Subject: [PATCH] Add peer id string for keys --- commonspace/object/accountdata/accountdata.go | 10 ++-------- util/crypto/ed25519.go | 9 ++++++++- util/crypto/key.go | 4 +++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/commonspace/object/accountdata/accountdata.go b/commonspace/object/accountdata/accountdata.go index 3ad47ca4..279a6cbd 100644 --- a/commonspace/object/accountdata/accountdata.go +++ b/commonspace/object/accountdata/accountdata.go @@ -3,7 +3,6 @@ package accountdata import ( "crypto/rand" "github.com/anytypeio/any-sync/util/crypto" - "github.com/anytypeio/any-sync/util/peer" ) type AccountKeys struct { @@ -13,11 +12,10 @@ type AccountKeys struct { } func New(peerKey crypto.PrivKey, signKey crypto.PrivKey) *AccountKeys { - peerId, _ := peer.IdFromSigningPubKey(peerKey.GetPublic()) return &AccountKeys{ PeerKey: peerKey, SignKey: signKey, - PeerId: peerId.String(), + PeerId: peerKey.GetPublic().PeerId(), } } @@ -30,13 +28,9 @@ func NewRandom() (*AccountKeys, error) { if err != nil { return nil, err } - peerId, err := peer.IdFromSigningPubKey(peerKey.GetPublic()) - if err != nil { - return nil, err - } return &AccountKeys{ PeerKey: peerKey, SignKey: signKey, - PeerId: peerId.String(), + PeerId: peerKey.GetPublic().PeerId(), }, nil } diff --git a/util/crypto/ed25519.go b/util/crypto/ed25519.go index b520bfcd..02cc9ed2 100644 --- a/util/crypto/ed25519.go +++ b/util/crypto/ed25519.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "github.com/anytypeio/any-sync/util/crypto/cryptoproto" + "github.com/anytypeio/any-sync/util/peer" "github.com/anytypeio/any-sync/util/strkey" "github.com/gogo/protobuf/proto" "github.com/libp2p/go-libp2p/core/crypto" @@ -152,12 +153,18 @@ func (k *Ed25519PrivKey) LibP2P() (crypto.PrivKey, error) { return crypto.UnmarshalEd25519PrivateKey(k.privKey) } -// String returns string representation of key +// Account returns string representation of key in anytype account format func (k *Ed25519PubKey) Account() string { res, _ := strkey.Encode(strkey.AccountAddressVersionByte, k.pubKey) return res } +// PeerId returns string representation of key for peer id +func (k *Ed25519PubKey) PeerId() string { + peerId, _ := peer.IdFromSigningPubKey(k) + return peerId.String() +} + // Raw public key bytes. func (k *Ed25519PubKey) Raw() ([]byte, error) { return k.pubKey, nil diff --git a/util/crypto/key.go b/util/crypto/key.go index 91390999..c5346841 100644 --- a/util/crypto/key.go +++ b/util/crypto/key.go @@ -45,8 +45,10 @@ type PubKey interface { Marshall() ([]byte, error) // Storage returns underlying key storage Storage() []byte - // Account returns string representation + // Account returns string representation for anytype account Account() string + // PeerId returns string representation for peer id + PeerId() string // LibP2P returns libp2p model LibP2P() (crypto.PubKey, error) }