@metamask/eth-hd-keyring 8.0.0 → 9.0.1
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/CHANGELOG.md +21 -1
- package/dist/index.cjs +232 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +54 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +54 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +230 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +24 -6
- package/index.js +0 -312
- package/jest.config.js +0 -32
- package/test/index.js +0 -960
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [9.0.1]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Use `ts-bridge/cli@0.6.1` ([#118](https://github.com/MetaMask/accounts/pull/118))
|
|
15
|
+
- This new version fixes a bug with CJS re-exports.
|
|
16
|
+
|
|
17
|
+
## [9.0.0]
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- **BREAKING**: Move seed generation to deserialization ([#100](https://github.com/MetaMask/accounts/pull/100))
|
|
22
|
+
- Using the constructor directly no longer generates the seed required for account derivation.
|
|
23
|
+
- Both `serialize` and `deserialize` are now proper `async` methods.
|
|
24
|
+
- Allow passing native custom cryptographic functions ([#102](https://github.com/MetaMask/accounts/pull/102))
|
|
25
|
+
- The seed generation is now relying `@metamask/key-tree` package (instead of `@metamask/scure-bip39`).
|
|
26
|
+
- The `constructor` now allows a new option `cryptographicFunctions` which allows the use of custom cryptographic functions during seed generation.
|
|
27
|
+
|
|
10
28
|
## [8.0.0]
|
|
11
29
|
|
|
12
30
|
### Changed
|
|
@@ -137,7 +155,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
137
155
|
- Deserialize method (and `HdKeyring` constructor by extension) can no longer be passed an options object containing a value for `numberOfAccounts` if it is not also containing a value for `mnemonic`.
|
|
138
156
|
- Package name changed from `eth-hd-keyring` to `@metamask/eth-hd-keyring`.
|
|
139
157
|
|
|
140
|
-
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@
|
|
158
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@9.0.1...HEAD
|
|
159
|
+
[9.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@9.0.0...@metamask/eth-hd-keyring@9.0.1
|
|
160
|
+
[9.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@8.0.0...@metamask/eth-hd-keyring@9.0.0
|
|
141
161
|
[8.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@7.0.4...@metamask/eth-hd-keyring@8.0.0
|
|
142
162
|
[7.0.4]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@7.0.3...@metamask/eth-hd-keyring@7.0.4
|
|
143
163
|
[7.0.3]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@7.0.2...@metamask/eth-hd-keyring@7.0.3
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const util_1 = require("@ethereumjs/util");
|
|
4
|
+
const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
5
|
+
const key_tree_1 = require("@metamask/key-tree");
|
|
6
|
+
const scure_bip39_1 = require("@metamask/scure-bip39");
|
|
7
|
+
const english_1 = require("@metamask/scure-bip39/dist/wordlists/english.js");
|
|
8
|
+
const utils_1 = require("@metamask/utils");
|
|
9
|
+
const hdkey_1 = require("ethereum-cryptography/hdkey");
|
|
10
|
+
const keccak_1 = require("ethereum-cryptography/keccak");
|
|
11
|
+
const utils_2 = require("ethereum-cryptography/utils");
|
|
12
|
+
// Options:
|
|
13
|
+
const hdPathString = `m/44'/60'/0'/0`;
|
|
14
|
+
const type = 'HD Key Tree';
|
|
15
|
+
class HdKeyring {
|
|
16
|
+
/* PUBLIC METHODS */
|
|
17
|
+
constructor(opts = {}) {
|
|
18
|
+
this.type = type;
|
|
19
|
+
this._wallets = [];
|
|
20
|
+
// Cryptographic functions to be used by `@metamask/key-tree`. It will use built-in implementations if not provided here.
|
|
21
|
+
this._cryptographicFunctions = opts.cryptographicFunctions;
|
|
22
|
+
}
|
|
23
|
+
async generateRandomMnemonic() {
|
|
24
|
+
await this._initFromMnemonic((0, scure_bip39_1.generateMnemonic)(english_1.wordlist));
|
|
25
|
+
}
|
|
26
|
+
_uint8ArrayToString(mnemonic) {
|
|
27
|
+
const recoveredIndices = Array.from(new Uint16Array(new Uint8Array(mnemonic).buffer));
|
|
28
|
+
return recoveredIndices.map((i) => english_1.wordlist[i]).join(' ');
|
|
29
|
+
}
|
|
30
|
+
_stringToUint8Array(mnemonic) {
|
|
31
|
+
const indices = mnemonic.split(' ').map((word) => english_1.wordlist.indexOf(word));
|
|
32
|
+
return new Uint8Array(new Uint16Array(indices).buffer);
|
|
33
|
+
}
|
|
34
|
+
_mnemonicToUint8Array(mnemonic) {
|
|
35
|
+
let mnemonicData = mnemonic;
|
|
36
|
+
// when encrypted/decrypted, buffers get cast into js object with a property type set to buffer
|
|
37
|
+
if (mnemonic && mnemonic.type && mnemonic.type === 'Buffer') {
|
|
38
|
+
mnemonicData = mnemonic.data;
|
|
39
|
+
}
|
|
40
|
+
if (
|
|
41
|
+
// this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings
|
|
42
|
+
typeof mnemonicData === 'string' ||
|
|
43
|
+
Buffer.isBuffer(mnemonicData) ||
|
|
44
|
+
Array.isArray(mnemonicData)) {
|
|
45
|
+
let mnemonicAsString = mnemonicData;
|
|
46
|
+
if (Array.isArray(mnemonicData)) {
|
|
47
|
+
mnemonicAsString = Buffer.from(mnemonicData).toString();
|
|
48
|
+
}
|
|
49
|
+
else if (Buffer.isBuffer(mnemonicData)) {
|
|
50
|
+
mnemonicAsString = mnemonicData.toString();
|
|
51
|
+
}
|
|
52
|
+
return this._stringToUint8Array(mnemonicAsString);
|
|
53
|
+
}
|
|
54
|
+
else if (mnemonicData instanceof Object &&
|
|
55
|
+
!(mnemonicData instanceof Uint8Array)) {
|
|
56
|
+
// when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array
|
|
57
|
+
return Uint8Array.from(Object.values(mnemonicData));
|
|
58
|
+
}
|
|
59
|
+
return mnemonicData;
|
|
60
|
+
}
|
|
61
|
+
async serialize() {
|
|
62
|
+
const mnemonicAsString = this._uint8ArrayToString(this.mnemonic);
|
|
63
|
+
const uint8ArrayMnemonic = new TextEncoder('utf-8').encode(mnemonicAsString);
|
|
64
|
+
return {
|
|
65
|
+
mnemonic: Array.from(uint8ArrayMnemonic),
|
|
66
|
+
numberOfAccounts: this._wallets.length,
|
|
67
|
+
hdPath: this.hdPath,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
async deserialize(opts = {}) {
|
|
71
|
+
if (opts.numberOfAccounts && !opts.mnemonic) {
|
|
72
|
+
throw new Error('Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic');
|
|
73
|
+
}
|
|
74
|
+
if (this.root) {
|
|
75
|
+
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
76
|
+
}
|
|
77
|
+
this.opts = opts;
|
|
78
|
+
this._wallets = [];
|
|
79
|
+
this.mnemonic = null;
|
|
80
|
+
this.root = null;
|
|
81
|
+
this.hdPath = opts.hdPath || hdPathString;
|
|
82
|
+
if (opts.mnemonic) {
|
|
83
|
+
await this._initFromMnemonic(opts.mnemonic);
|
|
84
|
+
}
|
|
85
|
+
if (opts.numberOfAccounts) {
|
|
86
|
+
return this.addAccounts(opts.numberOfAccounts);
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
addAccounts(numberOfAccounts = 1) {
|
|
91
|
+
if (!this.root) {
|
|
92
|
+
throw new Error('Eth-Hd-Keyring: No secret recovery phrase provided');
|
|
93
|
+
}
|
|
94
|
+
const oldLen = this._wallets.length;
|
|
95
|
+
const newWallets = [];
|
|
96
|
+
for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
|
|
97
|
+
const wallet = this.root.deriveChild(i);
|
|
98
|
+
newWallets.push(wallet);
|
|
99
|
+
this._wallets.push(wallet);
|
|
100
|
+
}
|
|
101
|
+
const hexWallets = newWallets.map((w) => {
|
|
102
|
+
return this._addressfromPublicKey(w.publicKey);
|
|
103
|
+
});
|
|
104
|
+
return Promise.resolve(hexWallets);
|
|
105
|
+
}
|
|
106
|
+
getAccounts() {
|
|
107
|
+
return this._wallets.map((w) => this._addressfromPublicKey(w.publicKey));
|
|
108
|
+
}
|
|
109
|
+
/* BASE KEYRING METHODS */
|
|
110
|
+
// returns an address specific to an app
|
|
111
|
+
async getAppKeyAddress(address, origin) {
|
|
112
|
+
if (!origin || typeof origin !== 'string') {
|
|
113
|
+
throw new Error(`'origin' must be a non-empty string`);
|
|
114
|
+
}
|
|
115
|
+
const wallet = this._getWalletForAccount(address, {
|
|
116
|
+
withAppKeyOrigin: origin,
|
|
117
|
+
});
|
|
118
|
+
const appKeyAddress = (0, eth_sig_util_1.normalize)((0, util_1.publicToAddress)(wallet.publicKey).toString('hex'));
|
|
119
|
+
return appKeyAddress;
|
|
120
|
+
}
|
|
121
|
+
// exportAccount should return a hex-encoded private key:
|
|
122
|
+
async exportAccount(address, opts = {}) {
|
|
123
|
+
const wallet = this._getWalletForAccount(address, opts);
|
|
124
|
+
return (0, utils_2.bytesToHex)(wallet.privateKey);
|
|
125
|
+
}
|
|
126
|
+
// tx is an instance of the ethereumjs-transaction class.
|
|
127
|
+
async signTransaction(address, tx, opts = {}) {
|
|
128
|
+
const privKey = this._getPrivateKeyFor(address, opts);
|
|
129
|
+
const signedTx = tx.sign(privKey);
|
|
130
|
+
// Newer versions of Ethereumjs-tx are immutable and return a new tx object
|
|
131
|
+
return signedTx === undefined ? tx : signedTx;
|
|
132
|
+
}
|
|
133
|
+
// For eth_sign, we need to sign arbitrary data:
|
|
134
|
+
async signMessage(address, data, opts = {}) {
|
|
135
|
+
(0, utils_1.assertIsHexString)(data);
|
|
136
|
+
const message = (0, utils_1.remove0x)(data);
|
|
137
|
+
const privKey = this._getPrivateKeyFor(address, opts);
|
|
138
|
+
const msgSig = (0, util_1.ecsign)(Buffer.from(message, 'hex'), privKey);
|
|
139
|
+
const rawMsgSig = (0, eth_sig_util_1.concatSig)(msgSig.v, msgSig.r, msgSig.s);
|
|
140
|
+
return rawMsgSig;
|
|
141
|
+
}
|
|
142
|
+
// For personal_sign, we need to prefix the message:
|
|
143
|
+
async signPersonalMessage(address, msgHex, opts = {}) {
|
|
144
|
+
const privKey = this._getPrivateKeyFor(address, opts);
|
|
145
|
+
const privateKey = Buffer.from(privKey, 'hex');
|
|
146
|
+
const sig = (0, eth_sig_util_1.personalSign)({ privateKey, data: msgHex });
|
|
147
|
+
return sig;
|
|
148
|
+
}
|
|
149
|
+
// For eth_decryptMessage:
|
|
150
|
+
async decryptMessage(withAccount, encryptedData) {
|
|
151
|
+
const wallet = this._getWalletForAccount(withAccount);
|
|
152
|
+
const { privateKey: privateKeyAsUint8Array } = wallet;
|
|
153
|
+
const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');
|
|
154
|
+
const sig = (0, eth_sig_util_1.decrypt)({ privateKey: privateKeyAsHex, encryptedData });
|
|
155
|
+
return sig;
|
|
156
|
+
}
|
|
157
|
+
// personal_signTypedData, signs data along with the schema
|
|
158
|
+
async signTypedData(withAccount, typedData, opts = { version: eth_sig_util_1.SignTypedDataVersion.V1 }) {
|
|
159
|
+
// Treat invalid versions as "V1"
|
|
160
|
+
const version = Object.keys(eth_sig_util_1.SignTypedDataVersion).includes(opts.version)
|
|
161
|
+
? opts.version
|
|
162
|
+
: eth_sig_util_1.SignTypedDataVersion.V1;
|
|
163
|
+
const privateKey = this._getPrivateKeyFor(withAccount, opts);
|
|
164
|
+
return (0, eth_sig_util_1.signTypedData)({ privateKey, data: typedData, version });
|
|
165
|
+
}
|
|
166
|
+
removeAccount(account) {
|
|
167
|
+
const address = (0, eth_sig_util_1.normalize)(account);
|
|
168
|
+
if (!this._wallets
|
|
169
|
+
.map(({ publicKey }) => this._addressfromPublicKey(publicKey))
|
|
170
|
+
.includes(address)) {
|
|
171
|
+
throw new Error(`Address ${address} not found in this keyring`);
|
|
172
|
+
}
|
|
173
|
+
this._wallets = this._wallets.filter(({ publicKey }) => this._addressfromPublicKey(publicKey) !== address);
|
|
174
|
+
}
|
|
175
|
+
// get public key for nacl
|
|
176
|
+
async getEncryptionPublicKey(withAccount, opts = {}) {
|
|
177
|
+
const privKey = this._getPrivateKeyFor(withAccount, opts);
|
|
178
|
+
const publicKey = (0, eth_sig_util_1.getEncryptionPublicKey)(privKey);
|
|
179
|
+
return publicKey;
|
|
180
|
+
}
|
|
181
|
+
_getPrivateKeyFor(address, opts = {}) {
|
|
182
|
+
if (!address) {
|
|
183
|
+
throw new Error('Must specify address.');
|
|
184
|
+
}
|
|
185
|
+
const wallet = this._getWalletForAccount(address, opts);
|
|
186
|
+
return wallet.privateKey;
|
|
187
|
+
}
|
|
188
|
+
_getWalletForAccount(account, opts = {}) {
|
|
189
|
+
const address = (0, eth_sig_util_1.normalize)(account);
|
|
190
|
+
let wallet = this._wallets.find(({ publicKey }) => {
|
|
191
|
+
return this._addressfromPublicKey(publicKey) === address;
|
|
192
|
+
});
|
|
193
|
+
if (!wallet) {
|
|
194
|
+
throw new Error('HD Keyring - Unable to find matching address.');
|
|
195
|
+
}
|
|
196
|
+
if (opts.withAppKeyOrigin) {
|
|
197
|
+
const { privateKey } = wallet;
|
|
198
|
+
const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');
|
|
199
|
+
const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
|
|
200
|
+
const appKeyPrivateKey = (0, util_1.arrToBufArr)((0, keccak_1.keccak256)(appKeyBuffer, 256));
|
|
201
|
+
const appKeyPublicKey = (0, util_1.privateToPublic)(appKeyPrivateKey);
|
|
202
|
+
wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
|
|
203
|
+
}
|
|
204
|
+
return wallet;
|
|
205
|
+
}
|
|
206
|
+
/* PRIVATE / UTILITY METHODS */
|
|
207
|
+
/**
|
|
208
|
+
* Sets appropriate properties for the keyring based on the given
|
|
209
|
+
* BIP39-compliant mnemonic.
|
|
210
|
+
*
|
|
211
|
+
* @param {string|Array<number>|Buffer} mnemonic - A seed phrase represented
|
|
212
|
+
* as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input
|
|
213
|
+
* passed as type buffer or array of UTF-8 bytes must be NFKD normalized.
|
|
214
|
+
*/
|
|
215
|
+
async _initFromMnemonic(mnemonic) {
|
|
216
|
+
if (this.root) {
|
|
217
|
+
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
218
|
+
}
|
|
219
|
+
this.mnemonic = this._mnemonicToUint8Array(mnemonic);
|
|
220
|
+
const seed = await (0, key_tree_1.mnemonicToSeed)(this.mnemonic, '', // No passphrase
|
|
221
|
+
this._cryptographicFunctions);
|
|
222
|
+
this.hdWallet = hdkey_1.HDKey.fromMasterSeed(seed);
|
|
223
|
+
this.root = this.hdWallet.derive(this.hdPath);
|
|
224
|
+
}
|
|
225
|
+
// small helper function to convert publicKey in Uint8Array form to a publicAddress as a hex
|
|
226
|
+
_addressfromPublicKey(publicKey) {
|
|
227
|
+
return (0, util_1.bufferToHex)((0, util_1.publicToAddress)(Buffer.from(publicKey), true)).toLowerCase();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
HdKeyring.type = type;
|
|
231
|
+
exports.default = HdKeyring;
|
|
232
|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AC;QAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC;IAED,4FAA4F;IAC5F,qBAAqB,CAAC,SAAS;QAC7B,OAAO,IAAA,kBAAW,EAChB,IAAA,sBAAe,EAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAC9C,CAAC,WAAW,EAAE,CAAC;IAClB,CAAC;CACF;AAED,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC;AACtB,kBAAe,SAAS,CAAC","sourcesContent":["import {\n privateToPublic,\n publicToAddress,\n ecsign,\n arrToBufArr,\n bufferToHex,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n getEncryptionPublicKey,\n normalize,\n personalSign,\n signTypedData,\n SignTypedDataVersion,\n} from '@metamask/eth-sig-util';\nimport { mnemonicToSeed } from '@metamask/key-tree';\nimport { generateMnemonic } from '@metamask/scure-bip39';\nimport { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';\nimport { assertIsHexString, remove0x } from '@metamask/utils';\nimport { HDKey } from 'ethereum-cryptography/hdkey';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\nimport { bytesToHex } from 'ethereum-cryptography/utils';\n\n// Options:\nconst hdPathString = `m/44'/60'/0'/0`;\nconst type = 'HD Key Tree';\n\nclass HdKeyring {\n /* PUBLIC METHODS */\n constructor(opts = {}) {\n this.type = type;\n this._wallets = [];\n // Cryptographic functions to be used by `@metamask/key-tree`. It will use built-in implementations if not provided here.\n this._cryptographicFunctions = opts.cryptographicFunctions;\n }\n\n async generateRandomMnemonic() {\n await this._initFromMnemonic(generateMnemonic(wordlist));\n }\n\n _uint8ArrayToString(mnemonic) {\n const recoveredIndices = Array.from(\n new Uint16Array(new Uint8Array(mnemonic).buffer),\n );\n return recoveredIndices.map((i) => wordlist[i]).join(' ');\n }\n\n _stringToUint8Array(mnemonic) {\n const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));\n return new Uint8Array(new Uint16Array(indices).buffer);\n }\n\n _mnemonicToUint8Array(mnemonic) {\n let mnemonicData = mnemonic;\n // when encrypted/decrypted, buffers get cast into js object with a property type set to buffer\n if (mnemonic && mnemonic.type && mnemonic.type === 'Buffer') {\n mnemonicData = mnemonic.data;\n }\n\n if (\n // this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings\n typeof mnemonicData === 'string' ||\n Buffer.isBuffer(mnemonicData) ||\n Array.isArray(mnemonicData)\n ) {\n let mnemonicAsString = mnemonicData;\n if (Array.isArray(mnemonicData)) {\n mnemonicAsString = Buffer.from(mnemonicData).toString();\n } else if (Buffer.isBuffer(mnemonicData)) {\n mnemonicAsString = mnemonicData.toString();\n }\n return this._stringToUint8Array(mnemonicAsString);\n } else if (\n mnemonicData instanceof Object &&\n !(mnemonicData instanceof Uint8Array)\n ) {\n // when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array\n return Uint8Array.from(Object.values(mnemonicData));\n }\n return mnemonicData;\n }\n\n async serialize() {\n const mnemonicAsString = this._uint8ArrayToString(this.mnemonic);\n const uint8ArrayMnemonic = new TextEncoder('utf-8').encode(\n mnemonicAsString,\n );\n\n return {\n mnemonic: Array.from(uint8ArrayMnemonic),\n numberOfAccounts: this._wallets.length,\n hdPath: this.hdPath,\n };\n }\n\n async deserialize(opts = {}) {\n if (opts.numberOfAccounts && !opts.mnemonic) {\n throw new Error(\n 'Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic',\n );\n }\n\n if (this.root) {\n throw new Error(\n 'Eth-Hd-Keyring: Secret recovery phrase already provided',\n );\n }\n this.opts = opts;\n this._wallets = [];\n this.mnemonic = null;\n this.root = null;\n this.hdPath = opts.hdPath || hdPathString;\n\n if (opts.mnemonic) {\n await this._initFromMnemonic(opts.mnemonic);\n }\n\n if (opts.numberOfAccounts) {\n return this.addAccounts(opts.numberOfAccounts);\n }\n\n return [];\n }\n\n addAccounts(numberOfAccounts = 1) {\n if (!this.root) {\n throw new Error('Eth-Hd-Keyring: No secret recovery phrase provided');\n }\n\n const oldLen = this._wallets.length;\n const newWallets = [];\n for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {\n const wallet = this.root.deriveChild(i);\n newWallets.push(wallet);\n this._wallets.push(wallet);\n }\n const hexWallets = newWallets.map((w) => {\n return this._addressfromPublicKey(w.publicKey);\n });\n return Promise.resolve(hexWallets);\n }\n\n getAccounts() {\n return this._wallets.map((w) => this._addressfromPublicKey(w.publicKey));\n }\n\n /* BASE KEYRING METHODS */\n\n // returns an address specific to an app\n async getAppKeyAddress(address, origin) {\n if (!origin || typeof origin !== 'string') {\n throw new Error(`'origin' must be a non-empty string`);\n }\n const wallet = this._getWalletForAccount(address, {\n withAppKeyOrigin: origin,\n });\n const appKeyAddress = normalize(\n publicToAddress(wallet.publicKey).toString('hex'),\n );\n\n return appKeyAddress;\n }\n\n // exportAccount should return a hex-encoded private key:\n async exportAccount(address, opts = {}) {\n const wallet = this._getWalletForAccount(address, opts);\n return bytesToHex(wallet.privateKey);\n }\n\n // tx is an instance of the ethereumjs-transaction class.\n async signTransaction(address, tx, opts = {}) {\n const privKey = this._getPrivateKeyFor(address, opts);\n const signedTx = tx.sign(privKey);\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx === undefined ? tx : signedTx;\n }\n\n // For eth_sign, we need to sign arbitrary data:\n async signMessage(address, data, opts = {}) {\n assertIsHexString(data);\n const message = remove0x(data);\n const privKey = this._getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), privKey);\n const rawMsgSig = concatSig(msgSig.v, msgSig.r, msgSig.s);\n return rawMsgSig;\n }\n\n // For personal_sign, we need to prefix the message:\n async signPersonalMessage(address, msgHex, opts = {}) {\n const privKey = this._getPrivateKeyFor(address, opts);\n const privateKey = Buffer.from(privKey, 'hex');\n const sig = personalSign({ privateKey, data: msgHex });\n return sig;\n }\n\n // For eth_decryptMessage:\n async decryptMessage(withAccount, encryptedData) {\n const wallet = this._getWalletForAccount(withAccount);\n const { privateKey: privateKeyAsUint8Array } = wallet;\n const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');\n const sig = decrypt({ privateKey: privateKeyAsHex, encryptedData });\n return sig;\n }\n\n // personal_signTypedData, signs data along with the schema\n async signTypedData(\n withAccount,\n typedData,\n opts = { version: SignTypedDataVersion.V1 },\n ) {\n // Treat invalid versions as \"V1\"\n const version = Object.keys(SignTypedDataVersion).includes(opts.version)\n ? opts.version\n : SignTypedDataVersion.V1;\n\n const privateKey = this._getPrivateKeyFor(withAccount, opts);\n return signTypedData({ privateKey, data: typedData, version });\n }\n\n removeAccount(account) {\n const address = normalize(account);\n if (\n !this._wallets\n .map(({ publicKey }) => this._addressfromPublicKey(publicKey))\n .includes(address)\n ) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this._wallets = this._wallets.filter(\n ({ publicKey }) => this._addressfromPublicKey(publicKey) !== address,\n );\n }\n\n // get public key for nacl\n async getEncryptionPublicKey(withAccount, opts = {}) {\n const privKey = this._getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(privKey);\n return publicKey;\n }\n\n _getPrivateKeyFor(address, opts = {}) {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this._getWalletForAccount(address, opts);\n return wallet.privateKey;\n }\n\n _getWalletForAccount(account, opts = {}) {\n const address = normalize(account);\n let wallet = this._wallets.find(({ publicKey }) => {\n return this._addressfromPublicKey(publicKey) === address;\n });\n if (!wallet) {\n throw new Error('HD Keyring - Unable to find matching address.');\n }\n\n if (opts.withAppKeyOrigin) {\n const { privateKey } = wallet;\n const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer, 256));\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n\n /* PRIVATE / UTILITY METHODS */\n\n /**\n * Sets appropriate properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @param {string|Array<number>|Buffer} mnemonic - A seed phrase represented\n * as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input\n * passed as type buffer or array of UTF-8 bytes must be NFKD normalized.\n */\n async _initFromMnemonic(mnemonic) {\n if (this.root) {\n throw new Error(\n 'Eth-Hd-Keyring: Secret recovery phrase already provided',\n );\n }\n\n this.mnemonic = this._mnemonicToUint8Array(mnemonic);\n\n const seed = await mnemonicToSeed(\n this.mnemonic,\n '', // No passphrase\n this._cryptographicFunctions,\n );\n this.hdWallet = HDKey.fromMasterSeed(seed);\n this.root = this.hdWallet.derive(this.hdPath);\n }\n\n // small helper function to convert publicKey in Uint8Array form to a publicAddress as a hex\n _addressfromPublicKey(publicKey) {\n return bufferToHex(\n publicToAddress(Buffer.from(publicKey), true),\n ).toLowerCase();\n }\n}\n\nHdKeyring.type = type;\nexport default HdKeyring;\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export default HdKeyring;
|
|
2
|
+
declare class HdKeyring {
|
|
3
|
+
constructor(opts?: {});
|
|
4
|
+
type: string;
|
|
5
|
+
_wallets: any[];
|
|
6
|
+
_cryptographicFunctions: any;
|
|
7
|
+
generateRandomMnemonic(): Promise<void>;
|
|
8
|
+
_uint8ArrayToString(mnemonic: any): string;
|
|
9
|
+
_stringToUint8Array(mnemonic: any): Uint8Array;
|
|
10
|
+
_mnemonicToUint8Array(mnemonic: any): any;
|
|
11
|
+
serialize(): Promise<{
|
|
12
|
+
mnemonic: number[];
|
|
13
|
+
numberOfAccounts: number;
|
|
14
|
+
hdPath: any;
|
|
15
|
+
}>;
|
|
16
|
+
deserialize(opts?: {}): Promise<string[]>;
|
|
17
|
+
opts: {} | undefined;
|
|
18
|
+
mnemonic: any;
|
|
19
|
+
root: HDKey | null | undefined;
|
|
20
|
+
hdPath: any;
|
|
21
|
+
addAccounts(numberOfAccounts?: number): Promise<string[]>;
|
|
22
|
+
getAccounts(): string[];
|
|
23
|
+
getAppKeyAddress(address: any, origin: any): Promise<string | undefined>;
|
|
24
|
+
exportAccount(address: any, opts?: {}): Promise<string>;
|
|
25
|
+
signTransaction(address: any, tx: any, opts?: {}): Promise<any>;
|
|
26
|
+
signMessage(address: any, data: any, opts?: {}): Promise<string>;
|
|
27
|
+
signPersonalMessage(address: any, msgHex: any, opts?: {}): Promise<string>;
|
|
28
|
+
decryptMessage(withAccount: any, encryptedData: any): Promise<string>;
|
|
29
|
+
signTypedData(withAccount: any, typedData: any, opts?: {
|
|
30
|
+
version: SignTypedDataVersion;
|
|
31
|
+
}): Promise<string>;
|
|
32
|
+
removeAccount(account: any): void;
|
|
33
|
+
getEncryptionPublicKey(withAccount: any, opts?: {}): Promise<string>;
|
|
34
|
+
_getPrivateKeyFor(address: any, opts?: {}): any;
|
|
35
|
+
_getWalletForAccount(account: any, opts?: {}): any;
|
|
36
|
+
/**
|
|
37
|
+
* Sets appropriate properties for the keyring based on the given
|
|
38
|
+
* BIP39-compliant mnemonic.
|
|
39
|
+
*
|
|
40
|
+
* @param {string|Array<number>|Buffer} mnemonic - A seed phrase represented
|
|
41
|
+
* as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input
|
|
42
|
+
* passed as type buffer or array of UTF-8 bytes must be NFKD normalized.
|
|
43
|
+
*/
|
|
44
|
+
_initFromMnemonic(mnemonic: string | Array<number> | Buffer): Promise<void>;
|
|
45
|
+
hdWallet: HDKey | undefined;
|
|
46
|
+
_addressfromPublicKey(publicKey: any): string;
|
|
47
|
+
}
|
|
48
|
+
declare namespace HdKeyring {
|
|
49
|
+
export { type };
|
|
50
|
+
}
|
|
51
|
+
import { HDKey } from "ethereum-cryptography/hdkey";
|
|
52
|
+
import { SignTypedDataVersion } from "@metamask/eth-sig-util";
|
|
53
|
+
declare const type: "HD Key Tree";
|
|
54
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AA4BA;IAEE,uBAKC;IAJC,aAAgB;IAChB,gBAAkB;IAElB,6BAA0D;IAG5D,wCAEC;IAED,2CAKC;IAED,+CAGC;IAED,0CA4BC;IAED;;;;OAWC;IAED,0CA2BC;IAfC,qBAAgB;IAEhB,cAAoB;IACpB,+BAAgB;IAChB,YAAyC;IAa3C,0DAgBC;IAED,wBAEC;IAKD,yEAYC;IAGD,wDAGC;IAGD,gEAKC;IAGD,iEAOC;IAGD,2EAKC;IAGD,sEAMC;IAGD;;wBAYC;IAED,kCAaC;IAGD,qEAIC;IAED,gDAMC;IAED,mDAmBC;IAID;;;;;;;OAOG;IACH,4BAJW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,GAAC,MAAM,iBAoBrC;IAFC,4BAA0C;IAK5C,8CAIC;CACF;;;;;;AAvRD,kCAA2B"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export default HdKeyring;
|
|
2
|
+
declare class HdKeyring {
|
|
3
|
+
constructor(opts?: {});
|
|
4
|
+
type: string;
|
|
5
|
+
_wallets: any[];
|
|
6
|
+
_cryptographicFunctions: any;
|
|
7
|
+
generateRandomMnemonic(): Promise<void>;
|
|
8
|
+
_uint8ArrayToString(mnemonic: any): string;
|
|
9
|
+
_stringToUint8Array(mnemonic: any): Uint8Array;
|
|
10
|
+
_mnemonicToUint8Array(mnemonic: any): any;
|
|
11
|
+
serialize(): Promise<{
|
|
12
|
+
mnemonic: number[];
|
|
13
|
+
numberOfAccounts: number;
|
|
14
|
+
hdPath: any;
|
|
15
|
+
}>;
|
|
16
|
+
deserialize(opts?: {}): Promise<string[]>;
|
|
17
|
+
opts: {} | undefined;
|
|
18
|
+
mnemonic: any;
|
|
19
|
+
root: HDKey | null | undefined;
|
|
20
|
+
hdPath: any;
|
|
21
|
+
addAccounts(numberOfAccounts?: number): Promise<string[]>;
|
|
22
|
+
getAccounts(): string[];
|
|
23
|
+
getAppKeyAddress(address: any, origin: any): Promise<string | undefined>;
|
|
24
|
+
exportAccount(address: any, opts?: {}): Promise<string>;
|
|
25
|
+
signTransaction(address: any, tx: any, opts?: {}): Promise<any>;
|
|
26
|
+
signMessage(address: any, data: any, opts?: {}): Promise<string>;
|
|
27
|
+
signPersonalMessage(address: any, msgHex: any, opts?: {}): Promise<string>;
|
|
28
|
+
decryptMessage(withAccount: any, encryptedData: any): Promise<string>;
|
|
29
|
+
signTypedData(withAccount: any, typedData: any, opts?: {
|
|
30
|
+
version: SignTypedDataVersion;
|
|
31
|
+
}): Promise<string>;
|
|
32
|
+
removeAccount(account: any): void;
|
|
33
|
+
getEncryptionPublicKey(withAccount: any, opts?: {}): Promise<string>;
|
|
34
|
+
_getPrivateKeyFor(address: any, opts?: {}): any;
|
|
35
|
+
_getWalletForAccount(account: any, opts?: {}): any;
|
|
36
|
+
/**
|
|
37
|
+
* Sets appropriate properties for the keyring based on the given
|
|
38
|
+
* BIP39-compliant mnemonic.
|
|
39
|
+
*
|
|
40
|
+
* @param {string|Array<number>|Buffer} mnemonic - A seed phrase represented
|
|
41
|
+
* as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input
|
|
42
|
+
* passed as type buffer or array of UTF-8 bytes must be NFKD normalized.
|
|
43
|
+
*/
|
|
44
|
+
_initFromMnemonic(mnemonic: string | Array<number> | Buffer): Promise<void>;
|
|
45
|
+
hdWallet: HDKey | undefined;
|
|
46
|
+
_addressfromPublicKey(publicKey: any): string;
|
|
47
|
+
}
|
|
48
|
+
declare namespace HdKeyring {
|
|
49
|
+
export { type };
|
|
50
|
+
}
|
|
51
|
+
import { HDKey } from "ethereum-cryptography/hdkey";
|
|
52
|
+
import { SignTypedDataVersion } from "@metamask/eth-sig-util";
|
|
53
|
+
declare const type: "HD Key Tree";
|
|
54
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AA4BA;IAEE,uBAKC;IAJC,aAAgB;IAChB,gBAAkB;IAElB,6BAA0D;IAG5D,wCAEC;IAED,2CAKC;IAED,+CAGC;IAED,0CA4BC;IAED;;;;OAWC;IAED,0CA2BC;IAfC,qBAAgB;IAEhB,cAAoB;IACpB,+BAAgB;IAChB,YAAyC;IAa3C,0DAgBC;IAED,wBAEC;IAKD,yEAYC;IAGD,wDAGC;IAGD,gEAKC;IAGD,iEAOC;IAGD,2EAKC;IAGD,sEAMC;IAGD;;wBAYC;IAED,kCAaC;IAGD,qEAIC;IAED,gDAMC;IAED,mDAmBC;IAID;;;;;;;OAOG;IACH,4BAJW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,GAAC,MAAM,iBAoBrC;IAFC,4BAA0C;IAK5C,8CAIC;CACF;;;;;;AAvRD,kCAA2B"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { privateToPublic, publicToAddress, ecsign, arrToBufArr, bufferToHex } from "@ethereumjs/util";
|
|
2
|
+
import { concatSig, decrypt, getEncryptionPublicKey, normalize, personalSign, signTypedData, SignTypedDataVersion } from "@metamask/eth-sig-util";
|
|
3
|
+
import { mnemonicToSeed } from "@metamask/key-tree";
|
|
4
|
+
import { generateMnemonic } from "@metamask/scure-bip39";
|
|
5
|
+
import { wordlist } from "@metamask/scure-bip39/dist/wordlists/english.js";
|
|
6
|
+
import { assertIsHexString, remove0x } from "@metamask/utils";
|
|
7
|
+
import { HDKey } from "ethereum-cryptography/hdkey";
|
|
8
|
+
import { keccak256 } from "ethereum-cryptography/keccak";
|
|
9
|
+
import { bytesToHex } from "ethereum-cryptography/utils";
|
|
10
|
+
// Options:
|
|
11
|
+
const hdPathString = `m/44'/60'/0'/0`;
|
|
12
|
+
const type = 'HD Key Tree';
|
|
13
|
+
class HdKeyring {
|
|
14
|
+
/* PUBLIC METHODS */
|
|
15
|
+
constructor(opts = {}) {
|
|
16
|
+
this.type = type;
|
|
17
|
+
this._wallets = [];
|
|
18
|
+
// Cryptographic functions to be used by `@metamask/key-tree`. It will use built-in implementations if not provided here.
|
|
19
|
+
this._cryptographicFunctions = opts.cryptographicFunctions;
|
|
20
|
+
}
|
|
21
|
+
async generateRandomMnemonic() {
|
|
22
|
+
await this._initFromMnemonic(generateMnemonic(wordlist));
|
|
23
|
+
}
|
|
24
|
+
_uint8ArrayToString(mnemonic) {
|
|
25
|
+
const recoveredIndices = Array.from(new Uint16Array(new Uint8Array(mnemonic).buffer));
|
|
26
|
+
return recoveredIndices.map((i) => wordlist[i]).join(' ');
|
|
27
|
+
}
|
|
28
|
+
_stringToUint8Array(mnemonic) {
|
|
29
|
+
const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));
|
|
30
|
+
return new Uint8Array(new Uint16Array(indices).buffer);
|
|
31
|
+
}
|
|
32
|
+
_mnemonicToUint8Array(mnemonic) {
|
|
33
|
+
let mnemonicData = mnemonic;
|
|
34
|
+
// when encrypted/decrypted, buffers get cast into js object with a property type set to buffer
|
|
35
|
+
if (mnemonic && mnemonic.type && mnemonic.type === 'Buffer') {
|
|
36
|
+
mnemonicData = mnemonic.data;
|
|
37
|
+
}
|
|
38
|
+
if (
|
|
39
|
+
// this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings
|
|
40
|
+
typeof mnemonicData === 'string' ||
|
|
41
|
+
Buffer.isBuffer(mnemonicData) ||
|
|
42
|
+
Array.isArray(mnemonicData)) {
|
|
43
|
+
let mnemonicAsString = mnemonicData;
|
|
44
|
+
if (Array.isArray(mnemonicData)) {
|
|
45
|
+
mnemonicAsString = Buffer.from(mnemonicData).toString();
|
|
46
|
+
}
|
|
47
|
+
else if (Buffer.isBuffer(mnemonicData)) {
|
|
48
|
+
mnemonicAsString = mnemonicData.toString();
|
|
49
|
+
}
|
|
50
|
+
return this._stringToUint8Array(mnemonicAsString);
|
|
51
|
+
}
|
|
52
|
+
else if (mnemonicData instanceof Object &&
|
|
53
|
+
!(mnemonicData instanceof Uint8Array)) {
|
|
54
|
+
// when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array
|
|
55
|
+
return Uint8Array.from(Object.values(mnemonicData));
|
|
56
|
+
}
|
|
57
|
+
return mnemonicData;
|
|
58
|
+
}
|
|
59
|
+
async serialize() {
|
|
60
|
+
const mnemonicAsString = this._uint8ArrayToString(this.mnemonic);
|
|
61
|
+
const uint8ArrayMnemonic = new TextEncoder('utf-8').encode(mnemonicAsString);
|
|
62
|
+
return {
|
|
63
|
+
mnemonic: Array.from(uint8ArrayMnemonic),
|
|
64
|
+
numberOfAccounts: this._wallets.length,
|
|
65
|
+
hdPath: this.hdPath,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async deserialize(opts = {}) {
|
|
69
|
+
if (opts.numberOfAccounts && !opts.mnemonic) {
|
|
70
|
+
throw new Error('Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic');
|
|
71
|
+
}
|
|
72
|
+
if (this.root) {
|
|
73
|
+
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
74
|
+
}
|
|
75
|
+
this.opts = opts;
|
|
76
|
+
this._wallets = [];
|
|
77
|
+
this.mnemonic = null;
|
|
78
|
+
this.root = null;
|
|
79
|
+
this.hdPath = opts.hdPath || hdPathString;
|
|
80
|
+
if (opts.mnemonic) {
|
|
81
|
+
await this._initFromMnemonic(opts.mnemonic);
|
|
82
|
+
}
|
|
83
|
+
if (opts.numberOfAccounts) {
|
|
84
|
+
return this.addAccounts(opts.numberOfAccounts);
|
|
85
|
+
}
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
addAccounts(numberOfAccounts = 1) {
|
|
89
|
+
if (!this.root) {
|
|
90
|
+
throw new Error('Eth-Hd-Keyring: No secret recovery phrase provided');
|
|
91
|
+
}
|
|
92
|
+
const oldLen = this._wallets.length;
|
|
93
|
+
const newWallets = [];
|
|
94
|
+
for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
|
|
95
|
+
const wallet = this.root.deriveChild(i);
|
|
96
|
+
newWallets.push(wallet);
|
|
97
|
+
this._wallets.push(wallet);
|
|
98
|
+
}
|
|
99
|
+
const hexWallets = newWallets.map((w) => {
|
|
100
|
+
return this._addressfromPublicKey(w.publicKey);
|
|
101
|
+
});
|
|
102
|
+
return Promise.resolve(hexWallets);
|
|
103
|
+
}
|
|
104
|
+
getAccounts() {
|
|
105
|
+
return this._wallets.map((w) => this._addressfromPublicKey(w.publicKey));
|
|
106
|
+
}
|
|
107
|
+
/* BASE KEYRING METHODS */
|
|
108
|
+
// returns an address specific to an app
|
|
109
|
+
async getAppKeyAddress(address, origin) {
|
|
110
|
+
if (!origin || typeof origin !== 'string') {
|
|
111
|
+
throw new Error(`'origin' must be a non-empty string`);
|
|
112
|
+
}
|
|
113
|
+
const wallet = this._getWalletForAccount(address, {
|
|
114
|
+
withAppKeyOrigin: origin,
|
|
115
|
+
});
|
|
116
|
+
const appKeyAddress = normalize(publicToAddress(wallet.publicKey).toString('hex'));
|
|
117
|
+
return appKeyAddress;
|
|
118
|
+
}
|
|
119
|
+
// exportAccount should return a hex-encoded private key:
|
|
120
|
+
async exportAccount(address, opts = {}) {
|
|
121
|
+
const wallet = this._getWalletForAccount(address, opts);
|
|
122
|
+
return bytesToHex(wallet.privateKey);
|
|
123
|
+
}
|
|
124
|
+
// tx is an instance of the ethereumjs-transaction class.
|
|
125
|
+
async signTransaction(address, tx, opts = {}) {
|
|
126
|
+
const privKey = this._getPrivateKeyFor(address, opts);
|
|
127
|
+
const signedTx = tx.sign(privKey);
|
|
128
|
+
// Newer versions of Ethereumjs-tx are immutable and return a new tx object
|
|
129
|
+
return signedTx === undefined ? tx : signedTx;
|
|
130
|
+
}
|
|
131
|
+
// For eth_sign, we need to sign arbitrary data:
|
|
132
|
+
async signMessage(address, data, opts = {}) {
|
|
133
|
+
assertIsHexString(data);
|
|
134
|
+
const message = remove0x(data);
|
|
135
|
+
const privKey = this._getPrivateKeyFor(address, opts);
|
|
136
|
+
const msgSig = ecsign(Buffer.from(message, 'hex'), privKey);
|
|
137
|
+
const rawMsgSig = concatSig(msgSig.v, msgSig.r, msgSig.s);
|
|
138
|
+
return rawMsgSig;
|
|
139
|
+
}
|
|
140
|
+
// For personal_sign, we need to prefix the message:
|
|
141
|
+
async signPersonalMessage(address, msgHex, opts = {}) {
|
|
142
|
+
const privKey = this._getPrivateKeyFor(address, opts);
|
|
143
|
+
const privateKey = Buffer.from(privKey, 'hex');
|
|
144
|
+
const sig = personalSign({ privateKey, data: msgHex });
|
|
145
|
+
return sig;
|
|
146
|
+
}
|
|
147
|
+
// For eth_decryptMessage:
|
|
148
|
+
async decryptMessage(withAccount, encryptedData) {
|
|
149
|
+
const wallet = this._getWalletForAccount(withAccount);
|
|
150
|
+
const { privateKey: privateKeyAsUint8Array } = wallet;
|
|
151
|
+
const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');
|
|
152
|
+
const sig = decrypt({ privateKey: privateKeyAsHex, encryptedData });
|
|
153
|
+
return sig;
|
|
154
|
+
}
|
|
155
|
+
// personal_signTypedData, signs data along with the schema
|
|
156
|
+
async signTypedData(withAccount, typedData, opts = { version: SignTypedDataVersion.V1 }) {
|
|
157
|
+
// Treat invalid versions as "V1"
|
|
158
|
+
const version = Object.keys(SignTypedDataVersion).includes(opts.version)
|
|
159
|
+
? opts.version
|
|
160
|
+
: SignTypedDataVersion.V1;
|
|
161
|
+
const privateKey = this._getPrivateKeyFor(withAccount, opts);
|
|
162
|
+
return signTypedData({ privateKey, data: typedData, version });
|
|
163
|
+
}
|
|
164
|
+
removeAccount(account) {
|
|
165
|
+
const address = normalize(account);
|
|
166
|
+
if (!this._wallets
|
|
167
|
+
.map(({ publicKey }) => this._addressfromPublicKey(publicKey))
|
|
168
|
+
.includes(address)) {
|
|
169
|
+
throw new Error(`Address ${address} not found in this keyring`);
|
|
170
|
+
}
|
|
171
|
+
this._wallets = this._wallets.filter(({ publicKey }) => this._addressfromPublicKey(publicKey) !== address);
|
|
172
|
+
}
|
|
173
|
+
// get public key for nacl
|
|
174
|
+
async getEncryptionPublicKey(withAccount, opts = {}) {
|
|
175
|
+
const privKey = this._getPrivateKeyFor(withAccount, opts);
|
|
176
|
+
const publicKey = getEncryptionPublicKey(privKey);
|
|
177
|
+
return publicKey;
|
|
178
|
+
}
|
|
179
|
+
_getPrivateKeyFor(address, opts = {}) {
|
|
180
|
+
if (!address) {
|
|
181
|
+
throw new Error('Must specify address.');
|
|
182
|
+
}
|
|
183
|
+
const wallet = this._getWalletForAccount(address, opts);
|
|
184
|
+
return wallet.privateKey;
|
|
185
|
+
}
|
|
186
|
+
_getWalletForAccount(account, opts = {}) {
|
|
187
|
+
const address = normalize(account);
|
|
188
|
+
let wallet = this._wallets.find(({ publicKey }) => {
|
|
189
|
+
return this._addressfromPublicKey(publicKey) === address;
|
|
190
|
+
});
|
|
191
|
+
if (!wallet) {
|
|
192
|
+
throw new Error('HD Keyring - Unable to find matching address.');
|
|
193
|
+
}
|
|
194
|
+
if (opts.withAppKeyOrigin) {
|
|
195
|
+
const { privateKey } = wallet;
|
|
196
|
+
const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');
|
|
197
|
+
const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
|
|
198
|
+
const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer, 256));
|
|
199
|
+
const appKeyPublicKey = privateToPublic(appKeyPrivateKey);
|
|
200
|
+
wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
|
|
201
|
+
}
|
|
202
|
+
return wallet;
|
|
203
|
+
}
|
|
204
|
+
/* PRIVATE / UTILITY METHODS */
|
|
205
|
+
/**
|
|
206
|
+
* Sets appropriate properties for the keyring based on the given
|
|
207
|
+
* BIP39-compliant mnemonic.
|
|
208
|
+
*
|
|
209
|
+
* @param {string|Array<number>|Buffer} mnemonic - A seed phrase represented
|
|
210
|
+
* as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input
|
|
211
|
+
* passed as type buffer or array of UTF-8 bytes must be NFKD normalized.
|
|
212
|
+
*/
|
|
213
|
+
async _initFromMnemonic(mnemonic) {
|
|
214
|
+
if (this.root) {
|
|
215
|
+
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
216
|
+
}
|
|
217
|
+
this.mnemonic = this._mnemonicToUint8Array(mnemonic);
|
|
218
|
+
const seed = await mnemonicToSeed(this.mnemonic, '', // No passphrase
|
|
219
|
+
this._cryptographicFunctions);
|
|
220
|
+
this.hdWallet = HDKey.fromMasterSeed(seed);
|
|
221
|
+
this.root = this.hdWallet.derive(this.hdPath);
|
|
222
|
+
}
|
|
223
|
+
// small helper function to convert publicKey in Uint8Array form to a publicAddress as a hex
|
|
224
|
+
_addressfromPublicKey(publicKey) {
|
|
225
|
+
return bufferToHex(publicToAddress(Buffer.from(publicKey), true)).toLowerCase();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
HdKeyring.type = type;
|
|
229
|
+
export default HdKeyring;
|
|
230
|
+
//# sourceMappingURL=index.mjs.map
|