@metamask-previews/eth-qr-keyring 1.1.0-fd40efd → 2.0.1-7275dbc

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +33 -2
  2. package/README.md +8 -0
  3. package/dist/device.cjs +0 -1
  4. package/dist/device.cjs.map +1 -1
  5. package/dist/device.d.cts +3 -3
  6. package/dist/device.d.cts.map +1 -1
  7. package/dist/device.d.mts +3 -3
  8. package/dist/device.d.mts.map +1 -1
  9. package/dist/device.mjs +0 -1
  10. package/dist/device.mjs.map +1 -1
  11. package/dist/index.cjs +1 -3
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.cts +0 -1
  14. package/dist/index.d.cts.map +1 -1
  15. package/dist/index.d.mts +0 -1
  16. package/dist/index.d.mts.map +1 -1
  17. package/dist/index.mjs +0 -1
  18. package/dist/index.mjs.map +1 -1
  19. package/dist/qr-keyring-deferred-promise-bridge.cjs.map +1 -1
  20. package/dist/qr-keyring-deferred-promise-bridge.d.cts.map +1 -1
  21. package/dist/qr-keyring-deferred-promise-bridge.d.mts.map +1 -1
  22. package/dist/qr-keyring-deferred-promise-bridge.mjs.map +1 -1
  23. package/dist/qr-keyring.cjs.map +1 -1
  24. package/dist/qr-keyring.d.cts +4 -3
  25. package/dist/qr-keyring.d.cts.map +1 -1
  26. package/dist/qr-keyring.d.mts +4 -3
  27. package/dist/qr-keyring.d.mts.map +1 -1
  28. package/dist/qr-keyring.mjs.map +1 -1
  29. package/dist/v2/index.cjs +6 -0
  30. package/dist/v2/index.cjs.map +1 -0
  31. package/dist/v2/index.d.cts +2 -0
  32. package/dist/v2/index.d.cts.map +1 -0
  33. package/dist/v2/index.d.mts +2 -0
  34. package/dist/v2/index.d.mts.map +1 -0
  35. package/dist/v2/index.mjs +2 -0
  36. package/dist/v2/index.mjs.map +1 -0
  37. package/dist/{qr-keyring-v2.cjs → v2/qr-keyring.cjs} +26 -24
  38. package/dist/v2/qr-keyring.cjs.map +1 -0
  39. package/dist/{qr-keyring-v2.d.mts → v2/qr-keyring.d.cts} +10 -8
  40. package/dist/v2/qr-keyring.d.cts.map +1 -0
  41. package/dist/{qr-keyring-v2.d.cts → v2/qr-keyring.d.mts} +10 -8
  42. package/dist/v2/qr-keyring.d.mts.map +1 -0
  43. package/dist/{qr-keyring-v2.mjs → v2/qr-keyring.mjs} +24 -22
  44. package/dist/v2/qr-keyring.mjs.map +1 -0
  45. package/package.json +43 -24
  46. package/v2.js +3 -0
  47. package/dist/qr-keyring-v2.cjs.map +0 -1
  48. package/dist/qr-keyring-v2.d.cts.map +0 -1
  49. package/dist/qr-keyring-v2.d.mts.map +0 -1
  50. package/dist/qr-keyring-v2.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -7,11 +7,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [2.0.1]
