everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
70 lines (68 loc) • 3.5 kB
JavaScript
let _outlayer_sdk = require("@outlayer/sdk");
//#region src/services/auth/custody-signer.ts
const PUBLISH_GAS = "30000000000000";
const MIN_GAS_BALANCE = "50000000000000000000000";
const OUTLAYER_HOSTS = {
mainnet: "https://api.outlayer.ai",
testnet: "https://testnet-api.outlayer.ai"
};
function custodyClient(custodyWallet) {
const network = custodyWallet.network;
return new _outlayer_sdk.OutlayerClient({
apiKey: custodyWallet.apiKey,
network,
baseUrl: OUTLAYER_HOSTS[network]
});
}
function custodyFundUrl(custodyWallet) {
return `https://app.outlayer.ai/wallet/fund?to=${custodyWallet.nearAccountId}&amount=0.05&token=near`;
}
async function ensureCustodyBalance(custodyWallet) {
const client = custodyClient(custodyWallet);
try {
const { balance } = await client.getBalance({ chain: "near" });
if (BigInt(balance) < BigInt(MIN_GAS_BALANCE)) throw new Error(`The custody wallet ${custodyWallet.nearAccountId} holds ${balance} yoctoNEAR on ${custodyWallet.network}, below the gas minimum. Fund it at ${custodyFundUrl(custodyWallet)}`);
} catch (error) {
throw custodyPublishError(error, custodyWallet);
}
}
async function submitRegistryWriteCustody(opts) {
const client = custodyClient(opts.custodyWallet);
try {
const result = await client.call({
receiver_id: opts.contract,
method_name: "__fastdata_kv",
args: opts.args,
gas: PUBLISH_GAS,
deposit: "0",
...opts.idempotencyKey ? { idempotencyKey: opts.idempotencyKey } : {}
});
if (result.status === "failed" || result.status === "rejected") throw new Error(`The custody publish did not settle (status ${result.status}). Inspect request ${result.request_id} at the OutLayer dashboard.`);
if (result.status === "pending_approval") throw new Error(`The custody publish needs multisig approval (${result.approved ?? 0}/${result.required ?? "?"} so far). Approve it at the OutLayer dashboard, then re-run the deploy.`);
return {
success: true,
txHash: result.tx_hash ?? void 0
};
} catch (error) {
if ((error instanceof _outlayer_sdk.OutlayerError ? error.code : "") === "onchain_tx_failed") return { success: true };
throw custodyPublishError(error, opts.custodyWallet);
}
}
function custodyPublishError(error, custodyWallet) {
if (!(error instanceof _outlayer_sdk.OutlayerError)) return error;
const code = error.code;
const context = custodyWallet.handoffUrl ? ` Manage the wallet at ${custodyWallet.handoffUrl}.` : "";
if (error instanceof _outlayer_sdk.PolicyDeniedError) return /* @__PURE__ */ new Error(`The custody wallet policy refused the publish.${context}`);
if (error instanceof _outlayer_sdk.WalletFrozenError) return /* @__PURE__ */ new Error(`The custody wallet is frozen.${context}`);
if (code === "insufficient_balance" || code === "wallet_underfunded") {
const detail = error.message ? ` (${error.message})` : "";
return /* @__PURE__ */ new Error(`The custody wallet ran out of gas for the publish${detail}. Fund it at ${custodyFundUrl(custodyWallet)}`);
}
if (code === "invalid_api_key" || code === "missing_auth") return /* @__PURE__ */ new Error(`The stored custody wallet key was rejected. Re-run \`bos login --custody\` to re-register.`);
return /* @__PURE__ */ new Error(`Custody publish failed (${code}): ${error.message}`);
}
//#endregion
exports.custodyFundUrl = custodyFundUrl;
exports.ensureCustodyBalance = ensureCustodyBalance;
exports.submitRegistryWriteCustody = submitRegistryWriteCustody;
//# sourceMappingURL=custody-signer.cjs.map