-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnot_so_simple_payment_api_usage.js
87 lines (72 loc) · 2.8 KB
/
not_so_simple_payment_api_usage.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* jshint -W101 */
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project
var bitcoin = require('bitcoinjs-lib');
var client = blocktrail.BlocktrailSDK({
apiKey : "YOUR_APIKEY_HERE",
apiSecret : "YOUR_APISECRET_HERE",
testnet : true
});
/*
* this example is for when you're storing the primary private and backup public key yourself
*/
var primaryPrivateKey = bitcoin.HDNode.fromBase58("tprv8ZgxMBicQKsPdMD2AYgpezVQZNi5kxsRJDpQWc5E9mxp747KgzekJbCkvhqv6sBTDErTjkWqZdY14rLP1YL3cJawEtEp2dufHxPhr1YUoeS", bitcoin.networks.testnet);
var backupPublicKey = bitcoin.HDNode.fromBase58("tpubD6NzVbkrYhZ4Y6Ny2VF2o5wkBGuZLQAsGPn88Y4JzKZH9siB85txQyYq3sDjRBFwnE1YhdthmHWWAurJu7EetmdeJH9M5jz3Chk7Ymw2oyf", bitcoin.networks.testnet);
var sendTransaction = function(wallet) {
wallet.getNewAddress(function(err, address, path) {
if (err) {
return console.log("getNewAddress ERR", err);
}
console.log('new address', address, path);
var pay = {};
pay[address] = blocktrail.toSatoshi(0.001);
wallet.pay(pay, function(err, result) {
if (err) {
return console.log("pay ERR", err);
}
console.log('transaction', result);
});
});
};
var action = 'default';
if (action === 'create') {
client.createNewWallet({
identifier: "example-wallet",
keyIndex: 9999,
primaryPrivateKey: primaryPrivateKey,
backupPublicKey: backupPublicKey
}, function(err, wallet, primaryMnemonic, backupMnemonic, blocktrailPubKeys) {
if (err) {
return console.log("createNewWallet ERR", err);
}
console.log('primary mnemonic', primaryMnemonic);
console.log('backup mnemonic', backupMnemonic);
console.log('blocktrail pubkeys', blocktrailPubKeys);
wallet.getNewAddress(function(err, address, path) {
if (err) {
return console.log("getNewAddress ERR", err);
}
console.log('new address', address, path);
});
}
);
} else {
client.initWallet({
identifier: "example-wallet",
keyIndex: 9999,
primaryPrivateKey: primaryPrivateKey,
primaryMnemonic: false
}, function(err, wallet) {
if (err) {
return console.log('initWallet ERR', err);
}
wallet.getBalance(function(err, confirmed, unconfirmed) {
if (err) {
return console.log("getBalance ERR", err);
}
console.log('confirmed balance', confirmed);
console.log('unconfirmed balance', unconfirmed);
sendTransaction(wallet);
});
}
);
}