Add decoders

This commit is contained in:
mcrakhman 2023-03-27 01:55:33 +02:00 committed by Mikhail Iudin
parent 407c9d316c
commit b3a4272188
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
4 changed files with 31 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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