1
|
|
|
/* jshint -W101 */ |
2
|
|
|
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project |
3
|
|
|
var bitcoin = require('bitcoinjs-lib'); |
4
|
|
|
|
5
|
|
|
var client = blocktrail.BlocktrailSDK({ |
6
|
|
|
apiKey : "YOUR_APIKEY_HERE", |
7
|
|
|
apiSecret : "YOUR_APISECRET_HERE", |
8
|
|
|
testnet : true |
9
|
|
|
}); |
10
|
|
|
|
11
|
|
|
/* |
12
|
|
|
* this example is for when you're storing the primary private and backup public key yourself |
13
|
|
|
*/ |
14
|
|
|
var primaryPrivateKey = bitcoin.HDNode.fromBase58("tprv8ZgxMBicQKsPdMD2AYgpezVQZNi5kxsRJDpQWc5E9mxp747KgzekJbCkvhqv6sBTDErTjkWqZdY14rLP1YL3cJawEtEp2dufHxPhr1YUoeS", bitcoin.networks.testnet); |
15
|
|
|
var backupPublicKey = bitcoin.HDNode.fromBase58("tpubD6NzVbkrYhZ4Y6Ny2VF2o5wkBGuZLQAsGPn88Y4JzKZH9siB85txQyYq3sDjRBFwnE1YhdthmHWWAurJu7EetmdeJH9M5jz3Chk7Ymw2oyf", bitcoin.networks.testnet); |
16
|
|
|
|
17
|
|
|
var sendTransaction = function(wallet) { |
18
|
|
|
wallet.getNewAddress(function(err, address, path) { |
19
|
|
|
if (err) { |
20
|
|
|
return console.log("getNewAddress ERR", err); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
console.log('new address', address, path); |
24
|
|
|
|
25
|
|
|
var pay = {}; |
26
|
|
|
pay[address] = blocktrail.toSatoshi(0.001); |
27
|
|
|
|
28
|
|
|
wallet.pay(pay, function(err, result) { |
29
|
|
|
if (err) { |
30
|
|
|
return console.log("pay ERR", err); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
console.log('transaction', result); |
|
|
|
|
34
|
|
|
}); |
|
|
|
|
35
|
|
|
}); |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
var action = 'default'; |
39
|
|
|
|
40
|
|
|
if (action === 'create') { |
41
|
|
|
client.createNewWallet({ |
42
|
|
|
identifier: "example-wallet", |
43
|
|
|
keyIndex: 9999, |
44
|
|
|
primaryPrivateKey: primaryPrivateKey, |
45
|
|
|
backupPublicKey: backupPublicKey |
46
|
|
|
}, function(err, wallet, primaryMnemonic, backupMnemonic, blocktrailPubKeys) { |
47
|
|
|
if (err) { |
48
|
|
|
return console.log("createNewWallet ERR", err); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
console.log('primary mnemonic', primaryMnemonic); |
52
|
|
|
console.log('backup mnemonic', backupMnemonic); |
53
|
|
|
console.log('blocktrail pubkeys', blocktrailPubKeys); |
54
|
|
|
|
55
|
|
|
wallet.getNewAddress(function(err, address, path) { |
56
|
|
|
if (err) { |
57
|
|
|
return console.log("getNewAddress ERR", err); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
console.log('new address', address, path); |
|
|
|
|
61
|
|
|
}); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
); |
64
|
|
|
} else { |
65
|
|
|
client.initWallet({ |
66
|
|
|
identifier: "example-wallet", |
67
|
|
|
keyIndex: 9999, |
68
|
|
|
primaryPrivateKey: primaryPrivateKey, |
69
|
|
|
primaryMnemonic: false |
70
|
|
|
}, function(err, wallet) { |
71
|
|
|
if (err) { |
72
|
|
|
return console.log('initWallet ERR', err); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
wallet.getBalance(function(err, confirmed, unconfirmed) { |
76
|
|
|
if (err) { |
77
|
|
|
return console.log("getBalance ERR", err); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
console.log('confirmed balance', confirmed); |
81
|
|
|
console.log('unconfirmed balance', unconfirmed); |
82
|
|
|
|
83
|
|
|
sendTransaction(wallet); |
|
|
|
|
84
|
|
|
}); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|