forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddressLabels.js
149 lines (133 loc) · 4.09 KB
/
addressLabels.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
List, set, or delete labels on addresses for a given wallet
Copyright 2018, BitGo, Inc. All Rights Reserved.
*/
const BitGoJS = require('../src/index.js');
const readline = require('readline');
const Q = require('q');
const _ = require('lodash');
_.string = require('underscore.string');
if (process.argv.length <= 4) {
console.log('usage:\n\t' + process.argv[0] + ' ' + process.argv[1] + ' <user> <pass> <otp>');
process.exit(-1);
}
const bitgo = new BitGoJS.BitGo();
const inputs = {}; // Inputs collected from the user & command line.
inputs.user = process.argv[2];
inputs.password = process.argv[3];
inputs.otp = process.argv[4];
//
// collectInputs
// Function to collect inputs from stdin
//
const collectInputs = function() {
const argv = require('minimist')(process.argv.slice(2));
// Prompt the user for input
const prompt = function(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const deferred = Q.defer();
rl.setPrompt(question);
rl.prompt();
rl.on('line', function(line) {
rl.close();
return deferred.resolve(line);
});
return deferred.promise;
};
const getVariable = function(variable, question) {
return function() {
const deferred = Q.defer();
if (argv[variable]) {
inputs[variable] = argv[variable];
return Q.when();
} else {
prompt(question).then(function(value) {
inputs[variable] = value;
deferred.resolve();
});
return deferred.promise;
}
};
};
const getCreateOrDeleteVariables = function() {
return function() {
const deferred = Q.defer();
if (inputs.action === 'set') {
return getVariable('address', 'On which address are we setting the label: ')()
.then(getVariable('label', 'What label do you want to set on the address: '));
} else if (inputs.action === 'delete') {
return getVariable('address', 'From which address are we removing the label: ')();
} else {
deferred.resolve();
return deferred.promise;
}
};
};
return getVariable('walletId', 'Enter the wallet ID: ')()
.then(getVariable('action', 'Which label action do you wish to perform? [list, set, delete]: '))
.then(getCreateOrDeleteVariables());
};
const authenticate = function() {
bitgo.authenticate({ username: inputs.user, password: inputs.password, otp: inputs.otp }, function(err, result) {
if (err) {
console.dir(err);
throw new Error('Authentication failure!');
}
});
};
const runCommands = function() {
// Now get the wallet
bitgo.wallets().get({ type: 'bitcoin', id: inputs.walletId }, function(err, wallet) {
if (err) {
console.log(err);
process.exit(-1);
}
switch (inputs.action) {
case 'list':
// Get the labels for the addresses in this wallet
wallet.labels({}, function(err, result) {
if (err) {
console.log(err);
process.exit(-1);
}
if (result) {
const sortedLabels = _.sortBy(result, function(label) { return label.label + label.address; });
sortedLabels.forEach(function(label) {
console.log(' ' + label.address + label.label);
});
}
});
break;
case 'set':
wallet.setLabel({ label: inputs.label, address: inputs.address }, function(err, result) {
if (err) {
console.log(err);
process.exit(-1);
}
console.log('Set label ' + result.label + ' on address ' + result.address);
});
break;
case 'delete':
wallet.deleteLabel({ address: inputs.address }, function(err, result) {
if (err) {
console.log(err);
process.exit(-1);
}
console.log('Deleted label from address ' + result.address);
});
break;
default:
console.log('Invalid action entered!');
}
});
};
authenticate();
collectInputs()
.then(runCommands)
.catch(function(e) {
console.log(e);
console.log(e.stack);
});