LoginSignup
1
1

More than 5 years have passed since last update.

Bitcoreを使ったトランザクションの生成(ターミナルから)

Posted at

Bitcoreを使ったトランザクションの生成(ターミナルから)
Macのターミナルを使用。

・bitcoreをインストールする手順は以下の通り
https://bitcore.io/guides/full-node/

$node
var bitcore = require(‘bitcore-lib’);
var privateKey = new bitcore.PrivateKey();
// 秘密鍵を生成
var send_address = privateKey.toAddress();
// PublicKeyHash型のアドレスを生成
var receive_address = ‘1EYPYinomdFgxwYKdjzaqxyLKSHZVYMXot’;
// 再度他の秘密鍵から生成した中身の無いアドレス(サンプルとして)
*予めsend_addressにお金を送金しておくこと。

■Get UTXOS
var Insight = require(‘bitcore-explorers’).Insight;
var insight = new Insight();
var UTXOS = ‘ ’;
insight.getUnspentUtxos(send_address, function(err, utxos) {
if (err) {
// エラー処理
} else {
UTXOS = utxos;
console.log(utxos);
}
});
■Transaction
var Transaction = bitcore.Transaction;
// トランザクションの変数を生成
var transaction = new Transaction().fee(1000) // 手数料の金額をsatoshi単位で指定
.from(UTXOS) // ■Get UTXOSの場面で取得したUTXOSを参照
.to(receive_address, 10000) // (送り先, 送金金額を指定)
.change(send_address) // 自分のアドレスへのおつり
.sign(privateKey) // 自分の秘密鍵で署名
■Broadcast
insight.broadcast(transaction, function(err, returnedTxID) {
if (err) {
// エラー処理
} else {
console.log(returnedTxID); //ブロードキャスト成功の場合、トランザクションのIDがreturn
}
});

【参照】
http://noumenon-th.net/webstrategy/2015/04/21/bitcore-2/
https://bitcore.io/api/

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1