6. Utilities

Complete reference for utility functions in the Amadeus Protocol SDK.

Crypto Utilities

Cryptographic operations for key generation and key derivation.

generateKeypair()

Generate a new BLS12-381 keypair.

import { generateKeypair } from '@amadeus-protocol/sdk'

const keypair = generateKeypair()

console.log('Public Key:', keypair.publicKey) // Base58 encoded
console.log('Private Key:', keypair.privateKey) // Base58 encoded seed

Returns: KeyPair with publicKey and privateKey (both Base58 encoded)

generatePrivateKey()

Generate a new random 64-byte seed.

import { generatePrivateKey } from '@amadeus-protocol/sdk'

const seed = generatePrivateKey() // Returns Uint8Array (64 bytes)

Returns: Uint8Array (64 bytes)

getPublicKey(seed: Uint8Array)

Derive a public key from a 64-byte seed.

Parameters:

  • seed (Uint8Array): 64-byte seed

Returns: Uint8Array (48 bytes) - BLS12-381 public key

derivePublicKeyFromSeedBase58(seedBase58: string)

Derive public key from Base58-encoded seed.

Parameters:

  • seedBase58 (string): Base58-encoded seed

Returns: string - Base58-encoded public key

deriveSkAndSeed64FromBase58Seed(seedBase58: string)

Derive secret key and seed from Base58-encoded seed.

Parameters:

  • seedBase58 (string): Base58-encoded seed

Returns: { sk: Uint8Array, seed64: Uint8Array }

Encoding Utilities

Convert between different binary formats and string encodings.

Base58 Encoding

toBase58(bytes: Uint8Array): string

Encode bytes to Base58 string.

Parameters:

  • bytes (Uint8Array): Bytes to encode

Returns: string - Base58 encoded string

fromBase58(str: string): Uint8Array

Decode Base58 string to bytes.

Parameters:

  • str (string): Base58 string to decode

Returns: Uint8Array - Decoded bytes

Base64 Encoding

uint8ArrayToBase64(bytes: Uint8Array): string

Convert Uint8Array to Base64 string.

Parameters:

  • bytes (Uint8Array): Bytes to encode

Returns: string - Base64 encoded string

base64ToUint8Array(base64: string): Uint8Array

Convert Base64 string to Uint8Array.

Parameters:

  • base64 (string): Base64 encoded string

Returns: Uint8Array - Decoded bytes

arrayBufferToBase64(buffer: ArrayBuffer): string

Convert ArrayBuffer to Base64 string.

Parameters:

  • buffer (ArrayBuffer): Buffer to encode

Returns: string - Base64 encoded string

base64ToArrayBuffer(base64: string): ArrayBuffer

Convert Base64 string to ArrayBuffer.

Parameters:

  • base64 (string): Base64 encoded string

Returns: ArrayBuffer - Decoded buffer

Array Conversion

uint8ArrayToArrayBuffer(bytes: Uint8Array): ArrayBuffer

Convert Uint8Array to ArrayBuffer.

Parameters:

  • bytes (Uint8Array): The Uint8Array to convert

Returns: ArrayBuffer

arrayBufferToUint8Array(buffer: ArrayBuffer): Uint8Array

Convert ArrayBuffer to Uint8Array.

Parameters:

  • buffer (ArrayBuffer): The ArrayBuffer to convert

Returns: Uint8Array - View of the buffer

Serialization

Canonical serialization using VecPack format.

encode(value: SerializableValue): Uint8Array

Encode a value using VecPack canonical serialization.

Parameters:

  • value: Serializable value (null, boolean, number, bigint, string, Uint8Array, array, object, Map)

Returns: Uint8Array - Encoded bytes

Supported Types:

  • null

  • boolean

  • number / bigint

  • string

  • Uint8Array

  • Arrays

  • Objects / Maps

decode(bytes: Uint8Array | number[]): DecodedValue

Decode VecPack-encoded bytes.

Parameters:

  • bytes (Uint8Array | number[]): Encoded bytes

Returns: DecodedValue - Decoded value

Conversion Utilities

Convert between human-readable amounts and atomic units.

toAtomicAma(amount: number): number

Convert AMA amount to atomic units.

Parameters:

  • amount (number): Amount in AMA (e.g., 1.5)

Returns: number - Amount in atomic units

Note: AMA tokens use 9 decimal places.

fromAtomicAma(atomic: number | string): number

Convert atomic units to AMA amount.

Parameters:

  • atomic (number | string): Amount in atomic units

Returns: number - Amount in AMA

Encryption Utilities

Password-based encryption for securing sensitive wallet data.

encryptWithPassword(plaintext: string, password: string): Promise<EncryptedPayload>

Encrypt plaintext using AES-GCM encryption with PBKDF2 key derivation.

Parameters:

  • plaintext (string): Data to encrypt

  • password (string): Password for encryption

Returns: Promise<EncryptedPayload> - Object containing:

  • encryptedData (string): Base64-encoded encrypted data

  • iv (string): Base64-encoded initialization vector

  • salt (string): Base64-encoded salt

Security Features:

  • AES-GCM encryption with 256-bit keys

  • PBKDF2 key derivation with 100,000 iterations

  • Unique salt and IV for each encryption

  • Authenticated encryption (prevents tampering)

decryptWithPassword(payload: EncryptedPayload, password: string): Promise<string>

Decrypt encrypted data using the provided password.

Parameters:

  • payload (EncryptedPayload): Encrypted payload with encryptedData, iv, and salt

  • password (string): Password used for encryption

Returns: Promise<string> - Decrypted plaintext

Throws: Error if decryption fails (wrong password or corrupted data)

generateSalt(): Uint8Array

Generate a cryptographically secure random salt.

Returns: Uint8Array - Random salt (16 bytes / 128 bits)

generateIV(): Uint8Array

Generate a cryptographically secure random IV for AES-GCM.

Returns: Uint8Array - Random IV (12 bytes / 96 bits)

deriveKey(password: string, salt: Uint8Array | ArrayBuffer): Promise<CryptoKey>

Derive an AES-GCM key from a password using PBKDF2.

Parameters:

  • password (string): Password string

  • salt (Uint8Array | ArrayBuffer): Salt for key derivation

Returns: Promise<CryptoKey> - Derived AES-GCM key (256 bits)

Constants

AMADEUS_PUBLIC_KEY_BYTE_LENGTH

Byte length of BLS12-381 public key: 48

AMADEUS_SEED_BYTE_LENGTH

Byte length of seed: 64

AMA_TOKEN_DECIMALS

Number of decimal places for AMA tokens: 9

AMA_TOKEN_DECIMALS_MULTIPLIER

Multiplier for conversions: 1000000000 (10^9)

AMA_TRANSFER_FEE

Network transfer fee: 0.02

EXPLORER_URL

Default explorer URL: https://explorer.ama.one

NODE_API_URL

Default node API URL: https://nodes.amadeus.bot/api

Usage Examples

Complete Encryption Flow

Address Validation

Amount Conversion

Next Steps

Last updated