@metamask/eth-simple-keyring 9.0.0 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [10.0.0]
11
+
12
+ ### Changed
13
+
14
+ - **BREAKING:** The method `signTransaction` can now returns various type of transactions ([#209](https://github.com/MetaMask/accounts/pull/209))
15
+ - Initially was supporting: `Transaction | AccessListEIP2930Transaction | FeeMarketEIP1559Transaction` (types from `@ethereumjs/tx`).
16
+ - Now also supports `BlobEIP4844Transaction | EOACodeEIP7702Transaction` (types from `@ethereumjs/tx`).
17
+ - **BREAKING:** Bump `@ethereumjs/tx` from `^4.2.0` to `^5.4.0` ([#209](https://github.com/MetaMask/accounts/pull/209))
18
+ - **BREAKING:** Bump `@ethereumjs/util` from `^8.1.0` to `^9.1.0` ([#209](https://github.com/MetaMask/accounts/pull/209))
19
+
10
20
  ## [9.0.0]
11
21
 
12
22
  ### Changed
@@ -132,7 +142,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
132
142
  - **BREAKING:** Remove redundant `newGethSignMessage` method ([#72](https://github.com/MetaMask/eth-simple-keyring/pull/72))
133
143
  - Consumers can use `signPersonalMessage` method as a replacement for `newGethSignMessage`.
134
144
 
135
- [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@9.0.0...HEAD
145
+ [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@10.0.0...HEAD
146
+ [10.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@9.0.0...@metamask/eth-simple-keyring@10.0.0
136
147
  [9.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@8.1.1...@metamask/eth-simple-keyring@9.0.0
137
148
  [8.1.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@8.1.0...@metamask/eth-simple-keyring@8.1.1
138
149
  [8.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@8.0.1...@metamask/eth-simple-keyring@8.1.0
@@ -41,7 +41,7 @@ class SimpleKeyring {
41
41
  __classPrivateFieldSet(this, _SimpleKeyring_wallets, privateKeys.map((hexPrivateKey) => {
42
42
  const strippedHexPrivateKey = (0, util_1.stripHexPrefix)(hexPrivateKey);
43
43
  const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');
44
- const publicKey = (0, util_1.privateToPublic)(privateKey);
44
+ const publicKey = Buffer.from((0, util_1.privateToPublic)(privateKey));
45
45
  return { privateKey, publicKey };
46
46
  }), "f");
47
47
  }
@@ -49,15 +49,15 @@ class SimpleKeyring {
49
49
  const newWallets = [];
50
50
  for (let i = 0; i < numAccounts; i++) {
51
51
  const privateKey = generateKey();
52
- const publicKey = (0, util_1.privateToPublic)(privateKey);
52
+ const publicKey = Buffer.from((0, util_1.privateToPublic)(privateKey));
53
53
  newWallets.push({ privateKey, publicKey });
54
54
  }
55
55
  __classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").concat(newWallets), "f");
56
- const hexWallets = newWallets.map(({ publicKey }) => (0, utils_1.add0x)((0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey))));
56
+ const hexWallets = newWallets.map(({ publicKey }) => (0, utils_1.add0x)((0, utils_1.bytesToHex)((0, util_1.publicToAddress)(publicKey))));
57
57
  return hexWallets;
58
58
  }
59
59
  async getAccounts() {
60
- return __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").map(({ publicKey }) => (0, utils_1.add0x)((0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey))));
60
+ return __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").map(({ publicKey }) => (0, utils_1.add0x)((0, utils_1.bytesToHex)((0, util_1.publicToAddress)(publicKey))));
61
61
  }
62
62
  async signTransaction(address, transaction, opts = {}) {
63
63
  const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
@@ -78,7 +78,7 @@ class SimpleKeyring {
78
78
  }
79
79
  const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
80
80
  const msgSig = (0, util_1.ecsign)(Buffer.from(message, 'hex'), privKey);
81
- const rawMsgSig = (0, eth_sig_util_1.concatSig)((0, util_1.toBuffer)(msgSig.v), msgSig.r, msgSig.s);
81
+ const rawMsgSig = (0, eth_sig_util_1.concatSig)(Buffer.from((0, utils_1.bigIntToBytes)(msgSig.v)), Buffer.from(msgSig.r), Buffer.from(msgSig.s));
82
82
  return rawMsgSig;
83
83
  }
84
84
  // For personal_sign, we need to prefix the message:
@@ -116,7 +116,7 @@ class SimpleKeyring {
116
116
  const wallet = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getWalletForAccount).call(this, address, {
117
117
  withAppKeyOrigin: origin,
118
118
  });
119
- const appKeyAddress = (0, utils_1.add0x)((0, util_1.bufferToHex)((0, util_1.publicToAddress)(wallet.publicKey)));
119
+ const appKeyAddress = (0, utils_1.add0x)((0, utils_1.bytesToHex)((0, util_1.publicToAddress)(wallet.publicKey)));
120
120
  return appKeyAddress;
121
121
  }
122
122
  // exportAccount should return a hex-encoded private key:
@@ -126,11 +126,11 @@ class SimpleKeyring {
126
126
  }
127
127
  removeAccount(address) {
128
128
  if (!__classPrivateFieldGet(this, _SimpleKeyring_wallets, "f")
129
- .map(({ publicKey }) => (0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey)).toLowerCase())
129
+ .map(({ publicKey }) => (0, utils_1.bytesToHex)((0, util_1.publicToAddress)(publicKey)).toLowerCase())
130
130
  .includes(address.toLowerCase())) {
131
131
  throw new Error(`Address ${address} not found in this keyring`);
132
132
  }
133
- __classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").filter(({ publicKey }) => (0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey)).toLowerCase() !==
133
+ __classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").filter(({ publicKey }) => (0, utils_1.bytesToHex)((0, util_1.publicToAddress)(publicKey)).toLowerCase() !==
134
134
  address.toLowerCase()), "f");
135
135
  }
136
136
  }
@@ -142,7 +142,7 @@ _SimpleKeyring_wallets = new WeakMap(), _SimpleKeyring_instances = new WeakSet()
142
142
  return wallet.privateKey;
143
143
  }, _SimpleKeyring_getWalletForAccount = function _SimpleKeyring_getWalletForAccount(account, opts = {}) {
144
144
  const address = (0, eth_sig_util_1.normalize)(account);
145
- let wallet = __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").find(({ publicKey }) => (0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey)) === address);
145
+ let wallet = __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").find(({ publicKey }) => (0, utils_1.bytesToHex)((0, util_1.publicToAddress)(publicKey)) === address);
146
146
  if (!wallet) {
147
147
  throw new Error('Simple Keyring - Unable to find matching address.');
148
148
  }
@@ -150,9 +150,12 @@ _SimpleKeyring_wallets = new WeakMap(), _SimpleKeyring_instances = new WeakSet()
150
150
  const { privateKey } = wallet;
151
151
  const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');
152
152
  const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
153
- const appKeyPrivateKey = (0, util_1.arrToBufArr)((0, keccak_1.keccak256)(appKeyBuffer));
153
+ const appKeyPrivateKey = (0, keccak_1.keccak256)(appKeyBuffer);
154
154
  const appKeyPublicKey = (0, util_1.privateToPublic)(appKeyPrivateKey);
155
- wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
155
+ wallet = {
156
+ privateKey: Buffer.from(appKeyPrivateKey),
157
+ publicKey: Buffer.from(appKeyPublicKey),
158
+ };
156
159
  }
157
160
  return wallet;
158
161
  };
@@ -1 +1 @@
1
- {"version":3,"file":"simple-keyring.cjs","sourceRoot":"","sources":["../src/simple-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,2CAS0B;AAC1B,yDAUgC;AAEhC,2CAAmE;AACnE,yDAAyD;AACzD,8DAAsC;AAYtC,MAAM,IAAI,GAAG,iBAAiB,CAAC;AAE/B,iDAAiD;AACjD,MAAqB,aAAa;IAOhC,YAAY,cAAwB,EAAE;;QANtC,yCAAsD;QAE7C,SAAI,GAAW,IAAI,CAAC;QAK3B,uBAAA,IAAI,0BAAY,EAAE,MAAA,CAAC;QAEnB;iHACyG;QACzG,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,CAAC,KAAY,EAAE,EAAE;YACnD,MAAM,IAAI,KAAK,CAAC,uCAAuC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QAC1E,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,SAAS;QACb,OAAO,uBAAA,IAAI,8BAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;IAChE,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,WAAqB;QACrC,uBAAA,IAAI,0BAAY,WAAW,CAAC,GAAG,CAAC,CAAC,aAAa,EAAE,EAAE;YAChD,MAAM,qBAAqB,GAAG,IAAA,qBAAc,EAAC,aAAa,CAAC,CAAC;YAC5D,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;YAC7D,MAAM,SAAS,GAAG,IAAA,sBAAe,EAAC,UAAU,CAAC,CAAC;YAC9C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC;QACnC,CAAC,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AG,IAAA,sBAAe,EAAC,gBAAgB,CAAC,CAAC;QAC1D,MAAM,GAAG,EAAE,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC;IACxE,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AA3MM,kBAAI,GAAW,IAAI,AAAf,CAAgB;kBALR,aAAa;AAmNlC;;;;GAIG;AACH,SAAS,WAAW;IAClB,MAAM,UAAU,GAAG,IAAA,qBAAW,EAAC,EAAE,CAAC,CAAC;IAEnC,IAAI,CAAC,IAAA,qBAAc,EAAC,UAAU,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,yEAAyE,CAC1E,CAAC;IACJ,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;GAKG;AACH,2CAA2C;AAC3C,SAAS,sBAAsB,CAC7B,OAAsC;IAEtC,OAAO,OAAO,IAAI,mCAAoB,CAAC;AACzC,CAAC","sourcesContent":["import { TypedTransaction } from '@ethereumjs/tx';\nimport {\n arrToBufArr,\n bufferToHex,\n ecsign,\n isValidPrivate,\n privateToPublic,\n publicToAddress,\n stripHexPrefix,\n toBuffer,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n EIP7702Authorization,\n getEncryptionPublicKey,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n} from '@metamask/eth-sig-util';\nimport { Keyring } from '@metamask/keyring-utils';\nimport { add0x, Eip1024EncryptedData, Hex } from '@metamask/utils';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\nimport randombytes from 'randombytes';\n\ntype KeyringOpt = {\n withAppKeyOrigin?: string;\n version?: SignTypedDataVersion | string;\n};\n\ntype Wallet = {\n privateKey: Buffer;\n publicKey: Buffer;\n};\n\nconst TYPE = 'Simple Key Pair';\n\n// FIXME: This should not be exported as default.\nexport default class SimpleKeyring implements Keyring {\n #wallets: { privateKey: Buffer; publicKey: Buffer }[];\n\n readonly type: string = TYPE;\n\n static type: string = TYPE;\n\n constructor(privateKeys: string[] = []) {\n this.#wallets = [];\n\n /* istanbul ignore next: It's not possible to write a unit test for this, because a constructor isn't allowed\n * to be async. Jest can't await the constructor, and when the error gets thrown, Jest can't catch it. */\n this.deserialize(privateKeys).catch((error: Error) => {\n throw new Error(`Problem deserializing SimpleKeyring ${error.message}`);\n });\n }\n\n async serialize(): Promise<string[]> {\n return this.#wallets.map((a) => a.privateKey.toString('hex'));\n }\n\n async deserialize(privateKeys: string[]): Promise<void> {\n this.#wallets = privateKeys.map((hexPrivateKey) => {\n const strippedHexPrivateKey = stripHexPrefix(hexPrivateKey);\n const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');\n const publicKey = privateToPublic(privateKey);\n return { privateKey, publicKey };\n });\n }\n\n async addAccounts(numAccounts = 1): Promise<Hex[]> {\n const newWallets = [];\n for (let i = 0; i < numAccounts; i++) {\n const privateKey = generateKey();\n const publicKey = privateToPublic(privateKey);\n newWallets.push({ privateKey, publicKey });\n }\n this.#wallets = this.#wallets.concat(newWallets);\n const hexWallets = newWallets.map(({ publicKey }) =>\n add0x(bufferToHex(publicToAddress(publicKey))),\n );\n return hexWallets;\n }\n\n async getAccounts(): Promise<Hex[]> {\n return this.#wallets.map(({ publicKey }) =>\n add0x(bufferToHex(publicToAddress(publicKey))),\n );\n }\n\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n opts: KeyringOpt = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = transaction.sign(privKey);\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? transaction;\n }\n\n async signEip7702Authorization(\n address: Hex,\n authorization: EIP7702Authorization,\n opts: KeyringOpt = {},\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signEIP7702Authorization({ privateKey, authorization });\n }\n\n // For eth_sign, we need to sign arbitrary data:\n async signMessage(\n address: Hex,\n data: string,\n opts = { withAppKeyOrigin: '', validateMessage: true },\n ): Promise<string> {\n const message = stripHexPrefix(data);\n if (\n opts.validateMessage &&\n (message.length === 0 || !message.match(/^[a-fA-F0-9]*$/u))\n ) {\n throw new Error('Cannot sign invalid message');\n }\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), privKey);\n const rawMsgSig = concatSig(toBuffer(msgSig.v), msgSig.r, msgSig.s);\n return rawMsgSig;\n }\n\n // For personal_sign, we need to prefix the message:\n async signPersonalMessage(\n address: Hex,\n msgHex: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n return personalSign({ privateKey: privKey, data: msgHex });\n }\n\n // For eth_decryptMessage:\n async decryptMessage(\n withAccount: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const privateKey = wallet.privateKey.toString('hex');\n return decrypt({ privateKey, encryptedData });\n }\n\n // personal_signTypedData, signs data along with the schema\n async signTypedData(\n address: Hex,\n typedData: any,\n opts: KeyringOpt = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\n // Treat invalid versions as \"V1\"\n let version = SignTypedDataVersion.V1;\n\n if (opts.version && isSignTypedDataVersion(opts.version)) {\n version = SignTypedDataVersion[opts.version];\n }\n\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signTypedData({ privateKey, data: typedData, version });\n }\n\n // get public key for nacl\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts?: KeyringOpt,\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(privKey.toString('hex'));\n return publicKey;\n }\n\n #getPrivateKeyFor(\n address: Hex,\n opts: KeyringOpt = { withAppKeyOrigin: '' },\n ): Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey;\n }\n\n // returns an address specific to an app\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 const appKeyAddress = add0x(bufferToHex(publicToAddress(wallet.publicKey)));\n return appKeyAddress;\n }\n\n // exportAccount should return a hex-encoded private key:\n async exportAccount(\n address: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey.toString('hex');\n }\n\n removeAccount(address: string): void {\n if (\n !this.#wallets\n .map(({ publicKey }) =>\n bufferToHex(publicToAddress(publicKey)).toLowerCase(),\n )\n .includes(address.toLowerCase())\n ) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.#wallets = this.#wallets.filter(\n ({ publicKey }) =>\n bufferToHex(publicToAddress(publicKey)).toLowerCase() !==\n address.toLowerCase(),\n );\n }\n\n #getWalletForAccount(\n account: string | number,\n opts: KeyringOpt = {},\n ): Wallet {\n const address = normalize(account);\n let wallet = this.#wallets.find(\n ({ publicKey }) => bufferToHex(publicToAddress(publicKey)) === address,\n );\n if (!wallet) {\n throw new Error('Simple Keyring - Unable to find matching address.');\n }\n\n if (opts.withAppKeyOrigin) {\n const { privateKey } = wallet;\n const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer));\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n}\n\n/**\n * Generate and validate a new random key of 32 bytes.\n *\n * @returns Buffer The generated key.\n */\nfunction generateKey(): Buffer {\n const privateKey = randombytes(32);\n\n if (!isValidPrivate(privateKey)) {\n throw new Error(\n 'Private key does not satisfy the curve requirements (ie. it is invalid)',\n );\n }\n return privateKey;\n}\n\n/**\n * Type predicate type guard to check if a string is in the enum SignTypedDataVersion.\n *\n * @param version - The string to check.\n * @returns Whether it's in the enum.\n */\n// TODO: Put this in @metamask/eth-sig-util\nfunction isSignTypedDataVersion(\n version: SignTypedDataVersion | string,\n): version is SignTypedDataVersion {\n return version in SignTypedDataVersion;\n}\n"]}
1
+ {"version":3,"file":"simple-keyring.cjs","sourceRoot":"","sources":["../src/simple-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,2CAM0B;AAC1B,yDAUgC;AAEhC,2CAMyB;AACzB,yDAAyD;AACzD,8DAAsC;AAYtC,MAAM,IAAI,GAAG,iBAAiB,CAAC;AAE/B,iDAAiD;AACjD,MAAqB,aAAa;IAOhC,YAAY,cAAwB,EAAE;;QANtC,yCAAsD;QAE7C,SAAI,GAAW,IAAI,CAAC;QAK3B,uBAAA,IAAI,0BAAY,EAAE,MAAA,CAAC;QAEnB;iHACyG;QACzG,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,CAAC,KAAY,EAAE,EAAE;YACnD,MAAM,IAAI,KAAK,CAAC,uCAAuC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QAC1E,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,SAAS;QACb,OAAO,uBAAA,IAAI,8BAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;IAChE,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,WAAqB;QACrC,uBAAA,IAAI,0BAAY,WAAW,CAAC,GAAG,CAAC,CAAC,aAAa,EAAE,EAAE;YAChD,MAAM,qBAAqB,GAAG,IAAA,qBAAc,EAAC,aAAa,CAAC,CAAC;YAC5D,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;YAC7D,MAA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C,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;QACtE,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC,CAAC;QACrE,MAAM,gBAAgB,GAAG,IAAA,kBAAS,EAAC,YAAY,CAAC,CAAC;QACjD,MAAM,eAAe,GAAG,IAAA,sBAAe,EAAC,gBAAgB,CAAC,CAAC;QAC1D,MAAM,GAAG;YACP,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC;YACzC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC;SACxC,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAlNM,kBAAI,GAAW,IAAI,AAAf,CAAgB;kBALR,aAAa;AA0NlC;;;;GAIG;AACH,SAAS,WAAW;IAClB,MAAM,UAAU,GAAG,IAAA,qBAAW,EAAC,EAAE,CAAC,CAAC;IAEnC,IAAI,CAAC,IAAA,qBAAc,EAAC,UAAU,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,yEAAyE,CAC1E,CAAC;IACJ,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;GAKG;AACH,2CAA2C;AAC3C,SAAS,sBAAsB,CAC7B,OAAsC;IAEtC,OAAO,OAAO,IAAI,mCAAoB,CAAC;AACzC,CAAC","sourcesContent":["import { TypedTransaction } from '@ethereumjs/tx';\nimport {\n ecsign,\n isValidPrivate,\n privateToPublic,\n publicToAddress,\n stripHexPrefix,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n EIP7702Authorization,\n getEncryptionPublicKey,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n} from '@metamask/eth-sig-util';\nimport { Keyring } from '@metamask/keyring-utils';\nimport {\n add0x,\n bigIntToBytes,\n bytesToHex,\n Eip1024EncryptedData,\n Hex,\n} from '@metamask/utils';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\nimport randombytes from 'randombytes';\n\ntype KeyringOpt = {\n withAppKeyOrigin?: string;\n version?: SignTypedDataVersion | string;\n};\n\ntype Wallet = {\n privateKey: Buffer;\n publicKey: Buffer;\n};\n\nconst TYPE = 'Simple Key Pair';\n\n// FIXME: This should not be exported as default.\nexport default class SimpleKeyring implements Keyring {\n #wallets: { privateKey: Buffer; publicKey: Buffer }[];\n\n readonly type: string = TYPE;\n\n static type: string = TYPE;\n\n constructor(privateKeys: string[] = []) {\n this.#wallets = [];\n\n /* istanbul ignore next: It's not possible to write a unit test for this, because a constructor isn't allowed\n * to be async. Jest can't await the constructor, and when the error gets thrown, Jest can't catch it. */\n this.deserialize(privateKeys).catch((error: Error) => {\n throw new Error(`Problem deserializing SimpleKeyring ${error.message}`);\n });\n }\n\n async serialize(): Promise<string[]> {\n return this.#wallets.map((a) => a.privateKey.toString('hex'));\n }\n\n async deserialize(privateKeys: string[]): Promise<void> {\n this.#wallets = privateKeys.map((hexPrivateKey) => {\n const strippedHexPrivateKey = stripHexPrefix(hexPrivateKey);\n const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');\n const publicKey = Buffer.from(privateToPublic(privateKey));\n return { privateKey, publicKey };\n });\n }\n\n async addAccounts(numAccounts = 1): Promise<Hex[]> {\n const newWallets = [];\n for (let i = 0; i < numAccounts; i++) {\n const privateKey = generateKey();\n const publicKey = Buffer.from(privateToPublic(privateKey));\n newWallets.push({ privateKey, publicKey });\n }\n this.#wallets = this.#wallets.concat(newWallets);\n const hexWallets = newWallets.map(({ publicKey }) =>\n add0x(bytesToHex(publicToAddress(publicKey))),\n );\n return hexWallets;\n }\n\n async getAccounts(): Promise<Hex[]> {\n return this.#wallets.map(({ publicKey }) =>\n add0x(bytesToHex(publicToAddress(publicKey))),\n );\n }\n\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n opts: KeyringOpt = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = transaction.sign(privKey);\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? transaction;\n }\n\n async signEip7702Authorization(\n address: Hex,\n authorization: EIP7702Authorization,\n opts: KeyringOpt = {},\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signEIP7702Authorization({ privateKey, authorization });\n }\n\n // For eth_sign, we need to sign arbitrary data:\n async signMessage(\n address: Hex,\n data: string,\n opts = { withAppKeyOrigin: '', validateMessage: true },\n ): Promise<string> {\n const message = stripHexPrefix(data);\n if (\n opts.validateMessage &&\n (message.length === 0 || !message.match(/^[a-fA-F0-9]*$/u))\n ) {\n throw new Error('Cannot sign invalid message');\n }\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), 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 // For personal_sign, we need to prefix the message:\n async signPersonalMessage(\n address: Hex,\n msgHex: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n return personalSign({ privateKey: privKey, data: msgHex });\n }\n\n // For eth_decryptMessage:\n async decryptMessage(\n withAccount: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const privateKey = wallet.privateKey.toString('hex');\n return decrypt({ privateKey, encryptedData });\n }\n\n // personal_signTypedData, signs data along with the schema\n async signTypedData(\n address: Hex,\n typedData: any,\n opts: KeyringOpt = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\n // Treat invalid versions as \"V1\"\n let version = SignTypedDataVersion.V1;\n\n if (opts.version && isSignTypedDataVersion(opts.version)) {\n version = SignTypedDataVersion[opts.version];\n }\n\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signTypedData({ privateKey, data: typedData, version });\n }\n\n // get public key for nacl\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts?: KeyringOpt,\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(privKey.toString('hex'));\n return publicKey;\n }\n\n #getPrivateKeyFor(\n address: Hex,\n opts: KeyringOpt = { withAppKeyOrigin: '' },\n ): Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey;\n }\n\n // returns an address specific to an app\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 const appKeyAddress = add0x(bytesToHex(publicToAddress(wallet.publicKey)));\n return appKeyAddress;\n }\n\n // exportAccount should return a hex-encoded private key:\n async exportAccount(\n address: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey.toString('hex');\n }\n\n removeAccount(address: string): void {\n if (\n !this.#wallets\n .map(({ publicKey }) =>\n bytesToHex(publicToAddress(publicKey)).toLowerCase(),\n )\n .includes(address.toLowerCase())\n ) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.#wallets = this.#wallets.filter(\n ({ publicKey }) =>\n bytesToHex(publicToAddress(publicKey)).toLowerCase() !==\n address.toLowerCase(),\n );\n }\n\n #getWalletForAccount(\n account: string | number,\n opts: KeyringOpt = {},\n ): Wallet {\n const address = normalize(account);\n let wallet = this.#wallets.find(\n ({ publicKey }) => bytesToHex(publicToAddress(publicKey)) === address,\n );\n if (!wallet) {\n throw new Error('Simple Keyring - Unable to find matching address.');\n }\n\n if (opts.withAppKeyOrigin) {\n const { privateKey } = wallet;\n const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = keccak256(appKeyBuffer);\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n wallet = {\n privateKey: Buffer.from(appKeyPrivateKey),\n publicKey: Buffer.from(appKeyPublicKey),\n };\n }\n\n return wallet;\n }\n}\n\n/**\n * Generate and validate a new random key of 32 bytes.\n *\n * @returns Buffer The generated key.\n */\nfunction generateKey(): Buffer {\n const privateKey = randombytes(32);\n\n if (!isValidPrivate(privateKey)) {\n throw new Error(\n 'Private key does not satisfy the curve requirements (ie. it is invalid)',\n );\n }\n return privateKey;\n}\n\n/**\n * Type predicate type guard to check if a string is in the enum SignTypedDataVersion.\n *\n * @param version - The string to check.\n * @returns Whether it's in the enum.\n */\n// TODO: Put this in @metamask/eth-sig-util\nfunction isSignTypedDataVersion(\n version: SignTypedDataVersion | string,\n): version is SignTypedDataVersion {\n return version in SignTypedDataVersion;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"simple-keyring.d.cts","sourceRoot":"","sources":["../src/simple-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,uBAAuB;AAWlD,OAAO,EAGL,oBAAoB,EAMpB,oBAAoB,EACrB,+BAA+B;AAChC,OAAO,EAAE,OAAO,EAAE,gCAAgC;AAClD,OAAO,EAAS,oBAAoB,EAAE,GAAG,EAAE,wBAAwB;AAInE,KAAK,UAAU,GAAG;IAChB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM,CAAC;CACzC,CAAC;AAUF,MAAM,CAAC,OAAO,OAAO,aAAc,YAAW,OAAO;;IAGnD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAQ;IAE7B,MAAM,CAAC,IAAI,EAAE,MAAM,CAAQ;gBAEf,WAAW,GAAE,MAAM,EAAO;IAUhC,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAI9B,WAAW,CAA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AW,EAAE,GAAG,EAChB,aAAa,EAAE,oBAAoB,GAClC,OAAO,CAAC,MAAM,CAAC;IAOZ,aAAa,CACjB,OAAO,EAAE,GAAG,EACZ,SAAS,EAAE,GAAG,EACd,IAAI,GAAE,UAAiD,GACtD,OAAO,CAAC,MAAM,CAAC;IAaZ,sBAAsB,CAC1B,WAAW,EAAE,GAAG,EAChB,IAAI,CAAC,EAAE,UAAU,GAChB,OAAO,CAAC,MAAM,CAAC;IAkBZ,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAY5D,aAAa,CACjB,OAAO,EAAE,GAAG,EACZ,IAAI;;KAA2B,GAC9B,OAAO,CAAC,MAAM,CAAC;IAKlB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;CA4CrC"}
@@ -16,9 +16,9 @@ function $importDefault(module) {
16
16
  }
17
17
  return module;
18
18
  }
19
- import { arrToBufArr, bufferToHex, ecsign, isValidPrivate, privateToPublic, publicToAddress, stripHexPrefix, toBuffer } from "@ethereumjs/util";
19
+ import { ecsign, isValidPrivate, privateToPublic, publicToAddress, stripHexPrefix } from "@ethereumjs/util";
20
20
  import { concatSig, decrypt, getEncryptionPublicKey, normalize, personalSign, signEIP7702Authorization, signTypedData, SignTypedDataVersion } from "@metamask/eth-sig-util";
21
- import { add0x } from "@metamask/utils";
21
+ import { add0x, bigIntToBytes, bytesToHex } from "@metamask/utils";
22
22
  import { keccak256 } from "ethereum-cryptography/keccak";
23
23
  import $randombytes from "randombytes";
24
24
  const randombytes = $importDefault($randombytes);
@@ -43,7 +43,7 @@ class SimpleKeyring {
43
43
  __classPrivateFieldSet(this, _SimpleKeyring_wallets, privateKeys.map((hexPrivateKey) => {
44
44
  const strippedHexPrivateKey = stripHexPrefix(hexPrivateKey);
45
45
  const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');
46
- const publicKey = privateToPublic(privateKey);
46
+ const publicKey = Buffer.from(privateToPublic(privateKey));
47
47
  return { privateKey, publicKey };
48
48
  }), "f");
49
49
  }
@@ -51,15 +51,15 @@ class SimpleKeyring {
51
51
  const newWallets = [];
52
52
  for (let i = 0; i < numAccounts; i++) {
53
53
  const privateKey = generateKey();
54
- const publicKey = privateToPublic(privateKey);
54
+ const publicKey = Buffer.from(privateToPublic(privateKey));
55
55
  newWallets.push({ privateKey, publicKey });
56
56
  }
57
57
  __classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").concat(newWallets), "f");
58
- const hexWallets = newWallets.map(({ publicKey }) => add0x(bufferToHex(publicToAddress(publicKey))));
58
+ const hexWallets = newWallets.map(({ publicKey }) => add0x(bytesToHex(publicToAddress(publicKey))));
59
59
  return hexWallets;
60
60
  }
61
61
  async getAccounts() {
62
- return __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").map(({ publicKey }) => add0x(bufferToHex(publicToAddress(publicKey))));
62
+ return __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").map(({ publicKey }) => add0x(bytesToHex(publicToAddress(publicKey))));
63
63
  }
64
64
  async signTransaction(address, transaction, opts = {}) {
65
65
  const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
@@ -80,7 +80,7 @@ class SimpleKeyring {
80
80
  }
81
81
  const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
82
82
  const msgSig = ecsign(Buffer.from(message, 'hex'), privKey);
83
- const rawMsgSig = concatSig(toBuffer(msgSig.v), msgSig.r, msgSig.s);
83
+ const rawMsgSig = concatSig(Buffer.from(bigIntToBytes(msgSig.v)), Buffer.from(msgSig.r), Buffer.from(msgSig.s));
84
84
  return rawMsgSig;
85
85
  }
86
86
  // For personal_sign, we need to prefix the message:
@@ -118,7 +118,7 @@ class SimpleKeyring {
118
118
  const wallet = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getWalletForAccount).call(this, address, {
119
119
  withAppKeyOrigin: origin,
120
120
  });
121
- const appKeyAddress = add0x(bufferToHex(publicToAddress(wallet.publicKey)));
121
+ const appKeyAddress = add0x(bytesToHex(publicToAddress(wallet.publicKey)));
122
122
  return appKeyAddress;
123
123
  }
124
124
  // exportAccount should return a hex-encoded private key:
@@ -128,11 +128,11 @@ class SimpleKeyring {
128
128
  }
129
129
  removeAccount(address) {
130
130
  if (!__classPrivateFieldGet(this, _SimpleKeyring_wallets, "f")
131
- .map(({ publicKey }) => bufferToHex(publicToAddress(publicKey)).toLowerCase())
131
+ .map(({ publicKey }) => bytesToHex(publicToAddress(publicKey)).toLowerCase())
132
132
  .includes(address.toLowerCase())) {
133
133
  throw new Error(`Address ${address} not found in this keyring`);
134
134
  }
135
- __classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").filter(({ publicKey }) => bufferToHex(publicToAddress(publicKey)).toLowerCase() !==
135
+ __classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").filter(({ publicKey }) => bytesToHex(publicToAddress(publicKey)).toLowerCase() !==
136
136
  address.toLowerCase()), "f");
137
137
  }
138
138
  }
@@ -144,7 +144,7 @@ _SimpleKeyring_wallets = new WeakMap(), _SimpleKeyring_instances = new WeakSet()
144
144
  return wallet.privateKey;
145
145
  }, _SimpleKeyring_getWalletForAccount = function _SimpleKeyring_getWalletForAccount(account, opts = {}) {
146
146
  const address = normalize(account);
147
- let wallet = __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").find(({ publicKey }) => bufferToHex(publicToAddress(publicKey)) === address);
147
+ let wallet = __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").find(({ publicKey }) => bytesToHex(publicToAddress(publicKey)) === address);
148
148
  if (!wallet) {
149
149
  throw new Error('Simple Keyring - Unable to find matching address.');
150
150
  }
@@ -152,9 +152,12 @@ _SimpleKeyring_wallets = new WeakMap(), _SimpleKeyring_instances = new WeakSet()
152
152
  const { privateKey } = wallet;
153
153
  const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');
154
154
  const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
155
- const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer));
155
+ const appKeyPrivateKey = keccak256(appKeyBuffer);
156
156
  const appKeyPublicKey = privateToPublic(appKeyPrivateKey);
157
- wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
157
+ wallet = {
158
+ privateKey: Buffer.from(appKeyPrivateKey),
159
+ publicKey: Buffer.from(appKeyPublicKey),
160
+ };
158
161
  }
159
162
  return wallet;
160
163
  };
@@ -1 +1 @@
1
- {"version":3,"file":"simple-keyring.mjs","sourceRoot":"","sources":["../src/simple-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,OAAO,EACL,WAAW,EACX,WAAW,EACX,MAAM,EACN,cAAc,EACd,eAAe,EACf,eAAe,EACf,cAAc,EACd,QAAQ,EACT,yBAAyB;AAC1B,OAAO,EACL,SAAS,EACT,OAAO,EAEP,sBAAsB,EACtB,SAAS,EACT,YAAY,EACZ,wBAAwB,EACxB,aAAa,EACb,oBAAoB,EACrB,+BAA+B;AAEhC,OAAO,EAAE,KAAK,EAA6B,wBAAwB;AACnE,OAAO,EAAE,SAAS,EAAE,qCAAqC;AACzD,OAAO,YAAW,oBAAoB;;AAYtC,MAAM,IAAI,GAAG,iBAAiB,CAAC;AAE/B,iDAAiD;AACjD,MAAqB,aAAa;IAOhC,YAAY,cAAwB,EAAE;;QANtC,yCAAsD;QAE7C,SAAI,GAAW,IAAI,CAAC;QAK3B,uBAAA,IAAI,0BAAY,EAAE,MAAA,CAAC;QAEnB;iHACyG;QACzG,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,CAAC,KAAY,EAAE,E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AAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACvE,CAAC;IAED,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;QAC1B,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;QAC9B,MAAM,kBAAkB,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;QACtE,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC,CAAC;QACrE,MAAM,gBAAgB,GAAG,WAAW,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAAC;QAC9D,MAAM,eAAe,GAAG,eAAe,CAAC,gBAAgB,CAAC,CAAC;QAC1D,MAAM,GAAG,EAAE,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC;IACxE,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AA3MM,kBAAI,GAAW,IAAI,AAAf,CAAgB;eALR,aAAa;AAmNlC;;;;GAIG;AACH,SAAS,WAAW;IAClB,MAAM,UAAU,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;IAEnC,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,yEAAyE,CAC1E,CAAC;IACJ,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;GAKG;AACH,2CAA2C;AAC3C,SAAS,sBAAsB,CAC7B,OAAsC;IAEtC,OAAO,OAAO,IAAI,oBAAoB,CAAC;AACzC,CAAC","sourcesContent":["import { TypedTransaction } from '@ethereumjs/tx';\nimport {\n arrToBufArr,\n bufferToHex,\n ecsign,\n isValidPrivate,\n privateToPublic,\n publicToAddress,\n stripHexPrefix,\n toBuffer,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n EIP7702Authorization,\n getEncryptionPublicKey,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n} from '@metamask/eth-sig-util';\nimport { Keyring } from '@metamask/keyring-utils';\nimport { add0x, Eip1024EncryptedData, Hex } from '@metamask/utils';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\nimport randombytes from 'randombytes';\n\ntype KeyringOpt = {\n withAppKeyOrigin?: string;\n version?: SignTypedDataVersion | string;\n};\n\ntype Wallet = {\n privateKey: Buffer;\n publicKey: Buffer;\n};\n\nconst TYPE = 'Simple Key Pair';\n\n// FIXME: This should not be exported as default.\nexport default class SimpleKeyring implements Keyring {\n #wallets: { privateKey: Buffer; publicKey: Buffer }[];\n\n readonly type: string = TYPE;\n\n static type: string = TYPE;\n\n constructor(privateKeys: string[] = []) {\n this.#wallets = [];\n\n /* istanbul ignore next: It's not possible to write a unit test for this, because a constructor isn't allowed\n * to be async. Jest can't await the constructor, and when the error gets thrown, Jest can't catch it. */\n this.deserialize(privateKeys).catch((error: Error) => {\n throw new Error(`Problem deserializing SimpleKeyring ${error.message}`);\n });\n }\n\n async serialize(): Promise<string[]> {\n return this.#wallets.map((a) => a.privateKey.toString('hex'));\n }\n\n async deserialize(privateKeys: string[]): Promise<void> {\n this.#wallets = privateKeys.map((hexPrivateKey) => {\n const strippedHexPrivateKey = stripHexPrefix(hexPrivateKey);\n const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');\n const publicKey = privateToPublic(privateKey);\n return { privateKey, publicKey };\n });\n }\n\n async addAccounts(numAccounts = 1): Promise<Hex[]> {\n const newWallets = [];\n for (let i = 0; i < numAccounts; i++) {\n const privateKey = generateKey();\n const publicKey = privateToPublic(privateKey);\n newWallets.push({ privateKey, publicKey });\n }\n this.#wallets = this.#wallets.concat(newWallets);\n const hexWallets = newWallets.map(({ publicKey }) =>\n add0x(bufferToHex(publicToAddress(publicKey))),\n );\n return hexWallets;\n }\n\n async getAccounts(): Promise<Hex[]> {\n return this.#wallets.map(({ publicKey }) =>\n add0x(bufferToHex(publicToAddress(publicKey))),\n );\n }\n\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n opts: KeyringOpt = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = transaction.sign(privKey);\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? transaction;\n }\n\n async signEip7702Authorization(\n address: Hex,\n authorization: EIP7702Authorization,\n opts: KeyringOpt = {},\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signEIP7702Authorization({ privateKey, authorization });\n }\n\n // For eth_sign, we need to sign arbitrary data:\n async signMessage(\n address: Hex,\n data: string,\n opts = { withAppKeyOrigin: '', validateMessage: true },\n ): Promise<string> {\n const message = stripHexPrefix(data);\n if (\n opts.validateMessage &&\n (message.length === 0 || !message.match(/^[a-fA-F0-9]*$/u))\n ) {\n throw new Error('Cannot sign invalid message');\n }\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), privKey);\n const rawMsgSig = concatSig(toBuffer(msgSig.v), msgSig.r, msgSig.s);\n return rawMsgSig;\n }\n\n // For personal_sign, we need to prefix the message:\n async signPersonalMessage(\n address: Hex,\n msgHex: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n return personalSign({ privateKey: privKey, data: msgHex });\n }\n\n // For eth_decryptMessage:\n async decryptMessage(\n withAccount: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const privateKey = wallet.privateKey.toString('hex');\n return decrypt({ privateKey, encryptedData });\n }\n\n // personal_signTypedData, signs data along with the schema\n async signTypedData(\n address: Hex,\n typedData: any,\n opts: KeyringOpt = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\n // Treat invalid versions as \"V1\"\n let version = SignTypedDataVersion.V1;\n\n if (opts.version && isSignTypedDataVersion(opts.version)) {\n version = SignTypedDataVersion[opts.version];\n }\n\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signTypedData({ privateKey, data: typedData, version });\n }\n\n // get public key for nacl\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts?: KeyringOpt,\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(privKey.toString('hex'));\n return publicKey;\n }\n\n #getPrivateKeyFor(\n address: Hex,\n opts: KeyringOpt = { withAppKeyOrigin: '' },\n ): Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey;\n }\n\n // returns an address specific to an app\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 const appKeyAddress = add0x(bufferToHex(publicToAddress(wallet.publicKey)));\n return appKeyAddress;\n }\n\n // exportAccount should return a hex-encoded private key:\n async exportAccount(\n address: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey.toString('hex');\n }\n\n removeAccount(address: string): void {\n if (\n !this.#wallets\n .map(({ publicKey }) =>\n bufferToHex(publicToAddress(publicKey)).toLowerCase(),\n )\n .includes(address.toLowerCase())\n ) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.#wallets = this.#wallets.filter(\n ({ publicKey }) =>\n bufferToHex(publicToAddress(publicKey)).toLowerCase() !==\n address.toLowerCase(),\n );\n }\n\n #getWalletForAccount(\n account: string | number,\n opts: KeyringOpt = {},\n ): Wallet {\n const address = normalize(account);\n let wallet = this.#wallets.find(\n ({ publicKey }) => bufferToHex(publicToAddress(publicKey)) === address,\n );\n if (!wallet) {\n throw new Error('Simple Keyring - Unable to find matching address.');\n }\n\n if (opts.withAppKeyOrigin) {\n const { privateKey } = wallet;\n const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer));\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n}\n\n/**\n * Generate and validate a new random key of 32 bytes.\n *\n * @returns Buffer The generated key.\n */\nfunction generateKey(): Buffer {\n const privateKey = randombytes(32);\n\n if (!isValidPrivate(privateKey)) {\n throw new Error(\n 'Private key does not satisfy the curve requirements (ie. it is invalid)',\n );\n }\n return privateKey;\n}\n\n/**\n * Type predicate type guard to check if a string is in the enum SignTypedDataVersion.\n *\n * @param version - The string to check.\n * @returns Whether it's in the enum.\n */\n// TODO: Put this in @metamask/eth-sig-util\nfunction isSignTypedDataVersion(\n version: SignTypedDataVersion | string,\n): version is SignTypedDataVersion {\n return version in SignTypedDataVersion;\n}\n"]}
1
+ {"version":3,"file":"simple-keyring.mjs","sourceRoot":"","sources":["../src/simple-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,OAAO,EACL,MAAM,EACN,cAAc,EACd,eAAe,EACf,eAAe,EACf,cAAc,EACf,yBAAyB;AAC1B,OAAO,EACL,SAAS,EACT,OAAO,EAEP,sBAAsB,EACtB,SAAS,EACT,YAAY,EACZ,wBAAwB,EACxB,aAAa,EACb,oBAAoB,EACrB,+BAA+B;AAEhC,OAAO,EACL,KAAK,EACL,aAAa,EACb,UAAU,EAGX,wBAAwB;AACzB,OAAO,EAAE,SAAS,EAAE,qCAAqC;AACzD,OAAO,YAAW,oBAAoB;;AAYtC,MAAM,IAAI,GAAG,iBAAiB,CAAC;AAE/B,iDAAiD;AACjD,MAAqB,aAAa;IAOhC,YAAY,cAAwB,EAAE;;QANtC,yCAAsD;QAE7C,SAAI,GAAW,IAAI,CAAC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AAC;AACpB,CAAC;AAED;;;;;GAKG;AACH,2CAA2C;AAC3C,SAAS,sBAAsB,CAC7B,OAAsC;IAEtC,OAAO,OAAO,IAAI,oBAAoB,CAAC;AACzC,CAAC","sourcesContent":["import { TypedTransaction } from '@ethereumjs/tx';\nimport {\n ecsign,\n isValidPrivate,\n privateToPublic,\n publicToAddress,\n stripHexPrefix,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n EIP7702Authorization,\n getEncryptionPublicKey,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n} from '@metamask/eth-sig-util';\nimport { Keyring } from '@metamask/keyring-utils';\nimport {\n add0x,\n bigIntToBytes,\n bytesToHex,\n Eip1024EncryptedData,\n Hex,\n} from '@metamask/utils';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\nimport randombytes from 'randombytes';\n\ntype KeyringOpt = {\n withAppKeyOrigin?: string;\n version?: SignTypedDataVersion | string;\n};\n\ntype Wallet = {\n privateKey: Buffer;\n publicKey: Buffer;\n};\n\nconst TYPE = 'Simple Key Pair';\n\n// FIXME: This should not be exported as default.\nexport default class SimpleKeyring implements Keyring {\n #wallets: { privateKey: Buffer; publicKey: Buffer }[];\n\n readonly type: string = TYPE;\n\n static type: string = TYPE;\n\n constructor(privateKeys: string[] = []) {\n this.#wallets = [];\n\n /* istanbul ignore next: It's not possible to write a unit test for this, because a constructor isn't allowed\n * to be async. Jest can't await the constructor, and when the error gets thrown, Jest can't catch it. */\n this.deserialize(privateKeys).catch((error: Error) => {\n throw new Error(`Problem deserializing SimpleKeyring ${error.message}`);\n });\n }\n\n async serialize(): Promise<string[]> {\n return this.#wallets.map((a) => a.privateKey.toString('hex'));\n }\n\n async deserialize(privateKeys: string[]): Promise<void> {\n this.#wallets = privateKeys.map((hexPrivateKey) => {\n const strippedHexPrivateKey = stripHexPrefix(hexPrivateKey);\n const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');\n const publicKey = Buffer.from(privateToPublic(privateKey));\n return { privateKey, publicKey };\n });\n }\n\n async addAccounts(numAccounts = 1): Promise<Hex[]> {\n const newWallets = [];\n for (let i = 0; i < numAccounts; i++) {\n const privateKey = generateKey();\n const publicKey = Buffer.from(privateToPublic(privateKey));\n newWallets.push({ privateKey, publicKey });\n }\n this.#wallets = this.#wallets.concat(newWallets);\n const hexWallets = newWallets.map(({ publicKey }) =>\n add0x(bytesToHex(publicToAddress(publicKey))),\n );\n return hexWallets;\n }\n\n async getAccounts(): Promise<Hex[]> {\n return this.#wallets.map(({ publicKey }) =>\n add0x(bytesToHex(publicToAddress(publicKey))),\n );\n }\n\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n opts: KeyringOpt = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = transaction.sign(privKey);\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? transaction;\n }\n\n async signEip7702Authorization(\n address: Hex,\n authorization: EIP7702Authorization,\n opts: KeyringOpt = {},\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signEIP7702Authorization({ privateKey, authorization });\n }\n\n // For eth_sign, we need to sign arbitrary data:\n async signMessage(\n address: Hex,\n data: string,\n opts = { withAppKeyOrigin: '', validateMessage: true },\n ): Promise<string> {\n const message = stripHexPrefix(data);\n if (\n opts.validateMessage &&\n (message.length === 0 || !message.match(/^[a-fA-F0-9]*$/u))\n ) {\n throw new Error('Cannot sign invalid message');\n }\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), 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 // For personal_sign, we need to prefix the message:\n async signPersonalMessage(\n address: Hex,\n msgHex: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n return personalSign({ privateKey: privKey, data: msgHex });\n }\n\n // For eth_decryptMessage:\n async decryptMessage(\n withAccount: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const privateKey = wallet.privateKey.toString('hex');\n return decrypt({ privateKey, encryptedData });\n }\n\n // personal_signTypedData, signs data along with the schema\n async signTypedData(\n address: Hex,\n typedData: any,\n opts: KeyringOpt = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\n // Treat invalid versions as \"V1\"\n let version = SignTypedDataVersion.V1;\n\n if (opts.version && isSignTypedDataVersion(opts.version)) {\n version = SignTypedDataVersion[opts.version];\n }\n\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signTypedData({ privateKey, data: typedData, version });\n }\n\n // get public key for nacl\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts?: KeyringOpt,\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(privKey.toString('hex'));\n return publicKey;\n }\n\n #getPrivateKeyFor(\n address: Hex,\n opts: KeyringOpt = { withAppKeyOrigin: '' },\n ): Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey;\n }\n\n // returns an address specific to an app\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 const appKeyAddress = add0x(bytesToHex(publicToAddress(wallet.publicKey)));\n return appKeyAddress;\n }\n\n // exportAccount should return a hex-encoded private key:\n async exportAccount(\n address: Hex,\n opts = { withAppKeyOrigin: '' },\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey.toString('hex');\n }\n\n removeAccount(address: string): void {\n if (\n !this.#wallets\n .map(({ publicKey }) =>\n bytesToHex(publicToAddress(publicKey)).toLowerCase(),\n )\n .includes(address.toLowerCase())\n ) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.#wallets = this.#wallets.filter(\n ({ publicKey }) =>\n bytesToHex(publicToAddress(publicKey)).toLowerCase() !==\n address.toLowerCase(),\n );\n }\n\n #getWalletForAccount(\n account: string | number,\n opts: KeyringOpt = {},\n ): Wallet {\n const address = normalize(account);\n let wallet = this.#wallets.find(\n ({ publicKey }) => bytesToHex(publicToAddress(publicKey)) === address,\n );\n if (!wallet) {\n throw new Error('Simple Keyring - Unable to find matching address.');\n }\n\n if (opts.withAppKeyOrigin) {\n const { privateKey } = wallet;\n const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = keccak256(appKeyBuffer);\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n wallet = {\n privateKey: Buffer.from(appKeyPrivateKey),\n publicKey: Buffer.from(appKeyPublicKey),\n };\n }\n\n return wallet;\n }\n}\n\n/**\n * Generate and validate a new random key of 32 bytes.\n *\n * @returns Buffer The generated key.\n */\nfunction generateKey(): Buffer {\n const privateKey = randombytes(32);\n\n if (!isValidPrivate(privateKey)) {\n throw new Error(\n 'Private key does not satisfy the curve requirements (ie. it is invalid)',\n );\n }\n return privateKey;\n}\n\n/**\n * Type predicate type guard to check if a string is in the enum SignTypedDataVersion.\n *\n * @param version - The string to check.\n * @returns Whether it's in the enum.\n */\n// TODO: Put this in @metamask/eth-sig-util\nfunction isSignTypedDataVersion(\n version: SignTypedDataVersion | string,\n): version is SignTypedDataVersion {\n return version in SignTypedDataVersion;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/eth-simple-keyring",
3
- "version": "9.0.0",
3
+ "version": "10.0.0",
4
4
  "description": "A simple standard interface for a series of Ethereum private keys.",
5
5
  "keywords": [
6
6
  "ethereum",
@@ -44,18 +44,18 @@
44
44
  "test:watch": "jest --watch"
45
45
  },
46
46
  "dependencies": {
47
- "@ethereumjs/util": "^8.1.0",
47
+ "@ethereumjs/util": "^9.1.0",
48
48
  "@metamask/eth-sig-util": "^8.2.0",
49
49
  "@metamask/utils": "^11.1.0",
50
50
  "ethereum-cryptography": "^2.1.2",
51
51
  "randombytes": "^2.1.0"
52
52
  },
53
53
  "devDependencies": {
54
- "@ethereumjs/tx": "^4.2.0",
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",
58
- "@metamask/keyring-utils": "^2.3.1",
58
+ "@metamask/keyring-utils": "^3.0.0",
59
59
  "@ts-bridge/cli": "^0.6.3",
60
60
  "@types/ethereumjs-tx": "^1.0.1",
61
61
  "@types/jest": "^29.5.12",