在bitcoincashjs的GitHub上面鞭衩,我沒有找到關(guān)于使用bitcoincashjs來創(chuàng)建多對(duì)多的交易踱蠢,于是我就仿照bitcoinjs里面的2-to-2創(chuàng)建交易呛梆,在這里我使用了一個(gè)有BCH測(cè)試幣的賬戶胧弛,分別轉(zhuǎn)給自己和另一個(gè)賬號(hào),相當(dāng)于是一個(gè)賬戶轉(zhuǎn)給兩個(gè)賬戶惹资。
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 = "";
//傳送到測(cè)試鏈上面去
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 就是這樣構(gòu)建的,運(yùn)行這段代碼航闺,成功的返回了一個(gè)交易號(hào)
5733d982b25d4f610a5602348850e0dada1deeb9f319d55c7096686b40e72fa3
通過交易號(hào)可以在這里(blocktrail)看到這一筆交易褪测。
我想這樣應(yīng)該是證明成功了。
這里是一個(gè)二對(duì)一的轉(zhuǎn)賬
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 = "";
//傳送到測(cè)試鏈上面去
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();