-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwallet_v1_recovery_example.js
81 lines (69 loc) · 2.56 KB
/
wallet_v1_recovery_example.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
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project
var backupMnemonic = "canal shrimp family budget husband ceiling vital pole senior eagle ceiling silver melt exit boss error feature battle floor danger orphan prize give step";
var identifier = "example-wallet-with-password-lost";
var apiKey = "MY_APIKEY";
var apiSecret = "MY_APISECRET";
var testnet = true;
var sendTo = [
"2NF2Urotmr39ESYp8sBxT6ZSEXwdfY78zU9"
];
var sendPercentage = 1;
var twoFactorToken = null;
var client = blocktrail.BlocktrailSDK({
apiKey : apiKey,
apiSecret : apiSecret,
testnet: testnet
});
/* // put // at the start of this line to create a wallet
return client.createNewWallet({
identifier: identifier,
passphrase: "example-password-that-we-forgot",
walletVersion: blocktrail.Wallet.WALLET_VERSION_V1,
keyIndex: 9999
})
.spread(
function(wallet, backupInfo) {
console.log(backupInfo.backupMnemonic);
return wallet.getNewAddress().spread(function(address) {
return client.faucetWithdrawl(address, blocktrail.toSatoshi(0.001)).then(function(result) {
console.log(result);
});
});
})
.catch(function(err) {
console.error(err);
throw err;
})
;
//*/
return client.initWallet({
identifier: identifier,
readOnly: true
})
.then(function(wallet) {
// 'unlock' using backupMnemonic
return client.mnemonicToPrivateKey(backupMnemonic, "").then(function(backupPrivateKey) {
wallet.backupPrivateKey = backupPrivateKey;
wallet.locked = false;
return wallet.maxSpendable({allowZeroConf: true, outputs: sendTo.length}).then(function(maxSpendable) {
console.log(maxSpendable);
var pay = {};
var sendAmount = Math.floor(maxSpendable.max * sendPercentage);
var spendable = sendAmount;
console.log(blocktrail.toBTC(maxSpendable.max), blocktrail.toBTC(sendAmount));
sendTo.forEach(function(address) {
var value = Math.min(spendable, Math.ceil(sendAmount / sendTo.length));
pay[address] = value;
spendable -= value;
});
return wallet.pay(pay, null, true, true, null, twoFactorToken).then(function(result) {
console.log(result);
});
});
});
})
.catch(function(err) {
console.error(err);
throw err;
})
;