UNPKG

test-ic-wallet-middleware-icrc

Version:
28 lines (27 loc) 1.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrincipalProvider = void 0; const ledger_icrc_1 = require("@dfinity/ledger-icrc"); const types_1 = require("../types"); class PrincipalProvider { static toAccountIdentifier(principal, subAccountId) { return (0, ledger_icrc_1.encodeIcrcAccount)({ owner: principal, subaccount: subAccountId.toUint8Array() }); } static toPrincipalAndSubAccountId(accountIdentifier) { const account = (0, ledger_icrc_1.decodeIcrcAccount)(accountIdentifier); return { owner: account.owner, subaccount: account.subaccount ? types_1.SubAccountId.parseFromUint8Array(account.subaccount) : types_1.SubAccountId.Default() }; } static toSubAccountId(principal) { const hex = principal.toHex().toLowerCase(); const length = principal.toUint8Array().length.toString(16); const result = types_1.SubAccountId.parseFromString(`0x${length}${hex}`); return result; } } exports.PrincipalProvider = PrincipalProvider;