zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
92 lines • 4.14 kB
JavaScript
import { Keyring } from '@polkadot/api';
import { encodeAddress } from '@polkadot/util-crypto';
import { VOLTA_CHAIN_SS58_PREFIX, ZKVERIFY_CHAIN_SS58_PREFIX, } from '../../config/index.js';
const VALID_MNEMONIC_WORD_COUNTS = new Set([12, 15, 18, 21, 24]);
const HEX_SEED_RE = /^0x[0-9a-fA-F]{64}$/;
function validateSeedPhrase(seed, isCustomNetwork) {
const trimmed = seed.trim();
if (trimmed.length === 0) {
throw new Error('Seed phrase must not be empty.');
}
if (trimmed.startsWith('//')) {
if (!isCustomNetwork) {
throw new Error(`Dev account SURI '${trimmed}' is not valid on Volta or zkVerify mainnet. ` +
`Use a real BIP39 mnemonic, or connect via .Custom() for local dev testing.`);
}
return;
}
const mnemonicPart = trimmed.split(/\/\/?/)[0].trim();
if (HEX_SEED_RE.test(mnemonicPart)) {
return;
}
const wordCount = mnemonicPart.split(/\s+/).filter(Boolean).length;
if (!VALID_MNEMONIC_WORD_COUNTS.has(wordCount)) {
throw new Error(`Invalid seed phrase: expected a BIP39 mnemonic of 12, 15, 18, 21, or 24 words, got ${wordCount}.`);
}
}
/**
* Sets up the account using the provided secret seed phrase.
*
* @param {string} secretSeedPhrase - The secret seed phrase used to create the account.
* Must be a BIP39 mnemonic (12/15/18/21/24 words) or a 32-byte 0x-prefixed hex seed.
* Substrate dev-account SURIs (e.g. `//Alice`) are rejected unless `isCustomNetwork` is true.
* @param {boolean} [isMainnetNetwork] - True for zkVerify mainnet (affects SS58 encoding).
* @param {boolean} [isCustomNetwork] - True when the session targets a Custom network;
* permits substrate dev SURIs that would never be accepted by Volta or zkVerify.
* @returns {KeyringPair} The initialized account.
* @throws Will throw an error if the seed phrase is invalid.
*/
export const setupAccount = (secretSeedPhrase, isMainnetNetwork, isCustomNetwork) => {
validateSeedPhrase(secretSeedPhrase, !!isCustomNetwork);
try {
const ss58Prefix = isMainnetNetwork
? ZKVERIFY_CHAIN_SS58_PREFIX
: VOLTA_CHAIN_SS58_PREFIX;
const keyring = new Keyring({ type: 'sr25519' });
keyring.setSS58Format(ss58Prefix);
return keyring.addFromUri(secretSeedPhrase);
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Invalid seed phrase provided: ${error.message}`);
}
else {
throw new Error('An unknown error occurred while setting up the account.');
}
}
};
/** Canonical SS58 address for a pair/public key (fixed to chain prefix). */
export const canonicalAddress = (pairOrPublicKey, isMainnetNetwork) => {
const pk = pairOrPublicKey instanceof Uint8Array
? pairOrPublicKey
: pairOrPublicKey.publicKey;
const ss58Prefix = isMainnetNetwork
? ZKVERIFY_CHAIN_SS58_PREFIX
: VOLTA_CHAIN_SS58_PREFIX;
return encodeAddress(pk, ss58Prefix);
};
/**
* Derives a hard child account at `//{index}` from `base`.
* Returns the derived keypair, its SS58-encoded address (using `CHAIN_SS58_PREFIX`), and the derivation path.
*
* @param {KeyringPair} base - The base sr25519 keypair to derive from.
* @param {number} index - The child index to derive at (hard path `//index`, appended to any existing path on `base`).
* @returns {{ pair: KeyringPair, address: string, path: string }} The derived `pair`, its SS58 `address`, and the `path`.
* @throws {Error} If derivation fails.
*/
export const deriveChildAt = (base, index, isMainnetNetwork) => {
const path = `//${index}`;
const ss58Prefix = isMainnetNetwork
? ZKVERIFY_CHAIN_SS58_PREFIX
: VOLTA_CHAIN_SS58_PREFIX;
try {
const pair = base.derive(path);
const address = encodeAddress(pair.publicKey, ss58Prefix);
return { pair, address, path };
}
catch (error) {
const msg = error instanceof Error ? error.message : 'unknown error';
throw new Error(`Failed to derive child at ${path}: ${msg}`);
}
};
//# sourceMappingURL=index.js.map