簡體   English   中英

如何創建比特幣測試網交易 - blockcypher?

[英]How to create a bitcoin-testnet transaction - blockcypher?

我是加密貨幣和區塊鏈的新手。 我需要將 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

BlockCypher的測試鏈: api.blockcypher.com/v1/bcy/test

bitcoin-Testnet3 blockcypher:主要api.blockcypher.com/v1/btc/test3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM