如何创建一个比特币测试网交易-Blockcypher?

时间:2019-04-07 21:50:17

标签: javascript jquery bitcoin bitcoin-testnet blockcypher

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

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

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

  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);

我找不到资源来帮助我学习有关testnet交易的更多信息。如果您能指出我宝贵的资源,我将不胜感激。

每当我请求使用有效地址(由我创建)进行密码解密时。我收到一个错误-400错误请求,并生成了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

0 个答案:

没有答案