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