UNPKG

@mayaprotocol/zcash-js

Version:

Zcash JavaScript library for Maya Protocol - Build and sign Zcash transparent transactions with memo support

73 lines (72 loc) 2.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUTXOS = getUTXOS; exports.waitForTransaction = waitForTransaction; exports.sendRawTransaction = sendRawTransaction; const axios_1 = __importDefault(require("axios")); const json_rpc_2_0_1 = require("json-rpc-2.0"); function makeClient(config) { const client = new json_rpc_2_0_1.JSONRPCClient(async (jsonRPCRequest) => { const response = await axios_1.default.post(config.server.host, jsonRPCRequest, { headers: { 'Content-Type': 'application/json' }, auth: { username: config.server.user, password: config.server.password } }); client.receive(response.data); }); return client; } async function getUTXOS(from, config) { const client = makeClient(config); const utxos = await client.request('getaddressutxos', [from]); return utxos; } async function waitForTransaction(txid, config, maxAttempts = 30) { const client = makeClient(config); for (let i = 0; i < maxAttempts; i++) { try { const tx = await client.request('gettransaction', [txid]); if (tx && tx.confirmations > 0) { return; } } catch (e) { // Transaction might not be in wallet } await new Promise(resolve => setTimeout(resolve, 1000)); } throw new Error(`Transaction ${txid} not confirmed after ${maxAttempts} attempts`); } async function sendRawTransaction(txb, config) { try { // Direct axios call to get better error details const response = await axios_1.default.post(config.server.host, { jsonrpc: '2.0', method: 'sendrawtransaction', params: [txb.toString('hex')], id: 1 }, { headers: { 'Content-Type': 'application/json' }, auth: { username: config.server.user, password: config.server.password } }); if (response.data.error) { console.error('RPC Error:', response.data.error); throw new Error(response.data.error.message); } return response.data.result; } catch (error) { if (error.response?.data?.error) { throw new Error(error.response.data.error.message); } throw error; } }