Defines the multibase_codec module, providing basic encoding and decoding operations
for multibase-formatted data without key type awareness.
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>
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
- Function
base58btc_name - Function
base32_name - Function
base64pad_name - Function
base16_name - Function
hex_name - Function
encode_base58btc - Function
encode_base64pad - Function
encode_base16 - Function
encode - Function
decode - Function
extract_prefix - Function
get_prefix_for_encoding - Function
get_encoding_from_prefix
use 0x1::option;
use 0x1::string;
use 0x1::vector;
use 0x2::base58;
use 0x2::base64;
use 0x2::hex;
use 0x2::string_utils;
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
const ErrorInvalidMultibasePrefix: u64 = 1;
Error when an unsupported encoding base is used
const ErrorUnsupportedBase: u64 = 2;
Returns the name of base58btc encoding
public fun base58btc_name(): string::String
Returns the name of base32 encoding
public fun base32_name(): string::String
Returns the name of base64pad encoding (RFC4648 with padding)
public fun base64pad_name(): string::String
Returns the name of base16/hex encoding
public fun base16_name(): string::String
Returns the alternate name (hex) for base16 encoding
public fun hex_name(): string::String
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
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
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
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>
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>>
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>
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>
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
public fun get_encoding_from_prefix(prefix: u8): option::Option<string::String>