How to create a bitcoin-testnet transaction - blockcypher?

1.7k Views Asked by At

I'm new to cryptocurrencies and blockchain. I need to send testnet-bitcoins from one address to the other. I want to use the blockcypher api to create a transaction:

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

I created two new addresses using the bitcore and buffer libraries like so:

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

I am unable to find a resource to help me with learning more about testnet transactions. I would appreciate if you could point me towards any valuable resource.

Whenever I make a request to blockcypher with valid addresses (created by me). I get an error - 400 Bad Request with the resulting 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."
    }
  ...
}

When I use addresses mentioned in the Blockcypher Documentation, the request is valid.

I created the transact function based on the code in blockcypher's documentation.

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

Expected: TXSkeleton Object

Actual: Error JSON

1

There are 1 best solutions below

0
On

BlockCypher's Test Chain: api.blockcypher.com/v1/bcy/test

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