Skip to content

Latest commit

 

History

History
396 lines (184 loc) · 11.6 KB

File metadata and controls

396 lines (184 loc) · 11.6 KB

Module 0x2::multibase_codec

Defines the multibase_codec module, providing basic encoding and decoding operations for multibase-formatted data without key type awareness.

Overview

Multibase is a protocol for self-identifying base encodings for binary data in text formats. This module handles the core encoding/decoding operations without any key type or DID-specific logic.

The format is: <base-encoding-code-point><base-encoded-data>

Supported Encodings

This module currently supports the following encodings:

  • Base16 (Hexadecimal):
  • Prefix: 'f' (ASCII: 102)
  • Name: "base16" (alias: "hex")
  • Base58 Bitcoin (base58btc):
  • Prefix: 'z' (ASCII: 122)
  • Name: "base58btc"
  • Base64 with Padding (base64pad):
  • Prefix: 'M' (ASCII: 77)
  • Name: "base64pad"

For more details on the Multibase standard, see: https://github.com/multiformats/multibase

-  [Overview](#@Overview_0)
-  [Supported Encodings](#@Supported_Encodings_1)

Constants

The prefix for base16 (hex) encoding ('f' in ASCII)

const BASE16_PREFIX: u8 = 102;

The prefix for base32 encoding ('b' in ASCII)

const BASE32_PREFIX: u8 = 98;

The prefix for base58btc encoding ('z' in ASCII)

const BASE58BTC_PREFIX: u8 = 122;

The prefix for base64pad encoding ('M' in ASCII)

const BASE64PAD_PREFIX: u8 = 77;

const ENCODING_BASE16: vector<u8> = [98, 97, 115, 101, 49, 54];

const ENCODING_BASE32: vector<u8> = [98, 97, 115, 101, 51, 50];

const ENCODING_BASE58BTC: vector<u8> = [98, 97, 115, 101, 53, 56, 98, 116, 99];

const ENCODING_BASE64PAD: vector<u8> = [98, 97, 115, 101, 54, 52, 112, 97, 100];

const ENCODING_HEX: vector<u8> = [104, 101, 120];

const ETestAssertionFailed: u64 = 100;

Error when base58 decoding fails

const ErrorBase58DecodingFailed: u64 = 4;

Error when the encoding process fails

const ErrorEncodingFailed: u64 = 5;

Error when an invalid base58 character is encountered

const ErrorInvalidBase58Char: u64 = 3;

Error when an invalid multibase prefix is provided

Error when an unsupported encoding base is used

const ErrorUnsupportedBase: u64 = 2;

Function base58btc_name

Returns the name of base58btc encoding

Function base32_name

Returns the name of base32 encoding

Function base64pad_name

Returns the name of base64pad encoding (RFC4648 with padding)

Function base16_name

Returns the name of base16/hex encoding

Function hex_name

Returns the alternate name (hex) for base16 encoding

public fun hex_name(): string::String

Function encode_base58btc

Encodes bytes using base58btc and adds the multibase prefix 'z'

@param bytes - The raw bytes to encode @return - A multibase encoded string with 'z' prefix

public fun encode_base58btc(bytes: &vector<u8>): string::String

Function encode_base64pad

Encodes bytes using base64pad (RFC4648 with padding) and adds the multibase prefix 'M'

@param bytes - The raw bytes to encode @return - A multibase encoded string with 'M' prefix

public fun encode_base64pad(bytes: &vector<u8>): string::String

Function encode_base16

Encodes bytes using base16 (hex) and adds the multibase prefix 'f'

@param bytes - The raw bytes to encode @return - A multibase encoded string with 'f' prefix

public fun encode_base16(bytes: &vector<u8>): string::String

Function encode

Encodes bytes using a specified encoding format

@param bytes - The raw bytes to encode @param encoding - The encoding format to use (e.g., "base58btc", "base64pad") @return - Option containing a multibase encoded string, or none if encoding is unsupported

public fun encode(bytes: &vector<u8>, encoding: &string::String): option::Option<string::String>

Function decode

Decodes a multibase-encoded string to its raw bytes

@param encoded_str - The multibase encoded string @return - Option containing the decoded bytes, or none if decoding fails

public fun decode(encoded_str: &string::String): option::Option<vector<u8>>

Function extract_prefix

Extracts the multibase prefix from an encoded string

@param encoded_str - The multibase encoded string @return - Option containing the prefix byte, or none if string is empty

public fun extract_prefix(encoded_str: &string::String): option::Option<u8>

Function get_prefix_for_encoding

Gets the multibase prefix character for a given encoding

@param encoding_name - The name of the encoding @return - Option containing the prefix byte, or none if encoding is unknown

public fun get_prefix_for_encoding(encoding_name: &string::String): option::Option<u8>

Function get_encoding_from_prefix

Gets the encoding name from a multibase prefix character

@param prefix - The multibase prefix byte @return - Option containing the encoding name, or none if prefix is unknown