@rabby-wallet/eth-hd-keyring 3.6.12 → 4.0.0
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 +20 -12
- package/index.ts +35 -15
- package/package.json +5 -5
- package/test/index.js +11 -9
package/dist/index.js
CHANGED
|
@@ -31,10 +31,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
31
31
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
32
|
};
|
|
33
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
-
|
|
34
|
+
// import Wallet, { hdkey } from 'ethereumjs-wallet';
|
|
35
|
+
const hdkey_1 = require("ethereum-cryptography/hdkey");
|
|
35
36
|
const eth_simple_keyring_1 = __importDefault(require("@rabby-wallet/eth-simple-keyring"));
|
|
36
|
-
const bip39 = __importStar(require("bip39"));
|
|
37
|
+
const bip39 = __importStar(require("@scure/bip39"));
|
|
38
|
+
const english_1 = require("@scure/bip39/wordlists/english");
|
|
37
39
|
const sigUtil = __importStar(require("eth-sig-util"));
|
|
40
|
+
const util_1 = require("@ethereumjs/util");
|
|
38
41
|
// Options:
|
|
39
42
|
const hdPathString = "m/44'/60'/0'/0";
|
|
40
43
|
const type = 'HD Key Tree';
|
|
@@ -83,9 +86,8 @@ class HdKeyring extends eth_simple_keyring_1.default {
|
|
|
83
86
|
return Promise.resolve([]);
|
|
84
87
|
}
|
|
85
88
|
initPublicKey() {
|
|
86
|
-
this.root = this.hdWallet.
|
|
87
|
-
|
|
88
|
-
this.publicKey = wallet.getPublicKey().toString('hex');
|
|
89
|
+
this.root = this.hdWallet.derive(this.hdPath);
|
|
90
|
+
this.publicKey = (0, util_1.bytesToHex)(this.root.publicKey);
|
|
89
91
|
}
|
|
90
92
|
getPublicKey() {
|
|
91
93
|
return this.publicKey;
|
|
@@ -94,15 +96,15 @@ class HdKeyring extends eth_simple_keyring_1.default {
|
|
|
94
96
|
this.mnemonic = mnemonic;
|
|
95
97
|
this._index2wallet = {};
|
|
96
98
|
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
97
|
-
this.hdWallet =
|
|
98
|
-
this.root = this.hdWallet.
|
|
99
|
+
this.hdWallet = hdkey_1.HDKey.fromMasterSeed(seed);
|
|
100
|
+
this.root = this.hdWallet.derive(this.hdPath);
|
|
99
101
|
if (!this.publicKey) {
|
|
100
102
|
this.initPublicKey();
|
|
101
103
|
}
|
|
102
104
|
}
|
|
103
105
|
addAccounts(numberOfAccounts = 1) {
|
|
104
106
|
if (!this.root) {
|
|
105
|
-
this.initFromMnemonic(bip39.generateMnemonic());
|
|
107
|
+
this.initFromMnemonic(bip39.generateMnemonic(english_1.wordlist));
|
|
106
108
|
}
|
|
107
109
|
let count = numberOfAccounts;
|
|
108
110
|
let currentIdx = 0;
|
|
@@ -120,7 +122,7 @@ class HdKeyring extends eth_simple_keyring_1.default {
|
|
|
120
122
|
}
|
|
121
123
|
}
|
|
122
124
|
const hexWallets = newWallets.map((w) => {
|
|
123
|
-
return sigUtil.normalize(
|
|
125
|
+
return sigUtil.normalize(this._addressfromPublicKey(w.publicKey));
|
|
124
126
|
});
|
|
125
127
|
return Promise.resolve(hexWallets);
|
|
126
128
|
}
|
|
@@ -183,7 +185,7 @@ class HdKeyring extends eth_simple_keyring_1.default {
|
|
|
183
185
|
}
|
|
184
186
|
getAccounts() {
|
|
185
187
|
return Promise.resolve(this.wallets.map((w) => {
|
|
186
|
-
return sigUtil.normalize(
|
|
188
|
+
return sigUtil.normalize(this._addressfromPublicKey(w.publicKey));
|
|
187
189
|
}));
|
|
188
190
|
}
|
|
189
191
|
getIndexByAddress(address) {
|
|
@@ -198,12 +200,18 @@ class HdKeyring extends eth_simple_keyring_1.default {
|
|
|
198
200
|
_addressFromIndex(i) {
|
|
199
201
|
if (!this._index2wallet[i]) {
|
|
200
202
|
const child = this.root.deriveChild(i);
|
|
201
|
-
const wallet =
|
|
202
|
-
|
|
203
|
+
const wallet = {
|
|
204
|
+
publicKey: (0, util_1.privateToPublic)(child.privateKey),
|
|
205
|
+
privateKey: child.privateKey,
|
|
206
|
+
};
|
|
207
|
+
const address = sigUtil.normalize(this._addressfromPublicKey(wallet.publicKey));
|
|
203
208
|
this._index2wallet[i] = [address, wallet];
|
|
204
209
|
}
|
|
205
210
|
return this._index2wallet[i];
|
|
206
211
|
}
|
|
212
|
+
_addressfromPublicKey(publicKey) {
|
|
213
|
+
return (0, util_1.bytesToHex)((0, util_1.publicToAddress)(publicKey, true)).toLowerCase();
|
|
214
|
+
}
|
|
207
215
|
}
|
|
208
216
|
HdKeyring.type = type;
|
|
209
217
|
exports.default = HdKeyring;
|
package/index.ts
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
import Wallet, { hdkey } from 'ethereumjs-wallet';
|
|
1
|
+
// import Wallet, { hdkey } from 'ethereumjs-wallet';
|
|
2
|
+
import { HDKey } from 'ethereum-cryptography/hdkey';
|
|
2
3
|
import SimpleKeyring from '@rabby-wallet/eth-simple-keyring';
|
|
3
|
-
import * as bip39 from 'bip39';
|
|
4
|
+
import * as bip39 from '@scure/bip39';
|
|
5
|
+
import { wordlist } from '@scure/bip39/wordlists/english';
|
|
4
6
|
import * as sigUtil from 'eth-sig-util';
|
|
7
|
+
import {
|
|
8
|
+
bytesToHex,
|
|
9
|
+
publicToAddress,
|
|
10
|
+
privateToPublic,
|
|
11
|
+
} from '@ethereumjs/util';
|
|
5
12
|
|
|
6
13
|
// Options:
|
|
7
14
|
const hdPathString = "m/44'/60'/0'/0";
|
|
8
15
|
const type = 'HD Key Tree';
|
|
9
16
|
|
|
17
|
+
interface Wallet {
|
|
18
|
+
publicKey: Uint8Array;
|
|
19
|
+
privateKey: Uint8Array;
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
interface DeserializeOption {
|
|
11
23
|
hdPath?: string;
|
|
12
24
|
mnemonic?: string;
|
|
@@ -22,8 +34,8 @@ class HdKeyring extends SimpleKeyring {
|
|
|
22
34
|
type = type;
|
|
23
35
|
mnemonic: string | null = null;
|
|
24
36
|
hdPath = hdPathString;
|
|
25
|
-
hdWallet?:
|
|
26
|
-
root:
|
|
37
|
+
hdWallet?: HDKey;
|
|
38
|
+
root: HDKey | null = null;
|
|
27
39
|
wallets: Wallet[] = [];
|
|
28
40
|
_index2wallet: Record<number, [string, Wallet]> = {};
|
|
29
41
|
activeIndexes: number[] = [];
|
|
@@ -71,9 +83,8 @@ class HdKeyring extends SimpleKeyring {
|
|
|
71
83
|
}
|
|
72
84
|
|
|
73
85
|
private initPublicKey() {
|
|
74
|
-
this.root = this.hdWallet!.
|
|
75
|
-
|
|
76
|
-
this.publicKey = wallet.getPublicKey().toString('hex');
|
|
86
|
+
this.root = this.hdWallet!.derive(this.hdPath);
|
|
87
|
+
this.publicKey = bytesToHex(this.root.publicKey!);
|
|
77
88
|
}
|
|
78
89
|
|
|
79
90
|
getPublicKey() {
|
|
@@ -84,8 +95,8 @@ class HdKeyring extends SimpleKeyring {
|
|
|
84
95
|
this.mnemonic = mnemonic;
|
|
85
96
|
this._index2wallet = {};
|
|
86
97
|
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
87
|
-
this.hdWallet =
|
|
88
|
-
this.root = this.hdWallet!.
|
|
98
|
+
this.hdWallet = HDKey.fromMasterSeed(seed);
|
|
99
|
+
this.root = this.hdWallet!.derive(this.hdPath);
|
|
89
100
|
|
|
90
101
|
if (!this.publicKey) {
|
|
91
102
|
this.initPublicKey();
|
|
@@ -94,13 +105,13 @@ class HdKeyring extends SimpleKeyring {
|
|
|
94
105
|
|
|
95
106
|
addAccounts(numberOfAccounts = 1) {
|
|
96
107
|
if (!this.root) {
|
|
97
|
-
this.initFromMnemonic(bip39.generateMnemonic());
|
|
108
|
+
this.initFromMnemonic(bip39.generateMnemonic(wordlist));
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
let count = numberOfAccounts;
|
|
101
112
|
let currentIdx = 0;
|
|
102
113
|
const newWallets: Wallet[] = [];
|
|
103
|
-
|
|
114
|
+
|
|
104
115
|
while (count) {
|
|
105
116
|
const [, wallet] = this._addressFromIndex(currentIdx);
|
|
106
117
|
if (this.wallets.includes(wallet)) {
|
|
@@ -114,7 +125,7 @@ class HdKeyring extends SimpleKeyring {
|
|
|
114
125
|
}
|
|
115
126
|
|
|
116
127
|
const hexWallets = newWallets.map((w) => {
|
|
117
|
-
return sigUtil.normalize(
|
|
128
|
+
return sigUtil.normalize(this._addressfromPublicKey(w.publicKey));
|
|
118
129
|
});
|
|
119
130
|
|
|
120
131
|
return Promise.resolve(hexWallets);
|
|
@@ -196,7 +207,7 @@ class HdKeyring extends SimpleKeyring {
|
|
|
196
207
|
getAccounts() {
|
|
197
208
|
return Promise.resolve(
|
|
198
209
|
this.wallets.map((w) => {
|
|
199
|
-
return sigUtil.normalize(
|
|
210
|
+
return sigUtil.normalize(this._addressfromPublicKey(w.publicKey));
|
|
200
211
|
}),
|
|
201
212
|
);
|
|
202
213
|
}
|
|
@@ -215,13 +226,22 @@ class HdKeyring extends SimpleKeyring {
|
|
|
215
226
|
_addressFromIndex(i: number): [string, Wallet] {
|
|
216
227
|
if (!this._index2wallet[i]) {
|
|
217
228
|
const child = this.root!.deriveChild(i);
|
|
218
|
-
const wallet =
|
|
219
|
-
|
|
229
|
+
const wallet = {
|
|
230
|
+
publicKey: privateToPublic(child.privateKey!),
|
|
231
|
+
privateKey: child.privateKey!,
|
|
232
|
+
};
|
|
233
|
+
const address = sigUtil.normalize(
|
|
234
|
+
this._addressfromPublicKey(wallet.publicKey),
|
|
235
|
+
);
|
|
220
236
|
this._index2wallet[i] = [address, wallet];
|
|
221
237
|
}
|
|
222
238
|
|
|
223
239
|
return this._index2wallet[i];
|
|
224
240
|
}
|
|
241
|
+
|
|
242
|
+
_addressfromPublicKey(publicKey: Uint8Array) {
|
|
243
|
+
return bytesToHex(publicToAddress(publicKey, true)).toLowerCase();
|
|
244
|
+
}
|
|
225
245
|
}
|
|
226
246
|
|
|
227
247
|
export default HdKeyring;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rabby-wallet/eth-hd-keyring",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "A simple standard interface for a seed phrase generated set of Ethereum accounts.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ethereum",
|
|
@@ -27,12 +27,12 @@
|
|
|
27
27
|
"test": "mocha"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
+
"@ethereumjs/util": "^9.0.0",
|
|
30
31
|
"@metamask/eth-sig-util": "^4.0.0",
|
|
31
|
-
"@rabby-wallet/eth-simple-keyring": "
|
|
32
|
-
"bip39": "^
|
|
32
|
+
"@rabby-wallet/eth-simple-keyring": "5.0.0-alpha",
|
|
33
|
+
"@scure/bip39": "^1.2.1",
|
|
33
34
|
"eth-sig-util": "^3.0.1",
|
|
34
|
-
"
|
|
35
|
-
"ethereumjs-wallet": "^1.0.1"
|
|
35
|
+
"ethereum-cryptography": "^2.1.2"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@lavamoat/allow-scripts": "^1.0.6",
|
package/test/index.js
CHANGED
|
@@ -8,6 +8,7 @@ const {
|
|
|
8
8
|
SignTypedDataVersion,
|
|
9
9
|
} = require('@metamask/eth-sig-util');
|
|
10
10
|
const ethUtil = require('ethereumjs-util');
|
|
11
|
+
const newEthUtil = require('@ethereumjs/util');
|
|
11
12
|
const HdKeyring = require('..').default;
|
|
12
13
|
// Sample account:
|
|
13
14
|
const privKeyHex =
|
|
@@ -109,19 +110,20 @@ describe('hd-keyring', function () {
|
|
|
109
110
|
describe('#getAccounts', function () {
|
|
110
111
|
it('calls getAddress on each wallet', function (done) {
|
|
111
112
|
// Push a mock wallet
|
|
112
|
-
const desiredOutput = '
|
|
113
|
+
const desiredOutput = '0x410264A247892c3b2912AeE58236036A82CA209e';
|
|
113
114
|
keyring.wallets.push({
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
115
|
+
publicKey: newEthUtil.importPublic(
|
|
116
|
+
newEthUtil.hexToBytes(
|
|
117
|
+
'0x0220381189b226eae955cf7331b649be61b6ec55ea678cb30c7371e9e07dc200bd',
|
|
118
|
+
),
|
|
119
|
+
),
|
|
120
|
+
privateKey: newEthUtil.hexToBytes(
|
|
121
|
+
'0x504560704904af362cab963188f571bccb1498f6ab5113b6bd8d76b6c53a963e',
|
|
122
|
+
),
|
|
121
123
|
});
|
|
122
124
|
|
|
123
125
|
keyring.getAccounts().then((output) => {
|
|
124
|
-
assert.equal(output[0],
|
|
126
|
+
assert.equal(output[0].toLowerCase(), desiredOutput.toLowerCase());
|
|
125
127
|
assert.equal(output.length, 1);
|
|
126
128
|
done();
|
|
127
129
|
});
|