@metamask-previews/eth-qr-keyring 1.0.0-8a027ff → 1.0.0-ae6ca94

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.
@@ -25,10 +25,11 @@ class QrKeyringScannerBridge {
25
25
  /**
26
26
  * Requests a QR code scan and returns the scanned data.
27
27
  *
28
+ * @param type - The type of QR scan request.
28
29
  * @returns The scanned data as a string.
29
30
  */
30
- async requestScan() {
31
- return __classPrivateFieldGet(this, _QrKeyringScannerBridge_requestScan, "f").call(this);
31
+ async requestScan(type) {
32
+ return __classPrivateFieldGet(this, _QrKeyringScannerBridge_requestScan, "f").call(this, type);
32
33
  }
33
34
  }
34
35
  exports.QrKeyringScannerBridge = QrKeyringScannerBridge;
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-bridge.cjs","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAYA;;;GAGG;AACH,MAAa,sBAAsB;IAGjC,YAAY,EAAE,WAAW,EAAiC;QAFjD,sDAA4C;QAGnD,uBAAA,IAAI,uCAAgB,WAAW,MAAA,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,WAAW;QACf,OAAO,uBAAA,IAAI,2CAAa,MAAjB,IAAI,CAAe,CAAC;IAC7B,CAAC;CACF;AAfD,wDAeC","sourcesContent":["import type { QrKeyringBridge, QrScanResponse } from './qr-keyring';\n\n/**\n * Options for the QrKeyringScannerBridge.\n */\nexport type QrKeyringScannerBridgeOptions = {\n /**\n * An injected function that the bridge uses to request a QR code scan.\n */\n requestScan: () => Promise<QrScanResponse>;\n};\n\n/**\n * A bridge that allows the QrKeyring to request a QR code scan\n * while keeping the implementation defined by the QrKeyring consumer.\n */\nexport class QrKeyringScannerBridge implements QrKeyringBridge {\n readonly #requestScan: () => Promise<QrScanResponse>;\n\n constructor({ requestScan }: QrKeyringScannerBridgeOptions) {\n this.#requestScan = requestScan;\n }\n\n /**\n * Requests a QR code scan and returns the scanned data.\n *\n * @returns The scanned data as a string.\n */\n async requestScan(): Promise<QrScanResponse> {\n return this.#requestScan();\n }\n}\n"]}
1
+ {"version":3,"file":"qr-keyring-bridge.cjs","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgBA;;;GAGG;AACH,MAAa,sBAAsB;IAGjC,YAAY,EAAE,WAAW,EAAiC;QAFjD,sDAAmE;QAG1E,uBAAA,IAAI,uCAAgB,WAAW,MAAA,CAAC;IAClC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,WAAW,CAAC,IAAuB;QACvC,OAAO,uBAAA,IAAI,2CAAa,MAAjB,IAAI,EAAc,IAAI,CAAC,CAAC;IACjC,CAAC;CACF;AAhBD,wDAgBC","sourcesContent":["import type {\n QrKeyringBridge,\n QrScanRequestType,\n QrScanResponse,\n} from './qr-keyring';\n\n/**\n * Options for the QrKeyringScannerBridge.\n */\nexport type QrKeyringScannerBridgeOptions = {\n /**\n * An injected function that the bridge uses to request a QR code scan.\n */\n requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;\n};\n\n/**\n * A bridge that allows the QrKeyring to request a QR code scan\n * while keeping the implementation defined by the QrKeyring consumer.\n */\nexport class QrKeyringScannerBridge implements QrKeyringBridge {\n readonly #requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;\n\n constructor({ requestScan }: QrKeyringScannerBridgeOptions) {\n this.#requestScan = requestScan;\n }\n\n /**\n * Requests a QR code scan and returns the scanned data.\n *\n * @param type - The type of QR scan request.\n * @returns The scanned data as a string.\n */\n async requestScan(type: QrScanRequestType): Promise<QrScanResponse> {\n return this.#requestScan(type);\n }\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { QrKeyringBridge, QrScanResponse } from "./qr-keyring.cjs";
1
+ import type { QrKeyringBridge, QrScanRequestType, QrScanResponse } from "./qr-keyring.cjs";
2
2
  /**
3
3
  * Options for the QrKeyringScannerBridge.
4
4
  */
@@ -6,7 +6,7 @@ export type QrKeyringScannerBridgeOptions = {
6
6
  /**
7
7
  * An injected function that the bridge uses to request a QR code scan.
8
8
  */
9
- requestScan: () => Promise<QrScanResponse>;
9
+ requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;
10
10
  };
11
11
  /**
12
12
  * A bridge that allows the QrKeyring to request a QR code scan
@@ -18,8 +18,9 @@ export declare class QrKeyringScannerBridge implements QrKeyringBridge {
18
18
  /**
19
19
  * Requests a QR code scan and returns the scanned data.
20
20
  *
21
+ * @param type - The type of QR scan request.
21
22
  * @returns The scanned data as a string.
22
23
  */
23
- requestScan(): Promise<QrScanResponse>;
24
+ requestScan(type: QrScanRequestType): Promise<QrScanResponse>;
24
25
  }
25
26
  //# sourceMappingURL=qr-keyring-bridge.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-bridge.d.cts","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,yBAAqB;AAEpE;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;OAEG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,CAAC;CAC5C,CAAC;AAEF;;;GAGG;AACH,qBAAa,sBAAuB,YAAW,eAAe;;gBAGhD,EAAE,WAAW,EAAE,EAAE,6BAA6B;IAI1D;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,cAAc,CAAC;CAG7C"}
1
+ {"version":3,"file":"qr-keyring-bridge.d.cts","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EACjB,cAAc,EACf,yBAAqB;AAEtB;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;OAEG;IACH,WAAW,EAAE,CAAC,IAAI,EAAE,iBAAiB,KAAK,OAAO,CAAC,cAAc,CAAC,CAAC;CACnE,CAAC;AAEF;;;GAGG;AACH,qBAAa,sBAAuB,YAAW,eAAe;;gBAGhD,EAAE,WAAW,EAAE,EAAE,6BAA6B;IAI1D;;;;;OAKG;IACG,WAAW,CAAC,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,cAAc,CAAC;CAGpE"}
@@ -1,4 +1,4 @@
1
- import type { QrKeyringBridge, QrScanResponse } from "./qr-keyring.mjs";
1
+ import type { QrKeyringBridge, QrScanRequestType, QrScanResponse } from "./qr-keyring.mjs";
2
2
  /**
3
3
  * Options for the QrKeyringScannerBridge.
4
4
  */
@@ -6,7 +6,7 @@ export type QrKeyringScannerBridgeOptions = {
6
6
  /**
7
7
  * An injected function that the bridge uses to request a QR code scan.
8
8
  */
9
- requestScan: () => Promise<QrScanResponse>;
9
+ requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;
10
10
  };
11
11
  /**
12
12
  * A bridge that allows the QrKeyring to request a QR code scan
@@ -18,8 +18,9 @@ export declare class QrKeyringScannerBridge implements QrKeyringBridge {
18
18
  /**
19
19
  * Requests a QR code scan and returns the scanned data.
20
20
  *
21
+ * @param type - The type of QR scan request.
21
22
  * @returns The scanned data as a string.
22
23
  */
23
- requestScan(): Promise<QrScanResponse>;
24
+ requestScan(type: QrScanRequestType): Promise<QrScanResponse>;
24
25
  }
25
26
  //# sourceMappingURL=qr-keyring-bridge.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-bridge.d.mts","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,yBAAqB;AAEpE;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;OAEG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,CAAC;CAC5C,CAAC;AAEF;;;GAGG;AACH,qBAAa,sBAAuB,YAAW,eAAe;;gBAGhD,EAAE,WAAW,EAAE,EAAE,6BAA6B;IAI1D;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,cAAc,CAAC;CAG7C"}
1
+ {"version":3,"file":"qr-keyring-bridge.d.mts","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EACjB,cAAc,EACf,yBAAqB;AAEtB;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;OAEG;IACH,WAAW,EAAE,CAAC,IAAI,EAAE,iBAAiB,KAAK,OAAO,CAAC,cAAc,CAAC,CAAC;CACnE,CAAC;AAEF;;;GAGG;AACH,qBAAa,sBAAuB,YAAW,eAAe;;gBAGhD,EAAE,WAAW,EAAE,EAAE,6BAA6B;IAI1D;;;;;OAKG;IACG,WAAW,CAAC,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,cAAc,CAAC;CAGpE"}
@@ -22,10 +22,11 @@ export class QrKeyringScannerBridge {
22
22
  /**
23
23
  * Requests a QR code scan and returns the scanned data.
24
24
  *
25
+ * @param type - The type of QR scan request.
25
26
  * @returns The scanned data as a string.
26
27
  */
27
- async requestScan() {
28
- return __classPrivateFieldGet(this, _QrKeyringScannerBridge_requestScan, "f").call(this);
28
+ async requestScan(type) {
29
+ return __classPrivateFieldGet(this, _QrKeyringScannerBridge_requestScan, "f").call(this, type);
29
30
  }
30
31
  }
31
32
  _QrKeyringScannerBridge_requestScan = new WeakMap();
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring-bridge.mjs","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;AAYA;;;GAGG;AACH,MAAM,OAAO,sBAAsB;IAGjC,YAAY,EAAE,WAAW,EAAiC;QAFjD,sDAA4C;QAGnD,uBAAA,IAAI,uCAAgB,WAAW,MAAA,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,WAAW;QACf,OAAO,uBAAA,IAAI,2CAAa,MAAjB,IAAI,CAAe,CAAC;IAC7B,CAAC;CACF","sourcesContent":["import type { QrKeyringBridge, QrScanResponse } from './qr-keyring';\n\n/**\n * Options for the QrKeyringScannerBridge.\n */\nexport type QrKeyringScannerBridgeOptions = {\n /**\n * An injected function that the bridge uses to request a QR code scan.\n */\n requestScan: () => Promise<QrScanResponse>;\n};\n\n/**\n * A bridge that allows the QrKeyring to request a QR code scan\n * while keeping the implementation defined by the QrKeyring consumer.\n */\nexport class QrKeyringScannerBridge implements QrKeyringBridge {\n readonly #requestScan: () => Promise<QrScanResponse>;\n\n constructor({ requestScan }: QrKeyringScannerBridgeOptions) {\n this.#requestScan = requestScan;\n }\n\n /**\n * Requests a QR code scan and returns the scanned data.\n *\n * @returns The scanned data as a string.\n */\n async requestScan(): Promise<QrScanResponse> {\n return this.#requestScan();\n }\n}\n"]}
1
+ {"version":3,"file":"qr-keyring-bridge.mjs","sourceRoot":"","sources":["../src/qr-keyring-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;AAgBA;;;GAGG;AACH,MAAM,OAAO,sBAAsB;IAGjC,YAAY,EAAE,WAAW,EAAiC;QAFjD,sDAAmE;QAG1E,uBAAA,IAAI,uCAAgB,WAAW,MAAA,CAAC;IAClC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,WAAW,CAAC,IAAuB;QACvC,OAAO,uBAAA,IAAI,2CAAa,MAAjB,IAAI,EAAc,IAAI,CAAC,CAAC;IACjC,CAAC;CACF","sourcesContent":["import type {\n QrKeyringBridge,\n QrScanRequestType,\n QrScanResponse,\n} from './qr-keyring';\n\n/**\n * Options for the QrKeyringScannerBridge.\n */\nexport type QrKeyringScannerBridgeOptions = {\n /**\n * An injected function that the bridge uses to request a QR code scan.\n */\n requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;\n};\n\n/**\n * A bridge that allows the QrKeyring to request a QR code scan\n * while keeping the implementation defined by the QrKeyring consumer.\n */\nexport class QrKeyringScannerBridge implements QrKeyringBridge {\n readonly #requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;\n\n constructor({ requestScan }: QrKeyringScannerBridgeOptions) {\n this.#requestScan = requestScan;\n }\n\n /**\n * Requests a QR code scan and returns the scanned data.\n *\n * @param type - The type of QR scan request.\n * @returns The scanned data as a string.\n */\n async requestScan(type: QrScanRequestType): Promise<QrScanResponse> {\n return this.#requestScan(type);\n }\n}\n"]}
@@ -12,10 +12,23 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
12
12
  };
13
13
  var _QrKeyring_instances, _QrKeyring_signer, _QrKeyring_accounts, _QrKeyring_accountToUnlock, _QrKeyring_currentPage, _QrKeyring_scanAndInitialize;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.QrKeyring = exports.getDefaultSerializedQrKeyringState = exports.QR_KEYRING_TYPE = void 0;
15
+ exports.QrKeyring = exports.getDefaultSerializedQrKeyringState = exports.QrScanRequestType = exports.QR_KEYRING_TYPE = void 0;
16
16
  const utils_1 = require("@metamask/utils");
17
17
  const airgapped_signer_1 = require("./airgapped-signer.cjs");
18
18
  exports.QR_KEYRING_TYPE = 'QR Hardware Wallet Device';
19
+ var QrScanRequestType;
20
+ (function (QrScanRequestType) {
21
+ /**
22
+ * Request a scan for a QR code containing a UR
23
+ * with information related to a hardware wallet.
24
+ */
25
+ QrScanRequestType["PAIR"] = "pair";
26
+ /**
27
+ * Request a scan for a QR code containing a
28
+ * UR-encoded transaction signature.
29
+ */
30
+ QrScanRequestType["SIGN"] = "sign";
31
+ })(QrScanRequestType || (exports.QrScanRequestType = QrScanRequestType = {}));
19
32
  /**
20
33
  * Returns the default serialized state of the QrKeyring.
21
34
  *
@@ -232,7 +245,7 @@ _QrKeyring_signer = new WeakMap(), _QrKeyring_accounts = new WeakMap(), _QrKeyri
232
245
  * scanned UR.
233
246
  */
234
247
  async function _QrKeyring_scanAndInitialize() {
235
- this.submitUR(await this.bridge.requestScan());
248
+ this.submitUR(await this.bridge.requestScan(QrScanRequestType.PAIR));
236
249
  };
237
250
  QrKeyring.type = exports.QR_KEYRING_TYPE;
238
251
  //# sourceMappingURL=qr-keyring.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring.cjs","sourceRoot":"","sources":["../src/qr-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAEA,2CAA4D;AAE5D,6DAK4B;AAEf,QAAA,eAAe,GAAG,2BAA2B,CAAC;AAkC3D;;;;GAIG;AACI,MAAM,kCAAkC,GAC7C,GAA6B,EAAE,CAAC,CAAC;IAC/B,WAAW,EAAE,KAAK;IAClB,QAAQ,EAAE,EAAE;CACb,CAAC,CAAC;AAJQ,QAAA,kCAAkC,sCAI1C;AAEL;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAe;IACvC,OAAO,IAAA,0BAAkB,EAAC,IAAA,aAAK,EAAC,OAAO,CAAC,CAAC,CAAC;AAC5C,CAAC;AAED,MAAa,SAAS;IAepB,YAAY,OAAyB;;QAZ5B,SAAI,GAAG,uBAAe,CAAC;QAIvB,4BAA2B,IAAI,kCAAe,EAAE,EAAC;QAE1D,8BAAmB,EAAE,EAAC;QAEtB,6CAAsC;QAEtC,iCAAuB,CAAC,EAAC;QAGvB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAE7B,IAAI,OAAO,EAAE,EAAE,EAAE,CAAC;YAChB,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,MAAM,GAAG,uBAAA,IAAI,yBAAQ,CAAC,gBAAgB,EAAE,CAAC;QAE/C,IACE,CAAC,MAAM;YACP,CAAC,CAAC,8BAAW,CAAC,EAAE,EAAE,8BAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,EACnE,CAAC;YACD,2DAA2D;YAC3D,OAAO,IAAA,0CAA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I,8BAAa,CAAC,CAAC;IAC1D,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;QACrB,uBAAA,IAAI,uBAAa,EAAE,MAAA,CAAC;QACpB,uBAAA,IAAI,8BAAoB,SAAS,MAAA,CAAC;QAClC,uBAAA,IAAI,0BAAgB,CAAC,MAAA,CAAC;IACxB,CAAC;;AA/NH,8BAwOC;;AAPC;;;GAGG;AACH,KAAK;IACH,IAAI,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;AACjD,CAAC;AAtOM,cAAI,GAAG,uBAAe,AAAlB,CAAmB","sourcesContent":["import type { Keyring } from '@metamask/keyring-utils';\nimport type { Hex } from '@metamask/utils';\nimport { add0x, getChecksumAddress } from '@metamask/utils';\n\nimport {\n type AirgappedSignerDetails,\n type IndexedAddress,\n AirgappedSigner,\n KeyringMode,\n} from './airgapped-signer';\n\nexport const QR_KEYRING_TYPE = 'QR Hardware Wallet Device';\n\nexport type QrScanResponse = {\n type: string;\n cbor: Hex;\n};\n\nexport type QrKeyringBridge = {\n requestScan: () => Promise<QrScanResponse>;\n};\n\nexport type QrKeyringOptions = {\n ur?: string;\n bridge: QrKeyringBridge;\n};\n\n/**\n * The state of the QrKeyring\n *\n * @property accounts - The accounts in the QrKeyring\n */\nexport type SerializedQrKeyringState = {\n version?: number;\n accounts?: string[];\n currentAccount?: number;\n} & (\n | {\n initialized?: false;\n }\n | ({\n initialized: true;\n } & AirgappedSignerDetails)\n);\n\n/**\n * Returns the default serialized state of the QrKeyring.\n *\n * @returns The default serialized state.\n */\nexport const getDefaultSerializedQrKeyringState =\n (): SerializedQrKeyringState => ({\n initialized: false,\n accounts: [],\n });\n\n/**\n * Normalizes an address to a 0x-prefixed checksum address.\n *\n * @param address - The address to normalize.\n * @returns The normalized address as a Hex string.\n */\nfunction normalizeAddress(address: string): Hex {\n return getChecksumAddress(add0x(address));\n}\n\nexport class QrKeyring implements Keyring {\n static type = QR_KEYRING_TYPE;\n\n readonly type = QR_KEYRING_TYPE;\n\n readonly bridge: QrKeyringBridge;\n\n readonly #signer: AirgappedSigner = new AirgappedSigner();\n\n #accounts: Hex[] = [];\n\n #accountToUnlock?: number | undefined;\n\n #currentPage: number = 0;\n\n constructor(options: QrKeyringOptions) {\n this.bridge = options.bridge;\n\n if (options?.ur) {\n this.submitUR(options.ur);\n }\n }\n\n /**\n * Serializes the QrKeyring state\n *\n * @returns The serialized state\n */\n async serialize(): Promise<SerializedQrKeyringState> {\n const source = this.#signer.getSourceDetails();\n\n if (\n !source ||\n ![KeyringMode.HD, KeyringMode.ACCOUNT].includes(source.keyringMode)\n ) {\n // the keyring has not initialized with a device source yet\n return getDefaultSerializedQrKeyringState();\n }\n\n const accounts = this.#accounts.slice();\n\n if (source.keyringMode === KeyringMode.HD) {\n // These properties are only relevant for HD Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.HD,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n xpub: source.xpub,\n hdPath: source.hdPath,\n childrenPath: source.childrenPath,\n accounts,\n indexes: source.indexes,\n };\n }\n // These properties are only relevant for Account Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.ACCOUNT,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n paths: source.paths,\n accounts,\n indexes: source.indexes,\n };\n }\n\n /**\n * Deserializes the QrKeyring state\n *\n * @param state - The serialized state to deserialize\n */\n async deserialize(state: SerializedQrKeyringState): Promise<void> {\n if (!state.initialized) {\n this.#accounts = [];\n this.#signer.clear();\n return;\n }\n\n this.#signer.init(state);\n this.#accounts = (state.accounts ?? []).map(normalizeAddress);\n }\n\n /**\n * Adds accounts to the QrKeyring\n *\n * @param accountsToAdd - The number of accounts to add\n * @returns The accounts added\n */\n async addAccounts(accountsToAdd: number): Promise<Hex[]> {\n const lastAccount = this.#accounts[this.#accounts.length - 1];\n const startIndex =\n this.#accountToUnlock ??\n (lastAccount ? this.#signer.indexFromAddress(lastAccount) : 0);\n const newAccounts: Hex[] = [];\n\n for (let i = 0; i < accountsToAdd; i++) {\n const index = startIndex + i;\n const address = this.#signer.addressFromIndex(index);\n\n if (this.#accounts.includes(address)) {\n continue;\n }\n\n this.#accounts.push(address);\n newAccounts.push(address);\n }\n\n this.#accountToUnlock = startIndex + accountsToAdd;\n return newAccounts;\n }\n\n /**\n * Gets the accounts in the QrKeyring\n *\n * @returns The accounts in the QrKeyring\n */\n async getAccounts(): Promise<Hex[]> {\n return Array.from(this.#accounts.values());\n }\n\n /**\n * Remove an account from the keyring\n *\n * @param address - The address of the account to remove\n */\n removeAccount(address: Hex): void {\n const normalizedAddress = normalizeAddress(address);\n this.#accounts = this.#accounts.filter(\n (account) => account !== normalizedAddress,\n );\n }\n\n /**\n * Submits a CBOR encoded UR to the QrKeyring\n *\n * @param ur - The CBOR encoded UR\n */\n submitUR(ur: string | QrScanResponse): void {\n this.#signer.init(ur);\n }\n\n /**\n * Sets the next account index to unlock\n *\n * @param index - The index of the account to unlock\n */\n setAccountToUnlock(index: number): void {\n this.#accountToUnlock = index;\n }\n\n /**\n * Get the name of the paired device or the keyring type\n * if unavailable.\n *\n * @returns The name of the paired device or the keyring type.\n */\n getName(): string {\n const source = this.#signer.getSourceDetails();\n return source?.name ?? QR_KEYRING_TYPE;\n }\n\n /**\n * Fetch the first page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The first page of accounts as an array of Hex strings.\n */\n async getFirstPage(): Promise<IndexedAddress[]> {\n this.#currentPage = 0;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the next page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The next page of accounts as an array of IndexedAddress objects.\n */\n async getNextPage(): Promise<IndexedAddress[]> {\n this.#currentPage += 1;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the previous page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The previous page of accounts as an array of IndexedAddress objects.\n */\n async getPreviousPage(): Promise<IndexedAddress[]> {\n if (this.#currentPage > 0) {\n this.#currentPage -= 1;\n } else {\n this.#currentPage = 0;\n }\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the current page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The current page of accounts as an array of IndexedAddress objects.\n */\n async getCurrentPage(): Promise<IndexedAddress[]> {\n if (!this.#signer.isInitialized()) {\n await this.#scanAndInitialize();\n }\n\n return this.#signer.getAddressesPage(this.#currentPage);\n }\n\n /**\n * Clear the keyring state and forget any paired device or accounts.\n */\n async forgetDevice(): Promise<void> {\n this.#signer.clear();\n this.#accounts = [];\n this.#accountToUnlock = undefined;\n this.#currentPage = 0;\n }\n\n /**\n * Scan for a QR code and initialize the keyring with the\n * scanned UR.\n */\n async #scanAndInitialize(): Promise<void> {\n this.submitUR(await this.bridge.requestScan());\n }\n}\n"]}
1
+ {"version":3,"file":"qr-keyring.cjs","sourceRoot":"","sources":["../src/qr-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAEA,2CAA4D;AAE5D,6DAK4B;AAEf,QAAA,eAAe,GAAG,2BAA2B,CAAC;AAE3D,IAAY,iBAWX;AAXD,WAAY,iBAAiB;IAC3B;;;OAGG;IACH,kCAAa,CAAA;IACb;;;OAGG;IACH,kCAAa,CAAA;AACf,CAAC,EAXW,iBAAiB,iCAAjB,iBAAiB,QAW5B;AAkCD;;;;GAIG;AACI,MAAM,kCAAkC,GAC7C,GAA6B,EAAE,CAAC,CAAC;IAC/B,WAAW,EAAE,KAAK;IAClB,QAAQ,EAAE,EAAE;CACb,CAAC,CAAC;AAJQ,QAAA,kCAAkC,sCAI1C;AAEL;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAe;IACvC,OAAO,IAAA,0BAAkB,EAAC,IAAA,aAAK,EAAC,OAAO,CAA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x, getChecksumAddress } from '@metamask/utils';\n\nimport {\n type AirgappedSignerDetails,\n type IndexedAddress,\n AirgappedSigner,\n KeyringMode,\n} from './airgapped-signer';\n\nexport const QR_KEYRING_TYPE = 'QR Hardware Wallet Device';\n\nexport enum QrScanRequestType {\n /**\n * Request a scan for a QR code containing a UR\n * with information related to a hardware wallet.\n */\n PAIR = 'pair',\n /**\n * Request a scan for a QR code containing a\n * UR-encoded transaction signature.\n */\n SIGN = 'sign',\n}\n\nexport type QrScanResponse = {\n type: string;\n cbor: Hex;\n};\n\nexport type QrKeyringBridge = {\n requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;\n};\n\nexport type QrKeyringOptions = {\n ur?: string;\n bridge: QrKeyringBridge;\n};\n\n/**\n * The state of the QrKeyring\n *\n * @property accounts - The accounts in the QrKeyring\n */\nexport type SerializedQrKeyringState = {\n version?: number;\n accounts?: string[];\n currentAccount?: number;\n} & (\n | {\n initialized?: false;\n }\n | ({\n initialized: true;\n } & AirgappedSignerDetails)\n);\n\n/**\n * Returns the default serialized state of the QrKeyring.\n *\n * @returns The default serialized state.\n */\nexport const getDefaultSerializedQrKeyringState =\n (): SerializedQrKeyringState => ({\n initialized: false,\n accounts: [],\n });\n\n/**\n * Normalizes an address to a 0x-prefixed checksum address.\n *\n * @param address - The address to normalize.\n * @returns The normalized address as a Hex string.\n */\nfunction normalizeAddress(address: string): Hex {\n return getChecksumAddress(add0x(address));\n}\n\nexport class QrKeyring implements Keyring {\n static type = QR_KEYRING_TYPE;\n\n readonly type = QR_KEYRING_TYPE;\n\n readonly bridge: QrKeyringBridge;\n\n readonly #signer: AirgappedSigner = new AirgappedSigner();\n\n #accounts: Hex[] = [];\n\n #accountToUnlock?: number | undefined;\n\n #currentPage: number = 0;\n\n constructor(options: QrKeyringOptions) {\n this.bridge = options.bridge;\n\n if (options?.ur) {\n this.submitUR(options.ur);\n }\n }\n\n /**\n * Serializes the QrKeyring state\n *\n * @returns The serialized state\n */\n async serialize(): Promise<SerializedQrKeyringState> {\n const source = this.#signer.getSourceDetails();\n\n if (\n !source ||\n ![KeyringMode.HD, KeyringMode.ACCOUNT].includes(source.keyringMode)\n ) {\n // the keyring has not initialized with a device source yet\n return getDefaultSerializedQrKeyringState();\n }\n\n const accounts = this.#accounts.slice();\n\n if (source.keyringMode === KeyringMode.HD) {\n // These properties are only relevant for HD Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.HD,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n xpub: source.xpub,\n hdPath: source.hdPath,\n childrenPath: source.childrenPath,\n accounts,\n indexes: source.indexes,\n };\n }\n // These properties are only relevant for Account Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.ACCOUNT,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n paths: source.paths,\n accounts,\n indexes: source.indexes,\n };\n }\n\n /**\n * Deserializes the QrKeyring state\n *\n * @param state - The serialized state to deserialize\n */\n async deserialize(state: SerializedQrKeyringState): Promise<void> {\n if (!state.initialized) {\n this.#accounts = [];\n this.#signer.clear();\n return;\n }\n\n this.#signer.init(state);\n this.#accounts = (state.accounts ?? []).map(normalizeAddress);\n }\n\n /**\n * Adds accounts to the QrKeyring\n *\n * @param accountsToAdd - The number of accounts to add\n * @returns The accounts added\n */\n async addAccounts(accountsToAdd: number): Promise<Hex[]> {\n const lastAccount = this.#accounts[this.#accounts.length - 1];\n const startIndex =\n this.#accountToUnlock ??\n (lastAccount ? this.#signer.indexFromAddress(lastAccount) : 0);\n const newAccounts: Hex[] = [];\n\n for (let i = 0; i < accountsToAdd; i++) {\n const index = startIndex + i;\n const address = this.#signer.addressFromIndex(index);\n\n if (this.#accounts.includes(address)) {\n continue;\n }\n\n this.#accounts.push(address);\n newAccounts.push(address);\n }\n\n this.#accountToUnlock = startIndex + accountsToAdd;\n return newAccounts;\n }\n\n /**\n * Gets the accounts in the QrKeyring\n *\n * @returns The accounts in the QrKeyring\n */\n async getAccounts(): Promise<Hex[]> {\n return Array.from(this.#accounts.values());\n }\n\n /**\n * Remove an account from the keyring\n *\n * @param address - The address of the account to remove\n */\n removeAccount(address: Hex): void {\n const normalizedAddress = normalizeAddress(address);\n this.#accounts = this.#accounts.filter(\n (account) => account !== normalizedAddress,\n );\n }\n\n /**\n * Submits a CBOR encoded UR to the QrKeyring\n *\n * @param ur - The CBOR encoded UR\n */\n submitUR(ur: string | QrScanResponse): void {\n this.#signer.init(ur);\n }\n\n /**\n * Sets the next account index to unlock\n *\n * @param index - The index of the account to unlock\n */\n setAccountToUnlock(index: number): void {\n this.#accountToUnlock = index;\n }\n\n /**\n * Get the name of the paired device or the keyring type\n * if unavailable.\n *\n * @returns The name of the paired device or the keyring type.\n */\n getName(): string {\n const source = this.#signer.getSourceDetails();\n return source?.name ?? QR_KEYRING_TYPE;\n }\n\n /**\n * Fetch the first page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The first page of accounts as an array of Hex strings.\n */\n async getFirstPage(): Promise<IndexedAddress[]> {\n this.#currentPage = 0;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the next page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The next page of accounts as an array of IndexedAddress objects.\n */\n async getNextPage(): Promise<IndexedAddress[]> {\n this.#currentPage += 1;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the previous page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The previous page of accounts as an array of IndexedAddress objects.\n */\n async getPreviousPage(): Promise<IndexedAddress[]> {\n if (this.#currentPage > 0) {\n this.#currentPage -= 1;\n } else {\n this.#currentPage = 0;\n }\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the current page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The current page of accounts as an array of IndexedAddress objects.\n */\n async getCurrentPage(): Promise<IndexedAddress[]> {\n if (!this.#signer.isInitialized()) {\n await this.#scanAndInitialize();\n }\n\n return this.#signer.getAddressesPage(this.#currentPage);\n }\n\n /**\n * Clear the keyring state and forget any paired device or accounts.\n */\n async forgetDevice(): Promise<void> {\n this.#signer.clear();\n this.#accounts = [];\n this.#accountToUnlock = undefined;\n this.#currentPage = 0;\n }\n\n /**\n * Scan for a QR code and initialize the keyring with the\n * scanned UR.\n */\n async #scanAndInitialize(): Promise<void> {\n this.submitUR(await this.bridge.requestScan(QrScanRequestType.PAIR));\n }\n}\n"]}
@@ -2,12 +2,24 @@ import type { Keyring } from "@metamask/keyring-utils";
2
2
  import type { Hex } from "@metamask/utils";
3
3
  import { type AirgappedSignerDetails, type IndexedAddress } from "./airgapped-signer.cjs";
4
4
  export declare const QR_KEYRING_TYPE = "QR Hardware Wallet Device";
5
+ export declare enum QrScanRequestType {
6
+ /**
7
+ * Request a scan for a QR code containing a UR
8
+ * with information related to a hardware wallet.
9
+ */
10
+ PAIR = "pair",
11
+ /**
12
+ * Request a scan for a QR code containing a
13
+ * UR-encoded transaction signature.
14
+ */
15
+ SIGN = "sign"
16
+ }
5
17
  export type QrScanResponse = {
6
18
  type: string;
7
19
  cbor: Hex;
8
20
  };
9
21
  export type QrKeyringBridge = {
10
- requestScan: () => Promise<QrScanResponse>;
22
+ requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;
11
23
  };
12
24
  export type QrKeyringOptions = {
13
25
  ur?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring.d.cts","sourceRoot":"","sources":["../src/qr-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,gCAAgC;AACvD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,cAAc,EAGpB,+BAA2B;AAE5B,eAAO,MAAM,eAAe,8BAA8B,CAAC;AAE3D,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,GAAG,C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AAO,CAAC,IAAI,CAAC;CAcpC"}
@@ -2,12 +2,24 @@ import type { Keyring } from "@metamask/keyring-utils";
2
2
  import type { Hex } from "@metamask/utils";
3
3
  import { type AirgappedSignerDetails, type IndexedAddress } from "./airgapped-signer.mjs";
4
4
  export declare const QR_KEYRING_TYPE = "QR Hardware Wallet Device";
5
+ export declare enum QrScanRequestType {
6
+ /**
7
+ * Request a scan for a QR code containing a UR
8
+ * with information related to a hardware wallet.
9
+ */
10
+ PAIR = "pair",
11
+ /**
12
+ * Request a scan for a QR code containing a
13
+ * UR-encoded transaction signature.
14
+ */
15
+ SIGN = "sign"
16
+ }
5
17
  export type QrScanResponse = {
6
18
  type: string;
7
19
  cbor: Hex;
8
20
  };
9
21
  export type QrKeyringBridge = {
10
- requestScan: () => Promise<QrScanResponse>;
22
+ requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;
11
23
  };
12
24
  export type QrKeyringOptions = {
13
25
  ur?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring.d.mts","sourceRoot":"","sources":["../src/qr-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,gCAAgC;AACvD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,cAAc,EAGpB,+BAA2B;AAE5B,eAAO,MAAM,eAAe,8BAA8B,CAAC;AAE3D,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,GAAG,CAAC;CACX,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,WAAW,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,CAAC;CAC5C,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,eAAe,CAAC;CACzB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,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CAAC,IAAI,SAAmB;IAE9B,QAAQ,CAAC,IAAI,+BAAmB;IAEhC,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;gBAUrB,OAAO,EAAE,gBAAgB;IAQrC;;;;OAIG;IACG,SAAS,IAAI,OAAO,CAAC,wBAAwB,CAAC;IAyCpD;;;;OAIG;IACG,WAAW,CAAC,KAAK,EAAE,wBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC;IAWjE;;;;;OAKG;IACG,WAAW,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IAuBxD;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;IAInC;;;;OAIG;IACH,aAAa,CAAC,OAAO,EAAE,GAAG,GAAG,IAAI;IAOjC;;;;OAIG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,cAAc,GAAG,IAAI;IAI3C;;;;OAIG;IACH,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAIvC;;;;;OAKG;IACH,OAAO,IAAI,MAAM;IAKjB;;;;;OAKG;IACG,YAAY,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAK/C;;;;;OAKG;IACG,WAAW,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAK9C;;;;;OAKG;IACG,eAAe,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IASlD;;;;;OAKG;IACG,cAAc,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAQjD;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAcpC"}
@@ -13,6 +13,19 @@ var _QrKeyring_instances, _QrKeyring_signer, _QrKeyring_accounts, _QrKeyring_acc
13
13
  import { add0x, getChecksumAddress } from "@metamask/utils";
14
14
  import { AirgappedSigner, KeyringMode } from "./airgapped-signer.mjs";
15
15
  export const QR_KEYRING_TYPE = 'QR Hardware Wallet Device';
16
+ export var QrScanRequestType;
17
+ (function (QrScanRequestType) {
18
+ /**
19
+ * Request a scan for a QR code containing a UR
20
+ * with information related to a hardware wallet.
21
+ */
22
+ QrScanRequestType["PAIR"] = "pair";
23
+ /**
24
+ * Request a scan for a QR code containing a
25
+ * UR-encoded transaction signature.
26
+ */
27
+ QrScanRequestType["SIGN"] = "sign";
28
+ })(QrScanRequestType || (QrScanRequestType = {}));
16
29
  /**
17
30
  * Returns the default serialized state of the QrKeyring.
18
31
  *
@@ -227,7 +240,7 @@ _QrKeyring_signer = new WeakMap(), _QrKeyring_accounts = new WeakMap(), _QrKeyri
227
240
  * scanned UR.
228
241
  */
229
242
  async function _QrKeyring_scanAndInitialize() {
230
- this.submitUR(await this.bridge.requestScan());
243
+ this.submitUR(await this.bridge.requestScan(QrScanRequestType.PAIR));
231
244
  };
232
245
  QrKeyring.type = QR_KEYRING_TYPE;
233
246
  //# sourceMappingURL=qr-keyring.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"qr-keyring.mjs","sourceRoot":"","sources":["../src/qr-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,wBAAwB;AAE5D,OAAO,EAGL,eAAe,EACf,WAAW,EACZ,+BAA2B;AAE5B,MAAM,CAAC,MAAM,eAAe,GAAG,2BAA2B,CAAC;AAkC3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,kCAAkC,GAC7C,GAA6B,EAAE,CAAC,CAAC;IAC/B,WAAW,EAAE,KAAK;IAClB,QAAQ,EAAE,EAAE;CACb,CAAC,CAAC;AAEL;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAe;IACvC,OAAO,kBAAkB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,OAAO,SAAS;IAepB,YAAY,OAAyB;;QAZ5B,SAAI,GAAG,eAAe,CAAC;QAIvB,4BAA2B,IAAI,eAAe,EAAE,EAAC;QAE1D,8BAAmB,EAAE,EAAC;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AED;;;;;OAKG;IACH,KAAK,CAAC,eAAe;QACnB,IAAI,uBAAA,IAAI,8BAAa,GAAG,CAAC,EAAE,CAAC;YAC1B,iHAAqB,CAAC,MAAA,CAAC;QACzB,CAAC;aAAM,CAAC;YACN,uBAAA,IAAI,0BAAgB,CAAC,MAAA,CAAC;QACxB,CAAC;QACD,OAAO,IAAI,CAAC,cAAc,EAAE,CAAC;IAC/B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,cAAc;QAClB,IAAI,CAAC,uBAAA,IAAI,yBAAQ,CAAC,aAAa,EAAE,EAAE,CAAC;YAClC,MAAM,uBAAA,IAAI,0DAAmB,MAAvB,IAAI,CAAqB,CAAC;QAClC,CAAC;QAED,OAAO,uBAAA,IAAI,yBAAQ,CAAC,gBAAgB,CAAC,uBAAA,IAAI,8BAAa,CAAC,CAAC;IAC1D,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;QACrB,uBAAA,IAAI,uBAAa,EAAE,MAAA,CAAC;QACpB,uBAAA,IAAI,8BAAoB,SAAS,MAAA,CAAC;QAClC,uBAAA,IAAI,0BAAgB,CAAC,MAAA,CAAC;IACxB,CAAC;;;AAED;;;GAGG;AACH,KAAK;IACH,IAAI,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;AACjD,CAAC;AAtOM,cAAI,GAAG,eAAe,AAAlB,CAAmB","sourcesContent":["import type { Keyring } from '@metamask/keyring-utils';\nimport type { Hex } from '@metamask/utils';\nimport { add0x, getChecksumAddress } from '@metamask/utils';\n\nimport {\n type AirgappedSignerDetails,\n type IndexedAddress,\n AirgappedSigner,\n KeyringMode,\n} from './airgapped-signer';\n\nexport const QR_KEYRING_TYPE = 'QR Hardware Wallet Device';\n\nexport type QrScanResponse = {\n type: string;\n cbor: Hex;\n};\n\nexport type QrKeyringBridge = {\n requestScan: () => Promise<QrScanResponse>;\n};\n\nexport type QrKeyringOptions = {\n ur?: string;\n bridge: QrKeyringBridge;\n};\n\n/**\n * The state of the QrKeyring\n *\n * @property accounts - The accounts in the QrKeyring\n */\nexport type SerializedQrKeyringState = {\n version?: number;\n accounts?: string[];\n currentAccount?: number;\n} & (\n | {\n initialized?: false;\n }\n | ({\n initialized: true;\n } & AirgappedSignerDetails)\n);\n\n/**\n * Returns the default serialized state of the QrKeyring.\n *\n * @returns The default serialized state.\n */\nexport const getDefaultSerializedQrKeyringState =\n (): SerializedQrKeyringState => ({\n initialized: false,\n accounts: [],\n });\n\n/**\n * Normalizes an address to a 0x-prefixed checksum address.\n *\n * @param address - The address to normalize.\n * @returns The normalized address as a Hex string.\n */\nfunction normalizeAddress(address: string): Hex {\n return getChecksumAddress(add0x(address));\n}\n\nexport class QrKeyring implements Keyring {\n static type = QR_KEYRING_TYPE;\n\n readonly type = QR_KEYRING_TYPE;\n\n readonly bridge: QrKeyringBridge;\n\n readonly #signer: AirgappedSigner = new AirgappedSigner();\n\n #accounts: Hex[] = [];\n\n #accountToUnlock?: number | undefined;\n\n #currentPage: number = 0;\n\n constructor(options: QrKeyringOptions) {\n this.bridge = options.bridge;\n\n if (options?.ur) {\n this.submitUR(options.ur);\n }\n }\n\n /**\n * Serializes the QrKeyring state\n *\n * @returns The serialized state\n */\n async serialize(): Promise<SerializedQrKeyringState> {\n const source = this.#signer.getSourceDetails();\n\n if (\n !source ||\n ![KeyringMode.HD, KeyringMode.ACCOUNT].includes(source.keyringMode)\n ) {\n // the keyring has not initialized with a device source yet\n return getDefaultSerializedQrKeyringState();\n }\n\n const accounts = this.#accounts.slice();\n\n if (source.keyringMode === KeyringMode.HD) {\n // These properties are only relevant for HD Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.HD,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n xpub: source.xpub,\n hdPath: source.hdPath,\n childrenPath: source.childrenPath,\n accounts,\n indexes: source.indexes,\n };\n }\n // These properties are only relevant for Account Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.ACCOUNT,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n paths: source.paths,\n accounts,\n indexes: source.indexes,\n };\n }\n\n /**\n * Deserializes the QrKeyring state\n *\n * @param state - The serialized state to deserialize\n */\n async deserialize(state: SerializedQrKeyringState): Promise<void> {\n if (!state.initialized) {\n this.#accounts = [];\n this.#signer.clear();\n return;\n }\n\n this.#signer.init(state);\n this.#accounts = (state.accounts ?? []).map(normalizeAddress);\n }\n\n /**\n * Adds accounts to the QrKeyring\n *\n * @param accountsToAdd - The number of accounts to add\n * @returns The accounts added\n */\n async addAccounts(accountsToAdd: number): Promise<Hex[]> {\n const lastAccount = this.#accounts[this.#accounts.length - 1];\n const startIndex =\n this.#accountToUnlock ??\n (lastAccount ? this.#signer.indexFromAddress(lastAccount) : 0);\n const newAccounts: Hex[] = [];\n\n for (let i = 0; i < accountsToAdd; i++) {\n const index = startIndex + i;\n const address = this.#signer.addressFromIndex(index);\n\n if (this.#accounts.includes(address)) {\n continue;\n }\n\n this.#accounts.push(address);\n newAccounts.push(address);\n }\n\n this.#accountToUnlock = startIndex + accountsToAdd;\n return newAccounts;\n }\n\n /**\n * Gets the accounts in the QrKeyring\n *\n * @returns The accounts in the QrKeyring\n */\n async getAccounts(): Promise<Hex[]> {\n return Array.from(this.#accounts.values());\n }\n\n /**\n * Remove an account from the keyring\n *\n * @param address - The address of the account to remove\n */\n removeAccount(address: Hex): void {\n const normalizedAddress = normalizeAddress(address);\n this.#accounts = this.#accounts.filter(\n (account) => account !== normalizedAddress,\n );\n }\n\n /**\n * Submits a CBOR encoded UR to the QrKeyring\n *\n * @param ur - The CBOR encoded UR\n */\n submitUR(ur: string | QrScanResponse): void {\n this.#signer.init(ur);\n }\n\n /**\n * Sets the next account index to unlock\n *\n * @param index - The index of the account to unlock\n */\n setAccountToUnlock(index: number): void {\n this.#accountToUnlock = index;\n }\n\n /**\n * Get the name of the paired device or the keyring type\n * if unavailable.\n *\n * @returns The name of the paired device or the keyring type.\n */\n getName(): string {\n const source = this.#signer.getSourceDetails();\n return source?.name ?? QR_KEYRING_TYPE;\n }\n\n /**\n * Fetch the first page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The first page of accounts as an array of Hex strings.\n */\n async getFirstPage(): Promise<IndexedAddress[]> {\n this.#currentPage = 0;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the next page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The next page of accounts as an array of IndexedAddress objects.\n */\n async getNextPage(): Promise<IndexedAddress[]> {\n this.#currentPage += 1;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the previous page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The previous page of accounts as an array of IndexedAddress objects.\n */\n async getPreviousPage(): Promise<IndexedAddress[]> {\n if (this.#currentPage > 0) {\n this.#currentPage -= 1;\n } else {\n this.#currentPage = 0;\n }\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the current page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The current page of accounts as an array of IndexedAddress objects.\n */\n async getCurrentPage(): Promise<IndexedAddress[]> {\n if (!this.#signer.isInitialized()) {\n await this.#scanAndInitialize();\n }\n\n return this.#signer.getAddressesPage(this.#currentPage);\n }\n\n /**\n * Clear the keyring state and forget any paired device or accounts.\n */\n async forgetDevice(): Promise<void> {\n this.#signer.clear();\n this.#accounts = [];\n this.#accountToUnlock = undefined;\n this.#currentPage = 0;\n }\n\n /**\n * Scan for a QR code and initialize the keyring with the\n * scanned UR.\n */\n async #scanAndInitialize(): Promise<void> {\n this.submitUR(await this.bridge.requestScan());\n }\n}\n"]}
1
+ {"version":3,"file":"qr-keyring.mjs","sourceRoot":"","sources":["../src/qr-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,OAAO,E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8BAAa,CAAC,CAAC;IAC1D,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY;QAChB,uBAAA,IAAI,yBAAQ,CAAC,KAAK,EAAE,CAAC;QACrB,uBAAA,IAAI,uBAAa,EAAE,MAAA,CAAC;QACpB,uBAAA,IAAI,8BAAoB,SAAS,MAAA,CAAC;QAClC,uBAAA,IAAI,0BAAgB,CAAC,MAAA,CAAC;IACxB,CAAC;;;AAED;;;GAGG;AACH,KAAK;IACH,IAAI,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC;AACvE,CAAC;AAtOM,cAAI,GAAG,eAAe,AAAlB,CAAmB","sourcesContent":["import type { Keyring } from '@metamask/keyring-utils';\nimport type { Hex } from '@metamask/utils';\nimport { add0x, getChecksumAddress } from '@metamask/utils';\n\nimport {\n type AirgappedSignerDetails,\n type IndexedAddress,\n AirgappedSigner,\n KeyringMode,\n} from './airgapped-signer';\n\nexport const QR_KEYRING_TYPE = 'QR Hardware Wallet Device';\n\nexport enum QrScanRequestType {\n /**\n * Request a scan for a QR code containing a UR\n * with information related to a hardware wallet.\n */\n PAIR = 'pair',\n /**\n * Request a scan for a QR code containing a\n * UR-encoded transaction signature.\n */\n SIGN = 'sign',\n}\n\nexport type QrScanResponse = {\n type: string;\n cbor: Hex;\n};\n\nexport type QrKeyringBridge = {\n requestScan: (type: QrScanRequestType) => Promise<QrScanResponse>;\n};\n\nexport type QrKeyringOptions = {\n ur?: string;\n bridge: QrKeyringBridge;\n};\n\n/**\n * The state of the QrKeyring\n *\n * @property accounts - The accounts in the QrKeyring\n */\nexport type SerializedQrKeyringState = {\n version?: number;\n accounts?: string[];\n currentAccount?: number;\n} & (\n | {\n initialized?: false;\n }\n | ({\n initialized: true;\n } & AirgappedSignerDetails)\n);\n\n/**\n * Returns the default serialized state of the QrKeyring.\n *\n * @returns The default serialized state.\n */\nexport const getDefaultSerializedQrKeyringState =\n (): SerializedQrKeyringState => ({\n initialized: false,\n accounts: [],\n });\n\n/**\n * Normalizes an address to a 0x-prefixed checksum address.\n *\n * @param address - The address to normalize.\n * @returns The normalized address as a Hex string.\n */\nfunction normalizeAddress(address: string): Hex {\n return getChecksumAddress(add0x(address));\n}\n\nexport class QrKeyring implements Keyring {\n static type = QR_KEYRING_TYPE;\n\n readonly type = QR_KEYRING_TYPE;\n\n readonly bridge: QrKeyringBridge;\n\n readonly #signer: AirgappedSigner = new AirgappedSigner();\n\n #accounts: Hex[] = [];\n\n #accountToUnlock?: number | undefined;\n\n #currentPage: number = 0;\n\n constructor(options: QrKeyringOptions) {\n this.bridge = options.bridge;\n\n if (options?.ur) {\n this.submitUR(options.ur);\n }\n }\n\n /**\n * Serializes the QrKeyring state\n *\n * @returns The serialized state\n */\n async serialize(): Promise<SerializedQrKeyringState> {\n const source = this.#signer.getSourceDetails();\n\n if (\n !source ||\n ![KeyringMode.HD, KeyringMode.ACCOUNT].includes(source.keyringMode)\n ) {\n // the keyring has not initialized with a device source yet\n return getDefaultSerializedQrKeyringState();\n }\n\n const accounts = this.#accounts.slice();\n\n if (source.keyringMode === KeyringMode.HD) {\n // These properties are only relevant for HD Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.HD,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n xpub: source.xpub,\n hdPath: source.hdPath,\n childrenPath: source.childrenPath,\n accounts,\n indexes: source.indexes,\n };\n }\n // These properties are only relevant for Account Keys\n return {\n initialized: true,\n name: source.name,\n keyringMode: KeyringMode.ACCOUNT,\n keyringAccount: source.keyringAccount,\n xfp: source.xfp,\n paths: source.paths,\n accounts,\n indexes: source.indexes,\n };\n }\n\n /**\n * Deserializes the QrKeyring state\n *\n * @param state - The serialized state to deserialize\n */\n async deserialize(state: SerializedQrKeyringState): Promise<void> {\n if (!state.initialized) {\n this.#accounts = [];\n this.#signer.clear();\n return;\n }\n\n this.#signer.init(state);\n this.#accounts = (state.accounts ?? []).map(normalizeAddress);\n }\n\n /**\n * Adds accounts to the QrKeyring\n *\n * @param accountsToAdd - The number of accounts to add\n * @returns The accounts added\n */\n async addAccounts(accountsToAdd: number): Promise<Hex[]> {\n const lastAccount = this.#accounts[this.#accounts.length - 1];\n const startIndex =\n this.#accountToUnlock ??\n (lastAccount ? this.#signer.indexFromAddress(lastAccount) : 0);\n const newAccounts: Hex[] = [];\n\n for (let i = 0; i < accountsToAdd; i++) {\n const index = startIndex + i;\n const address = this.#signer.addressFromIndex(index);\n\n if (this.#accounts.includes(address)) {\n continue;\n }\n\n this.#accounts.push(address);\n newAccounts.push(address);\n }\n\n this.#accountToUnlock = startIndex + accountsToAdd;\n return newAccounts;\n }\n\n /**\n * Gets the accounts in the QrKeyring\n *\n * @returns The accounts in the QrKeyring\n */\n async getAccounts(): Promise<Hex[]> {\n return Array.from(this.#accounts.values());\n }\n\n /**\n * Remove an account from the keyring\n *\n * @param address - The address of the account to remove\n */\n removeAccount(address: Hex): void {\n const normalizedAddress = normalizeAddress(address);\n this.#accounts = this.#accounts.filter(\n (account) => account !== normalizedAddress,\n );\n }\n\n /**\n * Submits a CBOR encoded UR to the QrKeyring\n *\n * @param ur - The CBOR encoded UR\n */\n submitUR(ur: string | QrScanResponse): void {\n this.#signer.init(ur);\n }\n\n /**\n * Sets the next account index to unlock\n *\n * @param index - The index of the account to unlock\n */\n setAccountToUnlock(index: number): void {\n this.#accountToUnlock = index;\n }\n\n /**\n * Get the name of the paired device or the keyring type\n * if unavailable.\n *\n * @returns The name of the paired device or the keyring type.\n */\n getName(): string {\n const source = this.#signer.getSourceDetails();\n return source?.name ?? QR_KEYRING_TYPE;\n }\n\n /**\n * Fetch the first page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The first page of accounts as an array of Hex strings.\n */\n async getFirstPage(): Promise<IndexedAddress[]> {\n this.#currentPage = 0;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the next page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The next page of accounts as an array of IndexedAddress objects.\n */\n async getNextPage(): Promise<IndexedAddress[]> {\n this.#currentPage += 1;\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the previous page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The previous page of accounts as an array of IndexedAddress objects.\n */\n async getPreviousPage(): Promise<IndexedAddress[]> {\n if (this.#currentPage > 0) {\n this.#currentPage -= 1;\n } else {\n this.#currentPage = 0;\n }\n return this.getCurrentPage();\n }\n\n /**\n * Fetch the current page of accounts. If the keyring is not currently initialized,\n * it will trigger a scan request to initialize it.\n *\n * @returns The current page of accounts as an array of IndexedAddress objects.\n */\n async getCurrentPage(): Promise<IndexedAddress[]> {\n if (!this.#signer.isInitialized()) {\n await this.#scanAndInitialize();\n }\n\n return this.#signer.getAddressesPage(this.#currentPage);\n }\n\n /**\n * Clear the keyring state and forget any paired device or accounts.\n */\n async forgetDevice(): Promise<void> {\n this.#signer.clear();\n this.#accounts = [];\n this.#accountToUnlock = undefined;\n this.#currentPage = 0;\n }\n\n /**\n * Scan for a QR code and initialize the keyring with the\n * scanned UR.\n */\n async #scanAndInitialize(): Promise<void> {\n this.submitUR(await this.bridge.requestScan(QrScanRequestType.PAIR));\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/eth-qr-keyring",
3
- "version": "1.0.0-8a027ff",
3
+ "version": "1.0.0-ae6ca94",
4
4
  "description": "A simple standard interface for a series of Ethereum private keys.",
5
5
  "keywords": [
6
6
  "ethereum",