everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
73 lines (71 loc) • 3.03 kB
JavaScript
const require_near_cli = require('./near-cli.cjs');
let near_kit = require("near-kit");
let near_kit_keys_file = require("near-kit/keys/file");
//#region src/near-signer.ts
function assertPrivateKey(key) {
if (!(0, near_kit.isPrivateKey)(key)) throw new require_near_cli.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 near_kit_keys_file.FileKeyStore("~/.near-credentials", opts.network).get(opts.account);
if (keyPair?.secretKey) return {
source: "credentials-file",
privateKey: keyPair.secretKey
};
throw new require_near_cli.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 require_near_cli.isNearCliInstalled()) return { strategy: "near-cli-keychain" };
throw new require_near_cli.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_kit.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 require_near_cli.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
exports.describeSigningStrategy = describeSigningStrategy;
exports.resolveSigningKey = resolveSigningKey;
exports.resolveSigningStrategy = resolveSigningStrategy;
exports.submitRegistryWrite = submitRegistryWrite;
//# sourceMappingURL=near-signer.cjs.map