forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistWalletTransactions.js
54 lines (44 loc) · 1.61 KB
/
listWalletTransactions.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
//
// Show recent transactions within a wallet
//
// Copyright 2014, BitGo, Inc. All Rights Reserved.
//
const BitGoJS = require('../src/index.js');
if (process.argv.length <= 5) {
console.log('usage:\n\t' + process.argv[0] + ' ' + process.argv[1] + ' <user> <pass> <otp> <walletId>');
process.exit(-1);
}
const user = process.argv[2];
const password = process.argv[3];
const otp = process.argv[4];
const id = process.argv[5];
const bitgo = new BitGoJS.BitGo();
// First, Authenticate
bitgo.authenticate({ username: user, password: password, otp: otp }, function(err, result) {
if (err) {
console.dir(err);
throw new Error('Could not auth!');
}
console.log('Logged in!' );
// Now get the wallet
bitgo.wallets().get({ type: 'bitcoin', id: id }, function(err, wallet) {
if (err) { console.log(err); process.exit(-1); }
console.log('Balance is: ' + (wallet.balance() / 1e8).toFixed(4));
// Now get the transactions
wallet.transactions({}, function(err, result) {
if (err) { console.log(err); process.exit(-1); }
for (let index = 0; index < result.transactions.length; ++index) {
const tx = result.transactions[index];
let value = 0;
for (let entriesIndex = 0; entriesIndex < tx.entries.length; ++entriesIndex) {
if (tx.entries[entriesIndex].account === wallet.id()) {
value += tx.entries[entriesIndex].value;
}
}
const verb = (value > 0) ? 'Received' : 'Sent';
const output = tx.id + ': ' + verb + ' ' + (value / 1e8).toFixed(8) + 'BTC on ' + tx.date;
console.log(output);
}
});
});
});