UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

70 lines (68 loc) • 2.84 kB
import { NearTransactionError, executeKeychainTransaction, isNearCliInstalled } from "./near-cli.mjs"; import { Near, isPrivateKey } from "near-kit"; import { FileKeyStore } from "near-kit/keys/file"; //#region src/near-signer.ts function assertPrivateKey(key) { if (!isPrivateKey(key)) throw new NearTransactionError(`Invalid private key format: must start with "ed25519:" or "secp256k1:" (got a ${key.length}-character value).`); return key; } async function resolveSigningKey(opts) { const explicitKey = opts.privateKey || process.env.NEAR_PRIVATE_KEY || process.env.BOS_NEAR_PRIVATE_KEY; if (explicitKey) return { source: "provided", privateKey: explicitKey }; const keyPair = await new FileKeyStore("~/.near-credentials", opts.network).get(opts.account); if (keyPair?.secretKey) return { source: "credentials-file", privateKey: keyPair.secretKey }; throw new NearTransactionError("No signing key available"); } async function resolveSigningStrategy(opts) { try { const resolved = await resolveSigningKey(opts); return { strategy: "near-kit", privateKey: resolved.privateKey, source: resolved.source }; } catch {} if (process.stdin.isTTY && await isNearCliInstalled()) return { strategy: "near-cli-keychain" }; throw new NearTransactionError(`No signing key available for ${opts.account} on ${opts.network}. Set NEAR_PRIVATE_KEY (or BOS_NEAR_PRIVATE_KEY), store credentials at ~/.near-credentials/${opts.network}/${opts.account}.json, or sign with the near-cli-rs keychain (install it, then retry from an interactive terminal). Generate a publish key with: bos key generate`); } function describeSigningStrategy(strategy) { if (strategy.strategy === "near-cli-keychain") return "near-cli-rs keychain"; return strategy.source === "provided" ? "the environment or --private-key flag" : `~/.near-credentials`; } async function submitWithNearKit(tx, privateKey) { return { success: true, txHash: (await new Near({ network: tx.network, defaultSignerId: tx.account, privateKey: assertPrivateKey(privateKey) }).transaction(tx.account).functionCall(tx.contract, tx.method, tx.args, { gas: "300 Tgas", attachedDeposit: "0 yocto" }).send({ waitUntil: "NONE" }))?.transaction?.hash, signedWith: "env" }; } async function submitRegistryWrite(tx, strategy) { const resolved = strategy ?? await resolveSigningStrategy(tx); if (resolved.strategy === "near-cli-keychain") return { ...await executeKeychainTransaction({ account: tx.account, contract: tx.contract, method: tx.method, args: tx.args, network: tx.network }), signedWith: "near-cli-keychain" }; return submitWithNearKit(tx, resolved.privateKey); } //#endregion export { describeSigningStrategy, resolveSigningKey, resolveSigningStrategy, submitRegistryWrite }; //# sourceMappingURL=near-signer.mjs.map