Defines the multibase_key module, providing key-type aware encoding and decoding
operations for cryptographic keys with multicodec prefixes.
This module builds on top of multibase_codec to provide specialized encoding/decoding
for cryptographic keys. It handles:
- Key type enumeration (Ed25519, Secp256k1, Secp256r1)
- Multicodec prefixes for different key types
- Key length validation
- Encoding/decoding with type information
- Ed25519: KEY_TYPE_ED25519 = 1, multicodec prefix = 0xed01
- Secp256k1: KEY_TYPE_SECP256K1 = 2, multicodec prefix = 0xe701
- Secp256r1 (ECDSA P-256): KEY_TYPE_ECDSAR1 = 3, multicodec prefix = 0x1200
The encoding process adds the appropriate multicodec prefix to the raw key bytes before applying base58btc encoding.
- [Overview](#@Overview_0)
- [Key Types and Multicodec Prefixes](#@Key_Types_and_Multicodec_Prefixes_1)
- Struct
KeyInfo - Constants
- Function
key_info_type - Function
key_info_bytes - Function
key_type_ed25519 - Function
key_type_secp256k1 - Function
key_type_ecdsar1 - Function
multicodec_prefix_for_type - Function
encode_with_type - Function
decode_with_type - Function
encode_ed25519_key - Function
encode_secp256k1_key - Function
encode_ecdsar1_key - Function
decode_ed25519_key - Function
decode_secp256k1_key - Function
decode_secp256r1_key - Function
decode_with_type_option
use 0x1::option;
use 0x1::string;
use 0x1::vector;
use 0x2::multibase_codec;
A struct to hold the key type and raw key bytes Used as a workaround for Move's lack of support for tuple types in Option
struct KeyInfo has copy, drop, store
const ETestAssertionFailed: u64 = 100;
The length of Secp256r1 compressed public keys in bytes
const ECDSAR1_COMPRESSED_PUBLIC_KEY_LENGTH: u64 = 33;
The length of Ed25519 public keys in bytes
const ED25519_PUBLIC_KEY_LENGTH: u64 = 32;
Error when the did:key identifier is invalid
const ErrorInvalidDidKeyIdentifier: u64 = 4;
Error when the Ed25519 key length is invalid
const ErrorInvalidEd25519KeyLength: u64 = 1;
Error when the format of the publicKeyMultibase string is invalid or cannot be parsed
const ErrorInvalidPublicKeyMultibaseFormat: u64 = 6;
Error when the Secp256k1 key length is invalid
const ErrorInvalidSecp256k1KeyLength: u64 = 2;
Error when the Secp256r1 key length is invalid
const ErrorInvalidSecp256r1KeyLength: u64 = 3;
Error when an unsupported key type is used
const ErrorUnsupportedKeyType: u64 = 5;
const KEY_TYPE_ECDSAR1: u8 = 3;
const KEY_TYPE_ED25519: u8 = 1;
const KEY_TYPE_SECP256K1: u8 = 2;
const MULTICODEC_ECDSA_R1_PREFIX: vector<u8> = [18, 0];
const MULTICODEC_ED25519_PREFIX: vector<u8> = [237, 1];
const MULTICODEC_SECP256K1_PREFIX: vector<u8> = [231, 1];
The length of Secp256k1 compressed public keys in bytes
const SECP256K1_COMPRESSED_PUBLIC_KEY_LENGTH: u64 = 33;
Get the key type from a KeyInfo struct
public fun key_info_type(key_info: &multibase_key::KeyInfo): u8
Get the key bytes from a KeyInfo struct
public fun key_info_bytes(key_info: &multibase_key::KeyInfo): vector<u8>
Returns the key type constant for Ed25519
public fun key_type_ed25519(): u8
Returns the key type constant for Secp256k1
public fun key_type_secp256k1(): u8
Returns the key type constant for Secp256r1 (ECDSA P-256)
public fun key_type_ecdsar1(): u8
Get the multicodec prefix for a given key type
@param key_type - The key type (1=Ed25519, 2=Secp256k1, 3=Secp256r1) @return - The multicodec prefix bytes
public fun multicodec_prefix_for_type(key_type: u8): vector<u8>
Encodes a public key with multicodec prefix and multibase encoding
@param pubkey - The raw public key bytes @param key_type - The key type (1=Ed25519, 2=Secp256k1, 3=Secp256r1) @return - A multibase encoded string with appropriate prefix
public fun encode_with_type(pubkey: &vector<u8>, key_type: u8): string::String
Decodes a multibase-encoded key string with multicodec prefix
@param encoded_str - The multibase encoded key string @return - A tuple of (key_type, raw_key_bytes), or abort if invalid
public fun decode_with_type(encoded_str: &string::String): (u8, vector<u8>)
Encodes an Ed25519 public key using base58btc with multibase prefix
@param pubkey - The raw Ed25519 public key bytes @return - A multibase encoded string with 'z' prefix
public fun encode_ed25519_key(pubkey: &vector<u8>): string::String
Encodes a Secp256k1 compressed public key using base58btc with multibase prefix
@param pubkey - The raw Secp256k1 compressed public key bytes (33 bytes) @return - A multibase encoded string with 'z' prefix
public fun encode_secp256k1_key(pubkey: &vector<u8>): string::String
Encodes a Secp256r1 compressed public key using base58btc with multibase prefix
@param pubkey - The raw Secp256r1 compressed public key bytes (33 bytes) @return - A multibase encoded string with 'z' prefix
public fun encode_ecdsar1_key(pubkey: &vector<u8>): string::String
Decodes a multibase-encoded Ed25519 public key
@param pk_mb_str - The multibase encoded Ed25519 public key string @return - Option containing the decoded public key bytes, or none if decoding fails
public fun decode_ed25519_key(pk_mb_str: &string::String): option::Option<vector<u8>>
Decodes a multibase-encoded Secp256k1 compressed public key
@param pk_mb_str - The multibase encoded Secp256k1 public key string @return - Option containing the decoded public key bytes, or none if decoding fails
public fun decode_secp256k1_key(pk_mb_str: &string::String): option::Option<vector<u8>>
Decodes a Secp256r1 public key from a multibase encoded string
@param pk_mb_str - The multibase encoded string @return - Option containing the raw Secp256r1 public key bytes, or none if decoding fails
public fun decode_secp256r1_key(pk_mb_str: &string::String): option::Option<vector<u8>>
Helper function to decode a multibase-encoded key string with multicodec prefix, returning an Option instead of aborting on failure
@param encoded_str - The multibase encoded key string @return - Option containing a KeyInfo struct with key_type and key_bytes, or none if invalid
public fun decode_with_type_option(encoded_str: &string::String): option::Option<multibase_key::KeyInfo>