simpleledgerinc / grpc-bchrpc-node

A BCHD gRPC client for node.js
11 stars 10 forks source link

Documenting var x = await grpc.getUnspentTransaction(?????? #2

Closed arsen3d closed 4 years ago

arsen3d commented 4 years ago

How do I know how to create parameters that go there. Some example code would be very helpful.

arsen3d commented 4 years ago

Here is a unit test example I got working. It would be nice to have the library return a javascript UTXOs object array so that there is not need to do the kind of transformation I had to do.

it('grpc.client.getAddressUtxos() to bchd.greyh.at:8335', async function () {
  let pks2 =  process.env.pks2;
  let privateKey = new bitcore.PrivateKey(pks2);
  //console.log(privateKey.toJSON().bn)
  let myAddress = privateKey.toAddress().toString().split(":")[1];
  let grpc = new GrpcClient();
  let utxosRaw = await grpc.getAddressUtxos("qpdjavghgulay9ym7rfd600dml5vlaqexguc482d62");
  assert.strictEqual(utxosRaw.toObject(false).outputsList.length,1)
  let jsUtxos = [];
  for (let x  = 0;x<utxosRaw.toObject(false).outputsList.length; x++){
    let id = Buffer.from(utxosRaw.getOutputsList()[x].getOutpoint().getHash_asU8().reverse()).toString("hex");
    let script =  Buffer.from(utxosRaw.getOutputsList()[x].getPubkeyScript_asU8()).toString("hex");
    let value = utxosRaw.toObject(false).outputsList[x].value;
    console.log("My Address: ",myAddress );
    console.log("Output TX: ",id );
    console.log("Script: ",script);
    console.log("Value: ",value);
    let utxo = {}
    utxo.txId = id;
    utxo.outputIndex= x;
    utxo.address= myAddress;
    utxo.script=  script;
    utxo.satoshis= value;
    jsUtxos.push(utxo)
  }
  if(jsUtxos.length == 0)
  {
    console.log("No UTXOS Check address")
    return;
  }

  console.log("UTXOs: ",JSON.stringify(jsUtxos,null,2));
  let t = bitcore.Transaction();
  t.from(jsUtxos)
  t.to("qqyf3gadtkruv0yzcmgq7tydcvg0guflec83sfdhdt", 1001)
  t.change(myAddress)
  t.sign(privateKey);

  console.log("Is verify: ",t.verify())
  console.log("isFullySigned: ",t.isFullySigned())

  console.log("RAW Transaction to broadcast ",t.toString());
});