forked from BitzenyCoreDevelopers/bitcore-bitzeny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateAndSignTx-PayToPubkeyHash.js
73 lines (63 loc) · 1.79 KB
/
CreateAndSignTx-PayToPubkeyHash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var run = function() {
bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore;
var priv = 'cTgGUrcro89yUtKeG6gHBAS14r3qp25KwTTxG9d4kEzcFxecuZDm';
var amt = '0.005';
var toAddress = 'myuAQcCc1REUgXGsCTiYhZvPPc3XxZ36G1';
var changeAddressString = 'moDz3jEo9q7CxjBDjmb13sL4SKkgo2AACE';
var utxos = [{
address: "mqSjTad2TKbPcKQ3Jq4kgCkKatyN44UMgZ",
txid: "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
vout: 1,
ts: 1394719301,
scriptPubKey: "76a9146ce4e1163eb18939b1440c42844d5f0261c0338288ac",
amount: 0.01,
confirmations: 2
}];
console.log('TX Data: BTC:' + amt + ' => ' + toAddress + ', change To:' + changeAddressString);
console.log('Unspends Outputs:', utxos);
var outs = [{
address: toAddress,
amount: amt
}];
var keys = [priv];
var opts = {
remainderOut: {
address: changeAddressString
}
};
var Builder = bitcore.TransactionBuilder;
var tx = new Builder(opts)
.setUnspent(utxos)
.setOutputs(outs)
.sign(keys)
.build();
/* create and signing can be done in multiple steps using:
*
* var builder = new bitcore.TransactionBuilder(opts)
* .setUnspent(utxos)
* .setOutputs(outs);
*
* builder.sign(key1);
* builder.sign(key2);
* ...
* if (builder.isFullySigned()){
* var tx = builder.build();
* }
*
* The selected Unspent Outputs for the transaction can be retrieved with:
*
* var selectedUnspent = build.getSelectedUnspent();
*/
var txHex = tx.serialize().toString('hex');
console.log('TX HEX IS: ', txHex);
};
// This is just for browser & mocha compatibility
if (typeof module !== 'undefined') {
module.exports.run = run;
if (require.main === module) {
run();
}
} else {
run();
}
////