-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·67 lines (57 loc) · 2.25 KB
/
index.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
console.log('\n'); // print newline to separate different runs
var bitcore = require('bitcore-lib');
var privateKeyWIF = 'cV4E8HKBsx22rvLxS8nzMBeW82LBpVKndphGvDbrd6f5trt37Pw8';
var privateKey = bitcore.PrivateKey.fromWIF(privateKeyWIF);
var address = privateKey.toAddress();
// console.log('address:');
// console.log(address); // should output: mtk5Avrm57v3cnsgKkECZrVv42gDYtZTiA
var value = new Buffer('this is a way to generate an address from a string--risky--not random--guessable!!!');
var hash = bitcore.crypto.Hash.sha256(value);
var bn = bitcore.crypto.BN.fromBuffer(hash);
var address2 = new bitcore.PrivateKey(bn,'testnet').toAddress();
// console.log('address2:');
// console.log(address2);
var Insight = require('bitcore-explorers').Insight;
var insight = new Insight('testnet');
insight.getUnspentUtxos(address, function(err, utxos) {
if (err) {
// Handle errors...
console.log('error getting unspent outputs')
} else {
// use the UTXOs to create a transaction
//console.log(utxos);
var tx = bitcore.Transaction();
tx.from(utxos);
tx.to(address2, 10000); // .0001 BTC
tx.change(address); // recommend new address here for better privacy
tx.fee(50000);
tx.sign(privateKey);
//console.log('transaction:');
//console.log(tx.toObject());
tx.serialize();
// alternate way to broadcast transaction.
// paste at https://test-insight.bitpay.com/tx/send
console.log('serialized output:');
console.log(tx.serialize());
// script printing
// var scriptIn = bitcore.Script(tx.toObject().inputs[0].script);
// console.log('input script string: ');
// console.log(scriptIn.toString());
// var scriptOut = bitcore.Script(tx.toObject().outputs[0].script);
// console.log('output script string: ');
// console.log(scriptOut.toString());
// Store data in a transaction (up to 80 bytes, amount sent can
// never be spent)
//tx.addData()
// broadcast using bitpay's insight server
// insight.broadcast(tx, function(err, returnedTxId) {
// if (err) {
// // Handle errors...
// console.log(err);
// } else {
// // Mark the transaction as broadcasted
// console.log('sucessful broadcast: ' + returnedTxId);
// }
// });
}
});