export function resolveAccountRef(ref, wasm) {
if (ref == null) {
throw new Error("Account reference cannot be null or undefined");
}
if (typeof ref === "string") {
if (ref.startsWith("0x") || ref.startsWith("0X")) {
return wasm.AccountId.fromHex(ref);
}
return wasm.AccountId.fromBech32(ref);
}
if (ref && typeof ref.id === "function") {
return ref.id();
}
return ref;
}
export function resolveAddress(ref, wasm) {
if (ref == null) {
throw new Error("Address reference cannot be null or undefined");
}
if (typeof ref === "string") {
if (ref.startsWith("0x") || ref.startsWith("0X")) {
const accountId = wasm.AccountId.fromHex(ref);
return wasm.Address.fromAccountId(accountId, undefined);
}
return wasm.Address.fromBech32(ref);
}
if (ref && typeof ref.id === "function") {
const accountId = ref.id();
return wasm.Address.fromAccountId(accountId, undefined);
}
return wasm.Address.fromAccountId(ref, undefined);
}
export function resolveNoteType(type, wasm) {
if (type === "private") {
return wasm.NoteType.Private;
}
if (type === "public" || type == null) {
return wasm.NoteType.Public;
}
throw new Error(
`Unknown note type: "${type}". Expected "public" or "private".`
);
}
export function resolveStorageMode(mode, wasm) {
switch (mode) {
case "public":
return wasm.AccountStorageMode.public();
case "network":
return wasm.AccountStorageMode.network();
case "private":
case undefined:
case null:
return wasm.AccountStorageMode.private();
default:
throw new Error(
`Unknown storage mode: "${mode}". Expected "private", "public", or "network".`
);
}
}
export function resolveAuthScheme(scheme, wasm) {
if (scheme === "ecdsa") {
return wasm.AuthScheme.AuthEcdsaK256Keccak;
}
if (scheme === "falcon" || scheme == null) {
return wasm.AuthScheme.AuthRpoFalcon512;
}
throw new Error(
`Unknown auth scheme: "${scheme}". Expected "falcon" or "ecdsa".`
);
}
export function resolveAccountMutability(accountType) {
if (
accountType == null ||
accountType === "MutableWallet" ||
accountType === 3
) {
return true;
}
if (accountType === "ImmutableWallet" || accountType === 2) {
return false;
}
throw new Error(
`Unknown wallet account type: "${accountType}". Expected AccountType.MutableWallet (3) or AccountType.ImmutableWallet (2).`
);
}
export function resolveNoteIdHex(input) {
if (input == null) {
throw new Error("Note ID cannot be null or undefined");
}
if (typeof input === "string") {
return input;
}
if (
typeof input.toString === "function" &&
typeof input.id !== "function" &&
input.constructor?.fromHex !== undefined
) {
return input.toString();
}
if (typeof input.id === "function") {
return input.id().toString();
}
throw new TypeError(
`Cannot resolve note ID: expected string, NoteId, InputNoteRecord, or Note, got ${typeof input}`
);
}
export function resolveTransactionIdHex(input) {
if (input == null) {
throw new Error("Transaction ID cannot be null or undefined");
}
if (typeof input === "string") {
return input;
}
if (typeof input.toHex === "function") {
return input.toHex();
}
throw new TypeError(
`Cannot resolve transaction ID: expected string or TransactionId, got ${typeof input}`
);
}
export async function hashSeed(seed) {
if (seed instanceof Uint8Array) {
return seed;
}
if (typeof seed === "string") {
const encoded = new TextEncoder().encode(seed);
const hash = await crypto.subtle.digest("SHA-256", encoded);
return new Uint8Array(hash);
}
throw new TypeError(
`Invalid seed type: expected string or Uint8Array, got ${typeof seed}`
);
}