/** * NEP-616 StateInit utilities for deterministic account IDs * * This module provides utilities for working with NEP-616 Deterministic AccountIds. * These account IDs are derived from the contract initialization state using the formula: * `"0s" + hex(keccak256(borsh(state_init))[12..32])` * * @see https://github.com/near/NEPs/pull/616 */ /** * Contract code reference - either by hash or by account ID */ export type ContractCode = { type: "codeHash"; hash: Uint8Array; } | { type: "accountId"; accountId: string; }; /** * StateInit structure for NEP-616 deterministic account IDs */ export interface StateInit { code: ContractCode; data: Map; } /** * Options for creating a StateInit */ export interface StateInitOptions { /** * Reference to the contract code * - Use { codeHash: Uint8Array | string } for hash-based reference (base58 string or raw bytes) * - Use { accountId: string } for account-based reference */ code: { codeHash: Uint8Array | string; } | { accountId: string; }; /** * Initial key-value pairs to populate in the contract's storage * Keys and values should be Borsh-serialized bytes */ data?: Map; } /** * Parse and validate a code hash from either base58 string or Uint8Array * * @param codeHash - Code hash as base58 string or Uint8Array * @returns Validated 32-byte hash as Uint8Array * @throws Error if base58 is invalid or hash is not 32 bytes */ export declare function parseCodeHash(codeHash: string | Uint8Array): Uint8Array; /** * Create a StateInit object from options * * @param options - StateInit configuration * @returns StateInit object */ export declare function createStateInit(options: StateInitOptions): StateInit; /** * Serialize a StateInit to Borsh bytes * * The `data` map is encoded as nearcore's `BTreeMap, Vec>`: entries * sorted bytewise by key, regardless of the `Map`'s insertion order. * * @param stateInit - StateInit object to serialize * @returns Borsh-serialized bytes * @throws Error if two keys have identical bytes */ export declare function serializeStateInit(stateInit: StateInit): Uint8Array; /** * Derive a deterministic account ID from a StateInit according to NEP-616. * * The account ID is derived as: `"0s" + hex(keccak256(borsh(state_init))[12..32])` * * This produces a 42-character account ID that: * - Starts with "0s" prefix (distinguishes from Ethereum implicit accounts "0x") * - Followed by 40 hex characters (20 bytes from the keccak256 hash) * * @param stateInit - StateInit object or options to create one * @returns The deterministically derived account ID * * @example * ```typescript * // From a global contract by account ID * const accountId = deriveAccountId({ * code: { accountId: "publisher.near" }, * }) * // => "0s1234567890abcdef1234567890abcdef12345678" * * // From a global contract by code hash * const accountId = deriveAccountId({ * code: { codeHash: hashBytes }, * }) * * // With initial storage data * const accountId = deriveAccountId({ * code: { accountId: "publisher.near" }, * data: new Map([[key1, value1], [key2, value2]]), * }) * ``` */ export declare function deriveAccountId(options: StateInitOptions): string; /** * Verify that an account ID matches the expected deterministic derivation from a StateInit. * * @param accountId - The account ID to verify * @param options - StateInit options to derive the expected account ID from * @returns true if the account ID matches the derivation, false otherwise * * @example * ```typescript * const isValid = verifyDeterministicAccountId( * "0s1234567890abcdef1234567890abcdef12345678", * { code: { accountId: "publisher.near" } } * ) * ``` */ export declare function verifyDeterministicAccountId(accountId: string, options: StateInitOptions): boolean; /** * Check if an account ID is a deterministic account ID (NEP-616). * * Deterministic account IDs start with "0s" followed by 40 hex characters. * * @param accountId - The account ID to check * @returns true if it's a deterministic account ID, false otherwise * * @example * ```typescript * isDeterministicAccountId("0s1234567890abcdef1234567890abcdef12345678") // true * isDeterministicAccountId("alice.near") // false * isDeterministicAccountId("0x1234567890abcdef1234567890abcdef12345678") // false (Ethereum) * ``` */ export declare function isDeterministicAccountId(accountId: string): boolean; //# sourceMappingURL=state-init.d.ts.map