@dfinity/ledger-icp
Version:
A library for interfacing with the ICP ledger on the Internet Computer.
113 lines (112 loc) • 4.4 kB
TypeScript
import type { Principal } from "@dfinity/principal";
/**
* A 32-byte account identifier used to send and receive ICP tokens.
*
* The ICP Ledger uses the concept of an `AccountIdentifier` to represent accounts.
* It’s a unique value derived from a principal (the identity controlling the account)
* and a subaccount. This design allows a single principal to control multiple accounts
* by using different subaccounts.
*
* @see https://internetcomputer.org/docs/references/ledger#_accounts
* @see https://internetcomputer.org/docs/defi/token-ledgers/setup/icp_ledger_setup
* @see https://internetcomputer.org/docs/references/ledger#_operations_transactions_blocks_transaction_ledger
*/
export declare class AccountIdentifier {
private readonly bytes;
private constructor();
/**
* Creates an `AccountIdentifier` object from a hexadecimal string (e.g. d3e13d4777e22367532053190b6c6ccf57444a61337e996242b1abfb52cf92c8).
* Validates the checksum in the process.
*
* @param hex - The 64-character hexadecimal representation of the account identifier.
* @throws If the length is not 32 bytes or if the checksum is invalid.
* @returns An instance of `AccountIdentifier`.
*/
static fromHex(hex: string): AccountIdentifier;
/**
* Creates an `AccountIdentifier` object from a principal and optional subaccount.
*
* When no subaccount is provided, a 32-byte array of zeros is used by default.
* You can use any arbitrary 32 bytes as a subaccount identifier to derive a distinct account identifier
* for the same principal.
*
* @param options.principal - The principal to derive the account from.
* @param options.subAccount - The optional subaccount (defaults to 0).
* @returns An instance of `AccountIdentifier`.
*/
static fromPrincipal({ principal, subAccount, }: {
principal: Principal;
subAccount?: SubAccount;
}): AccountIdentifier;
/**
* Returns the ICP Ledger Account Identifier as a 64-character hexadecimal string.
* This is the format typically used to display the account identifier in a human-readable way.
*
* @returns Hex representation (64-character string).
*/
toHex(): string;
/**
* Returns the raw 32-byte `Uint8Array` of the account identifier.
*
* @returns The raw bytes of the account identifier.
*/
toUint8Array(): Uint8Array;
/**
* Returns the account identifier as an array of numbers.
*
* @returns An array of byte values.
*/
toNumbers(): number[];
/**
* Returns the raw bytes wrapped in an object under the `hash` key.
*
* @returns `{ hash: Uint8Array }` where `hash` is the raw 32-byte `Uint8Array`.
*/
toAccountIdentifierHash(): {
hash: Uint8Array;
};
}
/**
* A subaccount in the ICP ledger is a 32-byte identifier that allows a principal (user or canister)
* to control multiple independent accounts under the same principal.
*
* @see https://internetcomputer.org/docs/references/ledger#_accounts
*/
export declare class SubAccount {
private readonly bytes;
private constructor();
/**
* Creates a `SubAccount` from a 32‑byte array.
*
* @param bytes - A `Uint8Array` of exactly 32 bytes.
* @throws If the byte array length is not 32.
* @returns A `SubAccount` instance.
*/
static fromBytes(bytes: Uint8Array): SubAccount;
/**
* Generates a `SubAccount` from a principal.
*
* The principal is embedded into the beginning of a 32‑byte array.
*
* @param principal - A principal to encode into the subaccount.
* @returns A `SubAccount` instance.
*/
static fromPrincipal(principal: Principal): SubAccount;
/**
* Generates a `SubAccount` from a non‑negative number.
*
* The number is encoded into the last 8 bytes of the 32‑byte array.
* This is a common pattern when using numbered subaccounts like 0, 1, 2...
*
* @param id - A non-negative integer.
* @throws If the number is negative or exceeds `Number.MAX_SAFE_INTEGER`.
* @returns A `SubAccount` instance.
*/
static fromID(id: number): SubAccount;
/**
* Returns the raw 32-byte `Uint8Array` representing this subaccount.
*
* @returns A 32-byte array.
*/
toUint8Array(): Uint8Array;
}