From b3a4272188ab1bf4211cc56ba112aac6eddf1137 Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Mon, 27 Mar 2023 01:55:33 +0200 Subject: [PATCH] Add decoders --- commonspace/object/acl/list/aclstate.go | 2 +- util/crypto/{encoder.go => decode.go} | 27 +++++++++++++++++++++++++ util/crypto/ed25519.go | 2 +- util/crypto/key.go | 4 ++-- 4 files changed, 31 insertions(+), 4 deletions(-) rename util/crypto/{encoder.go => decode.go} (50%) diff --git a/commonspace/object/acl/list/aclstate.go b/commonspace/object/acl/list/aclstate.go index 84d1e80a..c1ab749e 100644 --- a/commonspace/object/acl/list/aclstate.go +++ b/commonspace/object/acl/list/aclstate.go @@ -209,7 +209,7 @@ func (st *AclState) applyChangeData(record *AclRecord) (err error) { // only Admins can do non-user join changes if !st.HasPermission(record.Identity, aclrecordproto.AclUserPermissions_Admin) { // TODO: add string encoding - err = fmt.Errorf("user %s must have admin permissions", record.Identity.String()) + err = fmt.Errorf("user %s must have admin permissions", record.Identity.Account()) return } } diff --git a/util/crypto/encoder.go b/util/crypto/decode.go similarity index 50% rename from util/crypto/encoder.go rename to util/crypto/decode.go index 64432dee..91535849 100644 --- a/util/crypto/encoder.go +++ b/util/crypto/decode.go @@ -2,6 +2,8 @@ package crypto import ( "encoding/base64" + "github.com/anytypeio/any-sync/util/strkey" + "github.com/libp2p/go-libp2p/core/peer" ) func EncodeKeyToString[T Key](key T) (str string, err error) { @@ -28,3 +30,28 @@ func DecodeKeyFromString[T Key](str string, construct func([]byte) (T, error), d func DecodeBytesFromString(str string) (bytes []byte, err error) { return base64.StdEncoding.DecodeString(str) } + +func DecodeAccountAddress(address string) (PubKey, error) { + pubKeyRaw, err := strkey.Decode(strkey.AccountAddressVersionByte, address) + if err != nil { + return nil, err + } + return UnmarshalEd25519PublicKey(pubKeyRaw) +} + +func DecodePeerId(peerId string) (PubKey, error) { + decoded, err := peer.Decode(peerId) + if err != nil { + return nil, err + } + + pk, err := decoded.ExtractPublicKey() + if err != nil { + return nil, err + } + raw, err := pk.Raw() + if err != nil { + return nil, err + } + return UnmarshalEd25519PublicKey(raw) +} diff --git a/util/crypto/ed25519.go b/util/crypto/ed25519.go index faa64a40..608da2f5 100644 --- a/util/crypto/ed25519.go +++ b/util/crypto/ed25519.go @@ -132,7 +132,7 @@ func (k *Ed25519PrivKey) LibP2P() (crypto.PrivKey, error) { } // String returns string representation of key -func (k *Ed25519PubKey) String() string { +func (k *Ed25519PubKey) Account() string { res, _ := strkey.Encode(strkey.AccountAddressVersionByte, k.pubKey) return res } diff --git a/util/crypto/key.go b/util/crypto/key.go index 969a773f..10f08875 100644 --- a/util/crypto/key.go +++ b/util/crypto/key.go @@ -43,8 +43,8 @@ type PubKey interface { Marshall() ([]byte, error) // Storage returns underlying key storage Storage() []byte - // String returns string representation - String() string + // Account returns string representation + Account() string // LibP2P returns libp2p model LibP2P() (crypto.PubKey, error) }