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