@metamask/eth-hd-keyring 11.0.0 → 12.1.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/CHANGELOG.md +19 -1
- package/dist/hd-keyring.cjs +10 -12
- package/dist/hd-keyring.cjs.map +1 -1
- package/dist/hd-keyring.d.cts +1 -0
- package/dist/hd-keyring.d.cts.map +1 -1
- package/dist/hd-keyring.d.mts +1 -0
- package/dist/hd-keyring.d.mts.map +1 -1
- package/dist/hd-keyring.mjs +12 -14
- package/dist/hd-keyring.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [12.1.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Store mnemonic seed in `HdKeyring.seed` ([#244](https://github.com/MetaMask/accounts/pull/244))
|
|
15
|
+
|
|
16
|
+
## [12.0.0]
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- **BREAKING:** The method `signTransaction` can now returns various type of transactions ([#209](https://github.com/MetaMask/accounts/pull/209))
|
|
21
|
+
- Initially was supporting: `Transaction | AccessListEIP2930Transaction | FeeMarketEIP1559Transaction` (types from `@ethereumjs/tx`).
|
|
22
|
+
- Now also supports `BlobEIP4844Transaction | EOACodeEIP7702Transaction` (types from `@ethereumjs/tx`).
|
|
23
|
+
- **BREAKING:** Bump `@ethereumjs/tx` from `^4.2.0` to `^5.4.0` ([#209](https://github.com/MetaMask/accounts/pull/209))
|
|
24
|
+
- **BREAKING:** Bump `@ethereumjs/util` from `^8.1.0` to `^9.1.0` ([#209](https://github.com/MetaMask/accounts/pull/209))
|
|
25
|
+
|
|
10
26
|
## [11.0.0]
|
|
11
27
|
|
|
12
28
|
### Changed
|
|
@@ -181,7 +197,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
181
197
|
- 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`.
|
|
182
198
|
- Package name changed from `eth-hd-keyring` to `@metamask/eth-hd-keyring`.
|
|
183
199
|
|
|
184
|
-
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@
|
|
200
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@12.1.0...HEAD
|
|
201
|
+
[12.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@12.0.0...@metamask/eth-hd-keyring@12.1.0
|
|
202
|
+
[12.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@11.0.0...@metamask/eth-hd-keyring@12.0.0
|
|
185
203
|
[11.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@10.0.1...@metamask/eth-hd-keyring@11.0.0
|
|
186
204
|
[10.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@10.0.0...@metamask/eth-hd-keyring@10.0.1
|
|
187
205
|
[10.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@9.0.1...@metamask/eth-hd-keyring@10.0.0
|
package/dist/hd-keyring.cjs
CHANGED
|
@@ -21,7 +21,6 @@ const english_1 = require("@metamask/scure-bip39/dist/wordlists/english.js");
|
|
|
21
21
|
const utils_1 = require("@metamask/utils");
|
|
22
22
|
const hdkey_1 = require("ethereum-cryptography/hdkey");
|
|
23
23
|
const keccak_1 = require("ethereum-cryptography/keccak");
|
|
24
|
-
const utils_2 = require("ethereum-cryptography/utils");
|
|
25
24
|
// Options:
|
|
26
25
|
const hdPathString = `m/44'/60'/0'/0`;
|
|
27
26
|
const type = 'HD Key Tree';
|
|
@@ -90,6 +89,7 @@ class HdKeyring {
|
|
|
90
89
|
}
|
|
91
90
|
__classPrivateFieldSet(this, _HdKeyring_wallets, [], "f");
|
|
92
91
|
this.mnemonic = null;
|
|
92
|
+
this.seed = null;
|
|
93
93
|
this.root = null;
|
|
94
94
|
this.hdPath = opts.hdPath ?? hdPathString;
|
|
95
95
|
if (opts.mnemonic) {
|
|
@@ -150,7 +150,7 @@ class HdKeyring {
|
|
|
150
150
|
withAppKeyOrigin: origin,
|
|
151
151
|
});
|
|
152
152
|
(0, utils_1.assert)(wallet.publicKey, 'Expected public key to be set');
|
|
153
|
-
const appKeyAddress = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_normalizeAddress).call(this, (0, util_1.publicToAddress)(wallet.publicKey)
|
|
153
|
+
const appKeyAddress = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_normalizeAddress).call(this, (0, utils_1.bytesToHex)((0, util_1.publicToAddress)(wallet.publicKey)));
|
|
154
154
|
return appKeyAddress;
|
|
155
155
|
}
|
|
156
156
|
/**
|
|
@@ -168,7 +168,7 @@ class HdKeyring {
|
|
|
168
168
|
: __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getWalletForAccount).call(this, address);
|
|
169
169
|
const { privateKey } = wallet;
|
|
170
170
|
(0, utils_1.assert)(privateKey instanceof Uint8Array, 'Expected private key to be of type Uint8Array');
|
|
171
|
-
return (0,
|
|
171
|
+
return (0, utils_1.remove0x)((0, utils_1.bytesToHex)(privateKey));
|
|
172
172
|
}
|
|
173
173
|
/**
|
|
174
174
|
* Sign a transaction using the private key of the specified account.
|
|
@@ -197,9 +197,7 @@ class HdKeyring {
|
|
|
197
197
|
const message = (0, utils_1.remove0x)(data);
|
|
198
198
|
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
199
199
|
const msgSig = (0, util_1.ecsign)(Buffer.from(message, 'hex'), Buffer.from(privKey));
|
|
200
|
-
const rawMsgSig = (0, eth_sig_util_1.concatSig)(
|
|
201
|
-
// WARN: verify this cast to Buffer
|
|
202
|
-
msgSig.v, msgSig.r, msgSig.s);
|
|
200
|
+
const rawMsgSig = (0, eth_sig_util_1.concatSig)(Buffer.from((0, utils_1.bigIntToBytes)(msgSig.v)), Buffer.from(msgSig.r), Buffer.from(msgSig.s));
|
|
203
201
|
return rawMsgSig;
|
|
204
202
|
}
|
|
205
203
|
/**
|
|
@@ -292,7 +290,7 @@ class HdKeyring {
|
|
|
292
290
|
*/
|
|
293
291
|
async getEncryptionPublicKey(withAccount, opts = {}) {
|
|
294
292
|
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, withAccount, opts);
|
|
295
|
-
const publicKey = (0, eth_sig_util_1.getEncryptionPublicKey)((0,
|
|
293
|
+
const publicKey = (0, eth_sig_util_1.getEncryptionPublicKey)((0, utils_1.remove0x)((0, utils_1.bytesToHex)(privKey)));
|
|
296
294
|
return publicKey;
|
|
297
295
|
}
|
|
298
296
|
}
|
|
@@ -354,8 +352,8 @@ _HdKeyring_wallets = new WeakMap(), _HdKeyring_cryptographicFunctions = new Weak
|
|
|
354
352
|
(0, utils_1.assert)(privateKey, 'Expected private key to be set');
|
|
355
353
|
const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');
|
|
356
354
|
const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
|
|
357
|
-
const appKeyPrivateKey =
|
|
358
|
-
const appKeyPublicKey = (0, util_1.privateToPublic)(appKeyPrivateKey);
|
|
355
|
+
const appKeyPrivateKey = Buffer.from((0, keccak_1.keccak256)(appKeyBuffer));
|
|
356
|
+
const appKeyPublicKey = Buffer.from((0, util_1.privateToPublic)(appKeyPrivateKey));
|
|
359
357
|
return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
|
|
360
358
|
}
|
|
361
359
|
return wallet;
|
|
@@ -373,12 +371,12 @@ async function _HdKeyring_initFromMnemonic(mnemonic) {
|
|
|
373
371
|
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
374
372
|
}
|
|
375
373
|
this.mnemonic = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_mnemonicToUint8Array).call(this, mnemonic);
|
|
376
|
-
|
|
374
|
+
this.seed = await (0, key_tree_1.mnemonicToSeed)(this.mnemonic, '', // No passphrase
|
|
377
375
|
__classPrivateFieldGet(this, _HdKeyring_cryptographicFunctions, "f"));
|
|
378
|
-
this.hdWallet = hdkey_1.HDKey.fromMasterSeed(seed);
|
|
376
|
+
this.hdWallet = hdkey_1.HDKey.fromMasterSeed(this.seed);
|
|
379
377
|
this.root = this.hdWallet.derive(this.hdPath);
|
|
380
378
|
}, _HdKeyring_addressfromPublicKey = function _HdKeyring_addressfromPublicKey(publicKey) {
|
|
381
|
-
return (0, utils_1.add0x)((0,
|
|
379
|
+
return (0, utils_1.add0x)((0, utils_1.bytesToHex)((0, util_1.publicToAddress)(Buffer.from(publicKey), true)).toLowerCase());
|
|
382
380
|
}, _HdKeyring_normalizeAddress = function _HdKeyring_normalizeAddress(address) {
|
|
383
381
|
const normalized = (0, eth_sig_util_1.normalize)(address);
|
|
384
382
|
(0, utils_1.assert)(normalized, 'Expected address to be set');
|
package/dist/hd-keyring.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hd-keyring.cjs","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,2CAM0B;AAC1B,yDAcgC;AAChC,iDAG4B;AAC5B,uDAAyD;AACzD,6EAAwE;AACxE,2CAMyB;AACzB,uDAAoD;AACpD,yDAAyD;AACzD,uDAAyD;AAEzD,WAAW;AACX,MAAM,YAAY,GAAG,gBAAgB,CAAC;AACtC,MAAM,IAAI,GAAG,aAAa,CAAC;AA8B3B;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,KAAc;IACxC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KA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AAC,IAAI,CAAC,CAAC;IAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAChD,CAAC,6EAQqB,SAAqB;IACzC,OAAO,IAAA,aAAK,EACV,IAAA,kBAAW,EAAC,IAAA,sBAAe,EAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACzE,CAAC;AACJ,CAAC,qEAQiB,OAAe;IAC/B,MAAM,UAAU,GAAG,IAAA,wBAAS,EAAC,OAAO,CAAC,CAAC;IACtC,IAAA,cAAM,EAAC,UAAU,EAAE,4BAA4B,CAAC,CAAC;IACjD,OAAO,IAAA,aAAK,EAAC,UAAU,CAAC,CAAC;AAC3B,CAAC;AAxhBM,cAAI,GAAW,IAAI,AAAf,CAAgB","sourcesContent":["import type { TypedTransaction } from '@ethereumjs/tx';\nimport {\n privateToPublic,\n publicToAddress,\n ecsign,\n arrToBufArr,\n bufferToHex,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n type EIP7702Authorization,\n type EthEncryptedData,\n getEncryptionPublicKey,\n type MessageTypes,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n type TypedDataV1,\n type TypedMessage,\n} from '@metamask/eth-sig-util';\nimport {\n type CryptographicFunctions,\n mnemonicToSeed,\n} from '@metamask/key-tree';\nimport { generateMnemonic } from '@metamask/scure-bip39';\nimport { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';\nimport {\n add0x,\n assert,\n assertIsHexString,\n type Hex,\n remove0x,\n} 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\nexport type HDKeyringOptions = {\n cryptographicFunctions?: CryptographicFunctions;\n};\n\n/**\n * The serialized state of an `HDKeyring` instance.\n *\n * @property mnemonic - The mnemonic seed phrase as an array of numbers.\n * @property numberOfAccounts - The number of accounts in the keyring.\n * @property hdPath - The HD path used to derive accounts.\n */\nexport type HDKeyringState = {\n mnemonic: number[] | Uint8Array | Buffer | string;\n numberOfAccounts: number;\n hdPath: string;\n};\n\n/**\n * Options for selecting an account from an `HDKeyring` instance.\n *\n * @property withAppKeyOrigin - The origin of the app requesting the account.\n */\nexport type HDKeyringAccountSelectionOptions = {\n withAppKeyOrigin?: string;\n};\n\ntype SerializedBuffer = ReturnType<Buffer['toJSON']>;\n\n/**\n * Checks if the given value is a valid serialized Buffer compatible with\n * the return type of `Buffer.toJSON`.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a valid serialized buffer, `false` otherwise.\n */\nfunction isSerializedBuffer(value: unknown): value is SerializedBuffer {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'Buffer' &&\n 'data' in value &&\n Array.isArray(value.data)\n );\n}\n\nexport class HdKeyring {\n static type: string = type;\n\n type: string = type;\n\n mnemonic?: Uint8Array | null;\n\n root?: HDKey | null;\n\n hdWallet?: HDKey;\n\n hdPath: string = hdPathString;\n\n #wallets: HDKey[] = [];\n\n readonly #cryptographicFunctions?: CryptographicFunctions;\n\n constructor(opts: HDKeyringOptions = {}) {\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 /**\n * Initialize the keyring with a random mnemonic.\n *\n * @returns A promise that resolves when the process is complete.\n */\n async generateRandomMnemonic(): Promise<void> {\n await this.#initFromMnemonic(generateMnemonic(wordlist));\n }\n\n /**\n * Return the serialized state of the keyring.\n *\n * @returns The serialized state of the keyring.\n */\n async serialize(): Promise<HDKeyringState> {\n let mnemonic: number[] = [];\n\n if (this.mnemonic) {\n const mnemonicAsString = this.#uint8ArrayToString(this.mnemonic);\n mnemonic = Array.from(new TextEncoder().encode(mnemonicAsString));\n }\n\n return {\n mnemonic,\n numberOfAccounts: this.#wallets.length,\n hdPath: this.hdPath,\n };\n }\n\n /**\n * Initialize the keyring with the given serialized state.\n *\n * @param opts - The serialized state of the keyring.\n * @returns An empty array.\n */\n async deserialize(opts: Partial<HDKeyringState> = {}): Promise<string[]> {\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.#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 /**\n * Add new accounts to the keyring. The accounts will be derived\n * sequentially from the root HD wallet, using increasing indices.\n *\n * @param numberOfAccounts - The number of accounts to add.\n * @returns The addresses of the new accounts.\n */\n async addAccounts(numberOfAccounts = 1): Promise<Hex[]> {\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: HDKey[] = [];\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((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n return Promise.resolve(hexWallets);\n }\n\n /**\n * Get the addresses of all accounts in the keyring.\n *\n * @returns The addresses of all accounts in the keyring.\n */\n getAccounts(): Hex[] {\n return this.#wallets.map((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n }\n\n /**\n * Get the public address of the account for the given app key origin.\n *\n * @param address - The address of the account.\n * @param origin - The origin of the app requesting the account.\n * @returns The public address of the account.\n */\n async getAppKeyAddress(address: Hex, origin: string): Promise<Hex> {\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 assert(wallet.publicKey, 'Expected public key to be set');\n const appKeyAddress = this.#normalizeAddress(\n publicToAddress(wallet.publicKey).toString('hex'),\n );\n return appKeyAddress;\n }\n\n /**\n * Export the private key for a specific account.\n *\n * @param address - The address of the account.\n * @param opts - The options for exporting the account.\n * @param opts.withAppKeyOrigin - An optional string to export the account\n * for a specific app origin.\n * @returns The private key of the account.\n */\n async exportAccount(\n address: Hex,\n opts?: { withAppKeyOrigin: string },\n ): Promise<string> {\n const wallet = opts\n ? this.#getWalletForAccount(address, opts)\n : this.#getWalletForAccount(address);\n const { privateKey } = wallet;\n assert(\n privateKey instanceof Uint8Array,\n 'Expected private key to be of type Uint8Array',\n );\n return bytesToHex(privateKey);\n }\n\n /**\n * Sign a transaction using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param tx - The transaction to sign.\n * @param opts - The options for signing the transaction.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n tx: TypedTransaction,\n opts = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = tx.sign(Buffer.from(privKey));\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? tx;\n }\n\n /**\n * Sign a message using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param data - The data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signMessage(\n address: Hex,\n data: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n assertIsHexString(data);\n const message = remove0x(data);\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), Buffer.from(privKey));\n const rawMsgSig = concatSig(\n // WARN: verify this cast to Buffer\n msgSig.v as unknown as Buffer,\n msgSig.r,\n msgSig.s,\n );\n return rawMsgSig;\n }\n\n /**\n * Sign a personal message using the private key of the specified account.\n * This method is compatible with the `personal_sign` RPC method.\n *\n * @param address - The address of the account.\n * @param msgHex - The message to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signPersonalMessage(\n address: Hex,\n msgHex: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const privateKey = Buffer.from(privKey);\n return personalSign({ privateKey, data: msgHex });\n }\n\n /**\n * Decrypt an encrypted message using the private key of the specified account.\n * The message must be encrypted using the public key of the account.\n * This method is compatible with the `eth_decryptMessage` RPC method\n *\n * @param withAccount - The address of the account.\n * @param encryptedData - The encrypted data.\n * @returns The decrypted message.\n */\n async decryptMessage(\n withAccount: Hex,\n encryptedData: EthEncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const { privateKey: privateKeyAsUint8Array } = wallet;\n assert(privateKeyAsUint8Array, 'Expected private key to be set');\n const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');\n return decrypt({ privateKey: privateKeyAsHex, encryptedData });\n }\n\n /**\n * Sign a typed message using the private key of the specified account.\n * This method is compatible with the `eth_signTypedData` RPC method.\n *\n * @param withAccount - The address of the account.\n * @param typedData - The typed data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signTypedData<Types extends MessageTypes>(\n withAccount: Hex,\n typedData: TypedDataV1 | TypedMessage<Types>,\n opts: HDKeyringAccountSelectionOptions & {\n version: SignTypedDataVersion;\n } = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\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({\n privateKey: Buffer.from(privateKey),\n data: typedData,\n version,\n });\n }\n\n /**\n * Sign an EIP-7702 authorization using the private key of the specified account.\n * This method is compatible with the EIP-7702 standard for enabling smart contract code for EOAs.\n *\n * @param withAccount - The address of the account.\n * @param authorization - The EIP-7702 authorization to sign.\n * @param opts - The options for selecting the account.\n * @returns The signature of the authorization.\n */\n async signEip7702Authorization(\n withAccount: Hex,\n authorization: EIP7702Authorization,\n opts?: HDKeyringAccountSelectionOptions,\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(withAccount, opts);\n return signEIP7702Authorization({\n privateKey: Buffer.from(privateKey),\n authorization,\n });\n }\n\n /**\n * Remove an account from the keyring.\n *\n * @param account - The address of the account to remove.\n */\n removeAccount(account: Hex): void {\n const address = this.#normalizeAddress(account);\n if (\n !this.#wallets\n .map(\n ({ publicKey }) => publicKey && this.#addressfromPublicKey(publicKey),\n )\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 }) =>\n publicKey && this.#addressfromPublicKey(publicKey) !== address,\n );\n }\n\n /**\n * Get the public key of the account to be used for encryption.\n *\n * @param withAccount - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The public key of the account.\n */\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(bytesToHex(privKey));\n return publicKey;\n }\n\n /**\n * Convert a Uint8Array mnemonic to a secret recovery phrase,\n * using the english wordlist.\n *\n * @param mnemonic - The Uint8Array mnemonic.\n * @returns The string mnemonic.\n */\n #uint8ArrayToString(mnemonic: Uint8Array): string {\n const recoveredIndices = Array.from(\n new Uint16Array(new Uint8Array(mnemonic).buffer),\n );\n return recoveredIndices.map((i) => wordlist[i]).join(' ');\n }\n\n /**\n * Convert a secret recovery phrase to a Uint8Array mnemonic,\n * using the english wordlist.\n *\n * @param mnemonic - The string mnemonic.\n * @returns The Uint8Array mnemonic.\n */\n #stringToUint8Array(mnemonic: string): Uint8Array {\n const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));\n return new Uint8Array(new Uint16Array(indices).buffer);\n }\n\n /**\n * Convert a mnemonic to a Uint8Array mnemonic.\n *\n * @param mnemonic - The mnemonic seed phrase.\n * @returns The Uint8Array mnemonic.\n */\n #mnemonicToUint8Array(\n mnemonic: Buffer | SerializedBuffer | string | Uint8Array | number[],\n ): Uint8Array {\n let mnemonicData: unknown = mnemonic;\n // When using `Buffer.toJSON()`, the Buffer is serialized into an object\n // with the structure `{ type: 'Buffer', data: [...] }`\n if (isSerializedBuffer(mnemonic)) {\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: string;\n if (Array.isArray(mnemonicData)) {\n mnemonicAsString = Buffer.from(mnemonicData).toString();\n } else if (Buffer.isBuffer(mnemonicData)) {\n mnemonicAsString = mnemonicData.toString();\n } else {\n mnemonicAsString = mnemonicData;\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\n assert(mnemonicData instanceof Uint8Array, 'Expected Uint8Array mnemonic');\n return mnemonicData;\n }\n\n /**\n * Get the private key for the specified account.\n *\n * @param address - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The private key of the account.\n */\n #getPrivateKeyFor(\n address: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): Uint8Array | Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n assert(wallet.privateKey, 'Missing private key');\n return wallet.privateKey;\n }\n\n /**\n * Get the wallet for the specified account.\n *\n * @param account - The address of the account.\n * @returns The wallet for the account as HDKey.\n */\n #getWalletForAccount(account: Hex): HDKey;\n\n /**\n * Get the wallet for the specified account and app origin.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n accounts: Hex,\n opts: { withAppKeyOrigin: string },\n ): { privateKey: Buffer; publicKey: Buffer };\n\n /**\n * Get the wallet for the specified account with optional\n * additional options.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n account: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): HDKey | { privateKey: Buffer; publicKey: Buffer };\n\n #getWalletForAccount(\n account: Hex,\n { withAppKeyOrigin }: HDKeyringAccountSelectionOptions = {},\n ): HDKey | { privateKey: Buffer; publicKey: Buffer } {\n const address = this.#normalizeAddress(account);\n const wallet = this.#wallets.find(({ publicKey }) => {\n return publicKey && this.#addressfromPublicKey(publicKey) === address;\n });\n if (!wallet) {\n throw new Error('HD Keyring - Unable to find matching address.');\n }\n\n if (withAppKeyOrigin) {\n const { privateKey } = wallet;\n assert(privateKey, 'Expected private key to be set');\n const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer));\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n\n /**\n * Sets appropriate properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @param 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(\n mnemonic: string | number[] | Buffer | Uint8Array,\n ): Promise<void> {\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 /**\n * Get the address of the account from the public key.\n *\n * @param publicKey - The public key of the account.\n * @returns The address of the account.\n */\n #addressfromPublicKey(publicKey: Uint8Array): Hex {\n return add0x(\n bufferToHex(publicToAddress(Buffer.from(publicKey), true)).toLowerCase(),\n );\n }\n\n /**\n * Normalize an address to a lower-cased '0x'-prefixed hex string.\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\n #normalizeAddress(address: string): Hex {\n const normalized = normalize(address);\n assert(normalized, 'Expected address to be set');\n return add0x(normalized);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"hd-keyring.cjs","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,2CAA4E;AAC5E,yDAcgC;AAChC,iDAG4B;AAC5B,uDAAyD;AACzD,6EAAwE;AACxE,2CAQyB;AACzB,uDAAoD;AACpD,yDAAyD;AAEzD,WAAW;AACX,MAAM,YAAY,GAAG,gBAAgB,CAAC;AACtC,MAAM,IAAI,GAAG,aAAa,CAAC;AA8B3B;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,KAAc;IACxC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,IAAI,KAAK,QAAQ;QACvB,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAC1B,CAAC;AACJ,CAAC;AAED,MAAa,SAAS;IAmBpB,YAAY,OAAyB,EAAE;;QAhBvC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CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAChD,CAAC,6EAQqB,SAAqB;IACzC,OAAO,IAAA,aAAK,EACV,IAAA,kBAAU,EAAC,IAAA,sBAAe,EAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACxE,CAAC;AACJ,CAAC,qEAQiB,OAAe;IAC/B,MAAM,UAAU,GAAG,IAAA,wBAAS,EAAC,OAAO,CAAC,CAAC;IACtC,IAAA,cAAM,EAAC,UAAU,EAAE,4BAA4B,CAAC,CAAC;IACjD,OAAO,IAAA,aAAK,EAAC,UAAU,CAAC,CAAC;AAC3B,CAAC;AA1hBM,cAAI,GAAW,IAAI,AAAf,CAAgB","sourcesContent":["import type { TypedTransaction } from '@ethereumjs/tx';\nimport { privateToPublic, publicToAddress, ecsign } from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n type EIP7702Authorization,\n type EthEncryptedData,\n getEncryptionPublicKey,\n type MessageTypes,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n type TypedDataV1,\n type TypedMessage,\n} from '@metamask/eth-sig-util';\nimport {\n type CryptographicFunctions,\n mnemonicToSeed,\n} from '@metamask/key-tree';\nimport { generateMnemonic } from '@metamask/scure-bip39';\nimport { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';\nimport {\n add0x,\n assert,\n assertIsHexString,\n bigIntToBytes,\n bytesToHex,\n type Hex,\n remove0x,\n} from '@metamask/utils';\nimport { HDKey } from 'ethereum-cryptography/hdkey';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\n\n// Options:\nconst hdPathString = `m/44'/60'/0'/0`;\nconst type = 'HD Key Tree';\n\nexport type HDKeyringOptions = {\n cryptographicFunctions?: CryptographicFunctions;\n};\n\n/**\n * The serialized state of an `HDKeyring` instance.\n *\n * @property mnemonic - The mnemonic seed phrase as an array of numbers.\n * @property numberOfAccounts - The number of accounts in the keyring.\n * @property hdPath - The HD path used to derive accounts.\n */\nexport type HDKeyringState = {\n mnemonic: number[] | Uint8Array | Buffer | string;\n numberOfAccounts: number;\n hdPath: string;\n};\n\n/**\n * Options for selecting an account from an `HDKeyring` instance.\n *\n * @property withAppKeyOrigin - The origin of the app requesting the account.\n */\nexport type HDKeyringAccountSelectionOptions = {\n withAppKeyOrigin?: string;\n};\n\ntype SerializedBuffer = ReturnType<Buffer['toJSON']>;\n\n/**\n * Checks if the given value is a valid serialized Buffer compatible with\n * the return type of `Buffer.toJSON`.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a valid serialized buffer, `false` otherwise.\n */\nfunction isSerializedBuffer(value: unknown): value is SerializedBuffer {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'Buffer' &&\n 'data' in value &&\n Array.isArray(value.data)\n );\n}\n\nexport class HdKeyring {\n static type: string = type;\n\n type: string = type;\n\n mnemonic?: Uint8Array | null;\n\n seed?: Uint8Array | null;\n\n root?: HDKey | null;\n\n hdWallet?: HDKey;\n\n hdPath: string = hdPathString;\n\n #wallets: HDKey[] = [];\n\n readonly #cryptographicFunctions?: CryptographicFunctions;\n\n constructor(opts: HDKeyringOptions = {}) {\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 /**\n * Initialize the keyring with a random mnemonic.\n *\n * @returns A promise that resolves when the process is complete.\n */\n async generateRandomMnemonic(): Promise<void> {\n await this.#initFromMnemonic(generateMnemonic(wordlist));\n }\n\n /**\n * Return the serialized state of the keyring.\n *\n * @returns The serialized state of the keyring.\n */\n async serialize(): Promise<HDKeyringState> {\n let mnemonic: number[] = [];\n\n if (this.mnemonic) {\n const mnemonicAsString = this.#uint8ArrayToString(this.mnemonic);\n mnemonic = Array.from(new TextEncoder().encode(mnemonicAsString));\n }\n\n return {\n mnemonic,\n numberOfAccounts: this.#wallets.length,\n hdPath: this.hdPath,\n };\n }\n\n /**\n * Initialize the keyring with the given serialized state.\n *\n * @param opts - The serialized state of the keyring.\n * @returns An empty array.\n */\n async deserialize(opts: Partial<HDKeyringState> = {}): Promise<string[]> {\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.#wallets = [];\n this.mnemonic = null;\n this.seed = 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 /**\n * Add new accounts to the keyring. The accounts will be derived\n * sequentially from the root HD wallet, using increasing indices.\n *\n * @param numberOfAccounts - The number of accounts to add.\n * @returns The addresses of the new accounts.\n */\n async addAccounts(numberOfAccounts = 1): Promise<Hex[]> {\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: HDKey[] = [];\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((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n return Promise.resolve(hexWallets);\n }\n\n /**\n * Get the addresses of all accounts in the keyring.\n *\n * @returns The addresses of all accounts in the keyring.\n */\n getAccounts(): Hex[] {\n return this.#wallets.map((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n }\n\n /**\n * Get the public address of the account for the given app key origin.\n *\n * @param address - The address of the account.\n * @param origin - The origin of the app requesting the account.\n * @returns The public address of the account.\n */\n async getAppKeyAddress(address: Hex, origin: string): Promise<Hex> {\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 assert(wallet.publicKey, 'Expected public key to be set');\n const appKeyAddress = this.#normalizeAddress(\n bytesToHex(publicToAddress(wallet.publicKey)),\n );\n return appKeyAddress;\n }\n\n /**\n * Export the private key for a specific account.\n *\n * @param address - The address of the account.\n * @param opts - The options for exporting the account.\n * @param opts.withAppKeyOrigin - An optional string to export the account\n * for a specific app origin.\n * @returns The private key of the account.\n */\n async exportAccount(\n address: Hex,\n opts?: { withAppKeyOrigin: string },\n ): Promise<string> {\n const wallet = opts\n ? this.#getWalletForAccount(address, opts)\n : this.#getWalletForAccount(address);\n const { privateKey } = wallet;\n assert(\n privateKey instanceof Uint8Array,\n 'Expected private key to be of type Uint8Array',\n );\n return remove0x(bytesToHex(privateKey));\n }\n\n /**\n * Sign a transaction using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param tx - The transaction to sign.\n * @param opts - The options for signing the transaction.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n tx: TypedTransaction,\n opts = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = tx.sign(Buffer.from(privKey));\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? tx;\n }\n\n /**\n * Sign a message using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param data - The data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signMessage(\n address: Hex,\n data: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n assertIsHexString(data);\n const message = remove0x(data);\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), Buffer.from(privKey));\n const rawMsgSig = concatSig(\n Buffer.from(bigIntToBytes(msgSig.v)),\n Buffer.from(msgSig.r),\n Buffer.from(msgSig.s),\n );\n return rawMsgSig;\n }\n\n /**\n * Sign a personal message using the private key of the specified account.\n * This method is compatible with the `personal_sign` RPC method.\n *\n * @param address - The address of the account.\n * @param msgHex - The message to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signPersonalMessage(\n address: Hex,\n msgHex: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const privateKey = Buffer.from(privKey);\n return personalSign({ privateKey, data: msgHex });\n }\n\n /**\n * Decrypt an encrypted message using the private key of the specified account.\n * The message must be encrypted using the public key of the account.\n * This method is compatible with the `eth_decryptMessage` RPC method\n *\n * @param withAccount - The address of the account.\n * @param encryptedData - The encrypted data.\n * @returns The decrypted message.\n */\n async decryptMessage(\n withAccount: Hex,\n encryptedData: EthEncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const { privateKey: privateKeyAsUint8Array } = wallet;\n assert(privateKeyAsUint8Array, 'Expected private key to be set');\n const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');\n return decrypt({ privateKey: privateKeyAsHex, encryptedData });\n }\n\n /**\n * Sign a typed message using the private key of the specified account.\n * This method is compatible with the `eth_signTypedData` RPC method.\n *\n * @param withAccount - The address of the account.\n * @param typedData - The typed data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signTypedData<Types extends MessageTypes>(\n withAccount: Hex,\n typedData: TypedDataV1 | TypedMessage<Types>,\n opts: HDKeyringAccountSelectionOptions & {\n version: SignTypedDataVersion;\n } = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\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({\n privateKey: Buffer.from(privateKey),\n data: typedData,\n version,\n });\n }\n\n /**\n * Sign an EIP-7702 authorization using the private key of the specified account.\n * This method is compatible with the EIP-7702 standard for enabling smart contract code for EOAs.\n *\n * @param withAccount - The address of the account.\n * @param authorization - The EIP-7702 authorization to sign.\n * @param opts - The options for selecting the account.\n * @returns The signature of the authorization.\n */\n async signEip7702Authorization(\n withAccount: Hex,\n authorization: EIP7702Authorization,\n opts?: HDKeyringAccountSelectionOptions,\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(withAccount, opts);\n return signEIP7702Authorization({\n privateKey: Buffer.from(privateKey),\n authorization,\n });\n }\n\n /**\n * Remove an account from the keyring.\n *\n * @param account - The address of the account to remove.\n */\n removeAccount(account: Hex): void {\n const address = this.#normalizeAddress(account);\n if (\n !this.#wallets\n .map(\n ({ publicKey }) => publicKey && this.#addressfromPublicKey(publicKey),\n )\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 }) =>\n publicKey && this.#addressfromPublicKey(publicKey) !== address,\n );\n }\n\n /**\n * Get the public key of the account to be used for encryption.\n *\n * @param withAccount - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The public key of the account.\n */\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(remove0x(bytesToHex(privKey)));\n return publicKey;\n }\n\n /**\n * Convert a Uint8Array mnemonic to a secret recovery phrase,\n * using the english wordlist.\n *\n * @param mnemonic - The Uint8Array mnemonic.\n * @returns The string mnemonic.\n */\n #uint8ArrayToString(mnemonic: Uint8Array): string {\n const recoveredIndices = Array.from(\n new Uint16Array(new Uint8Array(mnemonic).buffer),\n );\n return recoveredIndices.map((i) => wordlist[i]).join(' ');\n }\n\n /**\n * Convert a secret recovery phrase to a Uint8Array mnemonic,\n * using the english wordlist.\n *\n * @param mnemonic - The string mnemonic.\n * @returns The Uint8Array mnemonic.\n */\n #stringToUint8Array(mnemonic: string): Uint8Array {\n const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));\n return new Uint8Array(new Uint16Array(indices).buffer);\n }\n\n /**\n * Convert a mnemonic to a Uint8Array mnemonic.\n *\n * @param mnemonic - The mnemonic seed phrase.\n * @returns The Uint8Array mnemonic.\n */\n #mnemonicToUint8Array(\n mnemonic: Buffer | SerializedBuffer | string | Uint8Array | number[],\n ): Uint8Array {\n let mnemonicData: unknown = mnemonic;\n // When using `Buffer.toJSON()`, the Buffer is serialized into an object\n // with the structure `{ type: 'Buffer', data: [...] }`\n if (isSerializedBuffer(mnemonic)) {\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: string;\n if (Array.isArray(mnemonicData)) {\n mnemonicAsString = Buffer.from(mnemonicData).toString();\n } else if (Buffer.isBuffer(mnemonicData)) {\n mnemonicAsString = mnemonicData.toString();\n } else {\n mnemonicAsString = mnemonicData;\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\n assert(mnemonicData instanceof Uint8Array, 'Expected Uint8Array mnemonic');\n return mnemonicData;\n }\n\n /**\n * Get the private key for the specified account.\n *\n * @param address - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The private key of the account.\n */\n #getPrivateKeyFor(\n address: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): Uint8Array | Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n assert(wallet.privateKey, 'Missing private key');\n return wallet.privateKey;\n }\n\n /**\n * Get the wallet for the specified account.\n *\n * @param account - The address of the account.\n * @returns The wallet for the account as HDKey.\n */\n #getWalletForAccount(account: Hex): HDKey;\n\n /**\n * Get the wallet for the specified account and app origin.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n accounts: Hex,\n opts: { withAppKeyOrigin: string },\n ): { privateKey: Buffer; publicKey: Buffer };\n\n /**\n * Get the wallet for the specified account with optional\n * additional options.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n account: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): HDKey | { privateKey: Buffer; publicKey: Buffer };\n\n #getWalletForAccount(\n account: Hex,\n { withAppKeyOrigin }: HDKeyringAccountSelectionOptions = {},\n ): HDKey | { privateKey: Buffer; publicKey: Buffer } {\n const address = this.#normalizeAddress(account);\n const wallet = this.#wallets.find(({ publicKey }) => {\n return publicKey && this.#addressfromPublicKey(publicKey) === address;\n });\n if (!wallet) {\n throw new Error('HD Keyring - Unable to find matching address.');\n }\n\n if (withAppKeyOrigin) {\n const { privateKey } = wallet;\n assert(privateKey, 'Expected private key to be set');\n const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = Buffer.from(keccak256(appKeyBuffer));\n const appKeyPublicKey = Buffer.from(privateToPublic(appKeyPrivateKey));\n return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n\n /**\n * Sets appropriate properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @param 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(\n mnemonic: string | number[] | Buffer | Uint8Array,\n ): Promise<void> {\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 this.seed = await mnemonicToSeed(\n this.mnemonic,\n '', // No passphrase\n this.#cryptographicFunctions,\n );\n this.hdWallet = HDKey.fromMasterSeed(this.seed);\n this.root = this.hdWallet.derive(this.hdPath);\n }\n\n /**\n * Get the address of the account from the public key.\n *\n * @param publicKey - The public key of the account.\n * @returns The address of the account.\n */\n #addressfromPublicKey(publicKey: Uint8Array): Hex {\n return add0x(\n bytesToHex(publicToAddress(Buffer.from(publicKey), true)).toLowerCase(),\n );\n }\n\n /**\n * Normalize an address to a lower-cased '0x'-prefixed hex string.\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\n #normalizeAddress(address: string): Hex {\n const normalized = normalize(address);\n assert(normalized, 'Expected address to be set');\n return add0x(normalized);\n }\n}\n"]}
|
package/dist/hd-keyring.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hd-keyring.d.cts","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,uBAAuB;
|
|
1
|
+
{"version":3,"file":"hd-keyring.d.cts","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,uBAAuB;AAEvD,OAAO,EAGL,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EAErB,KAAK,YAAY,EAKjB,oBAAoB,EACpB,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,+BAA+B;AAChC,OAAO,EACL,KAAK,sBAAsB,EAE5B,2BAA2B;AAG5B,OAAO,EAML,KAAK,GAAG,EAET,wBAAwB;AACzB,OAAO,EAAE,KAAK,EAAE,oCAAoC;AAOpD,MAAM,MAAM,gBAA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 @@
|
|
|
1
|
-
{"version":3,"file":"hd-keyring.d.mts","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,uBAAuB;
|
|
1
|
+
{"version":3,"file":"hd-keyring.d.mts","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,uBAAuB;AAEvD,OAAO,EAGL,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EAErB,KAAK,YAAY,EAKjB,oBAAoB,EACpB,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,+BAA+B;AAChC,OAAO,EACL,KAAK,sBAAsB,EAE5B,2BAA2B;AAG5B,OAAO,EAML,KAAK,GAAG,EAET,wBAAwB;AACzB,OAAO,EAAE,KAAK,EAAE,oCAAoC;AAOpD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,sBAAsB,CAAC,EAAE,sBAAsB,CAAC;CACjD,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,EAAE,MAAM,EAAE,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;IAClD,gBAAgB,EAAE,MAAM,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,gCAAgC,GAAG;IAC7C,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC;AAsBF,qBAAa,SAAS;;IACpB,MAAM,CAAC,IAAI,EAAE,MAAM,CAAQ;IAE3B,IAAI,EAAE,MAAM,CAAQ;IAEpB,QAAQ,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IAE7B,IAAI,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IAEzB,IAAI,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;IAEpB,QAAQ,CAAC,EAAE,KAAK,CAAC;IAEjB,MAAM,EAAE,MAAM,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AC5C,WAAW,EAAE,GAAG,EAChB,SAAS,EAAE,WAAW,GAAG,YAAY,CAAC,KAAK,CAAC,EAC5C,IAAI,GAAE,gCAAgC,GAAG;QACvC,OAAO,EAAE,oBAAoB,CAAC;KACQ,GACvC,OAAO,CAAC,MAAM,CAAC;IAclB;;;;;;;;OAQG;IACG,wBAAwB,CAC5B,WAAW,EAAE,GAAG,EAChB,aAAa,EAAE,oBAAoB,EACnC,IAAI,CAAC,EAAE,gCAAgC,GACtC,OAAO,CAAC,MAAM,CAAC;IAQlB;;;;OAIG;IACH,aAAa,CAAC,OAAO,EAAE,GAAG,GAAG,IAAI;IAkBjC;;;;;;OAMG;IACG,sBAAsB,CAC1B,WAAW,EAAE,GAAG,EAChB,IAAI,GAAE,gCAAqC,GAC1C,OAAO,CAAC,MAAM,CAAC;CA2MnB"}
|
package/dist/hd-keyring.mjs
CHANGED
|
@@ -10,15 +10,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _HdKeyring_instances, _HdKeyring_wallets, _HdKeyring_cryptographicFunctions, _HdKeyring_uint8ArrayToString, _HdKeyring_stringToUint8Array, _HdKeyring_mnemonicToUint8Array, _HdKeyring_getPrivateKeyFor, _HdKeyring_getWalletForAccount, _HdKeyring_initFromMnemonic, _HdKeyring_addressfromPublicKey, _HdKeyring_normalizeAddress;
|
|
13
|
-
import { privateToPublic, publicToAddress, ecsign
|
|
13
|
+
import { privateToPublic, publicToAddress, ecsign } from "@ethereumjs/util";
|
|
14
14
|
import { concatSig, decrypt, getEncryptionPublicKey, normalize, personalSign, signEIP7702Authorization, signTypedData, SignTypedDataVersion } from "@metamask/eth-sig-util";
|
|
15
15
|
import { mnemonicToSeed } from "@metamask/key-tree";
|
|
16
16
|
import { generateMnemonic } from "@metamask/scure-bip39";
|
|
17
17
|
import { wordlist } from "@metamask/scure-bip39/dist/wordlists/english.js";
|
|
18
|
-
import { add0x, assert, assertIsHexString, remove0x } from "@metamask/utils";
|
|
18
|
+
import { add0x, assert, assertIsHexString, bigIntToBytes, bytesToHex, remove0x } from "@metamask/utils";
|
|
19
19
|
import { HDKey } from "ethereum-cryptography/hdkey";
|
|
20
20
|
import { keccak256 } from "ethereum-cryptography/keccak";
|
|
21
|
-
import { bytesToHex } from "ethereum-cryptography/utils";
|
|
22
21
|
// Options:
|
|
23
22
|
const hdPathString = `m/44'/60'/0'/0`;
|
|
24
23
|
const type = 'HD Key Tree';
|
|
@@ -87,6 +86,7 @@ export class HdKeyring {
|
|
|
87
86
|
}
|
|
88
87
|
__classPrivateFieldSet(this, _HdKeyring_wallets, [], "f");
|
|
89
88
|
this.mnemonic = null;
|
|
89
|
+
this.seed = null;
|
|
90
90
|
this.root = null;
|
|
91
91
|
this.hdPath = opts.hdPath ?? hdPathString;
|
|
92
92
|
if (opts.mnemonic) {
|
|
@@ -147,7 +147,7 @@ export class HdKeyring {
|
|
|
147
147
|
withAppKeyOrigin: origin,
|
|
148
148
|
});
|
|
149
149
|
assert(wallet.publicKey, 'Expected public key to be set');
|
|
150
|
-
const appKeyAddress = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_normalizeAddress).call(this, publicToAddress(wallet.publicKey)
|
|
150
|
+
const appKeyAddress = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_normalizeAddress).call(this, bytesToHex(publicToAddress(wallet.publicKey)));
|
|
151
151
|
return appKeyAddress;
|
|
152
152
|
}
|
|
153
153
|
/**
|
|
@@ -165,7 +165,7 @@ export class HdKeyring {
|
|
|
165
165
|
: __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getWalletForAccount).call(this, address);
|
|
166
166
|
const { privateKey } = wallet;
|
|
167
167
|
assert(privateKey instanceof Uint8Array, 'Expected private key to be of type Uint8Array');
|
|
168
|
-
return bytesToHex(privateKey);
|
|
168
|
+
return remove0x(bytesToHex(privateKey));
|
|
169
169
|
}
|
|
170
170
|
/**
|
|
171
171
|
* Sign a transaction using the private key of the specified account.
|
|
@@ -194,9 +194,7 @@ export class HdKeyring {
|
|
|
194
194
|
const message = remove0x(data);
|
|
195
195
|
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
196
196
|
const msgSig = ecsign(Buffer.from(message, 'hex'), Buffer.from(privKey));
|
|
197
|
-
const rawMsgSig = concatSig(
|
|
198
|
-
// WARN: verify this cast to Buffer
|
|
199
|
-
msgSig.v, msgSig.r, msgSig.s);
|
|
197
|
+
const rawMsgSig = concatSig(Buffer.from(bigIntToBytes(msgSig.v)), Buffer.from(msgSig.r), Buffer.from(msgSig.s));
|
|
200
198
|
return rawMsgSig;
|
|
201
199
|
}
|
|
202
200
|
/**
|
|
@@ -289,7 +287,7 @@ export class HdKeyring {
|
|
|
289
287
|
*/
|
|
290
288
|
async getEncryptionPublicKey(withAccount, opts = {}) {
|
|
291
289
|
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, withAccount, opts);
|
|
292
|
-
const publicKey = getEncryptionPublicKey(bytesToHex(privKey));
|
|
290
|
+
const publicKey = getEncryptionPublicKey(remove0x(bytesToHex(privKey)));
|
|
293
291
|
return publicKey;
|
|
294
292
|
}
|
|
295
293
|
}
|
|
@@ -350,8 +348,8 @@ _HdKeyring_wallets = new WeakMap(), _HdKeyring_cryptographicFunctions = new Weak
|
|
|
350
348
|
assert(privateKey, 'Expected private key to be set');
|
|
351
349
|
const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');
|
|
352
350
|
const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
|
|
353
|
-
const appKeyPrivateKey =
|
|
354
|
-
const appKeyPublicKey = privateToPublic(appKeyPrivateKey);
|
|
351
|
+
const appKeyPrivateKey = Buffer.from(keccak256(appKeyBuffer));
|
|
352
|
+
const appKeyPublicKey = Buffer.from(privateToPublic(appKeyPrivateKey));
|
|
355
353
|
return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
|
|
356
354
|
}
|
|
357
355
|
return wallet;
|
|
@@ -369,12 +367,12 @@ async function _HdKeyring_initFromMnemonic(mnemonic) {
|
|
|
369
367
|
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
370
368
|
}
|
|
371
369
|
this.mnemonic = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_mnemonicToUint8Array).call(this, mnemonic);
|
|
372
|
-
|
|
370
|
+
this.seed = await mnemonicToSeed(this.mnemonic, '', // No passphrase
|
|
373
371
|
__classPrivateFieldGet(this, _HdKeyring_cryptographicFunctions, "f"));
|
|
374
|
-
this.hdWallet = HDKey.fromMasterSeed(seed);
|
|
372
|
+
this.hdWallet = HDKey.fromMasterSeed(this.seed);
|
|
375
373
|
this.root = this.hdWallet.derive(this.hdPath);
|
|
376
374
|
}, _HdKeyring_addressfromPublicKey = function _HdKeyring_addressfromPublicKey(publicKey) {
|
|
377
|
-
return add0x(
|
|
375
|
+
return add0x(bytesToHex(publicToAddress(Buffer.from(publicKey), true)).toLowerCase());
|
|
378
376
|
}, _HdKeyring_normalizeAddress = function _HdKeyring_normalizeAddress(address) {
|
|
379
377
|
const normalized = normalize(address);
|
|
380
378
|
assert(normalized, 'Expected address to be set');
|
package/dist/hd-keyring.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hd-keyring.mjs","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EACL,eAAe,EACf,eAAe,EACf,MAAM,EACN,WAAW,EACX,WAAW,EACZ,yBAAyB;AAC1B,OAAO,EACL,SAAS,EACT,OAAO,EAGP,sBAAsB,EAEtB,SAAS,EACT,YAAY,EACZ,wBAAwB,EACxB,aAAa,EACb,oBAAoB,EAGrB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MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,QAAQ,GAAG,uBAAA,IAAI,6DAAsB,MAA1B,IAAI,EAAuB,QAAQ,CAAC,CAAC;IAErD,MAAM,IAAI,GAAG,MAAM,cAAc,CAC/B,IAAI,CAAC,QAAQ,EACb,EAAE,EAAE,gBAAgB;IACpB,uBAAA,IAAI,yCAAwB,CAC7B,CAAC;IACF,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAChD,CAAC,6EAQqB,SAAqB;IACzC,OAAO,KAAK,CACV,WAAW,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACzE,CAAC;AACJ,CAAC,qEAQiB,OAAe;IAC/B,MAAM,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IACtC,MAAM,CAAC,UAAU,EAAE,4BAA4B,CAAC,CAAC;IACjD,OAAO,KAAK,CAAC,UAAU,CAAC,CAAC;AAC3B,CAAC;AAxhBM,cAAI,GAAW,IAAI,AAAf,CAAgB","sourcesContent":["import type { TypedTransaction } from '@ethereumjs/tx';\nimport {\n privateToPublic,\n publicToAddress,\n ecsign,\n arrToBufArr,\n bufferToHex,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n type EIP7702Authorization,\n type EthEncryptedData,\n getEncryptionPublicKey,\n type MessageTypes,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n type TypedDataV1,\n type TypedMessage,\n} from '@metamask/eth-sig-util';\nimport {\n type CryptographicFunctions,\n mnemonicToSeed,\n} from '@metamask/key-tree';\nimport { generateMnemonic } from '@metamask/scure-bip39';\nimport { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';\nimport {\n add0x,\n assert,\n assertIsHexString,\n type Hex,\n remove0x,\n} 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\nexport type HDKeyringOptions = {\n cryptographicFunctions?: CryptographicFunctions;\n};\n\n/**\n * The serialized state of an `HDKeyring` instance.\n *\n * @property mnemonic - The mnemonic seed phrase as an array of numbers.\n * @property numberOfAccounts - The number of accounts in the keyring.\n * @property hdPath - The HD path used to derive accounts.\n */\nexport type HDKeyringState = {\n mnemonic: number[] | Uint8Array | Buffer | string;\n numberOfAccounts: number;\n hdPath: string;\n};\n\n/**\n * Options for selecting an account from an `HDKeyring` instance.\n *\n * @property withAppKeyOrigin - The origin of the app requesting the account.\n */\nexport type HDKeyringAccountSelectionOptions = {\n withAppKeyOrigin?: string;\n};\n\ntype SerializedBuffer = ReturnType<Buffer['toJSON']>;\n\n/**\n * Checks if the given value is a valid serialized Buffer compatible with\n * the return type of `Buffer.toJSON`.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a valid serialized buffer, `false` otherwise.\n */\nfunction isSerializedBuffer(value: unknown): value is SerializedBuffer {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'Buffer' &&\n 'data' in value &&\n Array.isArray(value.data)\n );\n}\n\nexport class HdKeyring {\n static type: string = type;\n\n type: string = type;\n\n mnemonic?: Uint8Array | null;\n\n root?: HDKey | null;\n\n hdWallet?: HDKey;\n\n hdPath: string = hdPathString;\n\n #wallets: HDKey[] = [];\n\n readonly #cryptographicFunctions?: CryptographicFunctions;\n\n constructor(opts: HDKeyringOptions = {}) {\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 /**\n * Initialize the keyring with a random mnemonic.\n *\n * @returns A promise that resolves when the process is complete.\n */\n async generateRandomMnemonic(): Promise<void> {\n await this.#initFromMnemonic(generateMnemonic(wordlist));\n }\n\n /**\n * Return the serialized state of the keyring.\n *\n * @returns The serialized state of the keyring.\n */\n async serialize(): Promise<HDKeyringState> {\n let mnemonic: number[] = [];\n\n if (this.mnemonic) {\n const mnemonicAsString = this.#uint8ArrayToString(this.mnemonic);\n mnemonic = Array.from(new TextEncoder().encode(mnemonicAsString));\n }\n\n return {\n mnemonic,\n numberOfAccounts: this.#wallets.length,\n hdPath: this.hdPath,\n };\n }\n\n /**\n * Initialize the keyring with the given serialized state.\n *\n * @param opts - The serialized state of the keyring.\n * @returns An empty array.\n */\n async deserialize(opts: Partial<HDKeyringState> = {}): Promise<string[]> {\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.#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 /**\n * Add new accounts to the keyring. The accounts will be derived\n * sequentially from the root HD wallet, using increasing indices.\n *\n * @param numberOfAccounts - The number of accounts to add.\n * @returns The addresses of the new accounts.\n */\n async addAccounts(numberOfAccounts = 1): Promise<Hex[]> {\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: HDKey[] = [];\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((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n return Promise.resolve(hexWallets);\n }\n\n /**\n * Get the addresses of all accounts in the keyring.\n *\n * @returns The addresses of all accounts in the keyring.\n */\n getAccounts(): Hex[] {\n return this.#wallets.map((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n }\n\n /**\n * Get the public address of the account for the given app key origin.\n *\n * @param address - The address of the account.\n * @param origin - The origin of the app requesting the account.\n * @returns The public address of the account.\n */\n async getAppKeyAddress(address: Hex, origin: string): Promise<Hex> {\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 assert(wallet.publicKey, 'Expected public key to be set');\n const appKeyAddress = this.#normalizeAddress(\n publicToAddress(wallet.publicKey).toString('hex'),\n );\n return appKeyAddress;\n }\n\n /**\n * Export the private key for a specific account.\n *\n * @param address - The address of the account.\n * @param opts - The options for exporting the account.\n * @param opts.withAppKeyOrigin - An optional string to export the account\n * for a specific app origin.\n * @returns The private key of the account.\n */\n async exportAccount(\n address: Hex,\n opts?: { withAppKeyOrigin: string },\n ): Promise<string> {\n const wallet = opts\n ? this.#getWalletForAccount(address, opts)\n : this.#getWalletForAccount(address);\n const { privateKey } = wallet;\n assert(\n privateKey instanceof Uint8Array,\n 'Expected private key to be of type Uint8Array',\n );\n return bytesToHex(privateKey);\n }\n\n /**\n * Sign a transaction using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param tx - The transaction to sign.\n * @param opts - The options for signing the transaction.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n tx: TypedTransaction,\n opts = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = tx.sign(Buffer.from(privKey));\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? tx;\n }\n\n /**\n * Sign a message using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param data - The data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signMessage(\n address: Hex,\n data: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n assertIsHexString(data);\n const message = remove0x(data);\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), Buffer.from(privKey));\n const rawMsgSig = concatSig(\n // WARN: verify this cast to Buffer\n msgSig.v as unknown as Buffer,\n msgSig.r,\n msgSig.s,\n );\n return rawMsgSig;\n }\n\n /**\n * Sign a personal message using the private key of the specified account.\n * This method is compatible with the `personal_sign` RPC method.\n *\n * @param address - The address of the account.\n * @param msgHex - The message to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signPersonalMessage(\n address: Hex,\n msgHex: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const privateKey = Buffer.from(privKey);\n return personalSign({ privateKey, data: msgHex });\n }\n\n /**\n * Decrypt an encrypted message using the private key of the specified account.\n * The message must be encrypted using the public key of the account.\n * This method is compatible with the `eth_decryptMessage` RPC method\n *\n * @param withAccount - The address of the account.\n * @param encryptedData - The encrypted data.\n * @returns The decrypted message.\n */\n async decryptMessage(\n withAccount: Hex,\n encryptedData: EthEncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const { privateKey: privateKeyAsUint8Array } = wallet;\n assert(privateKeyAsUint8Array, 'Expected private key to be set');\n const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');\n return decrypt({ privateKey: privateKeyAsHex, encryptedData });\n }\n\n /**\n * Sign a typed message using the private key of the specified account.\n * This method is compatible with the `eth_signTypedData` RPC method.\n *\n * @param withAccount - The address of the account.\n * @param typedData - The typed data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signTypedData<Types extends MessageTypes>(\n withAccount: Hex,\n typedData: TypedDataV1 | TypedMessage<Types>,\n opts: HDKeyringAccountSelectionOptions & {\n version: SignTypedDataVersion;\n } = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\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({\n privateKey: Buffer.from(privateKey),\n data: typedData,\n version,\n });\n }\n\n /**\n * Sign an EIP-7702 authorization using the private key of the specified account.\n * This method is compatible with the EIP-7702 standard for enabling smart contract code for EOAs.\n *\n * @param withAccount - The address of the account.\n * @param authorization - The EIP-7702 authorization to sign.\n * @param opts - The options for selecting the account.\n * @returns The signature of the authorization.\n */\n async signEip7702Authorization(\n withAccount: Hex,\n authorization: EIP7702Authorization,\n opts?: HDKeyringAccountSelectionOptions,\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(withAccount, opts);\n return signEIP7702Authorization({\n privateKey: Buffer.from(privateKey),\n authorization,\n });\n }\n\n /**\n * Remove an account from the keyring.\n *\n * @param account - The address of the account to remove.\n */\n removeAccount(account: Hex): void {\n const address = this.#normalizeAddress(account);\n if (\n !this.#wallets\n .map(\n ({ publicKey }) => publicKey && this.#addressfromPublicKey(publicKey),\n )\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 }) =>\n publicKey && this.#addressfromPublicKey(publicKey) !== address,\n );\n }\n\n /**\n * Get the public key of the account to be used for encryption.\n *\n * @param withAccount - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The public key of the account.\n */\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(bytesToHex(privKey));\n return publicKey;\n }\n\n /**\n * Convert a Uint8Array mnemonic to a secret recovery phrase,\n * using the english wordlist.\n *\n * @param mnemonic - The Uint8Array mnemonic.\n * @returns The string mnemonic.\n */\n #uint8ArrayToString(mnemonic: Uint8Array): string {\n const recoveredIndices = Array.from(\n new Uint16Array(new Uint8Array(mnemonic).buffer),\n );\n return recoveredIndices.map((i) => wordlist[i]).join(' ');\n }\n\n /**\n * Convert a secret recovery phrase to a Uint8Array mnemonic,\n * using the english wordlist.\n *\n * @param mnemonic - The string mnemonic.\n * @returns The Uint8Array mnemonic.\n */\n #stringToUint8Array(mnemonic: string): Uint8Array {\n const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));\n return new Uint8Array(new Uint16Array(indices).buffer);\n }\n\n /**\n * Convert a mnemonic to a Uint8Array mnemonic.\n *\n * @param mnemonic - The mnemonic seed phrase.\n * @returns The Uint8Array mnemonic.\n */\n #mnemonicToUint8Array(\n mnemonic: Buffer | SerializedBuffer | string | Uint8Array | number[],\n ): Uint8Array {\n let mnemonicData: unknown = mnemonic;\n // When using `Buffer.toJSON()`, the Buffer is serialized into an object\n // with the structure `{ type: 'Buffer', data: [...] }`\n if (isSerializedBuffer(mnemonic)) {\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: string;\n if (Array.isArray(mnemonicData)) {\n mnemonicAsString = Buffer.from(mnemonicData).toString();\n } else if (Buffer.isBuffer(mnemonicData)) {\n mnemonicAsString = mnemonicData.toString();\n } else {\n mnemonicAsString = mnemonicData;\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\n assert(mnemonicData instanceof Uint8Array, 'Expected Uint8Array mnemonic');\n return mnemonicData;\n }\n\n /**\n * Get the private key for the specified account.\n *\n * @param address - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The private key of the account.\n */\n #getPrivateKeyFor(\n address: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): Uint8Array | Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n assert(wallet.privateKey, 'Missing private key');\n return wallet.privateKey;\n }\n\n /**\n * Get the wallet for the specified account.\n *\n * @param account - The address of the account.\n * @returns The wallet for the account as HDKey.\n */\n #getWalletForAccount(account: Hex): HDKey;\n\n /**\n * Get the wallet for the specified account and app origin.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n accounts: Hex,\n opts: { withAppKeyOrigin: string },\n ): { privateKey: Buffer; publicKey: Buffer };\n\n /**\n * Get the wallet for the specified account with optional\n * additional options.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n account: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): HDKey | { privateKey: Buffer; publicKey: Buffer };\n\n #getWalletForAccount(\n account: Hex,\n { withAppKeyOrigin }: HDKeyringAccountSelectionOptions = {},\n ): HDKey | { privateKey: Buffer; publicKey: Buffer } {\n const address = this.#normalizeAddress(account);\n const wallet = this.#wallets.find(({ publicKey }) => {\n return publicKey && this.#addressfromPublicKey(publicKey) === address;\n });\n if (!wallet) {\n throw new Error('HD Keyring - Unable to find matching address.');\n }\n\n if (withAppKeyOrigin) {\n const { privateKey } = wallet;\n assert(privateKey, 'Expected private key to be set');\n const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer));\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n\n /**\n * Sets appropriate properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @param 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(\n mnemonic: string | number[] | Buffer | Uint8Array,\n ): Promise<void> {\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 /**\n * Get the address of the account from the public key.\n *\n * @param publicKey - The public key of the account.\n * @returns The address of the account.\n */\n #addressfromPublicKey(publicKey: Uint8Array): Hex {\n return add0x(\n bufferToHex(publicToAddress(Buffer.from(publicKey), true)).toLowerCase(),\n );\n }\n\n /**\n * Normalize an address to a lower-cased '0x'-prefixed hex string.\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\n #normalizeAddress(address: string): Hex {\n const normalized = normalize(address);\n assert(normalized, 'Expected address to be set');\n return add0x(normalized);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"hd-keyring.mjs","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,EAAE,yBAAyB;AAC5E,OAAO,EACL,SAAS,EACT,OAAO,EAGP,sBAAsB,EAEtB,SAAS,EACT,YAAY,EACZ,wBAAwB,EACxB,aAAa,EACb,oBAAoB,EAGrB,+BAA+B;AAChC,OAAO,EAEL,cAAc,EACf,2BAA2B;AAC5B,OAAO,EAAE,gBAAg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,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,KAAK,sCACH,QAAiD;IAEjD,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,QAAQ,GAAG,uBAAA,IAAI,6DAAsB,MAA1B,IAAI,EAAuB,QAAQ,CAAC,CAAC;IAErD,IAAI,CAAC,IAAI,GAAG,MAAM,cAAc,CAC9B,IAAI,CAAC,QAAQ,EACb,EAAE,EAAE,gBAAgB;IACpB,uBAAA,IAAI,yCAAwB,CAC7B,CAAC;IACF,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAChD,CAAC,6EAQqB,SAAqB;IACzC,OAAO,KAAK,CACV,UAAU,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACxE,CAAC;AACJ,CAAC,qEAQiB,OAAe;IAC/B,MAAM,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IACtC,MAAM,CAAC,UAAU,EAAE,4BAA4B,CAAC,CAAC;IACjD,OAAO,KAAK,CAAC,UAAU,CAAC,CAAC;AAC3B,CAAC;AA1hBM,cAAI,GAAW,IAAI,AAAf,CAAgB","sourcesContent":["import type { TypedTransaction } from '@ethereumjs/tx';\nimport { privateToPublic, publicToAddress, ecsign } from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n type EIP7702Authorization,\n type EthEncryptedData,\n getEncryptionPublicKey,\n type MessageTypes,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n type TypedDataV1,\n type TypedMessage,\n} from '@metamask/eth-sig-util';\nimport {\n type CryptographicFunctions,\n mnemonicToSeed,\n} from '@metamask/key-tree';\nimport { generateMnemonic } from '@metamask/scure-bip39';\nimport { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';\nimport {\n add0x,\n assert,\n assertIsHexString,\n bigIntToBytes,\n bytesToHex,\n type Hex,\n remove0x,\n} from '@metamask/utils';\nimport { HDKey } from 'ethereum-cryptography/hdkey';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\n\n// Options:\nconst hdPathString = `m/44'/60'/0'/0`;\nconst type = 'HD Key Tree';\n\nexport type HDKeyringOptions = {\n cryptographicFunctions?: CryptographicFunctions;\n};\n\n/**\n * The serialized state of an `HDKeyring` instance.\n *\n * @property mnemonic - The mnemonic seed phrase as an array of numbers.\n * @property numberOfAccounts - The number of accounts in the keyring.\n * @property hdPath - The HD path used to derive accounts.\n */\nexport type HDKeyringState = {\n mnemonic: number[] | Uint8Array | Buffer | string;\n numberOfAccounts: number;\n hdPath: string;\n};\n\n/**\n * Options for selecting an account from an `HDKeyring` instance.\n *\n * @property withAppKeyOrigin - The origin of the app requesting the account.\n */\nexport type HDKeyringAccountSelectionOptions = {\n withAppKeyOrigin?: string;\n};\n\ntype SerializedBuffer = ReturnType<Buffer['toJSON']>;\n\n/**\n * Checks if the given value is a valid serialized Buffer compatible with\n * the return type of `Buffer.toJSON`.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a valid serialized buffer, `false` otherwise.\n */\nfunction isSerializedBuffer(value: unknown): value is SerializedBuffer {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'Buffer' &&\n 'data' in value &&\n Array.isArray(value.data)\n );\n}\n\nexport class HdKeyring {\n static type: string = type;\n\n type: string = type;\n\n mnemonic?: Uint8Array | null;\n\n seed?: Uint8Array | null;\n\n root?: HDKey | null;\n\n hdWallet?: HDKey;\n\n hdPath: string = hdPathString;\n\n #wallets: HDKey[] = [];\n\n readonly #cryptographicFunctions?: CryptographicFunctions;\n\n constructor(opts: HDKeyringOptions = {}) {\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 /**\n * Initialize the keyring with a random mnemonic.\n *\n * @returns A promise that resolves when the process is complete.\n */\n async generateRandomMnemonic(): Promise<void> {\n await this.#initFromMnemonic(generateMnemonic(wordlist));\n }\n\n /**\n * Return the serialized state of the keyring.\n *\n * @returns The serialized state of the keyring.\n */\n async serialize(): Promise<HDKeyringState> {\n let mnemonic: number[] = [];\n\n if (this.mnemonic) {\n const mnemonicAsString = this.#uint8ArrayToString(this.mnemonic);\n mnemonic = Array.from(new TextEncoder().encode(mnemonicAsString));\n }\n\n return {\n mnemonic,\n numberOfAccounts: this.#wallets.length,\n hdPath: this.hdPath,\n };\n }\n\n /**\n * Initialize the keyring with the given serialized state.\n *\n * @param opts - The serialized state of the keyring.\n * @returns An empty array.\n */\n async deserialize(opts: Partial<HDKeyringState> = {}): Promise<string[]> {\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.#wallets = [];\n this.mnemonic = null;\n this.seed = 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 /**\n * Add new accounts to the keyring. The accounts will be derived\n * sequentially from the root HD wallet, using increasing indices.\n *\n * @param numberOfAccounts - The number of accounts to add.\n * @returns The addresses of the new accounts.\n */\n async addAccounts(numberOfAccounts = 1): Promise<Hex[]> {\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: HDKey[] = [];\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((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n return Promise.resolve(hexWallets);\n }\n\n /**\n * Get the addresses of all accounts in the keyring.\n *\n * @returns The addresses of all accounts in the keyring.\n */\n getAccounts(): Hex[] {\n return this.#wallets.map((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n }\n\n /**\n * Get the public address of the account for the given app key origin.\n *\n * @param address - The address of the account.\n * @param origin - The origin of the app requesting the account.\n * @returns The public address of the account.\n */\n async getAppKeyAddress(address: Hex, origin: string): Promise<Hex> {\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 assert(wallet.publicKey, 'Expected public key to be set');\n const appKeyAddress = this.#normalizeAddress(\n bytesToHex(publicToAddress(wallet.publicKey)),\n );\n return appKeyAddress;\n }\n\n /**\n * Export the private key for a specific account.\n *\n * @param address - The address of the account.\n * @param opts - The options for exporting the account.\n * @param opts.withAppKeyOrigin - An optional string to export the account\n * for a specific app origin.\n * @returns The private key of the account.\n */\n async exportAccount(\n address: Hex,\n opts?: { withAppKeyOrigin: string },\n ): Promise<string> {\n const wallet = opts\n ? this.#getWalletForAccount(address, opts)\n : this.#getWalletForAccount(address);\n const { privateKey } = wallet;\n assert(\n privateKey instanceof Uint8Array,\n 'Expected private key to be of type Uint8Array',\n );\n return remove0x(bytesToHex(privateKey));\n }\n\n /**\n * Sign a transaction using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param tx - The transaction to sign.\n * @param opts - The options for signing the transaction.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n tx: TypedTransaction,\n opts = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = tx.sign(Buffer.from(privKey));\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? tx;\n }\n\n /**\n * Sign a message using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param data - The data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signMessage(\n address: Hex,\n data: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n assertIsHexString(data);\n const message = remove0x(data);\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), Buffer.from(privKey));\n const rawMsgSig = concatSig(\n Buffer.from(bigIntToBytes(msgSig.v)),\n Buffer.from(msgSig.r),\n Buffer.from(msgSig.s),\n );\n return rawMsgSig;\n }\n\n /**\n * Sign a personal message using the private key of the specified account.\n * This method is compatible with the `personal_sign` RPC method.\n *\n * @param address - The address of the account.\n * @param msgHex - The message to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signPersonalMessage(\n address: Hex,\n msgHex: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const privateKey = Buffer.from(privKey);\n return personalSign({ privateKey, data: msgHex });\n }\n\n /**\n * Decrypt an encrypted message using the private key of the specified account.\n * The message must be encrypted using the public key of the account.\n * This method is compatible with the `eth_decryptMessage` RPC method\n *\n * @param withAccount - The address of the account.\n * @param encryptedData - The encrypted data.\n * @returns The decrypted message.\n */\n async decryptMessage(\n withAccount: Hex,\n encryptedData: EthEncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const { privateKey: privateKeyAsUint8Array } = wallet;\n assert(privateKeyAsUint8Array, 'Expected private key to be set');\n const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');\n return decrypt({ privateKey: privateKeyAsHex, encryptedData });\n }\n\n /**\n * Sign a typed message using the private key of the specified account.\n * This method is compatible with the `eth_signTypedData` RPC method.\n *\n * @param withAccount - The address of the account.\n * @param typedData - The typed data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signTypedData<Types extends MessageTypes>(\n withAccount: Hex,\n typedData: TypedDataV1 | TypedMessage<Types>,\n opts: HDKeyringAccountSelectionOptions & {\n version: SignTypedDataVersion;\n } = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\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({\n privateKey: Buffer.from(privateKey),\n data: typedData,\n version,\n });\n }\n\n /**\n * Sign an EIP-7702 authorization using the private key of the specified account.\n * This method is compatible with the EIP-7702 standard for enabling smart contract code for EOAs.\n *\n * @param withAccount - The address of the account.\n * @param authorization - The EIP-7702 authorization to sign.\n * @param opts - The options for selecting the account.\n * @returns The signature of the authorization.\n */\n async signEip7702Authorization(\n withAccount: Hex,\n authorization: EIP7702Authorization,\n opts?: HDKeyringAccountSelectionOptions,\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(withAccount, opts);\n return signEIP7702Authorization({\n privateKey: Buffer.from(privateKey),\n authorization,\n });\n }\n\n /**\n * Remove an account from the keyring.\n *\n * @param account - The address of the account to remove.\n */\n removeAccount(account: Hex): void {\n const address = this.#normalizeAddress(account);\n if (\n !this.#wallets\n .map(\n ({ publicKey }) => publicKey && this.#addressfromPublicKey(publicKey),\n )\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 }) =>\n publicKey && this.#addressfromPublicKey(publicKey) !== address,\n );\n }\n\n /**\n * Get the public key of the account to be used for encryption.\n *\n * @param withAccount - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The public key of the account.\n */\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(remove0x(bytesToHex(privKey)));\n return publicKey;\n }\n\n /**\n * Convert a Uint8Array mnemonic to a secret recovery phrase,\n * using the english wordlist.\n *\n * @param mnemonic - The Uint8Array mnemonic.\n * @returns The string mnemonic.\n */\n #uint8ArrayToString(mnemonic: Uint8Array): string {\n const recoveredIndices = Array.from(\n new Uint16Array(new Uint8Array(mnemonic).buffer),\n );\n return recoveredIndices.map((i) => wordlist[i]).join(' ');\n }\n\n /**\n * Convert a secret recovery phrase to a Uint8Array mnemonic,\n * using the english wordlist.\n *\n * @param mnemonic - The string mnemonic.\n * @returns The Uint8Array mnemonic.\n */\n #stringToUint8Array(mnemonic: string): Uint8Array {\n const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));\n return new Uint8Array(new Uint16Array(indices).buffer);\n }\n\n /**\n * Convert a mnemonic to a Uint8Array mnemonic.\n *\n * @param mnemonic - The mnemonic seed phrase.\n * @returns The Uint8Array mnemonic.\n */\n #mnemonicToUint8Array(\n mnemonic: Buffer | SerializedBuffer | string | Uint8Array | number[],\n ): Uint8Array {\n let mnemonicData: unknown = mnemonic;\n // When using `Buffer.toJSON()`, the Buffer is serialized into an object\n // with the structure `{ type: 'Buffer', data: [...] }`\n if (isSerializedBuffer(mnemonic)) {\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: string;\n if (Array.isArray(mnemonicData)) {\n mnemonicAsString = Buffer.from(mnemonicData).toString();\n } else if (Buffer.isBuffer(mnemonicData)) {\n mnemonicAsString = mnemonicData.toString();\n } else {\n mnemonicAsString = mnemonicData;\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\n assert(mnemonicData instanceof Uint8Array, 'Expected Uint8Array mnemonic');\n return mnemonicData;\n }\n\n /**\n * Get the private key for the specified account.\n *\n * @param address - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The private key of the account.\n */\n #getPrivateKeyFor(\n address: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): Uint8Array | Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n assert(wallet.privateKey, 'Missing private key');\n return wallet.privateKey;\n }\n\n /**\n * Get the wallet for the specified account.\n *\n * @param account - The address of the account.\n * @returns The wallet for the account as HDKey.\n */\n #getWalletForAccount(account: Hex): HDKey;\n\n /**\n * Get the wallet for the specified account and app origin.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n accounts: Hex,\n opts: { withAppKeyOrigin: string },\n ): { privateKey: Buffer; publicKey: Buffer };\n\n /**\n * Get the wallet for the specified account with optional\n * additional options.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n account: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): HDKey | { privateKey: Buffer; publicKey: Buffer };\n\n #getWalletForAccount(\n account: Hex,\n { withAppKeyOrigin }: HDKeyringAccountSelectionOptions = {},\n ): HDKey | { privateKey: Buffer; publicKey: Buffer } {\n const address = this.#normalizeAddress(account);\n const wallet = this.#wallets.find(({ publicKey }) => {\n return publicKey && this.#addressfromPublicKey(publicKey) === address;\n });\n if (!wallet) {\n throw new Error('HD Keyring - Unable to find matching address.');\n }\n\n if (withAppKeyOrigin) {\n const { privateKey } = wallet;\n assert(privateKey, 'Expected private key to be set');\n const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = Buffer.from(keccak256(appKeyBuffer));\n const appKeyPublicKey = Buffer.from(privateToPublic(appKeyPrivateKey));\n return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n\n /**\n * Sets appropriate properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @param 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(\n mnemonic: string | number[] | Buffer | Uint8Array,\n ): Promise<void> {\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 this.seed = await mnemonicToSeed(\n this.mnemonic,\n '', // No passphrase\n this.#cryptographicFunctions,\n );\n this.hdWallet = HDKey.fromMasterSeed(this.seed);\n this.root = this.hdWallet.derive(this.hdPath);\n }\n\n /**\n * Get the address of the account from the public key.\n *\n * @param publicKey - The public key of the account.\n * @returns The address of the account.\n */\n #addressfromPublicKey(publicKey: Uint8Array): Hex {\n return add0x(\n bytesToHex(publicToAddress(Buffer.from(publicKey), true)).toLowerCase(),\n );\n }\n\n /**\n * Normalize an address to a lower-cased '0x'-prefixed hex string.\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\n #normalizeAddress(address: string): Hex {\n const normalized = normalize(address);\n assert(normalized, 'Expected address to be set');\n return add0x(normalized);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/eth-hd-keyring",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.1.0",
|
|
4
4
|
"description": "A simple standard interface for a seed phrase generated set of Ethereum accounts.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ethereum",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"test:clean": "jest --clearCache"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@ethereumjs/util": "^
|
|
46
|
+
"@ethereumjs/util": "^9.1.0",
|
|
47
47
|
"@metamask/eth-sig-util": "^8.2.0",
|
|
48
48
|
"@metamask/key-tree": "^10.0.2",
|
|
49
49
|
"@metamask/scure-bip39": "^2.1.1",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"ethereum-cryptography": "^2.1.2"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@ethereumjs/tx": "^4.
|
|
54
|
+
"@ethereumjs/tx": "^5.4.0",
|
|
55
55
|
"@lavamoat/allow-scripts": "^3.2.1",
|
|
56
56
|
"@lavamoat/preinstall-always-fail": "^2.1.0",
|
|
57
57
|
"@metamask/auto-changelog": "^3.4.4",
|