1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import { randomBytes } from 'crypto';
|
15 | import * as common from './common';
|
16 | import { Util } from './v2/internal/util';
|
17 | import * as bitcoin from '@bitgo/utxo-lib';
|
18 | import { hdPath } from './bitcoin';
|
19 | const _ = require('lodash');
|
20 | let ethereumUtil;
|
21 | import * as Bluebird from 'bluebird';
|
22 | const co = Bluebird.coroutine;
|
23 |
|
24 | try {
|
25 | ethereumUtil = require('ethereumjs-util');
|
26 | } catch (e) {
|
27 |
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const Keychains = function(bitgo) {
|
34 | this.bitgo = bitgo;
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | Keychains.prototype.isValid = function(params) {
|
42 | params = params || {};
|
43 | common.validateParams(params, [], []);
|
44 |
|
45 | if (params.ethAddress) {
|
46 | if (!_.isString(params.ethAddress)) {
|
47 | throw new Error('ethAddress must be a string');
|
48 | }
|
49 | return ethereumUtil.isValidAddress(params.ethAddress);
|
50 | }
|
51 |
|
52 | if (!_.isString(params.key) && !_.isObject(params.key)) {
|
53 | throw new Error('key must be a string or object');
|
54 | }
|
55 |
|
56 | try {
|
57 | if (!params.key.path) {
|
58 | bitcoin.HDNode.fromBase58(params.key);
|
59 | } else {
|
60 | const hdnode = bitcoin.HDNode.fromBase58(params.key.xpub);
|
61 | hdPath(hdnode).derive(params.key.path);
|
62 | }
|
63 | return true;
|
64 | } catch (e) {
|
65 | return false;
|
66 | }
|
67 | };
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | Keychains.prototype.create = function(params) {
|
77 | params = params || {};
|
78 | common.validateParams(params, [], []);
|
79 |
|
80 | let seed;
|
81 | if (!params.seed) {
|
82 |
|
83 |
|
84 |
|
85 | seed = randomBytes(512 / 8);
|
86 | } else {
|
87 | seed = params.seed;
|
88 | }
|
89 |
|
90 | const extendedKey = bitcoin.HDNode.fromSeedBuffer(seed);
|
91 | const xpub = extendedKey.neutered().toBase58();
|
92 |
|
93 | let ethAddress;
|
94 | try {
|
95 | ethAddress = Util.xpubToEthAddress(xpub);
|
96 | } catch (e) {
|
97 |
|
98 | }
|
99 |
|
100 | return {
|
101 | xpub: xpub,
|
102 | xprv: extendedKey.toBase58(),
|
103 | ethAddress: ethAddress
|
104 | };
|
105 | };
|
106 |
|
107 |
|
108 | const apiResponse = function(status, result, message) {
|
109 | const err: any = new Error(message);
|
110 | err.status = status;
|
111 | err.result = result;
|
112 | return err;
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | Keychains.prototype.deriveLocal = function(params) {
|
120 | params = params || {};
|
121 | common.validateParams(params, ['path'], ['xprv', 'xpub']);
|
122 |
|
123 | if (!params.xprv && !params.xpub) {
|
124 | throw new Error('must provide an xpub or xprv for derivation.');
|
125 | }
|
126 | if (params.xprv && params.xpub) {
|
127 | throw new Error('cannot provide both xpub and xprv');
|
128 | }
|
129 |
|
130 | let hdNode;
|
131 | try {
|
132 | hdNode = bitcoin.HDNode.fromBase58(params.xprv || params.xpub);
|
133 | } catch (e) {
|
134 | throw apiResponse(400, {}, 'Unable to parse the xprv or xpub');
|
135 | }
|
136 |
|
137 | let derivedNode;
|
138 | try {
|
139 | derivedNode = hdPath(hdNode).derive(params.path);
|
140 | } catch (e) {
|
141 | throw apiResponse(400, {}, 'Unable to derive HD key from path');
|
142 | }
|
143 |
|
144 | const xpub = derivedNode.neutered().toBase58();
|
145 |
|
146 | let ethAddress;
|
147 | try {
|
148 | ethAddress = Util.xpubToEthAddress(xpub);
|
149 | } catch (e) {
|
150 |
|
151 | }
|
152 |
|
153 | return {
|
154 | path: params.path,
|
155 | xpub: xpub,
|
156 | xprv: params.xprv && derivedNode.toBase58(),
|
157 | ethAddress: ethAddress
|
158 | };
|
159 | };
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | Keychains.prototype.list = function(params, callback) {
|
166 | params = params || {};
|
167 | common.validateParams(params, [], [], callback);
|
168 |
|
169 | return this.bitgo.get(this.bitgo.url('/keychain'))
|
170 | .result('keychains')
|
171 | .then(function(keychains) {
|
172 | keychains.map(function(keychain) {
|
173 | if (keychain.xpub && keychain.ethAddress && Util.xpubToEthAddress && keychain.ethAddress !== Util.xpubToEthAddress(keychain.xpub)) {
|
174 | throw new Error('ethAddress and xpub do not match');
|
175 | }
|
176 | });
|
177 | return keychains;
|
178 | })
|
179 | .nodeify(callback);
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | Keychains.prototype.updatePassword = function(params, callback) {
|
197 | return co(function *coUpdatePassword() {
|
198 | common.validateParams(params, ['oldPassword', 'newPassword'], [], callback);
|
199 | const encrypted = yield this.bitgo.post(this.bitgo.url('/user/encrypted')).result();
|
200 | const newKeychains = {};
|
201 | const self = this;
|
202 | _.forOwn((encrypted as any).keychains, function keychainsForOwn(oldEncryptedXprv, xpub) {
|
203 | try {
|
204 | const decryptedPrv = self.bitgo.decrypt({ input: oldEncryptedXprv, password: params.oldPassword });
|
205 | const newEncryptedPrv = self.bitgo.encrypt({ input: decryptedPrv, password: params.newPassword });
|
206 | newKeychains[xpub] = newEncryptedPrv;
|
207 | } catch (e) {
|
208 |
|
209 | newKeychains[xpub] = oldEncryptedXprv;
|
210 | }
|
211 | });
|
212 | return { keychains: newKeychains, version: (encrypted as any).version };
|
213 | }).call(this).asCallback(callback);
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | Keychains.prototype.add = function(params, callback) {
|
221 | params = params || {};
|
222 | common.validateParams(params, ['xpub'], ['encryptedXprv', 'type', 'isLedger'], callback);
|
223 |
|
224 | return this.bitgo.post(this.bitgo.url('/keychain'))
|
225 | .send({
|
226 | xpub: params.xpub,
|
227 | encryptedXprv: params.encryptedXprv,
|
228 | type: params.type,
|
229 | originalPasscodeEncryptionCode: params.originalPasscodeEncryptionCode,
|
230 | isLedger: params.isLedger
|
231 | })
|
232 | .result()
|
233 | .then(function(keychain) {
|
234 | if (keychain.xpub && keychain.ethAddress && Util.xpubToEthAddress && keychain.ethAddress !== Util.xpubToEthAddress(keychain.xpub)) {
|
235 | throw new Error('ethAddress and xpub do not match');
|
236 | }
|
237 | return keychain;
|
238 | })
|
239 | .nodeify(callback);
|
240 | };
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | Keychains.prototype.createBitGo = function(params, callback) {
|
247 | params = params || {};
|
248 | common.validateParams(params, [], [], callback);
|
249 |
|
250 | return this.bitgo.post(this.bitgo.url('/keychain/bitgo'))
|
251 | .send(params)
|
252 | .result()
|
253 | .then(function(keychain) {
|
254 | if (keychain.xpub && keychain.ethAddress && Util.xpubToEthAddress && keychain.ethAddress !== Util.xpubToEthAddress(keychain.xpub)) {
|
255 | throw new Error('ethAddress and xpub do not match');
|
256 | }
|
257 | return keychain;
|
258 | })
|
259 | .nodeify(callback);
|
260 | };
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | Keychains.prototype.createBackup = function(params, callback) {
|
267 | params = params || {};
|
268 | common.validateParams(params, ['provider'], [], callback);
|
269 |
|
270 | return this.bitgo.post(this.bitgo.url('/keychain/backup'))
|
271 | .send(params)
|
272 | .result()
|
273 | .then(function(keychain) {
|
274 |
|
275 | if (keychain.xpub && keychain.ethAddress && Util.xpubToEthAddress && keychain.ethAddress !== Util.xpubToEthAddress(keychain.xpub)) {
|
276 | throw new Error('ethAddress and xpub do not match');
|
277 | }
|
278 | return keychain;
|
279 | })
|
280 | .nodeify(callback);
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | Keychains.prototype.get = function(params, callback) {
|
290 | params = params || {};
|
291 | common.validateParams(params, [], ['xpub', 'ethAddress'], callback);
|
292 |
|
293 | if (!params.xpub && !params.ethAddress) {
|
294 | throw new Error('xpub or ethAddress must be defined');
|
295 | }
|
296 |
|
297 | const id = params.xpub || params.ethAddress;
|
298 | return this.bitgo.post(this.bitgo.url('/keychain/' + encodeURIComponent(id)))
|
299 | .send({})
|
300 | .result()
|
301 | .then(function(keychain) {
|
302 | if (keychain.xpub && keychain.ethAddress && Util.xpubToEthAddress && keychain.ethAddress !== Util.xpubToEthAddress(keychain.xpub)) {
|
303 | throw new Error('ethAddress and xpub do not match');
|
304 | }
|
305 | return keychain;
|
306 | })
|
307 | .nodeify(callback);
|
308 | };
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | Keychains.prototype.update = function(params, callback) {
|
317 | params = params || {};
|
318 | common.validateParams(params, ['xpub'], ['encryptedXprv'], callback);
|
319 |
|
320 | return this.bitgo.put(this.bitgo.url('/keychain/' + params.xpub))
|
321 | .send({
|
322 | encryptedXprv: params.encryptedXprv
|
323 | })
|
324 | .result()
|
325 | .then(function(keychain) {
|
326 | if (keychain.xpub && keychain.ethAddress && Util.xpubToEthAddress && keychain.ethAddress !== Util.xpubToEthAddress(keychain.xpub)) {
|
327 | throw new Error('ethAddress and xpub do not match');
|
328 | }
|
329 | return keychain;
|
330 | })
|
331 | .nodeify(callback);
|
332 | };
|
333 |
|
334 |
|
335 | module.exports = Keychains;
|