在节点js中通过比特币测试网络中的blockcypher api发送比特币

问题描述 投票:0回答:1

我正在尝试通过 Blockcypher API 在节点 js 的比特币测试网络中发送比特币。这是我的代码:

const axios = require("axios");
const bitcoin = require("bitcoinjs-lib");
const secp = require('tiny-secp256k1');
const ecfacory = require('ecpair');

require("dotenv").config();

const privateKey = process.env.PRIVATE_KEY;

const ECPair = ecfacory.ECPairFactory(secp);

const keyBuffer = Buffer.from(privateKey, 'hex')
const keys = ECPair.fromPrivateKey(keyBuffer)

const sendBtcURL = "https://api.blockcypher.com/v1/btc/test3/txs/new";
const finalSendBtcURL = "https://api.blockcypher.com/v1/btc/test3/txs/send";

const sendBitcoin = async (senderAddress, receiverAddress, amountToSendInSatoshis) => {
  const response = await axios.post(sendBtcURL, {
    inputs: [{
      addresses: [senderAddress]
    }],
    outputs: [{
      addresses: [receiverAddress],
      value: amountToSendInSatoshis
    }]
  });
  let res = response.data;
  res.pubkeys = []; 
  res.signatures = res.tosign.map(function (tosign, n) {
    res.pubkeys.push(publicKey);
    return bitcoin.script.signature.encode(
      keys.sign(Buffer.from(tosign, "hex")),
      0x01,
    ).toString("hex").slice(0, -2);
  }); 
  const sendResponse = await axios.post(finalSendBtcURL, JSON.stringify(res));
  console.log(sendResponse);
};

sendBitcoin(
  "tb1qcfc8mkqpakzrn63scq3l67tcgsxhdf3u2ux2ng",
  "tb1q5l26jmaanf2e0qcfcz5y6zevlv4qnrc9r92jj4",
  500
);

我收到以下错误:

Not enough funds after fees in 1 inputs to pay for 1 outputs, missing -4800.

我确实有 1546 satoshis 来自发送地址

tb1qcfc8mkqpakzrn63scq3l67tcgsxhdf3u2ux2ng
Explorer Link 中所示。

我正在关注 API 文档here。 如何解决这个错误?

node.js blockchain bitcoin bitcoin-testnet
1个回答
0
投票

也许你没有足够的btc,交易费用超过你的余额。尝试获得更多的硬币。

© www.soinside.com 2019 - 2024. All rights reserved.