美文网首页区块链大学区块链研习社
用bitconcashjs在BCH测试网络创建多对多的交易

用bitconcashjs在BCH测试网络创建多对多的交易

作者: 宋廉 | 来源:发表于2018-08-12 16:32 被阅读1次

在bitcoincashjs的GitHub上面,我没有找到关于使用bitcoincashjs来创建多对多的交易,于是我就仿照bitcoinjs里面的2-to-2创建交易,在这里我使用了一个有BCH测试币的账户,分别转给自己和另一个账号,相当于是一个账户转给两个账户。

const myPrivateKey1 = "私钥"; //string
const mytxId =
  "1081baf7b2123ebdc55e19402e7c1a0a79025297d24ab1b51564b40e3e975dcd"; //string
const myoutputIndex = 0; //int defult is 0
const myaddress = "mhMmYqtR9wzbW9otiurkG9AGVNzjfww36v"; //string
const myscriptPubKey = "76a914143389o9b3c7be44c525f5h649f69ecef941acad88ac"; //string
const mysatoshis = 260000000; //number  defult is 0
const toaddress1 = "mhMmYqtR9wzbW9otiurkG9AGVNzjfww36v"; //string
const toaddress2 = "mr9zbZsDtng5r7mv6N24cDAoQGLwZge1ps";
const toastoshis1 = 250000000; //number defult is 0
const toastoshis2 = 9990000;
var e = "";
//传送到测试链上面去
async function mytrade() {
  await mytransaction();
  await mysend();
}

function mytransaction() {
  const privateKey1 = new bch.PrivateKey(myPrivateKey1);
  const utxo1 = {
    txId: mytxId,
    outputIndex: myoutputIndex,
    address: myaddress,
    script: myscriptPubKey,
    satoshis: mysatoshis
  };

  const transaction = new bch.Transaction()
    .from(utxo1)
    .to(toaddress1, toastoshis1)
    .to(toaddress2, toastoshis2)
    .sign(privateKey1)
  e = transaction.toString();
  console.log(e);
}

function mysend() {
  client.cmd("sendrawtransaction", e, function(err, data) {
    if (err) {
      console.log(err);
      return;
    }
    console.log(data);
  });
}

mytrade();

ok 就是这样构建的,运行这段代码,成功的返回了一个交易号

5733d982b25d4f610a5602348850e0dada1deeb9f319d55c7096686b40e72fa3

通过交易号可以在这里(blocktrail)看到这一笔交易。

2018-08-12 下午4.30.41.png
我想这样应该是证明成功了。
这里是一个二对一的转账
const myPrivateKey1 = "私钥1"; //string
const myPrivateKey2 = "私钥2"; //string
const mytxId1 =
  "5733d982b25d4f610a5602348850e0dada1deeb9f319d55c7096686b40e72fa3"; //string
  const mytxId2 =
  "5733d982b25d4f610a5602348850e0dada1deeb9f319d55c7096686b40e72fa3"; //string
const myoutputIndex1 = 0; //int defult is 0
const myoutputIndex2 = 1; //int defult is 0

const myaddress1 = "mhMmYqtR9wzbW9otiurkG9AGVNzjfww36v"; //string
const myaddress2 = "mr9zbZsDtng5r7mv6N24cDAoQGLwZge1ps"; //string
const myscriptPubKey1 = "76a914143389d6b3c7be44c525f5f349f69ecef941acad88ac"; //string
const myscriptPubKey2 = "76a91474b291eb86b265beeabc8bd6d7d56557f56244dd88ac"; //string
const mysatoshis1 = 250000000; //number  defult is 0
const mysatoshis2 = 9990000; //number  defult is 0
const toaddress1 = "mhMmYqtR9wzbW9otiurkG9AGVNzjfww36v"; //string
const toaddress2 = "mhMmYqtR9wzbW9otiurkG9AGVNzjfww36v";
const toastoshis1 = 250000000; //number defult is 0
const toastoshis2 = 9980000;
var e = "";
//传送到测试链上面去
async function mytrade() {
  await mytransaction();
  await mysend();
}

function mytransaction() {
  const privateKey1 = new bch.PrivateKey(myPrivateKey1);
  const privateKey2 = new bch.PrivateKey(myPrivateKey2);
  const utxo1 = {
    txId: mytxId1,
    outputIndex: myoutputIndex1,
    address: myaddress1,
    script: myscriptPubKey1,
    satoshis: mysatoshis1
  };
  const utxo2 = {
    txId: mytxId2,
    outputIndex: myoutputIndex2,
    address: myaddress2,
    script: myscriptPubKey2,
    satoshis: mysatoshis2
  };
  const transaction = new bch.Transaction()
    .from(utxo1)
    .from(utxo2)
    .to(toaddress1, toastoshis1)
    .to(toaddress2, toastoshis2)
    .sign(privateKey1)
    .sign(privateKey2)
  e = transaction.toString();
  console.log(e);
}

function mysend() {
  client.cmd("sendrawtransaction", e, function(err, data) {
    if (err) {
      console.log(err);
      return;
    }
    console.log(data);
  });
}

mytrade();

相关文章

网友评论

    本文标题:用bitconcashjs在BCH测试网络创建多对多的交易

    本文链接:https://www.haomeiwen.com/subject/gaisbftx.html