1

我是加密货币和区块链的新手。我需要将 testnet-bitcoins 从一个地址发送到另一个地址。我想使用 blockcypher api 来创建一个事务:

https://www.blockcypher.com/dev/bitcoin/#creating-transactions

我使用 bitcore 和 buffer 库创建了两个新地址,如下所示:

  var rand_buff = bitcore.crypto.Random.getRandomBuffer(32);
  var rand_num = bitcore.crypto.BN.fromBuffer(rand_buff);
  var PrivateKeyWIF = bitcore.PrivateKey("testnet").toWIF();
  var privateKey = bitcore.PrivateKey.fromWIF(PrivateKeyWIF);
  var address = privateKey.toAddress();
  //print("address: " + address + "\nprivate key: " + privateKey);

我找不到资源来帮助我了解有关测试网交易的更多信息。如果您能指出任何有价值的资源,我将不胜感激。

每当我使用有效地址(由我创建)向 blockcypher 发出请求时。我收到一个错误 - 400 Bad Request 产生的 JSON:

  "errors": [
    {
      "error": "Unable to find a transaction to spend for address mxeHfaF413y6xzB3S1NnGsXzK6ewYxsQ2R."
    },
    {
      "error": "Error building transaction: Invalid output address: wrong network.."
    },
    {
      "error": "Not enough funds after fees in 0 inputs to pay for 0 outputs, missing -6800."
    },
    {
      "error": "Error validating generated transaction: Transaction missing input or output."
    }
  ...
}

当我使用 Blockcypher 文档中提到的地址时,请求是有效的。

我根据 blockcypher 文档中的代码创建了交易函数。

function transact() {
//getsourceaddress
  var sourceAddress = document.getElementById("transactionInput").value;
///getdesAddress
  var destinationAddress = document.getElementById("transactionOutput").value;
//get privateKey
  var pk = document.getElementById("pk").value;
//get Transaction Amt
  var transactionAmount = document.getElementById("transactionAmount").value;

//make sure these fields are complete
  if (destinationAddress === "" || pk === "" || transactionAmount === "") {
    print("EMPTY");
    return null;
  } else {
//convert to INT
    var transactionAmountSatoshis = parseInt(transactionAmount, 10);
    //var keys = new bitcoin.ECPair(bitcoin.bigi.fromHex(pk));
    var inaddress = [];
    inaddress.push(sourceAddress);
    var outaddress = [];
    outaddress.push(destinationAddress);
    var newtx = {
      inputs: [{ addresses: inaddress }],
      outputs: [{ addresses: outaddress, value: transactionAmountSatoshis }]
    };
    //print(newtx);
    //request
    $.post(
      "https://api.blockcypher.com/v1/bcy/test/txs/new",
      JSON.stringify(newtx)
    )
      .then(function(tmptx) {
        // signing each of the hex-encoded string required to finalize the transaction
        tmptx.pubkeys = [];
        tmptx.signatures = tmptx.tosign.map(function(tosign, n) {
          tmptx.pubkeys.push(keys.getPublicKeyBuffer().toString("hex"));
          return keys
            .sign(new buffer.Buffer(tosign, "hex"))
            .toDER()
            .toString("hex");
        });
        // sending back the transaction with all the signatures to broadcast
        $.post("https://api.blockcypher.com/v1/bcy/test/txs/send", tmptx).then(
          function(finaltx) {
            console.log(finaltx);
          }
        );
      })
      .catch(function(e) {
        window.alert("Failed!");
        console.log(e);
      });
  }

  print(newtx);
}

预期:TXSkeleton 对象

实际:错误 JSON

4

0 回答 0