1
|
|
|
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project |
2
|
|
|
|
3
|
|
|
var client = blocktrail.BlocktrailSDK({ |
4
|
|
|
apiKey : process.env.BLOCKTRAIL_SDK_APIKEY || "MY_APIKEY", |
5
|
|
|
apiSecret : process.env.BLOCKTRAIL_SDK_APISECRET || "MY_APISECRET", |
6
|
|
|
testnet : true |
7
|
|
|
}); |
8
|
|
|
|
9
|
|
|
var ALLOW_ZERO_CONF = true; |
10
|
|
|
|
11
|
|
|
var sendTransaction = function(wallet) { |
12
|
|
|
wallet.getNewAddress(function(err, address, path) { |
13
|
|
|
if (err) { |
14
|
|
|
return console.log("getNewAddress ERR", err); |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
console.log('new address', address, path); |
18
|
|
|
|
19
|
|
|
var pay = {}; |
20
|
|
|
pay[address] = blocktrail.toSatoshi(0.001); |
21
|
|
|
|
22
|
|
|
wallet.pay(pay, null, ALLOW_ZERO_CONF, function(err, result) { |
23
|
|
|
// wallet.pay(pay, null, true, true, blocktrail.Wallet.FEE_STRATEGY_LOW_PRIORITY, function(err, result) { |
24
|
|
|
if (err) { |
25
|
|
|
return console.log("pay ERR", err); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
console.log('transaction', result); |
|
|
|
|
29
|
|
|
}); |
|
|
|
|
30
|
|
|
}); |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
var action = 'default'; |
34
|
|
|
|
35
|
|
|
if (action === 'create') { |
36
|
|
|
client.createNewWallet({ |
37
|
|
|
identifier: "example-wallet-sw", |
38
|
|
|
passphrase: "example-strong-password", |
39
|
|
|
keyIndex: 9999 |
40
|
|
|
}, function(err, wallet, primaryMnemonic, backupMnemonic, blocktrailPubKeys) { |
41
|
|
|
if (err) { |
42
|
|
|
return console.log("createNewWallet ERR", err); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
console.log('primary mnemonic', primaryMnemonic); |
46
|
|
|
console.log('backup mnemonic', backupMnemonic); |
47
|
|
|
console.log('blocktrail pubkeys', blocktrailPubKeys); |
|
|
|
|
48
|
|
|
}); |
49
|
|
|
} else { |
50
|
|
|
client.initWallet({ |
51
|
|
|
identifier: "example-wallet-sw", |
52
|
|
|
readOnly: true |
53
|
|
|
}, function(err, wallet) { |
54
|
|
|
if (err) { |
55
|
|
|
console.log('initWallet ERR', err); |
|
|
|
|
56
|
|
|
throw err; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
wallet.getBalance(function(err, confirmed, unconfirmed) { |
60
|
|
|
if (err) { |
61
|
|
|
return console.log("getBalance ERR", err); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
console.log('confirmed balance', confirmed); |
65
|
|
|
console.log('unconfirmed balance', unconfirmed); |
66
|
|
|
|
67
|
|
|
wallet.getNewAddress(function(err, address) { |
68
|
|
|
if (err) { |
69
|
|
|
return console.log("getNewAddress ERR", err); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
console.log('address', address); |
73
|
|
|
|
74
|
|
|
wallet.unlock({passphrase: "example-strong-password"}, function(err) { |
75
|
|
|
if (err) { |
76
|
|
|
return console.log("unlock ERR", err); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
sendTransaction(wallet); |
|
|
|
|
80
|
|
|
}); |
|
|
|
|
81
|
|
|
}); |
|
|
|
|
82
|
|
|
}); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|