36 lines
992 B
Go
36 lines
992 B
Go
package helpers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
mbase "github.com/multiformats/go-multibase"
|
|
"github.com/multiformats/go-varint"
|
|
)
|
|
|
|
// PublicKeyMultibaseDecode is a helper for decoding multibase public keys.
|
|
func PublicKeyMultibaseDecode(multibase string) (uint64, []byte, error) {
|
|
baseCodec, bytes, err := mbase.Decode(multibase)
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
// the specification enforces that encoding
|
|
if baseCodec != mbase.Base58BTC {
|
|
return 0, nil, fmt.Errorf("not Base58BTC encoded")
|
|
}
|
|
code, read, err := varint.FromUvarint(bytes)
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
if read != 2 {
|
|
return 0, nil, fmt.Errorf("unexpected multibase")
|
|
}
|
|
return code, bytes[read:], nil
|
|
}
|
|
|
|
// PublicKeyMultibaseEncode is a helper for encoding multibase public keys.
|
|
func PublicKeyMultibaseEncode(code uint64, bytes []byte) string {
|
|
// can only fail with an invalid encoding, but it's hardcoded
|
|
res, _ := mbase.Encode(mbase.Base58BTC, append(varint.ToUvarint(code), bytes...))
|
|
return res
|
|
}
|