11
+
12
+ ### Changed
13
+
14
+ - Bump `@metamask/account-api` from `^1.0.2` to `^1.0.3` ([#518](https://github.com/MetaMask/accounts/pull/518))
15
+ - Bump `@metamask/keyring-api` from `^23.0.0` to `^23.0.1` ([#518](https://github.com/MetaMask/accounts/pull/518))
16
+ - Bump `@metamask/keyring-sdk` from `^2.0.0` to `^2.0.1` ([#518](https://github.com/MetaMask/accounts/pull/518))
17
+
18
+ ### Fixed
19
+
20
+ - Workaround Browserify subpath export for `/v2` ([#516](https://github.com/MetaMask/accounts/pull/516))
21
+
22
+ ## [2.0.0]
23
+
10
24
  ### Added
11
25
 
12
- - Add `QrKeyringV2` class implementing `KeyringV2` interface ([#411](https://github.com/MetaMask/accounts/pull/411)), ([#447](https://github.com/MetaMask/accounts/pull/447)), ([#451](https://github.com/MetaMask/accounts/pull/451)), ([#453](https://github.com/MetaMask/accounts/pull/453))
26
+ - Add `QrKeyringV2` class implementing `KeyringV2` interface ([#411](https://github.com/MetaMask/accounts/pull/411)), ([#447](https://github.com/MetaMask/accounts/pull/447)), ([#451](https://github.com/MetaMask/accounts/pull/451)), ([#453](https://github.com/MetaMask/accounts/pull/453)), ([#478](https://github.com/MetaMask/accounts/pull/478)), ([#482](https://github.com/MetaMask/accounts/pull/482)), ([#487](https://github.com/MetaMask/accounts/pull/487)), ([#496](https://github.com/MetaMask/accounts/pull/496)), ([#509](https://github.com/MetaMask/accounts/pull/509))
27
+ - Add new dependency `@metamask/keyring-api@22.0.0`.
28
+ - Add new dependency `@metamask/keyring-sdk@1.2.0`.
29
+ - Add new dependency `@metamask/account-api@1.0.1`.
13
30
  - Wraps legacy `QrKeyring` to expose accounts via the unified `KeyringV2` API and the `KeyringAccount` type.
14
31
  - Extends `EthKeyringWrapper` for common Ethereum logic.
32
+ - Add `./v2` subpath export for keyring v2 implementation ([#513](https://github.com/MetaMask/accounts/pull/513))
33
+ - `QrKeyring`, `QrKeyringOptions`, and `QrAccountModeCreateOptions` are now available from `@metamask/eth-qr-keyring/v2`.
34
+
35
+ ### Changed
36
+
37
+ - **BREAKING:** Rename and move `QrKeyringV2`, `QrKeyringV2Options`, and `QrAccountModeCreateOptions` to the new `./v2` subpath export ([#513](https://github.com/MetaMask/accounts/pull/513))
38
+ - `QrKeyringV2` is now `QrKeyring` from `@metamask/eth-qr-keyring/v2`.
39
+ - `QrKeyringV2Options` is now `QrKeyringOptions` from `@metamask/eth-qr-keyring/v2`.
40
+ - Bump `@metamask/utils` from `^11.1.0` to `^11.11.0` ([#489](https://github.com/MetaMask/accounts/pull/489), [#483](https://github.com/MetaMask/accounts/pull/483))
41
+ - Bump `@metamask/account-api` from `^1.0.1` to `^1.0.2` ([#515](https://github.com/MetaMask/accounts/pull/515))
42
+ - Bump `@metamask/keyring-api` from `^22.0.0` to `^23.0.0` ([#515](https://github.com/MetaMask/accounts/pull/515))
43
+ - Bump `@metamask/keyring-sdk` from `^1.2.0` to `^2.0.0` ([#515](https://github.com/MetaMask/accounts/pull/515))
15
44
 
16
45
  ## [1.1.0]
17
46
 
@@ -29,6 +58,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
29
58
 
30
59
  - Initial release ([#60](https://github.com/MetaMask/accounts/pull/60))
31
60
 
32
- [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-qr-keyring@1.1.0...HEAD
61
+ [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-qr-keyring@2.0.1...HEAD
62
+ [2.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-qr-keyring@2.0.0...@metamask/eth-qr-keyring@2.0.1
63
+ [2.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-qr-keyring@1.1.0...@metamask/eth-qr-keyring@2.0.0
33
64
  [1.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-qr-keyring@1.0.0...@metamask/eth-qr-keyring@1.1.0
34
65
  [1.0.0]: https://github.com/MetaMask/accounts/releases/tag/@metamask/eth-qr-keyring@1.0.0
package/README.md CHANGED
@@ -1 +1,9 @@
1
1
  # QR Keyring
2
+
3
+ ## Installation
4
+
5
+ `yarn add @metamask/eth-qr-keyring`
6
+
7
+ or
8
+
9
+ `npm install @metamask/eth-qr-keyring`
package/dist/device.cjs CHANGED
@@ -21,7 +21,6 @@ const tx_1 = require("@ethereumjs/tx");
21
21
  const util_1 = require("@ethereumjs/util");
22
22
  const bc_ur_registry_eth_1 = require("@keystonehq/bc-ur-registry-eth");
23
23
  const utils_1 = require("@metamask/utils");
24
- // eslint-disable-next-line @typescript-eslint/naming-convention
25
24
  const hdkey_1 = __importDefault(require("hdkey"));
26
25
  const uuid_1 = require("uuid");
27
26
  const qr_keyring_1 = require("./qr-keyring.cjs");
@@ -1 +1 @@
1
- {"version":3,"file":"device.cjs","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,yCAAsC;AACtC,uCAKwB;AACxB,2CAAmD;AACnD,uEAOwC;AAExC,2CAAgF;AAChF,gEAAgE;AA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S,CAAC,QAAQ,CAAC,EAAE,CAAC;KAC1B,CAAC;AACJ,CAAC,+CASS,EAAyB;IACjC,MAAM,SAAS,GACb,OAAO,EAAE,KAAK,QAAQ;QACpB,CAAC,CAAC,sCAAiB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9B,CAAC,CAAC;YACE,IAAI,EAAE,EAAE,CAAC,IAAI;YACb,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC;SAClC,CAAC;IAER,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,SAAS,CAAC;IAEjC,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,yBAAiB,CAAC,YAAY;YACjC,OAAO,gCAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACpC,KAAK,yBAAiB,CAAC,cAAc;YACnC,OAAO,kCAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACtC;YACE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC3C,CAAC;AACH,CAAC","sourcesContent":["import { RLP } from '@ethereumjs/rlp';\nimport {\n type TypedTransaction,\n TransactionType,\n type TypedTxData,\n TransactionFactory,\n} from '@ethereumjs/tx';\nimport { publicToAddress } from '@ethereumjs/util';\nimport {\n CryptoAccount,\n CryptoHDKey,\n DataType,\n ETHSignature,\n EthSignRequest,\n URRegistryDecoder,\n} from '@keystonehq/bc-ur-registry-eth';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport { add0x, getChecksumAddress, remove0x, type Hex } from '@metamask/utils';\n// eslint-disable-next-line @typescript-eslint/naming-convention\nimport HdKey from 'hdkey';\nimport { stringify, v4 as uuidv4 } from 'uuid';\n\nimport {\n QrScanRequestType,\n type QrKeyringBridge,\n type QrSignatureRequest,\n type SerializedUR,\n} from './qr-keyring';\n\nexport const SUPPORTED_UR_TYPE = {\n CRYPTO_HDKEY: 'crypto-hdkey',\n CRYPTO_ACCOUNT: 'crypto-account',\n ETH_SIGNATURE: 'eth-signature',\n};\n\nexport enum DeviceMode {\n HD = 'hd',\n ACCOUNT = 'account',\n}\n\nconst DEFAULT_CHILDREN_PATH = '0/*';\n\nconst MAX_INDEX = 1_000;\n\n/**\n * Common details for the device source, which can be either a CryptoAccount or CryptoHDKey.\n */\nexport type CommonDeviceDetails = {\n /**\n * Value take out from the device note field, if available.\n */\n keyringAccount: string;\n /**\n * The name of the device\n */\n name: string;\n /**\n * The device fingerprint, hex-encoded\n */\n xfp: string;\n /**\n * Indexes of the accounts derived from the device\n * in the form of a map from address to index\n */\n indexes: Record<Hex, number>;\n};\n\n/**\n * Details for the HD mode of the Device. This mode derives\n * accounts from a root public key (xpub) and a derivation path.\n */\nexport type HDModeDeviceDetails = {\n /**\n * The device mode is HD, indicating that it derives accounts from a\n * root public key (xpub) and a derivation path.\n */\n keyringMode: DeviceMode.HD;\n /**\n * The xpub of the HD key\n */\n xpub: string;\n /**\n * The derivation path of the HD key\n */\n hdPath: string;\n /**\n * The path used to derive child accounts\n */\n childrenPath: string;\n};\n\n/**\n * Details for the Account mode of the Device. This mode derives\n * accounts from a set of addresses and their corresponding paths.\n */\nexport type AccountModeDeviceDetails = {\n /**\n * The device mode is ACCOUNT, indicating that it derives accounts from\n * a set of addresses and their corresponding paths.\n */\n keyringMode: DeviceMode.ACCOUNT;\n /**\n * The derivation paths for each hex-encoded address in the device\n */\n paths: Record<Hex, string>;\n};\n\n/**\n * An address with its corresponding index.\n */\nexport type IndexedAddress = {\n address: Hex;\n index: number;\n};\n\n/**\n * The details of the source CryptoAccount or CryptoHDKey\n * that the Device uses to derive accounts.\n */\nexport type DeviceDetails = CommonDeviceDetails &\n (HDModeDeviceDetails | AccountModeDeviceDetails);\n\nexport type DeviceOptions = {\n /**\n * The requestScan function to scan the QR code\n */\n requestScan: QrKeyringBridge['requestScan'];\n /**\n * The source of the device, which can be of type `DeviceDetails`,\n * `string`, or `SerializedUR`.\n *\n * When a `string` or `SerializedUR` is provided, the Device will\n * initialize itself from the UR.\n */\n source: DeviceDetails | string | SerializedUR;\n};\n\n/**\n * Get the fingerprint of the source CryptoAccount or CryptoHDKey\n *\n * @param source - The source CryptoAccount or CryptoHDKey\n * @returns The fingerprint of the source\n */\nfunction getFingerprintFromSource(source: CryptoAccount | CryptoHDKey): string {\n return source instanceof CryptoAccount\n ? source.getMasterFingerprint()?.toString('hex')\n : source.getOrigin()?.getSourceFingerprint()?.toString('hex');\n}\n\n/**\n * Get fingerprint, account paths and names from the a CryptoAccount\n *\n * Note: This function emulates the behavior of the `@keystonehq/base-eth-keyring`\n * library when dealing with CryptoAccount objects (for backwards compatibility\n * reasons). Though, the way it retrieves `name` and `keyringAccount` is questionable,\n * as `name` and `keyringAccount` are updated after each descriptor discovery, effectively\n * returning the last descriptor's `name` and `keyringAccount`.\n *\n * @param source - The source CryptoAccount\n * @returns The paths\n */\nfunction readCryptoAccountOutputDescriptors(source: CryptoAccount): {\n xfp: string;\n paths: Record<Hex, string>;\n name: string;\n keyringAccount: string;\n} {\n const descriptors = source.getOutputDescriptors();\n\n if (!descriptors || descriptors.length === 0) {\n throw new Error('No output descriptors found in CryptoAccount');\n }\n\n let name = '';\n let keyringAccount = '';\n const paths = descriptors.reduce(\n (descriptorsPaths: Record<Hex, string>, current) => {\n const hdKey = current.getHDKey();\n if (hdKey) {\n const path = `M/${hdKey.getOrigin().getPath()}`;\n const address = getChecksumAddress(\n add0x(\n Buffer.from(publicToAddress(hdKey.getKey(), true)).toString('hex'),\n ),\n );\n descriptorsPaths[address] = path;\n name = hdKey.getName();\n keyringAccount = hdKey.getNote();\n }\n return descriptorsPaths;\n },\n {},\n );\n\n return {\n paths,\n name,\n keyringAccount,\n xfp: getFingerprintFromSource(source),\n };\n}\n\nexport class Device {\n readonly #requestScan: QrKeyringBridge['requestScan'];\n\n readonly #pairedDevice: DeviceDetails;\n\n /**\n * Create a new Device instance.\n *\n * @param options - The options for the Device, including the requestScan function\n * and the source of the device details.\n * @param options.requestScan - The function to request a scan of the QR code.\n * @param options.source - The source of the device details, which can be a\n * UR string, a `SerializedUR`, or a `DeviceDetails` object.\n */\n constructor({ requestScan, source }: DeviceOptions) {\n this.#requestScan = requestScan;\n this.#pairedDevice =\n typeof source === 'string' || 'cbor' in source\n ? this.#deviceDetailsFromUR(source)\n : source;\n }\n\n /**\n * Derive an address from the source at a given index\n *\n * @param index - The index to derive the address from\n * @returns The derived address in hex format\n * @throws Will throw an error if the source is not initialized\n */\n addressFromIndex(index: number): Hex {\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const address = Object.keys(this.#pairedDevice.paths)[index];\n if (!address) {\n throw new Error(`Address not found for index ${index}`);\n }\n return add0x(address);\n }\n const childPath = `m/${this.#pairedDevice.childrenPath.replace(\n '*',\n index.toString(),\n )}`;\n\n const hdKey = HdKey.fromExtendedKey(this.#pairedDevice.xpub);\n const childKey = hdKey.derive(childPath);\n\n const address = Buffer.from(\n publicToAddress(childKey.publicKey, true),\n ).toString('hex');\n\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n this.#pairedDevice.indexes[normalizedAddress] = index;\n\n return normalizedAddress;\n }\n\n /**\n * Retrieve the path of an address derived from the source.\n *\n * @param address - The address to retrieve the path for\n * @returns The path of the address\n */\n pathFromAddress(address: Hex): string {\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const path = this.#pairedDevice.paths[normalizedAddress];\n if (path === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n return path;\n }\n\n let index = this.#pairedDevice.indexes[normalizedAddress];\n if (index === undefined) {\n for (let i = 0; i < MAX_INDEX; i++) {\n const derivedAddress = this.addressFromIndex(i);\n if (derivedAddress === normalizedAddress) {\n index = i;\n break;\n }\n }\n if (index === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n }\n\n return `${this.#pairedDevice.hdPath}/${this.#pairedDevice.childrenPath\n .replace('*', index.toString())\n .replace(/\\*/gu, '0')}`;\n }\n\n /**\n * Get a page of addresses derived from the source.\n *\n * @param page - The page number to retrieve\n * @param pageSize - The number of addresses per page\n * @returns An array of IndexedAddress objects, each containing the address and its index\n * @throws Will throw an error if the source is not initialized\n */\n getAddressesPage(page: number, pageSize = 5): IndexedAddress[] {\n const startIndex = page * pageSize;\n const endIndex = startIndex + pageSize;\n const addresses: IndexedAddress[] = [];\n\n for (let i = startIndex; i < endIndex; i++) {\n const address = this.addressFromIndex(i);\n addresses.push({ address, index: i });\n }\n\n return addresses;\n }\n\n /**\n * Retrieve the details of the paired device.\n *\n * @returns Thea paired device details\n */\n getDeviceDetails(): DeviceDetails {\n return this.#pairedDevice;\n }\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n ): Promise<TypedTxData> {\n const dataType =\n transaction.type === TransactionType.Legacy\n ? DataType.transaction\n : DataType.typedTransaction;\n\n const messageToSign = transaction.getMessageToSign();\n const rawTransaction = Buffer.from(\n Array.isArray(messageToSign) ? RLP.encode(messageToSign) : messageToSign,\n );\n\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n rawTransaction,\n dataType,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n Number(transaction.common.chainId()),\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n requestTitle: 'Scan with your hardware wallet',\n requestDescription:\n 'After your device has signed this message, click on \"Scan\" to receive the signature',\n });\n\n return TransactionFactory.fromTxData(\n {\n ...transaction.toJSON(),\n r,\n s,\n v,\n },\n {\n common: transaction.common,\n },\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` v4 Ethereum\n * JSON-RPC method.\n *\n * @param address - The address of the account to use for signing.\n * @param data - The data to sign.\n * @returns The signed message.\n */\n async signTypedData<Types extends MessageTypes>(\n address: Hex,\n data: TypedMessage<Types>,\n ): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(JSON.stringify(data), 'utf8'),\n DataType.typedData,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @returns The signed message.\n */\n async signPersonalMessage(address: Hex, message: Hex): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(remove0x(message), 'hex'),\n DataType.personalMessage,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Derive the device details from a UR string\n *\n * @param ur - The UR string to set the root account from\n * @returns The device details derived from the UR\n */\n #deviceDetailsFromUR(ur: string | SerializedUR): DeviceDetails {\n const source = this.#decodeUR(ur);\n const fingerprint = getFingerprintFromSource(source);\n\n if (source instanceof CryptoAccount) {\n const { name, xfp, paths, keyringAccount } =\n readCryptoAccountOutputDescriptors(source);\n return {\n keyringMode: DeviceMode.ACCOUNT,\n keyringAccount,\n name,\n xfp,\n paths,\n indexes: {},\n };\n }\n\n const { getBip32Key, getOrigin, getChildren, getName, getNote } = source;\n return {\n keyringMode: DeviceMode.HD,\n keyringAccount: getNote(),\n name: getName(),\n xfp: fingerprint,\n hdPath: `m/${getOrigin().getPath()}`,\n childrenPath: getChildren()?.getPath() || DEFAULT_CHILDREN_PATH,\n xpub: getBip32Key(),\n indexes: {},\n };\n }\n\n /**\n * Request a signature for a transaction or message.\n *\n * @param request - The signature request containing the data to sign.\n * @returns The signature as an object containing r, s, and v values.\n */\n async #requestSignature(\n request: QrSignatureRequest,\n ): Promise<{ r: Buffer; s: Buffer; v: Buffer }> {\n const response = await this.#requestScan({\n type: QrScanRequestType.SIGN,\n request,\n });\n const signatureEnvelope = ETHSignature.fromCBOR(\n Buffer.from(response.cbor, 'hex'),\n );\n const signature = signatureEnvelope.getSignature();\n const requestId = signatureEnvelope.getRequestId();\n\n if (!requestId) {\n throw new Error('Signature request ID is missing.');\n }\n\n if (request.requestId !== stringify(requestId)) {\n throw new Error(\n `Signature request ID mismatch. Expected: ${\n request.requestId\n }, received: ${requestId.toString('hex')}`,\n );\n }\n\n return {\n r: signature.subarray(0, 32),\n s: signature.subarray(32, 64),\n v: signature.subarray(64),\n };\n }\n\n /**\n * Decodes a UR\n *\n * @param ur - The UR to decode\n * @returns The decoded CryptoAccount or CryptoHDKey\n * @throws Will throw an error if the UR type is not supported\n */\n #decodeUR(ur: string | SerializedUR): CryptoAccount | CryptoHDKey {\n const decodedUR =\n typeof ur === 'string'\n ? URRegistryDecoder.decode(ur)\n : {\n type: ur.type,\n cbor: Buffer.from(ur.cbor, 'hex'),\n };\n\n const { type, cbor } = decodedUR;\n\n switch (type) {\n case SUPPORTED_UR_TYPE.CRYPTO_HDKEY:\n return CryptoHDKey.fromCBOR(cbor);\n case SUPPORTED_UR_TYPE.CRYPTO_ACCOUNT:\n return CryptoAccount.fromCBOR(cbor);\n default:\n throw new Error('Unsupported UR type');\n }\n }\n}\n"]}
1
+ {"version":3,"file":"device.cjs","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,yCAAsC;AACtC,uCAAqE;AAErE,2CAAmD;AACnD,uEAOwC;AAExC,2CAAsE;AAEtE,kDAA0B;AAC1B,+BAA+C;AAE/C,iDAAiD;AAOpC,QAAA,iBAAiB,GAAG;IAC/B,YAAY,EAAE,cAAc;IAC5B,cAAc,EAAE,gBAAgB;IAChC,aAAa,EAAE,eAAe;CAC/B,CAAC;AAEF,IAAY,UAGX;AAHD,WAAY,UAAU;IACpB,uBAAS,CAAA;IACT,iCAAmB,CAAA;AACrB,CAAC,EAHW,UAAU,0BAAV,UAAU,QAGrB;AAED,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC,MAAM,SAAS,GAAG,IAAK,CAAC;AA+FxB;;;;;GAKG;AACH,SAAS,wBAAwB,CAAC,MAAmC;IACnE,OAAO,MAAM,YAAY,kCAAa;QACpC,CAAC,CAAC,MAAM,CAAC,oBAAoB,EAAE,EAAE,QAAQ,CAAC,KAAK,CAAC;QAChD,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,oBAAoB,EAAE,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClE,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAS,kCAAkC,CAAC,MAAqB;IAM/D,MAAM,WAAW,GAAG,MAAM,CAAC,oBAAoB,EAAE,CAAC;IAElD,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;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ED,IAAI,OAAO,CAAC,SAAS,KAAK,IAAA,gBAAS,EAAC,SAAS,CAAC,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CACb,4CACE,OAAO,CAAC,SACV,eAAe,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAC3C,CAAC;IACJ,CAAC;IAED,OAAO;QACL,CAAC,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC;QAC5B,CAAC,EAAE,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC;QAC7B,CAAC,EAAE,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC;KAC1B,CAAC;AACJ,CAAC,+CASS,EAAyB;IACjC,MAAM,SAAS,GACb,OAAO,EAAE,KAAK,QAAQ;QACpB,CAAC,CAAC,sCAAiB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9B,CAAC,CAAC;YACE,IAAI,EAAE,EAAE,CAAC,IAAI;YACb,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC;SAClC,CAAC;IAER,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,SAAS,CAAC;IAEjC,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,yBAAiB,CAAC,YAAY;YACjC,OAAO,gCAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACpC,KAAK,yBAAiB,CAAC,cAAc;YACnC,OAAO,kCAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACtC;YACE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC3C,CAAC;AACH,CAAC","sourcesContent":["import { RLP } from '@ethereumjs/rlp';\nimport { TransactionType, TransactionFactory } from '@ethereumjs/tx';\nimport type { TypedTransaction, TypedTxData } from '@ethereumjs/tx';\nimport { publicToAddress } from '@ethereumjs/util';\nimport {\n CryptoAccount,\n CryptoHDKey,\n DataType,\n ETHSignature,\n EthSignRequest,\n URRegistryDecoder,\n} from '@keystonehq/bc-ur-registry-eth';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport { add0x, getChecksumAddress, remove0x } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport HdKey from 'hdkey';\nimport { stringify, v4 as uuidv4 } from 'uuid';\n\nimport { QrScanRequestType } from './qr-keyring';\nimport type {\n QrKeyringBridge,\n QrSignatureRequest,\n SerializedUR,\n} from './qr-keyring';\n\nexport const SUPPORTED_UR_TYPE = {\n CRYPTO_HDKEY: 'crypto-hdkey',\n CRYPTO_ACCOUNT: 'crypto-account',\n ETH_SIGNATURE: 'eth-signature',\n};\n\nexport enum DeviceMode {\n HD = 'hd',\n ACCOUNT = 'account',\n}\n\nconst DEFAULT_CHILDREN_PATH = '0/*';\n\nconst MAX_INDEX = 1_000;\n\n/**\n * Common details for the device source, which can be either a CryptoAccount or CryptoHDKey.\n */\nexport type CommonDeviceDetails = {\n /**\n * Value take out from the device note field, if available.\n */\n keyringAccount: string;\n /**\n * The name of the device\n */\n name: string;\n /**\n * The device fingerprint, hex-encoded\n */\n xfp: string;\n /**\n * Indexes of the accounts derived from the device\n * in the form of a map from address to index\n */\n indexes: Record<Hex, number>;\n};\n\n/**\n * Details for the HD mode of the Device. This mode derives\n * accounts from a root public key (xpub) and a derivation path.\n */\nexport type HDModeDeviceDetails = {\n /**\n * The device mode is HD, indicating that it derives accounts from a\n * root public key (xpub) and a derivation path.\n */\n keyringMode: DeviceMode.HD;\n /**\n * The xpub of the HD key\n */\n xpub: string;\n /**\n * The derivation path of the HD key\n */\n hdPath: string;\n /**\n * The path used to derive child accounts\n */\n childrenPath: string;\n};\n\n/**\n * Details for the Account mode of the Device. This mode derives\n * accounts from a set of addresses and their corresponding paths.\n */\nexport type AccountModeDeviceDetails = {\n /**\n * The device mode is ACCOUNT, indicating that it derives accounts from\n * a set of addresses and their corresponding paths.\n */\n keyringMode: DeviceMode.ACCOUNT;\n /**\n * The derivation paths for each hex-encoded address in the device\n */\n paths: Record<Hex, string>;\n};\n\n/**\n * An address with its corresponding index.\n */\nexport type IndexedAddress = {\n address: Hex;\n index: number;\n};\n\n/**\n * The details of the source CryptoAccount or CryptoHDKey\n * that the Device uses to derive accounts.\n */\nexport type DeviceDetails = CommonDeviceDetails &\n (HDModeDeviceDetails | AccountModeDeviceDetails);\n\nexport type DeviceOptions = {\n /**\n * The requestScan function to scan the QR code\n */\n requestScan: QrKeyringBridge['requestScan'];\n /**\n * The source of the device, which can be of type `DeviceDetails`,\n * `string`, or `SerializedUR`.\n *\n * When a `string` or `SerializedUR` is provided, the Device will\n * initialize itself from the UR.\n */\n source: DeviceDetails | string | SerializedUR;\n};\n\n/**\n * Get the fingerprint of the source CryptoAccount or CryptoHDKey\n *\n * @param source - The source CryptoAccount or CryptoHDKey\n * @returns The fingerprint of the source\n */\nfunction getFingerprintFromSource(source: CryptoAccount | CryptoHDKey): string {\n return source instanceof CryptoAccount\n ? source.getMasterFingerprint()?.toString('hex')\n : source.getOrigin()?.getSourceFingerprint()?.toString('hex');\n}\n\n/**\n * Get fingerprint, account paths and names from the a CryptoAccount\n *\n * Note: This function emulates the behavior of the `@keystonehq/base-eth-keyring`\n * library when dealing with CryptoAccount objects (for backwards compatibility\n * reasons). Though, the way it retrieves `name` and `keyringAccount` is questionable,\n * as `name` and `keyringAccount` are updated after each descriptor discovery, effectively\n * returning the last descriptor's `name` and `keyringAccount`.\n *\n * @param source - The source CryptoAccount\n * @returns The paths\n */\nfunction readCryptoAccountOutputDescriptors(source: CryptoAccount): {\n xfp: string;\n paths: Record<Hex, string>;\n name: string;\n keyringAccount: string;\n} {\n const descriptors = source.getOutputDescriptors();\n\n if (!descriptors || descriptors.length === 0) {\n throw new Error('No output descriptors found in CryptoAccount');\n }\n\n let name = '';\n let keyringAccount = '';\n const paths = descriptors.reduce(\n (descriptorsPaths: Record<Hex, string>, current) => {\n const hdKey = current.getHDKey();\n if (hdKey) {\n const path = `M/${hdKey.getOrigin().getPath()}`;\n const address = getChecksumAddress(\n add0x(\n Buffer.from(publicToAddress(hdKey.getKey(), true)).toString('hex'),\n ),\n );\n descriptorsPaths[address] = path;\n name = hdKey.getName();\n keyringAccount = hdKey.getNote();\n }\n return descriptorsPaths;\n },\n {},\n );\n\n return {\n paths,\n name,\n keyringAccount,\n xfp: getFingerprintFromSource(source),\n };\n}\n\nexport class Device {\n readonly #requestScan: QrKeyringBridge['requestScan'];\n\n readonly #pairedDevice: DeviceDetails;\n\n /**\n * Create a new Device instance.\n *\n * @param options - The options for the Device, including the requestScan function\n * and the source of the device details.\n * @param options.requestScan - The function to request a scan of the QR code.\n * @param options.source - The source of the device details, which can be a\n * UR string, a `SerializedUR`, or a `DeviceDetails` object.\n */\n constructor({ requestScan, source }: DeviceOptions) {\n this.#requestScan = requestScan;\n this.#pairedDevice =\n typeof source === 'string' || 'cbor' in source\n ? this.#deviceDetailsFromUR(source)\n : source;\n }\n\n /**\n * Derive an address from the source at a given index\n *\n * @param index - The index to derive the address from\n * @returns The derived address in hex format\n * @throws Will throw an error if the source is not initialized\n */\n addressFromIndex(index: number): Hex {\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const address = Object.keys(this.#pairedDevice.paths)[index];\n if (!address) {\n throw new Error(`Address not found for index ${index}`);\n }\n return add0x(address);\n }\n const childPath = `m/${this.#pairedDevice.childrenPath.replace(\n '*',\n index.toString(),\n )}`;\n\n const hdKey = HdKey.fromExtendedKey(this.#pairedDevice.xpub);\n const childKey = hdKey.derive(childPath);\n\n const address = Buffer.from(\n publicToAddress(childKey.publicKey, true),\n ).toString('hex');\n\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n this.#pairedDevice.indexes[normalizedAddress] = index;\n\n return normalizedAddress;\n }\n\n /**\n * Retrieve the path of an address derived from the source.\n *\n * @param address - The address to retrieve the path for\n * @returns The path of the address\n */\n pathFromAddress(address: Hex): string {\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const path = this.#pairedDevice.paths[normalizedAddress];\n if (path === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n return path;\n }\n\n let index = this.#pairedDevice.indexes[normalizedAddress];\n if (index === undefined) {\n for (let i = 0; i < MAX_INDEX; i++) {\n const derivedAddress = this.addressFromIndex(i);\n if (derivedAddress === normalizedAddress) {\n index = i;\n break;\n }\n }\n if (index === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n }\n\n return `${this.#pairedDevice.hdPath}/${this.#pairedDevice.childrenPath\n .replace('*', index.toString())\n .replace(/\\*/gu, '0')}`;\n }\n\n /**\n * Get a page of addresses derived from the source.\n *\n * @param page - The page number to retrieve\n * @param pageSize - The number of addresses per page\n * @returns An array of IndexedAddress objects, each containing the address and its index\n * @throws Will throw an error if the source is not initialized\n */\n getAddressesPage(page: number, pageSize = 5): IndexedAddress[] {\n const startIndex = page * pageSize;\n const endIndex = startIndex + pageSize;\n const addresses: IndexedAddress[] = [];\n\n for (let i = startIndex; i < endIndex; i++) {\n const address = this.addressFromIndex(i);\n addresses.push({ address, index: i });\n }\n\n return addresses;\n }\n\n /**\n * Retrieve the details of the paired device.\n *\n * @returns Thea paired device details\n */\n getDeviceDetails(): DeviceDetails {\n return this.#pairedDevice;\n }\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n ): Promise<TypedTxData> {\n const dataType =\n transaction.type === TransactionType.Legacy\n ? DataType.transaction\n : DataType.typedTransaction;\n\n const messageToSign = transaction.getMessageToSign();\n const rawTransaction = Buffer.from(\n Array.isArray(messageToSign) ? RLP.encode(messageToSign) : messageToSign,\n );\n\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n rawTransaction,\n dataType,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n Number(transaction.common.chainId()),\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n requestTitle: 'Scan with your hardware wallet',\n requestDescription:\n 'After your device has signed this message, click on \"Scan\" to receive the signature',\n });\n\n return TransactionFactory.fromTxData(\n {\n ...transaction.toJSON(),\n r,\n s,\n v,\n },\n {\n common: transaction.common,\n },\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` v4 Ethereum\n * JSON-RPC method.\n *\n * @param address - The address of the account to use for signing.\n * @param data - The data to sign.\n * @returns The signed message.\n */\n async signTypedData<Types extends MessageTypes>(\n address: Hex,\n data: TypedMessage<Types>,\n ): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(JSON.stringify(data), 'utf8'),\n DataType.typedData,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @returns The signed message.\n */\n async signPersonalMessage(address: Hex, message: Hex): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(remove0x(message), 'hex'),\n DataType.personalMessage,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Derive the device details from a UR string\n *\n * @param ur - The UR string to set the root account from\n * @returns The device details derived from the UR\n */\n #deviceDetailsFromUR(ur: string | SerializedUR): DeviceDetails {\n const source = this.#decodeUR(ur);\n const fingerprint = getFingerprintFromSource(source);\n\n if (source instanceof CryptoAccount) {\n const { name, xfp, paths, keyringAccount } =\n readCryptoAccountOutputDescriptors(source);\n return {\n keyringMode: DeviceMode.ACCOUNT,\n keyringAccount,\n name,\n xfp,\n paths,\n indexes: {},\n };\n }\n\n const { getBip32Key, getOrigin, getChildren, getName, getNote } = source;\n return {\n keyringMode: DeviceMode.HD,\n keyringAccount: getNote(),\n name: getName(),\n xfp: fingerprint,\n hdPath: `m/${getOrigin().getPath()}`,\n childrenPath: getChildren()?.getPath() || DEFAULT_CHILDREN_PATH,\n xpub: getBip32Key(),\n indexes: {},\n };\n }\n\n /**\n * Request a signature for a transaction or message.\n *\n * @param request - The signature request containing the data to sign.\n * @returns The signature as an object containing r, s, and v values.\n */\n async #requestSignature(\n request: QrSignatureRequest,\n ): Promise<{ r: Buffer; s: Buffer; v: Buffer }> {\n const response = await this.#requestScan({\n type: QrScanRequestType.SIGN,\n request,\n });\n const signatureEnvelope = ETHSignature.fromCBOR(\n Buffer.from(response.cbor, 'hex'),\n );\n const signature = signatureEnvelope.getSignature();\n const requestId = signatureEnvelope.getRequestId();\n\n if (!requestId) {\n throw new Error('Signature request ID is missing.');\n }\n\n if (request.requestId !== stringify(requestId)) {\n throw new Error(\n `Signature request ID mismatch. Expected: ${\n request.requestId\n }, received: ${requestId.toString('hex')}`,\n );\n }\n\n return {\n r: signature.subarray(0, 32),\n s: signature.subarray(32, 64),\n v: signature.subarray(64),\n };\n }\n\n /**\n * Decodes a UR\n *\n * @param ur - The UR to decode\n * @returns The decoded CryptoAccount or CryptoHDKey\n * @throws Will throw an error if the UR type is not supported\n */\n #decodeUR(ur: string | SerializedUR): CryptoAccount | CryptoHDKey {\n const decodedUR =\n typeof ur === 'string'\n ? URRegistryDecoder.decode(ur)\n : {\n type: ur.type,\n cbor: Buffer.from(ur.cbor, 'hex'),\n };\n\n const { type, cbor } = decodedUR;\n\n switch (type) {\n case SUPPORTED_UR_TYPE.CRYPTO_HDKEY:\n return CryptoHDKey.fromCBOR(cbor);\n case SUPPORTED_UR_TYPE.CRYPTO_ACCOUNT:\n return CryptoAccount.fromCBOR(cbor);\n default:\n throw new Error('Unsupported UR type');\n }\n }\n}\n"]}
package/dist/device.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- import { type TypedTransaction, type TypedTxData } from "@ethereumjs/tx";
1
+ import type { TypedTransaction, TypedTxData } from "@ethereumjs/tx";
2
2
  import type { MessageTypes, TypedMessage } from "@metamask/eth-sig-util";
3
- import { type Hex } from "@metamask/utils";
4
- import { type QrKeyringBridge, type SerializedUR } from "./qr-keyring.cjs";
3
+ import type { Hex } from "@metamask/utils";
4
+ import type { QrKeyringBridge, SerializedUR } from "./qr-keyring.cjs";
5
5
  export declare const SUPPORTED_UR_TYPE: {
6
6
  CRYPTO_HDKEY: string;
7
7
  CRYPTO_ACCOUNT: string;
@@ -1 +1 @@
1
- {"version":3,"file":"device.d.cts","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,gBAAgB,EAErB,KAAK,WAAW,EAEjB,uBAAuB;AAUxB,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,+BAA+B;AACzE,OAAO,EAAuC,KAAK,GAAG,EAAE,wBAAw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rC;;;;;;;OAOG;IACH,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,SAAI,GAAG,cAAc,EAAE;IAa9D;;;;OAIG;IACH,gBAAgB,IAAI,aAAa;IAIjC;;;;;;;;OAQG;IACG,eAAe,CACnB,OAAO,EAAE,GAAG,EACZ,WAAW,EAAE,gBAAgB,GAC5B,OAAO,CAAC,WAAW,CAAC;IA6CvB;;;;;;;OAOG;IACG,aAAa,CAAC,KAAK,SAAS,YAAY,EAC5C,OAAO,EAAE,GAAG,EACZ,IAAI,EAAE,YAAY,CAAC,KAAK,CAAC,GACxB,OAAO,CAAC,MAAM,CAAC;IA6BlB;;;;;;;;;;;OAWG;IACG,mBAAmB,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;CAkIvE"}
package/dist/device.d.mts CHANGED
@@ -1,7 +1,7 @@
1
- import { type TypedTransaction, type TypedTxData } from "@ethereumjs/tx";
1
+ import type { TypedTransaction, TypedTxData } from "@ethereumjs/tx";
2
2
  import type { MessageTypes, TypedMessage } from "@metamask/eth-sig-util";
3
- import { type Hex } from "@metamask/utils";
4
- import { type QrKeyringBridge, type SerializedUR } from "./qr-keyring.mjs";
3
+ import type { Hex } from "@metamask/utils";
4
+ import type { QrKeyringBridge, SerializedUR } from "./qr-keyring.mjs";
5
5
  export declare const SUPPORTED_UR_TYPE: {
6
6
  CRYPTO_HDKEY: string;
7
7
  CRYPTO_ACCOUNT: string;
@@ -1 +1 @@
1
- {"version":3,"file":"device.d.mts","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,gBAAgB,EAErB,KAAK,WAAW,EAEjB,uBAAuB;AAUxB,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,+BAA+B;AACzE,OAAO,EAAuC,KAAK,GAAG,EAAE,wBAAwB;AAKhF,OAAO,EAEL,KAAK,eAAe,EAEpB,KAAK,YAAY,EA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AAE,QAAQ,SAAI,GAAG,cAAc,EAAE;IAa9D;;;;OAIG;IACH,gBAAgB,IAAI,aAAa;IAIjC;;;;;;;;OAQG;IACG,eAAe,CACnB,OAAO,EAAE,GAAG,EACZ,WAAW,EAAE,gBAAgB,GAC5B,OAAO,CAAC,WAAW,CAAC;IA6CvB;;;;;;;OAOG;IACG,aAAa,CAAC,KAAK,SAAS,YAAY,EAC5C,OAAO,EAAE,GAAG,EACZ,IAAI,EAAE,YAAY,CAAC,KAAK,CAAC,GACxB,OAAO,CAAC,MAAM,CAAC;IA6BlB;;;;;;;;;;;OAWG;IACG,mBAAmB,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;CAkIvE"}
package/dist/device.mjs CHANGED
@@ -21,7 +21,6 @@ import { TransactionType, TransactionFactory } from "@ethereumjs/tx";
21
21
  import { publicToAddress } from "@ethereumjs/util";
22
22
  import { CryptoAccount, CryptoHDKey, DataType, ETHSignature, EthSignRequest, URRegistryDecoder } from "@keystonehq/bc-ur-registry-eth";
23
23
  import { add0x, getChecksumAddress, remove0x } from "@metamask/utils";
24
- // eslint-disable-next-line @typescript-eslint/naming-convention
25
24
  import $HdKey from "hdkey";
26
25
  const HdKey = $importDefault($HdKey);
27
26
  import { stringify, v4 as uuidv4 } from "uuid";
@@ -1 +1 @@
1
- {"version":3,"file":"device.mjs","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,wBAAwB;AACtC,OAAO,EAEL,eAAe,EAEf,kBAAkB,EACnB,uBAAuB;AACxB,OAAO,EAAE,eAAe,EAAE,yBAAyB;AACnD,OAAO,EACL,aAAa,EACb,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,cAAc,EACd,iBAAiB,EAClB,uCAAuC;AAExC,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,QAAQ,EAAY,wBAAwB;AAChF,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AC,QAAQ,CAAC,EAAE,CAAC;KAC1B,CAAC;AACJ,CAAC,+CASS,EAAyB;IACjC,MAAM,SAAS,GACb,OAAO,EAAE,KAAK,QAAQ;QACpB,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9B,CAAC,CAAC;YACE,IAAI,EAAE,EAAE,CAAC,IAAI;YACb,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC;SAClC,CAAC;IAER,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,SAAS,CAAC;IAEjC,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,iBAAiB,CAAC,YAAY;YACjC,OAAO,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACpC,KAAK,iBAAiB,CAAC,cAAc;YACnC,OAAO,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACtC;YACE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC3C,CAAC;AACH,CAAC","sourcesContent":["import { RLP } from '@ethereumjs/rlp';\nimport {\n type TypedTransaction,\n TransactionType,\n type TypedTxData,\n TransactionFactory,\n} from '@ethereumjs/tx';\nimport { publicToAddress } from '@ethereumjs/util';\nimport {\n CryptoAccount,\n CryptoHDKey,\n DataType,\n ETHSignature,\n EthSignRequest,\n URRegistryDecoder,\n} from '@keystonehq/bc-ur-registry-eth';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport { add0x, getChecksumAddress, remove0x, type Hex } from '@metamask/utils';\n// eslint-disable-next-line @typescript-eslint/naming-convention\nimport HdKey from 'hdkey';\nimport { stringify, v4 as uuidv4 } from 'uuid';\n\nimport {\n QrScanRequestType,\n type QrKeyringBridge,\n type QrSignatureRequest,\n type SerializedUR,\n} from './qr-keyring';\n\nexport const SUPPORTED_UR_TYPE = {\n CRYPTO_HDKEY: 'crypto-hdkey',\n CRYPTO_ACCOUNT: 'crypto-account',\n ETH_SIGNATURE: 'eth-signature',\n};\n\nexport enum DeviceMode {\n HD = 'hd',\n ACCOUNT = 'account',\n}\n\nconst DEFAULT_CHILDREN_PATH = '0/*';\n\nconst MAX_INDEX = 1_000;\n\n/**\n * Common details for the device source, which can be either a CryptoAccount or CryptoHDKey.\n */\nexport type CommonDeviceDetails = {\n /**\n * Value take out from the device note field, if available.\n */\n keyringAccount: string;\n /**\n * The name of the device\n */\n name: string;\n /**\n * The device fingerprint, hex-encoded\n */\n xfp: string;\n /**\n * Indexes of the accounts derived from the device\n * in the form of a map from address to index\n */\n indexes: Record<Hex, number>;\n};\n\n/**\n * Details for the HD mode of the Device. This mode derives\n * accounts from a root public key (xpub) and a derivation path.\n */\nexport type HDModeDeviceDetails = {\n /**\n * The device mode is HD, indicating that it derives accounts from a\n * root public key (xpub) and a derivation path.\n */\n keyringMode: DeviceMode.HD;\n /**\n * The xpub of the HD key\n */\n xpub: string;\n /**\n * The derivation path of the HD key\n */\n hdPath: string;\n /**\n * The path used to derive child accounts\n */\n childrenPath: string;\n};\n\n/**\n * Details for the Account mode of the Device. This mode derives\n * accounts from a set of addresses and their corresponding paths.\n */\nexport type AccountModeDeviceDetails = {\n /**\n * The device mode is ACCOUNT, indicating that it derives accounts from\n * a set of addresses and their corresponding paths.\n */\n keyringMode: DeviceMode.ACCOUNT;\n /**\n * The derivation paths for each hex-encoded address in the device\n */\n paths: Record<Hex, string>;\n};\n\n/**\n * An address with its corresponding index.\n */\nexport type IndexedAddress = {\n address: Hex;\n index: number;\n};\n\n/**\n * The details of the source CryptoAccount or CryptoHDKey\n * that the Device uses to derive accounts.\n */\nexport type DeviceDetails = CommonDeviceDetails &\n (HDModeDeviceDetails | AccountModeDeviceDetails);\n\nexport type DeviceOptions = {\n /**\n * The requestScan function to scan the QR code\n */\n requestScan: QrKeyringBridge['requestScan'];\n /**\n * The source of the device, which can be of type `DeviceDetails`,\n * `string`, or `SerializedUR`.\n *\n * When a `string` or `SerializedUR` is provided, the Device will\n * initialize itself from the UR.\n */\n source: DeviceDetails | string | SerializedUR;\n};\n\n/**\n * Get the fingerprint of the source CryptoAccount or CryptoHDKey\n *\n * @param source - The source CryptoAccount or CryptoHDKey\n * @returns The fingerprint of the source\n */\nfunction getFingerprintFromSource(source: CryptoAccount | CryptoHDKey): string {\n return source instanceof CryptoAccount\n ? source.getMasterFingerprint()?.toString('hex')\n : source.getOrigin()?.getSourceFingerprint()?.toString('hex');\n}\n\n/**\n * Get fingerprint, account paths and names from the a CryptoAccount\n *\n * Note: This function emulates the behavior of the `@keystonehq/base-eth-keyring`\n * library when dealing with CryptoAccount objects (for backwards compatibility\n * reasons). Though, the way it retrieves `name` and `keyringAccount` is questionable,\n * as `name` and `keyringAccount` are updated after each descriptor discovery, effectively\n * returning the last descriptor's `name` and `keyringAccount`.\n *\n * @param source - The source CryptoAccount\n * @returns The paths\n */\nfunction readCryptoAccountOutputDescriptors(source: CryptoAccount): {\n xfp: string;\n paths: Record<Hex, string>;\n name: string;\n keyringAccount: string;\n} {\n const descriptors = source.getOutputDescriptors();\n\n if (!descriptors || descriptors.length === 0) {\n throw new Error('No output descriptors found in CryptoAccount');\n }\n\n let name = '';\n let keyringAccount = '';\n const paths = descriptors.reduce(\n (descriptorsPaths: Record<Hex, string>, current) => {\n const hdKey = current.getHDKey();\n if (hdKey) {\n const path = `M/${hdKey.getOrigin().getPath()}`;\n const address = getChecksumAddress(\n add0x(\n Buffer.from(publicToAddress(hdKey.getKey(), true)).toString('hex'),\n ),\n );\n descriptorsPaths[address] = path;\n name = hdKey.getName();\n keyringAccount = hdKey.getNote();\n }\n return descriptorsPaths;\n },\n {},\n );\n\n return {\n paths,\n name,\n keyringAccount,\n xfp: getFingerprintFromSource(source),\n };\n}\n\nexport class Device {\n readonly #requestScan: QrKeyringBridge['requestScan'];\n\n readonly #pairedDevice: DeviceDetails;\n\n /**\n * Create a new Device instance.\n *\n * @param options - The options for the Device, including the requestScan function\n * and the source of the device details.\n * @param options.requestScan - The function to request a scan of the QR code.\n * @param options.source - The source of the device details, which can be a\n * UR string, a `SerializedUR`, or a `DeviceDetails` object.\n */\n constructor({ requestScan, source }: DeviceOptions) {\n this.#requestScan = requestScan;\n this.#pairedDevice =\n typeof source === 'string' || 'cbor' in source\n ? this.#deviceDetailsFromUR(source)\n : source;\n }\n\n /**\n * Derive an address from the source at a given index\n *\n * @param index - The index to derive the address from\n * @returns The derived address in hex format\n * @throws Will throw an error if the source is not initialized\n */\n addressFromIndex(index: number): Hex {\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const address = Object.keys(this.#pairedDevice.paths)[index];\n if (!address) {\n throw new Error(`Address not found for index ${index}`);\n }\n return add0x(address);\n }\n const childPath = `m/${this.#pairedDevice.childrenPath.replace(\n '*',\n index.toString(),\n )}`;\n\n const hdKey = HdKey.fromExtendedKey(this.#pairedDevice.xpub);\n const childKey = hdKey.derive(childPath);\n\n const address = Buffer.from(\n publicToAddress(childKey.publicKey, true),\n ).toString('hex');\n\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n this.#pairedDevice.indexes[normalizedAddress] = index;\n\n return normalizedAddress;\n }\n\n /**\n * Retrieve the path of an address derived from the source.\n *\n * @param address - The address to retrieve the path for\n * @returns The path of the address\n */\n pathFromAddress(address: Hex): string {\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const path = this.#pairedDevice.paths[normalizedAddress];\n if (path === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n return path;\n }\n\n let index = this.#pairedDevice.indexes[normalizedAddress];\n if (index === undefined) {\n for (let i = 0; i < MAX_INDEX; i++) {\n const derivedAddress = this.addressFromIndex(i);\n if (derivedAddress === normalizedAddress) {\n index = i;\n break;\n }\n }\n if (index === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n }\n\n return `${this.#pairedDevice.hdPath}/${this.#pairedDevice.childrenPath\n .replace('*', index.toString())\n .replace(/\\*/gu, '0')}`;\n }\n\n /**\n * Get a page of addresses derived from the source.\n *\n * @param page - The page number to retrieve\n * @param pageSize - The number of addresses per page\n * @returns An array of IndexedAddress objects, each containing the address and its index\n * @throws Will throw an error if the source is not initialized\n */\n getAddressesPage(page: number, pageSize = 5): IndexedAddress[] {\n const startIndex = page * pageSize;\n const endIndex = startIndex + pageSize;\n const addresses: IndexedAddress[] = [];\n\n for (let i = startIndex; i < endIndex; i++) {\n const address = this.addressFromIndex(i);\n addresses.push({ address, index: i });\n }\n\n return addresses;\n }\n\n /**\n * Retrieve the details of the paired device.\n *\n * @returns Thea paired device details\n */\n getDeviceDetails(): DeviceDetails {\n return this.#pairedDevice;\n }\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n ): Promise<TypedTxData> {\n const dataType =\n transaction.type === TransactionType.Legacy\n ? DataType.transaction\n : DataType.typedTransaction;\n\n const messageToSign = transaction.getMessageToSign();\n const rawTransaction = Buffer.from(\n Array.isArray(messageToSign) ? RLP.encode(messageToSign) : messageToSign,\n );\n\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n rawTransaction,\n dataType,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n Number(transaction.common.chainId()),\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n requestTitle: 'Scan with your hardware wallet',\n requestDescription:\n 'After your device has signed this message, click on \"Scan\" to receive the signature',\n });\n\n return TransactionFactory.fromTxData(\n {\n ...transaction.toJSON(),\n r,\n s,\n v,\n },\n {\n common: transaction.common,\n },\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` v4 Ethereum\n * JSON-RPC method.\n *\n * @param address - The address of the account to use for signing.\n * @param data - The data to sign.\n * @returns The signed message.\n */\n async signTypedData<Types extends MessageTypes>(\n address: Hex,\n data: TypedMessage<Types>,\n ): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(JSON.stringify(data), 'utf8'),\n DataType.typedData,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @returns The signed message.\n */\n async signPersonalMessage(address: Hex, message: Hex): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(remove0x(message), 'hex'),\n DataType.personalMessage,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Derive the device details from a UR string\n *\n * @param ur - The UR string to set the root account from\n * @returns The device details derived from the UR\n */\n #deviceDetailsFromUR(ur: string | SerializedUR): DeviceDetails {\n const source = this.#decodeUR(ur);\n const fingerprint = getFingerprintFromSource(source);\n\n if (source instanceof CryptoAccount) {\n const { name, xfp, paths, keyringAccount } =\n readCryptoAccountOutputDescriptors(source);\n return {\n keyringMode: DeviceMode.ACCOUNT,\n keyringAccount,\n name,\n xfp,\n paths,\n indexes: {},\n };\n }\n\n const { getBip32Key, getOrigin, getChildren, getName, getNote } = source;\n return {\n keyringMode: DeviceMode.HD,\n keyringAccount: getNote(),\n name: getName(),\n xfp: fingerprint,\n hdPath: `m/${getOrigin().getPath()}`,\n childrenPath: getChildren()?.getPath() || DEFAULT_CHILDREN_PATH,\n xpub: getBip32Key(),\n indexes: {},\n };\n }\n\n /**\n * Request a signature for a transaction or message.\n *\n * @param request - The signature request containing the data to sign.\n * @returns The signature as an object containing r, s, and v values.\n */\n async #requestSignature(\n request: QrSignatureRequest,\n ): Promise<{ r: Buffer; s: Buffer; v: Buffer }> {\n const response = await this.#requestScan({\n type: QrScanRequestType.SIGN,\n request,\n });\n const signatureEnvelope = ETHSignature.fromCBOR(\n Buffer.from(response.cbor, 'hex'),\n );\n const signature = signatureEnvelope.getSignature();\n const requestId = signatureEnvelope.getRequestId();\n\n if (!requestId) {\n throw new Error('Signature request ID is missing.');\n }\n\n if (request.requestId !== stringify(requestId)) {\n throw new Error(\n `Signature request ID mismatch. Expected: ${\n request.requestId\n }, received: ${requestId.toString('hex')}`,\n );\n }\n\n return {\n r: signature.subarray(0, 32),\n s: signature.subarray(32, 64),\n v: signature.subarray(64),\n };\n }\n\n /**\n * Decodes a UR\n *\n * @param ur - The UR to decode\n * @returns The decoded CryptoAccount or CryptoHDKey\n * @throws Will throw an error if the UR type is not supported\n */\n #decodeUR(ur: string | SerializedUR): CryptoAccount | CryptoHDKey {\n const decodedUR =\n typeof ur === 'string'\n ? URRegistryDecoder.decode(ur)\n : {\n type: ur.type,\n cbor: Buffer.from(ur.cbor, 'hex'),\n };\n\n const { type, cbor } = decodedUR;\n\n switch (type) {\n case SUPPORTED_UR_TYPE.CRYPTO_HDKEY:\n return CryptoHDKey.fromCBOR(cbor);\n case SUPPORTED_UR_TYPE.CRYPTO_ACCOUNT:\n return CryptoAccount.fromCBOR(cbor);\n default:\n throw new Error('Unsupported UR type');\n }\n }\n}\n"]}
1
+ {"version":3,"file":"device.mjs","sourceRoot":"","sources":["../src/device.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,wBAAwB;AACtC,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,uBAAuB;AAErE,OAAO,EAAE,eAAe,EAAE,yBAAyB;AACnD,OAAO,EACL,aAAa,EACb,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,cAAc,EACd,iBAAiB,EAClB,uCAAuC;AAExC,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,QAAQ,EAAE,wBAAwB;AAEtE,OAAO,MAAK,cAAc;;AAC1B,OAAO,EAAE,SAAS,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAE/C,OAAO,EAAE,iBAAiB,EAAE,yBAAqB;AAOjD,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,YAAY,EAAE,cAAc;IAC5B,cAAc,EAAE,gBAAgB;IAChC,aAAa,EAAE,eAAe;CAC/B,CAAC;AAEF,MAAM,CAAN,IAAY,UAGX;AAHD,WAAY,UAAU;IACpB,uBAAS,CAAA;IACT,iCAAmB,CAAA;AACrB,CAAC,EAHW,UAAU,KAAV,UAAU,QAGrB;AAED,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC,MAAM,SAAS,GAAG,IAAK,CAAC;AA+FxB;;;;;GAKG;AACH,SAAS,wBAAwB,CAAC,MAAmC;IACnE,OAAO,MAAM,YAAY,aAAa;QACpC,CAAC,CAAC,MAAM,CAAC,oBAAoB,EAAE,EAAE,QAAQ,CAAC,KAAK,CAAC;QAChD,CAAC,CAAC,MAAM,CAAC,S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sourcesContent":["import { RLP } from '@ethereumjs/rlp';\nimport { TransactionType, TransactionFactory } from '@ethereumjs/tx';\nimport type { TypedTransaction, TypedTxData } from '@ethereumjs/tx';\nimport { publicToAddress } from '@ethereumjs/util';\nimport {\n CryptoAccount,\n CryptoHDKey,\n DataType,\n ETHSignature,\n EthSignRequest,\n URRegistryDecoder,\n} from '@keystonehq/bc-ur-registry-eth';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport { add0x, getChecksumAddress, remove0x } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport HdKey from 'hdkey';\nimport { stringify, v4 as uuidv4 } from 'uuid';\n\nimport { QrScanRequestType } from './qr-keyring';\nimport type {\n QrKeyringBridge,\n QrSignatureRequest,\n SerializedUR,\n} from './qr-keyring';\n\nexport const SUPPORTED_UR_TYPE = {\n CRYPTO_HDKEY: 'crypto-hdkey',\n CRYPTO_ACCOUNT: 'crypto-account',\n ETH_SIGNATURE: 'eth-signature',\n};\n\nexport enum DeviceMode {\n HD = 'hd',\n ACCOUNT = 'account',\n}\n\nconst DEFAULT_CHILDREN_PATH = '0/*';\n\nconst MAX_INDEX = 1_000;\n\n/**\n * Common details for the device source, which can be either a CryptoAccount or CryptoHDKey.\n */\nexport type CommonDeviceDetails = {\n /**\n * Value take out from the device note field, if available.\n */\n keyringAccount: string;\n /**\n * The name of the device\n */\n name: string;\n /**\n * The device fingerprint, hex-encoded\n */\n xfp: string;\n /**\n * Indexes of the accounts derived from the device\n * in the form of a map from address to index\n */\n indexes: Record<Hex, number>;\n};\n\n/**\n * Details for the HD mode of the Device. This mode derives\n * accounts from a root public key (xpub) and a derivation path.\n */\nexport type HDModeDeviceDetails = {\n /**\n * The device mode is HD, indicating that it derives accounts from a\n * root public key (xpub) and a derivation path.\n */\n keyringMode: DeviceMode.HD;\n /**\n * The xpub of the HD key\n */\n xpub: string;\n /**\n * The derivation path of the HD key\n */\n hdPath: string;\n /**\n * The path used to derive child accounts\n */\n childrenPath: string;\n};\n\n/**\n * Details for the Account mode of the Device. This mode derives\n * accounts from a set of addresses and their corresponding paths.\n */\nexport type AccountModeDeviceDetails = {\n /**\n * The device mode is ACCOUNT, indicating that it derives accounts from\n * a set of addresses and their corresponding paths.\n */\n keyringMode: DeviceMode.ACCOUNT;\n /**\n * The derivation paths for each hex-encoded address in the device\n */\n paths: Record<Hex, string>;\n};\n\n/**\n * An address with its corresponding index.\n */\nexport type IndexedAddress = {\n address: Hex;\n index: number;\n};\n\n/**\n * The details of the source CryptoAccount or CryptoHDKey\n * that the Device uses to derive accounts.\n */\nexport type DeviceDetails = CommonDeviceDetails &\n (HDModeDeviceDetails | AccountModeDeviceDetails);\n\nexport type DeviceOptions = {\n /**\n * The requestScan function to scan the QR code\n */\n requestScan: QrKeyringBridge['requestScan'];\n /**\n * The source of the device, which can be of type `DeviceDetails`,\n * `string`, or `SerializedUR`.\n *\n * When a `string` or `SerializedUR` is provided, the Device will\n * initialize itself from the UR.\n */\n source: DeviceDetails | string | SerializedUR;\n};\n\n/**\n * Get the fingerprint of the source CryptoAccount or CryptoHDKey\n *\n * @param source - The source CryptoAccount or CryptoHDKey\n * @returns The fingerprint of the source\n */\nfunction getFingerprintFromSource(source: CryptoAccount | CryptoHDKey): string {\n return source instanceof CryptoAccount\n ? source.getMasterFingerprint()?.toString('hex')\n : source.getOrigin()?.getSourceFingerprint()?.toString('hex');\n}\n\n/**\n * Get fingerprint, account paths and names from the a CryptoAccount\n *\n * Note: This function emulates the behavior of the `@keystonehq/base-eth-keyring`\n * library when dealing with CryptoAccount objects (for backwards compatibility\n * reasons). Though, the way it retrieves `name` and `keyringAccount` is questionable,\n * as `name` and `keyringAccount` are updated after each descriptor discovery, effectively\n * returning the last descriptor's `name` and `keyringAccount`.\n *\n * @param source - The source CryptoAccount\n * @returns The paths\n */\nfunction readCryptoAccountOutputDescriptors(source: CryptoAccount): {\n xfp: string;\n paths: Record<Hex, string>;\n name: string;\n keyringAccount: string;\n} {\n const descriptors = source.getOutputDescriptors();\n\n if (!descriptors || descriptors.length === 0) {\n throw new Error('No output descriptors found in CryptoAccount');\n }\n\n let name = '';\n let keyringAccount = '';\n const paths = descriptors.reduce(\n (descriptorsPaths: Record<Hex, string>, current) => {\n const hdKey = current.getHDKey();\n if (hdKey) {\n const path = `M/${hdKey.getOrigin().getPath()}`;\n const address = getChecksumAddress(\n add0x(\n Buffer.from(publicToAddress(hdKey.getKey(), true)).toString('hex'),\n ),\n );\n descriptorsPaths[address] = path;\n name = hdKey.getName();\n keyringAccount = hdKey.getNote();\n }\n return descriptorsPaths;\n },\n {},\n );\n\n return {\n paths,\n name,\n keyringAccount,\n xfp: getFingerprintFromSource(source),\n };\n}\n\nexport class Device {\n readonly #requestScan: QrKeyringBridge['requestScan'];\n\n readonly #pairedDevice: DeviceDetails;\n\n /**\n * Create a new Device instance.\n *\n * @param options - The options for the Device, including the requestScan function\n * and the source of the device details.\n * @param options.requestScan - The function to request a scan of the QR code.\n * @param options.source - The source of the device details, which can be a\n * UR string, a `SerializedUR`, or a `DeviceDetails` object.\n */\n constructor({ requestScan, source }: DeviceOptions) {\n this.#requestScan = requestScan;\n this.#pairedDevice =\n typeof source === 'string' || 'cbor' in source\n ? this.#deviceDetailsFromUR(source)\n : source;\n }\n\n /**\n * Derive an address from the source at a given index\n *\n * @param index - The index to derive the address from\n * @returns The derived address in hex format\n * @throws Will throw an error if the source is not initialized\n */\n addressFromIndex(index: number): Hex {\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const address = Object.keys(this.#pairedDevice.paths)[index];\n if (!address) {\n throw new Error(`Address not found for index ${index}`);\n }\n return add0x(address);\n }\n const childPath = `m/${this.#pairedDevice.childrenPath.replace(\n '*',\n index.toString(),\n )}`;\n\n const hdKey = HdKey.fromExtendedKey(this.#pairedDevice.xpub);\n const childKey = hdKey.derive(childPath);\n\n const address = Buffer.from(\n publicToAddress(childKey.publicKey, true),\n ).toString('hex');\n\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n this.#pairedDevice.indexes[normalizedAddress] = index;\n\n return normalizedAddress;\n }\n\n /**\n * Retrieve the path of an address derived from the source.\n *\n * @param address - The address to retrieve the path for\n * @returns The path of the address\n */\n pathFromAddress(address: Hex): string {\n const normalizedAddress = getChecksumAddress(add0x(address));\n\n if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {\n const path = this.#pairedDevice.paths[normalizedAddress];\n if (path === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n return path;\n }\n\n let index = this.#pairedDevice.indexes[normalizedAddress];\n if (index === undefined) {\n for (let i = 0; i < MAX_INDEX; i++) {\n const derivedAddress = this.addressFromIndex(i);\n if (derivedAddress === normalizedAddress) {\n index = i;\n break;\n }\n }\n if (index === undefined) {\n throw new Error(`Unknown address ${normalizedAddress}`);\n }\n }\n\n return `${this.#pairedDevice.hdPath}/${this.#pairedDevice.childrenPath\n .replace('*', index.toString())\n .replace(/\\*/gu, '0')}`;\n }\n\n /**\n * Get a page of addresses derived from the source.\n *\n * @param page - The page number to retrieve\n * @param pageSize - The number of addresses per page\n * @returns An array of IndexedAddress objects, each containing the address and its index\n * @throws Will throw an error if the source is not initialized\n */\n getAddressesPage(page: number, pageSize = 5): IndexedAddress[] {\n const startIndex = page * pageSize;\n const endIndex = startIndex + pageSize;\n const addresses: IndexedAddress[] = [];\n\n for (let i = startIndex; i < endIndex; i++) {\n const address = this.addressFromIndex(i);\n addresses.push({ address, index: i });\n }\n\n return addresses;\n }\n\n /**\n * Retrieve the details of the paired device.\n *\n * @returns Thea paired device details\n */\n getDeviceDetails(): DeviceDetails {\n return this.#pairedDevice;\n }\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n ): Promise<TypedTxData> {\n const dataType =\n transaction.type === TransactionType.Legacy\n ? DataType.transaction\n : DataType.typedTransaction;\n\n const messageToSign = transaction.getMessageToSign();\n const rawTransaction = Buffer.from(\n Array.isArray(messageToSign) ? RLP.encode(messageToSign) : messageToSign,\n );\n\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n rawTransaction,\n dataType,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n Number(transaction.common.chainId()),\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n requestTitle: 'Scan with your hardware wallet',\n requestDescription:\n 'After your device has signed this message, click on \"Scan\" to receive the signature',\n });\n\n return TransactionFactory.fromTxData(\n {\n ...transaction.toJSON(),\n r,\n s,\n v,\n },\n {\n common: transaction.common,\n },\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` v4 Ethereum\n * JSON-RPC method.\n *\n * @param address - The address of the account to use for signing.\n * @param data - The data to sign.\n * @returns The signed message.\n */\n async signTypedData<Types extends MessageTypes>(\n address: Hex,\n data: TypedMessage<Types>,\n ): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(JSON.stringify(data), 'utf8'),\n DataType.typedData,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @returns The signed message.\n */\n async signPersonalMessage(address: Hex, message: Hex): Promise<string> {\n const requestId = uuidv4();\n const ethSignRequestUR = EthSignRequest.constructETHRequest(\n Buffer.from(remove0x(message), 'hex'),\n DataType.personalMessage,\n this.pathFromAddress(address),\n this.#pairedDevice.xfp,\n requestId,\n undefined,\n address,\n ).toUR();\n\n const { r, s, v } = await this.#requestSignature({\n requestId,\n payload: {\n type: ethSignRequestUR.type,\n cbor: ethSignRequestUR.cbor.toString('hex'),\n },\n });\n\n return add0x(\n Buffer.concat([\n Uint8Array.from(r),\n Uint8Array.from(s),\n Uint8Array.from(v),\n ]).toString('hex'),\n );\n }\n\n /**\n * Derive the device details from a UR string\n *\n * @param ur - The UR string to set the root account from\n * @returns The device details derived from the UR\n */\n #deviceDetailsFromUR(ur: string | SerializedUR): DeviceDetails {\n const source = this.#decodeUR(ur);\n const fingerprint = getFingerprintFromSource(source);\n\n if (source instanceof CryptoAccount) {\n const { name, xfp, paths, keyringAccount } =\n readCryptoAccountOutputDescriptors(source);\n return {\n keyringMode: DeviceMode.ACCOUNT,\n keyringAccount,\n name,\n xfp,\n paths,\n indexes: {},\n };\n }\n\n const { getBip32Key, getOrigin, getChildren, getName, getNote } = source;\n return {\n keyringMode: DeviceMode.HD,\n keyringAccount: getNote(),\n name: getName(),\n xfp: fingerprint,\n hdPath: `m/${getOrigin().getPath()}`,\n childrenPath: getChildren()?.getPath() || DEFAULT_CHILDREN_PATH,\n xpub: getBip32Key(),\n indexes: {},\n };\n }\n\n /**\n * Request a signature for a transaction or message.\n *\n * @param request - The signature request containing the data to sign.\n * @returns The signature as an object containing r, s, and v values.\n */\n async #requestSignature(\n request: QrSignatureRequest,\n ): Promise<{ r: Buffer; s: Buffer; v: Buffer }> {\n const response = await this.#requestScan({\n type: QrScanRequestType.SIGN,\n request,\n });\n const signatureEnvelope = ETHSignature.fromCBOR(\n Buffer.from(response.cbor, 'hex'),\n );\n const signature = signatureEnvelope.getSignature();\n const requestId = signatureEnvelope.getRequestId();\n\n if (!requestId) {\n throw new Error('Signature request ID is missing.');\n }\n\n if (request.requestId !== stringify(requestId)) {\n throw new Error(\n `Signature request ID mismatch. Expected: ${\n request.requestId\n }, received: ${requestId.toString('hex')}`,\n );\n }\n\n return {\n r: signature.subarray(0, 32),\n s: signature.subarray(32, 64),\n v: signature.subarray(64),\n };\n }\n\n /**\n * Decodes a UR\n *\n * @param ur - The UR to decode\n * @returns The decoded CryptoAccount or CryptoHDKey\n * @throws Will throw an error if the UR type is not supported\n */\n #decodeUR(ur: string | SerializedUR): CryptoAccount | CryptoHDKey {\n const decodedUR =\n typeof ur === 'string'\n ? URRegistryDecoder.decode(ur)\n : {\n type: ur.type,\n cbor: Buffer.from(ur.cbor, 'hex'),\n };\n\n const { type, cbor } = decodedUR;\n\n switch (type) {\n case SUPPORTED_UR_TYPE.CRYPTO_HDKEY:\n return CryptoHDKey.fromCBOR(cbor);\n case SUPPORTED_UR_TYPE.CRYPTO_ACCOUNT:\n return CryptoAccount.fromCBOR(cbor);\n default:\n throw new Error('Unsupported UR type');\n }\n }\n}\n"]}
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QrKeyringV2 = exports.QrScanRequestType = exports.QR_KEYRING_TYPE = exports.QrKeyring = exports.QrKeyringDeferredPromiseBridge = exports.QrKeyringScannerBridge = void 0;
3
+ exports.QrScanRequestType = exports.QR_KEYRING_TYPE = exports.QrKeyring = exports.QrKeyringDeferredPromiseBridge = exports.QrKeyringScannerBridge = void 0;
4
4
  var qr_keyring_scanner_bridge_1 = require("./qr-keyring-scanner-bridge.cjs");
5
5
  Object.defineProperty(exports, "QrKeyringScannerBridge", { enumerable: true, get: function () { return qr_keyring_scanner_bridge_1.QrKeyringScannerBridge; } });
6
6
  var qr_keyring_deferred_promise_bridge_1 = require("./qr-keyring-deferred-promise-bridge.cjs");
@@ -9,6 +9,4 @@ var qr_keyring_1 = require("./qr-keyring.cjs");
9
9
  Object.defineProperty(exports, "QrKeyring", { enumerable: true, get: function () { return qr_keyring_1.QrKeyring; } });
10
10
  Object.defineProperty(exports, "QR_KEYRING_TYPE", { enumerable: true, get: function () { return qr_keyring_1.QR_KEYRING_TYPE; } });
11
11
  Object.defineProperty(exports, "QrScanRequestType", { enumerable: true, get: function () { return qr_keyring_1.QrScanRequestType; } });
12
- var qr_keyring_v2_1 = require("./qr-keyring-v2.cjs");
13
- Object.defineProperty(exports, "QrKeyringV2", { enumerable: true, get: function () { return qr_keyring_v2_1.QrKeyringV2; } });
14
12
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,6EAGqC;AAFnC,mIAAA,sBAAsB,OAAA;AAGxB,+FAG8C;AAF5C,oJAAA,8BAA8B,OAAA;AAGhC,+CAUsB;AATpB,uGAAA,SAAS,OAAA;AACT,6GAAA,eAAe,OAAA;AACf,+GAAA,iBAAiB,OAAA;AAQnB,qDAIyB;AAHvB,4GAAA,WAAW,OAAA","sourcesContent":["export {\n QrKeyringScannerBridge,\n type QrKeyringScannerBridgeOptions,\n} from './qr-keyring-scanner-bridge';\nexport {\n QrKeyringDeferredPromiseBridge,\n type QrKeyringDeferredPromiseBridgeOptions,\n} from './qr-keyring-deferred-promise-bridge';\nexport {\n QrKeyring,\n QR_KEYRING_TYPE,\n QrScanRequestType,\n type QrScanRequest,\n type QrKeyringBridge,\n type QrKeyringOptions,\n type QrSignatureRequest,\n type SerializedQrKeyringState,\n type SerializedUR,\n} from './qr-keyring';\nexport {\n QrKeyringV2,\n type QrKeyringV2Options,\n type QrAccountModeCreateOptions,\n} from './qr-keyring-v2';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,6EAGqC;AAFnC,mIAAA,sBAAsB,OAAA;AAGxB,+FAG8C;AAF5C,oJAAA,8BAA8B,OAAA;AAGhC,+CAUsB;AATpB,uGAAA,SAAS,OAAA;AACT,6GAAA,eAAe,OAAA;AACf,+GAAA,iBAAiB,OAAA","sourcesContent":["export {\n QrKeyringScannerBridge,\n type QrKeyringScannerBridgeOptions,\n} from './qr-keyring-scanner-bridge';\nexport {\n QrKeyringDeferredPromiseBridge,\n type QrKeyringDeferredPromiseBridgeOptions,\n} from './qr-keyring-deferred-promise-bridge';\nexport {\n QrKeyring,\n QR_KEYRING_TYPE,\n QrScanRequestType,\n type QrScanRequest,\n type QrKeyringBridge,\n type QrKeyringOptions,\n type QrSignatureRequest,\n type SerializedQrKeyringState,\n type SerializedUR,\n} from './qr-keyring';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { QrKeyringScannerBridge, type QrKeyringScannerBridgeOptions, } from "./qr-keyring-scanner-bridge.cjs";
2
2
  export { QrKeyringDeferredPromiseBridge, type QrKeyringDeferredPromiseBridgeOptions, } from "./qr-keyring-deferred-promise-bridge.cjs";
3
3
  export { QrKeyring, QR_KEYRING_TYPE, QrScanRequestType, type QrScanRequest, type QrKeyringBridge, type QrKeyringOptions, type QrSignatureRequest, type SerializedQrKeyringState, type SerializedUR, } from "./qr-keyring.cjs";
4
- export { QrKeyringV2, type QrKeyringV2Options, type QrAccountModeCreateOptions, } from "./qr-keyring-v2.cjs";
5
4
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,KAAK,6BAA6B,GACnC,wCAAoC;AACrC,OAAO,EACL,8BAA8B,EAC9B,KAAK,qCAAqC,GAC3C,iDAA6C;AAC9C,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,EAC7B,KAAK,YAAY,GAClB,yBAAqB;AACtB,OAAO,EACL,WAAW,EACX,KAAK,kBAAkB,EACvB,KAAK,0BAA0B,GAChC,4BAAwB"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,KAAK,6BAA6B,GACnC,wCAAoC;AACrC,OAAO,EACL,8BAA8B,EAC9B,KAAK,qCAAqC,GAC3C,iDAA6C;AAC9C,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,EAC7B,KAAK,YAAY,GAClB,yBAAqB"}
package/dist/index.d.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { QrKeyringScannerBridge, type QrKeyringScannerBridgeOptions, } from "./qr-keyring-scanner-bridge.mjs";
2
2
  export { QrKeyringDeferredPromiseBridge, type QrKeyringDeferredPromiseBridgeOptions, } from "./qr-keyring-deferred-promise-bridge.mjs";
3
3
  export { QrKeyring, QR_KEYRING_TYPE, QrScanRequestType, type QrScanRequest, type QrKeyringBridge, type QrKeyringOptions, type QrSignatureRequest, type SerializedQrKeyringState, type SerializedUR, } from "./qr-keyring.mjs";
4
- export { QrKeyringV2, type QrKeyringV2Options, type QrAccountModeCreateOptions, } from "./qr-keyring-v2.mjs";
5
4
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,KAAK,6BAA6B,GACnC,wCAAoC;AACrC,OAAO,EACL,8BAA8B,EAC9B,KAAK,qCAAqC,GAC3C,iDAA6C;AAC9C,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,EAC7B,KAAK,YAAY,GAClB,yBAAqB;AACtB,OAAO,EACL,WAAW,EACX,KAAK,kBAAkB,EACvB,KAAK,0BAA0B,GAChC,4BAAwB"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,KAAK,6BAA6B,GACnC,wCAAoC;AACrC,OAAO,EACL,8BAA8B,EAC9B,KAAK,qCAAqC,GAC3C,iDAA6C;AAC9C,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,EAC7B,KAAK,YAAY,GAClB,yBAAqB"}
package/dist/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  export { QrKeyringScannerBridge } from "./qr-keyring-scanner-bridge.mjs";
2
2
  export { QrKeyringDeferredPromiseBridge } from "./qr-keyring-deferred-promise-bridge.mjs";
3
3
  export { QrKeyring, QR_KEYRING_TYPE, QrScanRequestType } from "./qr-keyring.mjs";
4
- export { QrKeyringV2 } from "./qr-keyring-v2.mjs";
5
4
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EAEvB,wCAAoC;AACrC,OAAO,EACL,8BAA8B,EAE/B,iDAA6C;AAC9C,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EAOlB,yBAAqB;AACtB,OAAO,EACL,WAAW,EAGZ,4BAAwB","sourcesContent":["export {\n QrKeyringScannerBridge,\n type QrKeyringScannerBridgeOptions,\n} from './qr-keyring-scanner-bridge';\nexport {\n QrKeyringDeferredPromiseBridge,\n type QrKeyringDeferredPromiseBridgeOptions,\n} from './qr-keyring-deferred-promise-bridge';\nexport {\n QrKeyring,\n QR_KEYRING_TYPE,\n QrScanRequestType,\n type QrScanRequest,\n type QrKeyringBridge,\n type QrKeyringOptions,\n type QrSignatureRequest,\n type SerializedQrKeyringState,\n type SerializedUR,\n} from './qr-keyring';\nexport {\n QrKeyringV2,\n type QrKeyringV2Options,\n type QrAccountModeCreateOptions,\n} from './qr-keyring-v2';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EAEvB,wCAAoC;AACrC,OAAO,EACL,8BAA8B,EAE/B,iDAA6C;AAC9C,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EAOlB,yBAAqB","sourcesContent":["export {\n QrKeyringScannerBridge,\n type QrKeyringScannerBridgeOptions,\n} from './qr-keyring-scanner-bridge';\nexport {\n QrKeyringDeferredPromiseBridge,\n type QrKeyringDeferredPromiseBridgeOptions,\n} from './qr-keyring-deferred-promise-bridge';\nexport {\n QrKeyring,\n QR_KEYRING_TYPE,\n QrScanRequestType,\n type QrScanRequest,\n type QrKeyringBridge,\n type QrKeyringOptions,\n type QrSignatureRequest,\n type SerializedQrKeyringState,\n type SerializedUR,\n} from './qr-keyring';\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-deferred-promise-bridge.cjs","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8E;AAC9E,6CAAoC;AA0BpC;;;GAGG;AACH,MAAa,8BAA8B;IAWzC,YAAY,EACV,eAAe,EACf,cAAc,EACd,cAAc,MAC2B,EAAE;QAdpC,+CAAQ,IAAI,mBAAK,EAAE,EAAC;QAEpB,kEAAkE;QAElE,iEAA+D;QAE/D,iEAAuD;QAEhE,8DAAo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ent":["import { type DeferredPromise, createDeferredPromise } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type {\n QrKeyringBridge,\n QrScanRequest,\n SerializedUR,\n} from './qr-keyring';\n\nexport type QrKeyringDeferredPromiseBridgeOptions = {\n /**\n * Callback invoked when a scan request is made.\n * This can be used to trigger the actual scanning process.\n */\n onScanRequested?: (request: QrScanRequest) => void;\n /**\n * Callback invoked when a scan is successfully resolved.\n * This can be used to handle the result of the scan.\n */\n onScanResolved?: (result: SerializedUR) => void;\n /**\n * Callback invoked when a scan is rejected with an error.\n * This can be used to handle errors that occur during the scan.\n */\n onScanRejected?: (error: Error) => void;\n};\n\n/**\n * A bridge that turns the scan request into a deferred promise, allowing\n * the consumer to control the resolution and rejection of the scan.\n */\nexport class QrKeyringDeferredPromiseBridge implements QrKeyringBridge {\n readonly #lock = new Mutex();\n\n readonly #onScanRequested?: ((request: QrScanRequest) => void) | undefined;\n\n readonly #onScanResolved?: ((result: SerializedUR) => void) | undefined;\n\n readonly #onScanRejected?: ((error: Error) => void) | undefined;\n\n #pendingScan?: DeferredPromise<SerializedUR> | null;\n\n constructor({\n onScanRequested,\n onScanResolved,\n onScanRejected,\n }: QrKeyringDeferredPromiseBridgeOptions = {}) {\n this.#onScanRequested = onScanRequested;\n this.#onScanResolved = onScanResolved;\n this.#onScanRejected = onScanRejected;\n }\n\n /**\n * Request a QR code scan, obtaining a CBOR and a type as response.\n *\n * @param request - The type of QR scan request.\n * @returns A promise that resolves with the scanned data as a serialized UR.\n */\n async requestScan(request: QrScanRequest): Promise<SerializedUR> {\n return this.#lock.runExclusive(async () => {\n const deferredPromise = createDeferredPromise<SerializedUR>();\n this.#pendingScan = deferredPromise;\n this.#onScanRequested?.(request);\n return deferredPromise.promise;\n });\n }\n\n /**\n * Resolve the pending scan with the given result.\n *\n * @param result - The scanned data as a serialized UR.\n */\n resolvePendingScan(result: SerializedUR): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to resolve.');\n }\n this.#pendingScan.resolve(result);\n this.#pendingScan = null;\n this.#onScanResolved?.(result);\n }\n\n /**\n * Reject the pending scan with the given error.\n *\n * @param error - The error to reject the scan with.\n */\n rejectPendingScan(error: Error): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to reject.');\n }\n this.#pendingScan.reject(error);\n this.#pendingScan = null;\n this.#onScanRejected?.(error);\n }\n}\n"]}
1
+ {"version":3,"file":"qr-keyring-deferred-promise-bridge.cjs","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAwD;AAExD,6CAAoC;AA0BpC;;;GAGG;AACH,MAAa,8BAA8B;IAWzC,YAAY,EACV,eAAe,EACf,cAAc,EACd,cAAc,MAC2B,EAAE;QAdpC,+CAAQ,IAAI,mBAAK,EAAE,EAAC;QAEpB,kEAAkE;QAElE,iEAA+D;QAE/D,iEAAuD;QAEhE,8DAAoD;QAOlD,uBAAA,IAAI,mDAAoB,eAAe,MAAA,CAAC;QACxC,uBAAA,IAAI,kDAAmB,cAAc,MAAA,CAAC;QACtC,uBAAA,IAAI,kDAAmB,cAAc,MAAA,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,WAAW,CAAC,OAAsB;QACtC,OAAO,uBAAA,IAAI,4CAAM,CAAC,YAAY,CAAC,KAAK,IAAI,EAAE;YACxC,MAAM,eAAe,GAAG,IAAA,6BAAqB,GAAgB,CAAC;YAC9D,uBAAA,IAAI,+CAAgB,eAAe,MAAA,CAAC;YACpC,uBAAA,IAAI,uDAAiB,EAAE,KAAvB,IAAI,EAAoB,OAAO,CAAC,CAAC;YACjC,OAAO,eAAe,CAAC,OAAO,CAAC;QACjC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,kBAAkB,CAAC,MAAoB;QACrC,IAAI,CAAC,uBAAA,IAAI,mDAAa,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;QACD,uBAAA,IAAI,mDAAa,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAClC,uBAAA,IAAI,+CAAgB,IAAI,MAAA,CAAC;QACzB,uBAAA,IAAI,sDAAgB,EAAE,KAAtB,IAAI,EAAmB,MAAM,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,iBAAiB,CAAC,KAAY;QAC5B,IAAI,CAAC,uBAAA,IAAI,mDAAa,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAChD,CAAC;QACD,uBAAA,IAAI,mDAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAChC,uBAAA,IAAI,+CAAgB,IAAI,MAAA,CAAC;QACzB,uBAAA,IAAI,sDAAgB,EAAE,KAAtB,IAAI,EAAmB,KAAK,CAAC,CAAC;IAChC,CAAC;CACF;AA/DD,wEA+DC","sourcesContent":["import { createDeferredPromise } from '@metamask/utils';\nimport type { DeferredPromise } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type {\n QrKeyringBridge,\n QrScanRequest,\n SerializedUR,\n} from './qr-keyring';\n\nexport type QrKeyringDeferredPromiseBridgeOptions = {\n /**\n * Callback invoked when a scan request is made.\n * This can be used to trigger the actual scanning process.\n */\n onScanRequested?: (request: QrScanRequest) => void;\n /**\n * Callback invoked when a scan is successfully resolved.\n * This can be used to handle the result of the scan.\n */\n onScanResolved?: (result: SerializedUR) => void;\n /**\n * Callback invoked when a scan is rejected with an error.\n * This can be used to handle errors that occur during the scan.\n */\n onScanRejected?: (error: Error) => void;\n};\n\n/**\n * A bridge that turns the scan request into a deferred promise, allowing\n * the consumer to control the resolution and rejection of the scan.\n */\nexport class QrKeyringDeferredPromiseBridge implements QrKeyringBridge {\n readonly #lock = new Mutex();\n\n readonly #onScanRequested?: ((request: QrScanRequest) => void) | undefined;\n\n readonly #onScanResolved?: ((result: SerializedUR) => void) | undefined;\n\n readonly #onScanRejected?: ((error: Error) => void) | undefined;\n\n #pendingScan?: DeferredPromise<SerializedUR> | null;\n\n constructor({\n onScanRequested,\n onScanResolved,\n onScanRejected,\n }: QrKeyringDeferredPromiseBridgeOptions = {}) {\n this.#onScanRequested = onScanRequested;\n this.#onScanResolved = onScanResolved;\n this.#onScanRejected = onScanRejected;\n }\n\n /**\n * Request a QR code scan, obtaining a CBOR and a type as response.\n *\n * @param request - The type of QR scan request.\n * @returns A promise that resolves with the scanned data as a serialized UR.\n */\n async requestScan(request: QrScanRequest): Promise<SerializedUR> {\n return this.#lock.runExclusive(async () => {\n const deferredPromise = createDeferredPromise<SerializedUR>();\n this.#pendingScan = deferredPromise;\n this.#onScanRequested?.(request);\n return deferredPromise.promise;\n });\n }\n\n /**\n * Resolve the pending scan with the given result.\n *\n * @param result - The scanned data as a serialized UR.\n */\n resolvePendingScan(result: SerializedUR): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to resolve.');\n }\n this.#pendingScan.resolve(result);\n this.#pendingScan = null;\n this.#onScanResolved?.(result);\n }\n\n /**\n * Reject the pending scan with the given error.\n *\n * @param error - The error to reject the scan with.\n */\n rejectPendingScan(error: Error): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to reject.');\n }\n this.#pendingScan.reject(error);\n this.#pendingScan = null;\n this.#onScanRejected?.(error);\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-deferred-promise-bridge.d.cts","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,YAAY,EACb,yBAAqB;AAEtB,MAAM,MAAM,qCAAqC,GAAG;IAClD;;;OAGG;IACH,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;IACnD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;IAChD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF;;;GAGG;AACH,qBAAa,8BAA+B,YAAW,eAAe;;gBAWxD,EACV,eAAe,EACf,cAAc,EACd,cAAc,GACf,GAAE,qCAA0C;IAM7C;;;;;OAKG;IACG,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IAShE;;;;OAIG;IACH,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI;IAS9C;;;;OAIG;IACH,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAQtC"}
1
+ {"version":3,"file":"qr-keyring-deferred-promise-bridge.d.cts","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,YAAY,EACb,yBAAqB;AAEtB,MAAM,MAAM,qCAAqC,GAAG;IAClD;;;OAGG;IACH,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;IACnD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;IAChD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF;;;GAGG;AACH,qBAAa,8BAA+B,YAAW,eAAe;;gBAWxD,EACV,eAAe,EACf,cAAc,EACd,cAAc,GACf,GAAE,qCAA0C;IAM7C;;;;;OAKG;IACG,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IAShE;;;;OAIG;IACH,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI;IAS9C;;;;OAIG;IACH,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAQtC"}
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-deferred-promise-bridge.d.mts","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,YAAY,EACb,yBAAqB;AAEtB,MAAM,MAAM,qCAAqC,GAAG;IAClD;;;OAGG;IACH,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;IACnD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;IAChD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF;;;GAGG;AACH,qBAAa,8BAA+B,YAAW,eAAe;;gBAWxD,EACV,eAAe,EACf,cAAc,EACd,cAAc,GACf,GAAE,qCAA0C;IAM7C;;;;;OAKG;IACG,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IAShE;;;;OAIG;IACH,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI;IAS9C;;;;OAIG;IACH,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAQtC"}
1
+ {"version":3,"file":"qr-keyring-deferred-promise-bridge.d.mts","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,YAAY,EACb,yBAAqB;AAEtB,MAAM,MAAM,qCAAqC,GAAG;IAClD;;;OAGG;IACH,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;IACnD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;IAChD;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF;;;GAGG;AACH,qBAAa,8BAA+B,YAAW,eAAe;;gBAWxD,EACV,eAAe,EACf,cAAc,EACd,cAAc,GACf,GAAE,qCAA0C;IAM7C;;;;;OAKG;IACG,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IAShE;;;;OAIG;IACH,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI;IAS9C;;;;OAIG;IACH,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAQtC"}
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-deferred-promise-bridge.mjs","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAwB,qBAAqB,EAAE,wBAAwB;AAC9E,OAAO,EAAE,KAAK,EAAE,oBAAoB;AA0BpC;;;GAGG;AACH,MAAM,OAAO,8BAA8B;IAWzC,YAAY,EACV,eAAe,EACf,cAAc,EACd,cAAc,MAC2B,EAAE;QAdpC,+CAAQ,IAAI,KAAK,EAAE,EAAC;QAEpB,kEAAkE;QAElE,iEAA+D;QAE/D,iEAAuD;QAEhE,8DAAoD;QAOlD,uBAAA,IAAI,mDAAoB,eAAe,MAAA,CAAC;QACxC,uBAAA,IAAI,kDAAmB,cAAc,MAAA,CAAC;QACtC,uBAAA,IAAI,kDAAmB,cAAc,MAAA,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,WAAW,CAAC,OAAsB;QACtC,OAAO,uBAAA,IAAI,4CAAM,CAAC,YAAY,CAAC,KAAK,IAAI,EAAE;YACxC,MAAM,eAAe,GAAG,qBAAqB,EAAgB,CAAC;YAC9D,uBAAA,IAAI,+CAAgB,eAAe,MAAA,CAAC;YACpC,uBAAA,IAAI,uDAAiB,EAAE,KAAvB,IAAI,EAAoB,OAAO,CAAC,CAAC;YACjC,OAAO,eAAe,CAAC,OAAO,CAAC;QACjC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,kBAAkB,CAAC,MAAoB;QACrC,IAAI,CAAC,uBAAA,IAAI,mDAAa,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;QACD,uBAAA,IAAI,mDAAa,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAClC,uBAAA,IAAI,+CAAgB,IAAI,MAAA,CAAC;QACzB,uBAAA,IAAI,sDAAgB,EAAE,KAAtB,IAAI,EAAmB,MAAM,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,iBAAiB,CAAC,KAAY;QAC5B,IAAI,CAAC,uBAAA,IAAI,mDAAa,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAChD,CAAC;QACD,uBAAA,IAAI,mDAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAChC,uBAAA,IAAI,+CAAgB,IAAI,MAAA,CAAC;QACzB,uBAAA,IAAI,sDAAgB,EAAE,KAAtB,IAAI,EAAmB,KAAK,CAAC,CAAC;IAChC,CAAC;CACF","sourcesContent":["import { type DeferredPromise, createDeferredPromise } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type {\n QrKeyringBridge,\n QrScanRequest,\n SerializedUR,\n} from './qr-keyring';\n\nexport type QrKeyringDeferredPromiseBridgeOptions = {\n /**\n * Callback invoked when a scan request is made.\n * This can be used to trigger the actual scanning process.\n */\n onScanRequested?: (request: QrScanRequest) => void;\n /**\n * Callback invoked when a scan is successfully resolved.\n * This can be used to handle the result of the scan.\n */\n onScanResolved?: (result: SerializedUR) => void;\n /**\n * Callback invoked when a scan is rejected with an error.\n * This can be used to handle errors that occur during the scan.\n */\n onScanRejected?: (error: Error) => void;\n};\n\n/**\n * A bridge that turns the scan request into a deferred promise, allowing\n * the consumer to control the resolution and rejection of the scan.\n */\nexport class QrKeyringDeferredPromiseBridge implements QrKeyringBridge {\n readonly #lock = new Mutex();\n\n readonly #onScanRequested?: ((request: QrScanRequest) => void) | undefined;\n\n readonly #onScanResolved?: ((result: SerializedUR) => void) | undefined;\n\n readonly #onScanRejected?: ((error: Error) => void) | undefined;\n\n #pendingScan?: DeferredPromise<SerializedUR> | null;\n\n constructor({\n onScanRequested,\n onScanResolved,\n onScanRejected,\n }: QrKeyringDeferredPromiseBridgeOptions = {}) {\n this.#onScanRequested = onScanRequested;\n this.#onScanResolved = onScanResolved;\n this.#onScanRejected = onScanRejected;\n }\n\n /**\n * Request a QR code scan, obtaining a CBOR and a type as response.\n *\n * @param request - The type of QR scan request.\n * @returns A promise that resolves with the scanned data as a serialized UR.\n */\n async requestScan(request: QrScanRequest): Promise<SerializedUR> {\n return this.#lock.runExclusive(async () => {\n const deferredPromise = createDeferredPromise<SerializedUR>();\n this.#pendingScan = deferredPromise;\n this.#onScanRequested?.(request);\n return deferredPromise.promise;\n });\n }\n\n /**\n * Resolve the pending scan with the given result.\n *\n * @param result - The scanned data as a serialized UR.\n */\n resolvePendingScan(result: SerializedUR): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to resolve.');\n }\n this.#pendingScan.resolve(result);\n this.#pendingScan = null;\n this.#onScanResolved?.(result);\n }\n\n /**\n * Reject the pending scan with the given error.\n *\n * @param error - The error to reject the scan with.\n */\n rejectPendingScan(error: Error): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to reject.');\n }\n this.#pendingScan.reject(error);\n this.#pendingScan = null;\n this.#onScanRejected?.(error);\n }\n}\n"]}
1
+ {"version":3,"file":"qr-keyring-deferred-promise-bridge.mjs","sourceRoot":"","sources":["../src/qr-keyring-deferred-promise-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,qBAAqB,EAAE,wBAAwB;AAExD,OAAO,EAAE,KAAK,EAAE,oBAAoB;AA0BpC;;;GAGG;AACH,MAAM,OAAO,8BAA8B;IAWzC,YAAY,EACV,eAAe,EACf,cAAc,EACd,cAAc,MAC2B,EAAE;Q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sDAAgB,EAAE,KAAtB,IAAI,EAAmB,KAAK,CAAC,CAAC;IAChC,CAAC;CACF","sourcesContent":["import { createDeferredPromise } from '@metamask/utils';\nimport type { DeferredPromise } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type {\n QrKeyringBridge,\n QrScanRequest,\n SerializedUR,\n} from './qr-keyring';\n\nexport type QrKeyringDeferredPromiseBridgeOptions = {\n /**\n * Callback invoked when a scan request is made.\n * This can be used to trigger the actual scanning process.\n */\n onScanRequested?: (request: QrScanRequest) => void;\n /**\n * Callback invoked when a scan is successfully resolved.\n * This can be used to handle the result of the scan.\n */\n onScanResolved?: (result: SerializedUR) => void;\n /**\n * Callback invoked when a scan is rejected with an error.\n * This can be used to handle errors that occur during the scan.\n */\n onScanRejected?: (error: Error) => void;\n};\n\n/**\n * A bridge that turns the scan request into a deferred promise, allowing\n * the consumer to control the resolution and rejection of the scan.\n */\nexport class QrKeyringDeferredPromiseBridge implements QrKeyringBridge {\n readonly #lock = new Mutex();\n\n readonly #onScanRequested?: ((request: QrScanRequest) => void) | undefined;\n\n readonly #onScanResolved?: ((result: SerializedUR) => void) | undefined;\n\n readonly #onScanRejected?: ((error: Error) => void) | undefined;\n\n #pendingScan?: DeferredPromise<SerializedUR> | null;\n\n constructor({\n onScanRequested,\n onScanResolved,\n onScanRejected,\n }: QrKeyringDeferredPromiseBridgeOptions = {}) {\n this.#onScanRequested = onScanRequested;\n this.#onScanResolved = onScanResolved;\n this.#onScanRejected = onScanRejected;\n }\n\n /**\n * Request a QR code scan, obtaining a CBOR and a type as response.\n *\n * @param request - The type of QR scan request.\n * @returns A promise that resolves with the scanned data as a serialized UR.\n */\n async requestScan(request: QrScanRequest): Promise<SerializedUR> {\n return this.#lock.runExclusive(async () => {\n const deferredPromise = createDeferredPromise<SerializedUR>();\n this.#pendingScan = deferredPromise;\n this.#onScanRequested?.(request);\n return deferredPromise.promise;\n });\n }\n\n /**\n * Resolve the pending scan with the given result.\n *\n * @param result - The scanned data as a serialized UR.\n */\n resolvePendingScan(result: SerializedUR): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to resolve.');\n }\n this.#pendingScan.resolve(result);\n this.#pendingScan = null;\n this.#onScanResolved?.(result);\n }\n\n /**\n * Reject the pending scan with the given error.\n *\n * @param error - The error to reject the scan with.\n */\n rejectPendingScan(error: Error): void {\n if (!this.#pendingScan) {\n throw new Error('No pending scan to reject.');\n }\n this.#pendingScan.reject(error);\n this.#pendingScan = null;\n this.#onScanRejected?.(error);\n }\n}\n"]}