参考
How to create a raw Bitcoin Cash transaction using JavaScript
ライブラリのインストール
npm install bch-lib
ライブラリのインポート
var bchLib = require('@owstack/bch-lib')
秘密鍵の設定
var privateKey = new bchLib.PrivateKey('your private key here');
transactionのUTXOを設定
var utxo = { "txId" : "f8a258720cb68be265baf6828612a401567f916ee50fc276d817f647e4bc1246", "vout" : 0, "address" : "158avSA4mpuCsKDn8ntCdf5XCrofvmy7M1", "scriptPubKey" : bchLib.Script.buildPublicKeyHashOut("158avSA4mpuCsKDn8ntCdf5XCrofvmy7M1").toString(), "amount" : 0.0009887 };
transactionの生成
var transaction = new bchLib.Transaction()
.from(utxo)
.to('18dHsjN9ZZxt99cd8bFJ3r7ajWmidgJEQq', 20000)
.fee(1000)
.change('158avSA4mpuCsKDn8ntCdf5XCrofvmy7M1')
.sign(privateKey);
raw transactionの確認
console.log(transaction.toString());
例
01000000014612bce447f617d876c20fe56e917f5601a4128682f6ba65e28bb60c7258a2f8010000008a4730440220176c12031476cb9c781cd3dd47b931ae658394eaf4d22f6376d1a4ab8b0bfc5f022006deb0679cbb600899fa0345d5bf824e34fb9c5e640f8d3aa99bed34c070446d4141047aa0717672b1c86aa13d83e7c7b7cef010f8f1289d41cb14bbef20884efa5342a34a955663ace7fd5bf879f7972a97e7f28a067f74bc78a1287b06f8d6691f54ffffffff01204e0000000000001976a91453a5f0fd9b32593e0f1b4ac418773021e996457c88ac00000000
サンプル
// using osx, install brew, npm and bch-lib
// brew install npm
// npm install bch-lib
// to run the script 'node raw.js'
var bchLib = require('@owstack/bch-lib')
// you can create a paper wallet from https://walletgenerator.net/?currency=BitcoinCash
// of course, you can also create a wallet here using bch-lib
var privateKey = new bchLib.PrivateKey('your private key here');
// notice that the unspent amount has to be exact or the transaction will fail with a the following error
// 16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element) (code -26)"
var utxo = {
"txId" : "f8a258720cb68be265baf6828612a401567f916ee50fc276d817f647e4bc1246",
"vout" : 0,
"address" : "158avSA4mpuCsKDn8ntCdf5XCrofvmy7M1",
"scriptPubKey" : bchLib.Script.buildPublicKeyHashOut("158avSA4mpuCsKDn8ntCdf5XCrofvmy7M1").toString(),
"amount" : 0.0009887
};
// notice that the change address is the same as the address in the utxo
var transaction = new bchLib.Transaction()
.from(utxo)
.to('18dHsjN9ZZxt99cd8bFJ3r7ajWmidgJEQq', 20000)
.fee(1000)
.change('158avSA4mpuCsKDn8ntCdf5XCrofvmy7M1')
.sign(privateKey);
console.log(transaction.toString());