zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
69 lines (68 loc) • 2.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deriveChildAt = exports.canonicalAddress = exports.setupAccount = void 0;
const api_1 = require("@polkadot/api");
const util_crypto_1 = require("@polkadot/util-crypto");
const config_1 = require("../../config");
/**
* Sets up the account using the provided secret seed phrase.
*
* @param {string} secretSeedPhrase - The secret seed phrase used to create the account.
* @returns {KeyringPair} The initialized account.
* @throws Will throw an error if the seed phrase is invalid.
*/
const setupAccount = (secretSeedPhrase, isMainnetNetwork) => {
try {
const ss58Prefix = isMainnetNetwork
? config_1.ZKVERIFY_CHAIN_SS58_PREFIX
: config_1.VOLTA_CHAIN_SS58_PREFIX;
const keyring = new api_1.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.');
}
}
};
exports.setupAccount = setupAccount;
/** Canonical SS58 address for a pair/public key (fixed to chain prefix). */
const canonicalAddress = (pairOrPublicKey, isMainnetNetwork) => {
const pk = pairOrPublicKey instanceof Uint8Array
? pairOrPublicKey
: pairOrPublicKey.publicKey;
const ss58Prefix = isMainnetNetwork
? config_1.ZKVERIFY_CHAIN_SS58_PREFIX
: config_1.VOLTA_CHAIN_SS58_PREFIX;
return (0, util_crypto_1.encodeAddress)(pk, ss58Prefix);
};
exports.canonicalAddress = canonicalAddress;
/**
* 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.
*/
const deriveChildAt = (base, index, isMainnetNetwork) => {
const path = `//${index}`;
const ss58Prefix = isMainnetNetwork
? config_1.ZKVERIFY_CHAIN_SS58_PREFIX
: config_1.VOLTA_CHAIN_SS58_PREFIX;
try {
const pair = base.derive(path);
const address = (0, util_crypto_1.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}`);
}
};
exports.deriveChildAt = deriveChildAt;