@rabby-wallet/eth-hd-keyring 3.6.10 → 3.6.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -53,6 +53,7 @@ class HdKeyring extends eth_simple_keyring_1.default {
53
53
  this.page = 0;
54
54
  this.perPage = 5;
55
55
  this.byImport = false;
56
+ this.publicKey = '';
56
57
  this.deserialize(opts);
57
58
  }
58
59
  serialize() {
@@ -62,6 +63,7 @@ class HdKeyring extends eth_simple_keyring_1.default {
62
63
  hdPath: this.hdPath,
63
64
  byImport: this.byImport,
64
65
  index: this.index,
66
+ publicKey: this.publicKey,
65
67
  });
66
68
  }
67
69
  deserialize(opts = {}) {
@@ -71,6 +73,7 @@ class HdKeyring extends eth_simple_keyring_1.default {
71
73
  this.hdPath = opts.hdPath || hdPathString;
72
74
  this.byImport = !!opts.byImport;
73
75
  this.index = opts.index || 0;
76
+ this.publicKey = opts.publicKey || '';
74
77
  if (opts.mnemonic) {
75
78
  this.initFromMnemonic(opts.mnemonic);
76
79
  }
@@ -79,12 +82,23 @@ class HdKeyring extends eth_simple_keyring_1.default {
79
82
  }
80
83
  return Promise.resolve([]);
81
84
  }
85
+ initPublicKey() {
86
+ this.root = this.hdWallet.derivePath(this.hdPath);
87
+ const wallet = this.root.getWallet();
88
+ this.publicKey = wallet.getPublicKey().toString('hex');
89
+ }
90
+ getPublicKey() {
91
+ return this.publicKey;
92
+ }
82
93
  initFromMnemonic(mnemonic) {
83
94
  this.mnemonic = mnemonic;
84
95
  this._index2wallet = {};
85
96
  const seed = bip39.mnemonicToSeedSync(mnemonic);
86
97
  this.hdWallet = ethereumjs_wallet_1.hdkey.fromMasterSeed(seed);
87
98
  this.root = this.hdWallet.derivePath(this.hdPath);
99
+ if (!this.publicKey) {
100
+ this.initPublicKey();
101
+ }
88
102
  }
89
103
  addAccounts(numberOfAccounts = 1) {
90
104
  if (!this.root) {
@@ -143,6 +157,11 @@ class HdKeyring extends eth_simple_keyring_1.default {
143
157
  }
144
158
  return accounts;
145
159
  }
160
+ removeAccount(address) {
161
+ super.removeAccount(address);
162
+ const index = this.getIndexByAddress(address);
163
+ this.activeIndexes = this.activeIndexes.filter((i) => i !== index);
164
+ }
146
165
  __getPage(increment) {
147
166
  return __awaiter(this, void 0, void 0, function* () {
148
167
  this.page += increment;
package/index.ts CHANGED
@@ -13,6 +13,7 @@ interface DeserializeOption {
13
13
  activeIndexes?: number[];
14
14
  byImport?: boolean;
15
15
  index?: number;
16
+ publicKey?: string;
16
17
  }
17
18
 
18
19
  class HdKeyring extends SimpleKeyring {
@@ -30,6 +31,7 @@ class HdKeyring extends SimpleKeyring {
30
31
  page = 0;
31
32
  perPage = 5;
32
33
  byImport = false;
34
+ publicKey: string = '';
33
35
 
34
36
  /* PUBLIC METHODS */
35
37
  constructor(opts = {}) {
@@ -44,6 +46,7 @@ class HdKeyring extends SimpleKeyring {
44
46
  hdPath: this.hdPath,
45
47
  byImport: this.byImport,
46
48
  index: this.index,
49
+ publicKey: this.publicKey,
47
50
  });
48
51
  }
49
52
 
@@ -54,6 +57,7 @@ class HdKeyring extends SimpleKeyring {
54
57
  this.hdPath = opts.hdPath || hdPathString;
55
58
  this.byImport = !!opts.byImport;
56
59
  this.index = opts.index || 0;
60
+ this.publicKey = opts.publicKey || '';
57
61
 
58
62
  if (opts.mnemonic) {
59
63
  this.initFromMnemonic(opts.mnemonic);
@@ -66,12 +70,26 @@ class HdKeyring extends SimpleKeyring {
66
70
  return Promise.resolve([]);
67
71
  }
68
72
 
73
+ private initPublicKey() {
74
+ this.root = this.hdWallet!.derivePath(this.hdPath);
75
+ const wallet = this.root.getWallet();
76
+ this.publicKey = wallet.getPublicKey().toString('hex');
77
+ }
78
+
79
+ getPublicKey() {
80
+ return this.publicKey;
81
+ }
82
+
69
83
  initFromMnemonic(mnemonic) {
70
84
  this.mnemonic = mnemonic;
71
85
  this._index2wallet = {};
72
86
  const seed = bip39.mnemonicToSeedSync(mnemonic);
73
87
  this.hdWallet = hdkey.fromMasterSeed(seed);
74
88
  this.root = this.hdWallet!.derivePath(this.hdPath);
89
+
90
+ if (!this.publicKey) {
91
+ this.initPublicKey();
92
+ }
75
93
  }
76
94
 
77
95
  addAccounts(numberOfAccounts = 1) {
@@ -141,6 +159,12 @@ class HdKeyring extends SimpleKeyring {
141
159
  return accounts;
142
160
  }
143
161
 
162
+ removeAccount(address) {
163
+ super.removeAccount(address);
164
+ const index = this.getIndexByAddress(address);
165
+ this.activeIndexes = this.activeIndexes.filter((i) => i !== index);
166
+ }
167
+
144
168
  async __getPage(increment: number): Promise<
145
169
  Array<{
146
170
  address: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/eth-hd-keyring",
3
- "version": "3.6.10",
3
+ "version": "3.6.12",
4
4
  "description": "A simple standard interface for a seed phrase generated set of Ethereum accounts.",
5
5
  "keywords": [
6
6
  "ethereum",
package/test/index.js CHANGED
@@ -507,4 +507,20 @@ describe('hd-keyring', function () {
507
507
  });
508
508
  });
509
509
  });
510
+
511
+ describe('removeAccount', function () {
512
+ it('should return correct activeIndexes', async function () {
513
+ const address = firstAcct;
514
+
515
+ keyring = new HdKeyring({
516
+ mnemonic: sampleMnemonic,
517
+ activeIndexes: [0, 2, 3, 6],
518
+ });
519
+ await keyring.removeAccount(address);
520
+ const { activeIndexes } = await keyring.serialize();
521
+
522
+ assert.equal(activeIndexes.length, 3);
523
+ assert.deepEqual(activeIndexes, [2, 3, 6]);
524
+ });
525
+ });
510
526
  });