Skip to content

Latest commit

 

History

History
445 lines (205 loc) · 13.7 KB

File metadata and controls

445 lines (205 loc) · 13.7 KB

Module 0x2::multibase_key

Defines the multibase_key module, providing key-type aware encoding and decoding operations for cryptographic keys with multicodec prefixes.

Overview

This module builds on top of multibase_codec to provide specialized encoding/decoding for cryptographic keys. It handles:

  1. Key type enumeration (Ed25519, Secp256k1, Secp256r1)
  2. Multicodec prefixes for different key types
  3. Key length validation
  4. Encoding/decoding with type information

Key Types and Multicodec Prefixes

  • 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

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

Constants

const ETestAssertionFailed: u64 = 100;

The length of Secp256r1 compressed public keys in bytes

The length of Ed25519 public keys in bytes

const ED25519_PUBLIC_KEY_LENGTH: u64 = 32;

Error when the did:key identifier is invalid

Error when the Ed25519 key length is invalid

Error when the format of the publicKeyMultibase string is invalid or cannot be parsed

Error when the Secp256k1 key length is invalid

Error when the Secp256r1 key length is invalid

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

Function key_info_type

Get the key type from a KeyInfo struct

public fun key_info_type(key_info: &multibase_key::KeyInfo): u8

Function key_info_bytes

Get the key bytes from a KeyInfo struct

public fun key_info_bytes(key_info: &multibase_key::KeyInfo): vector<u8>

Function key_type_ed25519

Returns the key type constant for Ed25519

public fun key_type_ed25519(): u8

Function key_type_secp256k1

Returns the key type constant for Secp256k1

public fun key_type_secp256k1(): u8

Function key_type_ecdsar1

Returns the key type constant for Secp256r1 (ECDSA P-256)

public fun key_type_ecdsar1(): u8

Function multicodec_prefix_for_type

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>

Function encode_with_type

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

Function decode_with_type

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

Function encode_ed25519_key

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

Function encode_secp256k1_key

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

Function encode_ecdsar1_key

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

Function decode_ed25519_key

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

Function decode_secp256k1_key

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

Function decode_secp256r1_key

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

Function decode_with_type_option

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