1 | var Collection, Custodian, Document, HDKey, PURPOSE_CODE, Wallet, bip39, coininfo, exports;
|
2 |
|
3 | HDKey = require('hdkey');
|
4 |
|
5 | Document = require('./document');
|
6 |
|
7 | Custodian = require('./custodian');
|
8 |
|
9 | Collection = require('./collection');
|
10 |
|
11 | PURPOSE_CODE = 1337;
|
12 |
|
13 | bip39 = require('bip39');
|
14 |
|
15 | coininfo = require('coininfo');
|
16 |
|
17 | Wallet = (function() {
|
18 | function Wallet(custodian, mnemonic, config) {
|
19 | var _ref, _ref1;
|
20 | this.custodian = custodian;
|
21 | this.mnemonic = mnemonic != null ? mnemonic : bip39.generateMnemonic();
|
22 | this.config = config != null ? config : {};
|
23 | if (!(this instanceof Wallet)) {
|
24 | return new Wallet(this.custodian, this.mnemonic, this.config);
|
25 | }
|
26 | if (!bip39.validateMnemonic(this.mnemonic)) {
|
27 | throw Error('invalid Mnemonic');
|
28 | }
|
29 | this.coin = (_ref = this.config.coin) != null ? _ref : 'BTC';
|
30 | this.versions = (_ref1 = coininfo(this.coin)) != null ? _ref1.versions : void 0;
|
31 | this.seed = bip39.mnemonicToSeed(this.mnemonic);
|
32 | this.rootkey = HDKey.fromMasterSeed(this.seed, this.versions.bip32);
|
33 | this.hdkey = this.rootkey.derive("m/" + PURPOSE_CODE + "'");
|
34 | this.privateExtendedKey = this.hdkey.privateExtendedKey;
|
35 | this.profiles = Collection(this.custodian, Document, this.privateExtendedKey, true);
|
36 | }
|
37 |
|
38 | return Wallet;
|
39 |
|
40 | })();
|
41 |
|
42 | exports = module.exports = Wallet;
|