Add peer id string for keys

This commit is contained in:
mcrakhman 2023-03-27 17:04:59 +02:00 committed by Mikhail Iudin
parent c3e6af0032
commit 06222e33ff
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
3 changed files with 13 additions and 10 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)
}