Add decoders
This commit is contained in:
parent
407c9d316c
commit
b3a4272188
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user