@metamask-previews/mfa-recovery-controller 0.0.0-preview-c33f4af3a → 0.0.0-preview-e1e83cae0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
12
- - Initial release of `MfaRecoveryController`, which manages MFA recovery flows and persists in-flight operations in its state ([#10022](https://github.com/MetaMask/core/pull/10022))
12
+ - Initial release of `MfaRecoveryController`, which manages MFA recovery flows and persists in-flight operations in its state ([#10022](https://github.com/MetaMask/core/pull/10022), [#11280](https://github.com/MetaMask/core/pull/11280))
13
13
  - Requires recent, request-bound AAL2 approvals for writes and reads.
14
14
  - Sends the approval's identifiers to escrows as `approvedIdentifiers` with `register` and `updateIdentifiers` mutations, and rejects payload identifiers that are not exactly among them before writing ([#10883](https://github.com/MetaMask/core/pull/10883))
15
15
  - Adds `getIdentifiers`, which returns the enrolled identifiers at the highest escrow version as `epoch` along with Auth's identifiers, for building `updateIdentifiers` calls ([#11149](https://github.com/MetaMask/core/pull/11149))
@@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
20
20
  - Clears a `writing` mutation in `abort()` and `resume()` once its approval has expired and no escrow applied it; `resume()` then throws `MutationRejectedError` ([#11151](https://github.com/MetaMask/core/pull/11151))
21
21
  - Exports `MfaRecoveryController`, `getDefaultMfaRecoveryControllerState`, and the controller's action, event, messenger, state, and options types.
22
22
  - Exports `AuthApiRecoveryAuthProvider`, `AuthApiRecoveryAuthProviderOptions` and `AuthApiRecoveryAuthProviderMessenger` for requesting approvals through `AuthenticationController:requestMfaApproval`, using the profile id from `AuthenticationController:refreshCanonicalProfileId` ([#10883](https://github.com/MetaMask/core/pull/10883))
23
- - Exports `CubistEscrowProvider` and `CubistEscrowProviderOptions` for storing recovery secrets with CubeSigner.
23
+ - Exports `CubistEscrowProvider` and `CubistEscrowProviderOptions` for storing recovery secrets with CubeSigner. `CubistEscrowProviderOptions` takes a `getClient` callback instead of a CubeSigner client so callers can create and refresh the session lazily ([#11280](https://github.com/MetaMask/core/pull/11280))
24
24
  - Exports `MfaRecoveryError`, `IncompleteMutationError`, and `MutationRejectedError`.
25
25
  - Exports `MIN_IDENTIFIERS`, `IDENTIFIER_AUTH_MODES`, and `getIdentifierAuthMode` for identifier authentication.
26
26
  - Exports types for the recovery providers (`RecoveryAuthProvider`, `RecoveryIdentifierAuthProvider`, `RecoveryEscrowProvider`) and the pending operation encryptor (`PendingOperationEncryptor`), along with the related identifier, mutation, and recovery phase types, including `GetIdentifiersResponse` and `RecoveryIdentifiers` ([#11149](https://github.com/MetaMask/core/pull/11149))
@@ -10,9 +10,11 @@ import type { AuthControllerToken, GetIdentifiersResponse, GetRecoverySecretResp
10
10
  */
11
11
  export type CubistEscrowProviderOptions = {
12
12
  /**
13
- * Authenticated CubeSigner client used to invoke the escrow C2F.
13
+ * Returns the authenticated CubeSigner client used to invoke the escrow C2F.
14
+ * Called for every command, so the caller owns session caching and refresh.
15
+ * Commands propagate its errors unchanged; `isAvailable` resolves `false`.
14
16
  */
15
- client: CubeSignerClient;
17
+ getClient: () => Promise<CubeSignerClient>;
16
18
  /**
17
19
  * Must match the escrow's AUTH_CONTROLLER_AUDIENCE configuration.
18
20
  */
@@ -40,7 +42,8 @@ export declare class CubistEscrowProvider implements RecoveryEscrowProvider {
40
42
  readonly authAudience: string;
41
43
  readonly wrapPublicKey: Uint8Array;
42
44
  /**
43
- * @param options - CubeSigner client, pinned keys, and optional C2F id/version.
45
+ * @param options - CubeSigner client getter, pinned keys, and optional C2F
46
+ * id/version.
44
47
  */
45
48
  constructor(options: CubistEscrowProviderOptions);
46
49
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"cubist-escrow-provider.d.ts","sourceRoot":"","sources":["../../src/escrow-providers/cubist-escrow-provider.ts"],"names":[],"mappings":"AAAA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QAAQ,EAAE,QAAQ,EAClB,mBAAmB,EAAE,mBAAmB,EACxC,wBAAwB,EAAE,uBAAuB,EAAE,EACnD,OAAO,EAAE,eAAe,EACxB,mBAAmB,EAAE,UAAU,EAAE,GAChC,OAAO,CAAC,eAAe,CAAC,CAa1B;IAED;;;;;;;OAOG;IACG,aAAa,CACjB,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,QAAQ,EAClB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,OAAO,CAAC,CAsBlB;CA0CF"}
1
+ {"version":3,"file":"cubist-escrow-provider.d.ts","sourceRoot":"","sources":["../../src/escrow-providers/cubist-escrow-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAEhB,OAAO,EACR,MAAM,6BAA6B,CAAC;AAyBrC,OAAO,KAAK,EACV,mBAAmB,EACnB,sBAAsB,EACtB,yBAAyB,EACzB,UAAU,EACV,uBAAuB,EACvB,QAAQ,EACR,eAAe,EACf,eAAe,EACf,YAAY,EACZ,sBAAsB,EACvB,MAAM,aAAa,CAAC;AAMrB;;;;;;;GAOG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;;OAIG;IACH,SAAS,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC3C;;OAEG;IACH,YAAY,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;OAEG;IACH,aAAa,EAAE,UAAU,CAAC;IAC1B;;OAEG;IACH,gBAAgB,EAAE,UAAU,CAAC;CAC9B,CAAC;AAEF,qBAAa,oBAAqB,YAAW,sBAAsB;;IACjE,QAAQ,CAAC,EAAE,YAAa;IAExB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAE9B,QAAQ,CAAC,aAAa,EAAE,UAAU,CAAC;IAUnC;;;OAGG;IACH,YAAY,OAAO,EAAE,2BAA2B,EAO/C;IAED;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAOpC;IAED;;;OAGG;IACG,iBAAiB,CACrB,mBAAmB,EAAE,mBAAmB,GACvC,OAAO,CAAC,YAAY,CAAC,CAMvB;IAED;;;OAGG;IACG,cAAc,CAClB,mBAAmB,EAAE,mBAAmB,GACvC,OAAO,CAAC,sBAAsB,CAAC,CAMjC;IAED;;;;;;OAMG;IACG,SAAS,CACb,mBAAmB,EAAE,mBAAmB,EACxC,aAAa,EAAE,uBAAuB,EACtC,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,UAAU,GACd,OAAO,CAAC,yBAAyB,CAAC,CAWpC;IAED;;;;;;;;OAQG;IACG,aAAa,CACjB,QAAQ,EAAE,QAAQ,EAClB,mBAAmB,EAAE,mBAAmB,EACxC,wBAAwB,EAAE,uBAAuB,EAAE,EACnD,OAAO,EAAE,eAAe,EACxB,mBAAmB,EAAE,UAAU,EAAE,GAChC,OAAO,CAAC,eAAe,CAAC,CAa1B;IAED;;;;;;;OAOG;IACG,aAAa,CACjB,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,QAAQ,EAClB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,OAAO,CAAC,CAsBlB;CA2CF"}
@@ -11,17 +11,18 @@ export class CubistEscrowProvider {
11
11
  authAudience;
12
12
  wrapPublicKey;
13
13
  #receiptPublicKey;
14
- #client;
14
+ #getClient;
15
15
  #functionId;
16
16
  #version;
17
17
  /**
18
- * @param options - CubeSigner client, pinned keys, and optional C2F id/version.
18
+ * @param options - CubeSigner client getter, pinned keys, and optional C2F
19
+ * id/version.
19
20
  */
20
21
  constructor(options) {
21
22
  this.authAudience = options.authAudience;
22
23
  this.wrapPublicKey = options.wrapPublicKey;
23
24
  this.#receiptPublicKey = options.receiptPublicKey;
24
- this.#client = options.client;
25
+ this.#getClient = options.getClient;
25
26
  this.#functionId = options.functionId ?? DEFAULT_FUNCTION_ID;
26
27
  this.#version = options.version ?? DEFAULT_VERSION;
27
28
  }
@@ -110,9 +111,10 @@ export class CubistEscrowProvider {
110
111
  }));
111
112
  }
112
113
  async #invoke(cmd, body, parse, denyIsRejection = false) {
114
+ const client = await this.#getClient();
113
115
  let invocation;
114
116
  try {
115
- invocation = await this.#client.apiClient.policyInvoke(this.#functionId, this.#version, { request: toWire({ cmd, ...body }) });
117
+ invocation = await client.apiClient.policyInvoke(this.#functionId, this.#version, { request: toWire({ cmd, ...body }) });
116
118
  }
117
119
  catch (error) {
118
120
  throw new MfaRecoveryError(error instanceof Error ? error.message : 'Escrow request failed', 'escrow_request_failed');
@@ -1 +1 @@
1
- {"version":3,"file":"cubist-escrow-provider.js","sourceRoot":"","sources":["../../src/escrow-providers/cubist-escrow-provider.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,EACL,OAAO,EACP,EAAE,EACF,OAAO,EACP,GAAG,EACH,MAAM,EACN,IAAI,EACJ,OAAO,GACR,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,UAAU,EACV,aAAa,EACb,UAAU,EACV,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,mBAAmB,EACnB,YAAY,EACZ,eAAe,GAChB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAcvD,MAAM,SAAS,GAAG,QAAQ,CAAC;AAC3B,MAAM,mBAAmB,GAAG,sBAAsB,CAAC;AACnD,MAAM,eAAe,GAAY,QAAQ,CAAC;AAqC1C,MAAM,OAAO,oBAAoB;IACtB,EAAE,GAAG,SAAS,CAAC;IAEf,YAAY,CAAS;IAErB,aAAa,CAAa;IAE1B,iBAAiB,CAAa;IAE9B,OAAO,CAAmB;IAE1B,WAAW,CAAS;IAEpB,QAAQ,CAAU;IAE3B;;OAEG;IACH,YAAY,OAAoC;QAC9C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AAK,CAAC,SAAS;KAC3B,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IAClC,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,gBAAgB,CACxB,0BAA0B,EAC1B,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type {\n CubeSignerClient,\n InvokePolicyResponse,\n Version,\n} from '@cubist-labs/cubesigner-sdk';\nimport {\n array,\n integer,\n is,\n literal,\n min,\n string,\n type,\n unknown,\n} from '@metamask/superstruct';\nimport {\n bytesToHex,\n bytesToString,\n hexToBytes,\n isStrictHexString,\n} from '@metamask/utils';\n\nimport {\n hashMutationReceipt,\n receiptKeyId,\n verifySignature,\n} from '../crypto.js';\nimport { MfaRecoveryError, MutationRejectedError } from '../errors.js';\nimport { isMutationReceipt } from '../escrow-utils.js';\nimport type {\n AuthControllerToken,\n GetIdentifiersResponse,\n GetRecoverySecretResponse,\n Identifier,\n IdentifierAuthorization,\n Mutation,\n MutationPayload,\n MutationReceipt,\n PoPChallenge,\n RecoveryEscrowProvider,\n} from '../types.js';\n\nconst ESCROW_ID = 'cubist';\nconst DEFAULT_FUNCTION_ID = 'cubist_secret_escrow';\nconst DEFAULT_VERSION: Version = 'latest';\n\n/**\n * CubeSigner C2F client for the Cubist recovery escrow.\n *\n * Commands are sent with `policyInvoke` as `{ cmd, ...fields }` on\n * `cubist_secret_escrow`.\n * Escrow JSON is read from C2F stdout. Receipt signatures are checked locally\n * with the pinned receipt public key.\n */\nexport type CubistEscrowProviderOptions = {\n /**\n * Authenticated CubeSigner client used to invoke the escrow C2F.\n */\n client: CubeSignerClient;\n /**\n * Must match the escrow's AUTH_CONTROLLER_AUDIENCE configuration.\n */\n authAudience: string;\n /**\n * C2F name or named-policy id. Defaults to `cubist_secret_escrow`.\n */\n functionId?: string;\n /**\n * C2F version to invoke. Defaults to `latest`.\n */\n version?: Version;\n /**\n * Raw 32-byte X25519 public key of the escrow wrap key.\n */\n wrapPublicKey: Uint8Array;\n /**\n * Raw 32-byte Ed25519 public key of the escrow receipt key.\n */\n receiptPublicKey: Uint8Array;\n};\n\nexport class CubistEscrowProvider implements RecoveryEscrowProvider {\n readonly id = ESCROW_ID;\n\n readonly authAudience: string;\n\n readonly wrapPublicKey: Uint8Array;\n\n readonly #receiptPublicKey: Uint8Array;\n\n readonly #client: CubeSignerClient;\n\n readonly #functionId: string;\n\n readonly #version: Version;\n\n /**\n * @param options - CubeSigner client, pinned keys, and optional C2F id/version.\n */\n constructor(options: CubistEscrowProviderOptions) {\n this.authAudience = options.authAudience;\n this.wrapPublicKey = options.wrapPublicKey;\n this.#receiptPublicKey = options.receiptPublicKey;\n this.#client = options.client;\n this.#functionId = options.functionId ?? DEFAULT_FUNCTION_ID;\n this.#version = options.version ?? DEFAULT_VERSION;\n }\n\n /**\n * @returns Whether a ping of the escrow C2F is allowed and reports ok.\n */\n async isAvailable(): Promise<boolean> {\n try {\n await this.#invoke('ping', {}, parsePing);\n return true;\n } catch {\n return false;\n }\n }\n\n /**\n * @param authControllerToken - Approval for the profile the challenge is for.\n * @returns A proof-of-possession challenge for this escrow.\n */\n async generateChallenge(\n authControllerToken: AuthControllerToken,\n ): Promise<PoPChallenge> {\n return await this.#invoke(\n 'generateChallenge',\n { authControllerToken },\n parseChallenge,\n );\n }\n\n /**\n * @param authControllerToken - Approval for the profile to read.\n * @returns The enrolled identifiers and record version.\n */\n async getIdentifiers(\n authControllerToken: AuthControllerToken,\n ): Promise<GetIdentifiersResponse> {\n return await this.#invoke(\n 'getIdentifiers',\n { authControllerToken },\n parseIdentifiers,\n );\n }\n\n /**\n * @param authControllerToken - Request-bound approval for this read.\n * @param authorization - Identifier proof for this read.\n * @param requestId - Client request id bound into the read hash.\n * @param pkE - Ephemeral wrap public key for the response.\n * @returns The wrapped recovery secret and its version.\n */\n async getSecret(\n authControllerToken: AuthControllerToken,\n authorization: IdentifierAuthorization,\n requestId: string,\n pkE: Uint8Array,\n ): Promise<GetRecoverySecretResponse> {\n return await this.#invoke(\n 'getSecret',\n {\n identifierAuthorization: authorization,\n requestId,\n pkE,\n authControllerToken,\n },\n parseGetSecretResponse,\n );\n }\n\n /**\n * @param mutation - Mutation to apply.\n * @param authControllerToken - Request-bound AuthController token.\n * @param identifierAuthorizations - Identifier proofs sharing one challenge.\n * @param payload - Per-escrow mutation payload.\n * @param approvedIdentifiers - The approval's identifiers, which non-SIWE\n * payload identifiers must be drawn from.\n * @returns The escrow's mutation receipt.\n */\n async applyMutation(\n mutation: Mutation,\n authControllerToken: AuthControllerToken,\n identifierAuthorizations: IdentifierAuthorization[],\n payload: MutationPayload,\n approvedIdentifiers: Identifier[],\n ): Promise<MutationReceipt> {\n return await this.#invoke(\n 'applyMutation',\n {\n mutation,\n authControllerToken,\n identifierAuthorizations,\n payload,\n approvedIdentifiers,\n },\n parseReceipt,\n true, // a policy Deny did not apply the mutation\n );\n }\n\n /**\n * Verifies a receipt with the pinned receipt public key.\n *\n * @param receipt - Receipt returned by the escrow.\n * @param mutation - Mutation the receipt must acknowledge.\n * @param expectedEscrowId - Escrow identity expected by the caller.\n * @returns Whether the receipt is valid for this escrow.\n */\n async verifyReceipt(\n receipt: MutationReceipt,\n mutation: Mutation,\n expectedEscrowId: string,\n ): Promise<boolean> {\n if (\n receipt.mutationId !== mutation.id ||\n receipt.requestHash !== mutation.requestHash ||\n receipt.escrowId !== expectedEscrowId ||\n expectedEscrowId !== this.id ||\n receipt.version !== mutation.newVersion ||\n receipt.receiptKeyId !== (await receiptKeyId(this.#receiptPublicKey))\n ) {\n return false;\n }\n return await verifySignature(\n this.#receiptPublicKey,\n receipt.signature,\n await hashMutationReceipt({\n escrowId: receipt.escrowId,\n mutationId: receipt.mutationId,\n receiptKeyId: receipt.receiptKeyId,\n requestHash: receipt.requestHash,\n version: receipt.version,\n }),\n );\n }\n\n async #invoke<Result>(\n cmd: string,\n body: Record<string, unknown>,\n parse: (value: unknown) => Result,\n denyIsRejection = false,\n ): Promise<Result> {\n let invocation: InvokePolicyResponse;\n try {\n invocation = await this.#client.apiClient.policyInvoke(\n this.#functionId,\n this.#version,\n { request: toWire({ cmd, ...body }) },\n );\n } catch (error) {\n throw new MfaRecoveryError(\n error instanceof Error ? error.message : 'Escrow request failed',\n 'escrow_request_failed',\n );\n }\n const policy = invocation.response;\n if (policy.response !== 'Allow') {\n if (denyIsRejection && policy.response === 'Deny') {\n throw new MutationRejectedError(policy.reason);\n }\n throw new MfaRecoveryError(\n policy.response === 'Deny' ? policy.reason : policy.error,\n 'escrow_request_failed',\n );\n }\n let json: unknown;\n try {\n json = JSON.parse(bytesToString(hexToBytes(invocation.stdout)));\n } catch {\n throw new MfaRecoveryError(\n 'Escrow response was not JSON',\n 'escrow_response_invalid',\n );\n }\n return parse(json);\n }\n}\n\n// `type()` is non-strict, so unknown fields are ignored. The explicit field\n// mapping below still strips them from the returned value.\nconst PingStruct = type({ ok: literal(true) });\n\nconst ChallengeStruct = type({\n id: string(),\n escrowId: literal(ESCROW_ID),\n expiresAt: integer(),\n});\n\nconst IdentifiersStruct = type({\n identifiers: array(\n type({\n type: string(),\n namespace: string(),\n value: string(),\n verifier: unknown(),\n }),\n ),\n version: min(integer(), 0),\n});\n\nconst GetSecretResponseStruct = type({\n recoverySecret: type({ ciphertext: string() }),\n version: min(integer(), 0),\n lastMutationId: string(),\n wrapKeyId: string(),\n});\n\nfunction parsePing(value: unknown): void {\n if (!is(value, PingStruct)) {\n throw new MfaRecoveryError(\n 'Invalid ping response',\n 'escrow_response_invalid',\n );\n }\n}\n\nfunction parseChallenge(value: unknown): PoPChallenge {\n if (!is(value, ChallengeStruct)) {\n throw new MfaRecoveryError(\n 'Invalid PoP challenge',\n 'escrow_response_invalid',\n );\n }\n return {\n id: value.id,\n escrowId: value.escrowId,\n expiresAt: value.expiresAt,\n };\n}\n\nfunction parseIdentifiers(value: unknown): GetIdentifiersResponse {\n if (!is(value, IdentifiersStruct)) {\n throw new MfaRecoveryError(\n 'Invalid identifiers response',\n 'escrow_response_invalid',\n );\n }\n return {\n identifiers: value.identifiers.map((identifier) => ({\n type: identifier.type,\n namespace: identifier.namespace,\n value: identifier.value,\n ...(identifier.verifier === undefined\n ? {}\n : { verifier: identifier.verifier }),\n })),\n version: value.version,\n };\n}\n\n/**\n * Encodes a request for the escrow wire: every byte field becomes a 0x-prefixed\n * hex string. Typed arrays have no `toJSON`, so the replacer sees them directly.\n *\n * @param value - Request body.\n * @returns JSON-compatible request body.\n */\nfunction toWire(value: Record<string, unknown>): Record<string, unknown> {\n return JSON.parse(\n JSON.stringify(value, (_key, node: unknown) =>\n node instanceof Uint8Array ? bytesToHex(node) : node,\n ),\n ) as Record<string, unknown>;\n}\n\nfunction parseGetSecretResponse(value: unknown): GetRecoverySecretResponse {\n if (\n !is(value, GetSecretResponseStruct) ||\n !isStrictHexString(value.recoverySecret.ciphertext)\n ) {\n throw new MfaRecoveryError(\n 'Invalid recovery secret response',\n 'escrow_response_invalid',\n );\n }\n return {\n recoverySecret: {\n ciphertext: hexToBytes(value.recoverySecret.ciphertext),\n },\n version: value.version,\n lastMutationId: value.lastMutationId,\n wrapKeyId: value.wrapKeyId,\n };\n}\n\nfunction parseReceipt(value: unknown): MutationReceipt {\n if (!isMutationReceipt(value)) {\n throw new MfaRecoveryError(\n 'Invalid mutation receipt',\n 'escrow_response_invalid',\n );\n }\n return value;\n}\n"]}
1
+ {"version":3,"file":"cubist-escrow-provider.js","sourceRoot":"","sources":["../../src/escrow-providers/cubist-escrow-provider.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,EACL,OAAO,EACP,EAAE,EACF,OAAO,EACP,GAAG,EACH,MAAM,EACN,IAAI,EACJ,OAAO,GACR,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,UAAU,EACV,aAAa,EACb,UAAU,EACV,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,mBAAmB,EACn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AAI,YAAY,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CACrD,CACyB,CAAC;AAC/B,CAAC;AAED,SAAS,sBAAsB,CAAC,KAAc;IAC5C,IACE,CAAC,EAAE,CAAC,KAAK,EAAE,uBAAuB,CAAC;QACnC,CAAC,iBAAiB,CAAC,KAAK,CAAC,cAAc,CAAC,UAAU,CAAC,EACnD,CAAC;QACD,MAAM,IAAI,gBAAgB,CACxB,kCAAkC,EAClC,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IACD,OAAO;QACL,cAAc,EAAE;YACd,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC,cAAc,CAAC,UAAU,CAAC;SACxD;QACD,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,cAAc,EAAE,KAAK,CAAC,cAAc;QACpC,SAAS,EAAE,KAAK,CAAC,SAAS;KAC3B,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IAClC,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,gBAAgB,CACxB,0BAA0B,EAC1B,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type {\n CubeSignerClient,\n InvokePolicyResponse,\n Version,\n} from '@cubist-labs/cubesigner-sdk';\nimport {\n array,\n integer,\n is,\n literal,\n min,\n string,\n type,\n unknown,\n} from '@metamask/superstruct';\nimport {\n bytesToHex,\n bytesToString,\n hexToBytes,\n isStrictHexString,\n} from '@metamask/utils';\n\nimport {\n hashMutationReceipt,\n receiptKeyId,\n verifySignature,\n} from '../crypto.js';\nimport { MfaRecoveryError, MutationRejectedError } from '../errors.js';\nimport { isMutationReceipt } from '../escrow-utils.js';\nimport type {\n AuthControllerToken,\n GetIdentifiersResponse,\n GetRecoverySecretResponse,\n Identifier,\n IdentifierAuthorization,\n Mutation,\n MutationPayload,\n MutationReceipt,\n PoPChallenge,\n RecoveryEscrowProvider,\n} from '../types.js';\n\nconst ESCROW_ID = 'cubist';\nconst DEFAULT_FUNCTION_ID = 'cubist_secret_escrow';\nconst DEFAULT_VERSION: Version = 'latest';\n\n/**\n * CubeSigner C2F client for the Cubist recovery escrow.\n *\n * Commands are sent with `policyInvoke` as `{ cmd, ...fields }` on\n * `cubist_secret_escrow`.\n * Escrow JSON is read from C2F stdout. Receipt signatures are checked locally\n * with the pinned receipt public key.\n */\nexport type CubistEscrowProviderOptions = {\n /**\n * Returns the authenticated CubeSigner client used to invoke the escrow C2F.\n * Called for every command, so the caller owns session caching and refresh.\n * Commands propagate its errors unchanged; `isAvailable` resolves `false`.\n */\n getClient: () => Promise<CubeSignerClient>;\n /**\n * Must match the escrow's AUTH_CONTROLLER_AUDIENCE configuration.\n */\n authAudience: string;\n /**\n * C2F name or named-policy id. Defaults to `cubist_secret_escrow`.\n */\n functionId?: string;\n /**\n * C2F version to invoke. Defaults to `latest`.\n */\n version?: Version;\n /**\n * Raw 32-byte X25519 public key of the escrow wrap key.\n */\n wrapPublicKey: Uint8Array;\n /**\n * Raw 32-byte Ed25519 public key of the escrow receipt key.\n */\n receiptPublicKey: Uint8Array;\n};\n\nexport class CubistEscrowProvider implements RecoveryEscrowProvider {\n readonly id = ESCROW_ID;\n\n readonly authAudience: string;\n\n readonly wrapPublicKey: Uint8Array;\n\n readonly #receiptPublicKey: Uint8Array;\n\n readonly #getClient: () => Promise<CubeSignerClient>;\n\n readonly #functionId: string;\n\n readonly #version: Version;\n\n /**\n * @param options - CubeSigner client getter, pinned keys, and optional C2F\n * id/version.\n */\n constructor(options: CubistEscrowProviderOptions) {\n this.authAudience = options.authAudience;\n this.wrapPublicKey = options.wrapPublicKey;\n this.#receiptPublicKey = options.receiptPublicKey;\n this.#getClient = options.getClient;\n this.#functionId = options.functionId ?? DEFAULT_FUNCTION_ID;\n this.#version = options.version ?? DEFAULT_VERSION;\n }\n\n /**\n * @returns Whether a ping of the escrow C2F is allowed and reports ok.\n */\n async isAvailable(): Promise<boolean> {\n try {\n await this.#invoke('ping', {}, parsePing);\n return true;\n } catch {\n return false;\n }\n }\n\n /**\n * @param authControllerToken - Approval for the profile the challenge is for.\n * @returns A proof-of-possession challenge for this escrow.\n */\n async generateChallenge(\n authControllerToken: AuthControllerToken,\n ): Promise<PoPChallenge> {\n return await this.#invoke(\n 'generateChallenge',\n { authControllerToken },\n parseChallenge,\n );\n }\n\n /**\n * @param authControllerToken - Approval for the profile to read.\n * @returns The enrolled identifiers and record version.\n */\n async getIdentifiers(\n authControllerToken: AuthControllerToken,\n ): Promise<GetIdentifiersResponse> {\n return await this.#invoke(\n 'getIdentifiers',\n { authControllerToken },\n parseIdentifiers,\n );\n }\n\n /**\n * @param authControllerToken - Request-bound approval for this read.\n * @param authorization - Identifier proof for this read.\n * @param requestId - Client request id bound into the read hash.\n * @param pkE - Ephemeral wrap public key for the response.\n * @returns The wrapped recovery secret and its version.\n */\n async getSecret(\n authControllerToken: AuthControllerToken,\n authorization: IdentifierAuthorization,\n requestId: string,\n pkE: Uint8Array,\n ): Promise<GetRecoverySecretResponse> {\n return await this.#invoke(\n 'getSecret',\n {\n identifierAuthorization: authorization,\n requestId,\n pkE,\n authControllerToken,\n },\n parseGetSecretResponse,\n );\n }\n\n /**\n * @param mutation - Mutation to apply.\n * @param authControllerToken - Request-bound AuthController token.\n * @param identifierAuthorizations - Identifier proofs sharing one challenge.\n * @param payload - Per-escrow mutation payload.\n * @param approvedIdentifiers - The approval's identifiers, which non-SIWE\n * payload identifiers must be drawn from.\n * @returns The escrow's mutation receipt.\n */\n async applyMutation(\n mutation: Mutation,\n authControllerToken: AuthControllerToken,\n identifierAuthorizations: IdentifierAuthorization[],\n payload: MutationPayload,\n approvedIdentifiers: Identifier[],\n ): Promise<MutationReceipt> {\n return await this.#invoke(\n 'applyMutation',\n {\n mutation,\n authControllerToken,\n identifierAuthorizations,\n payload,\n approvedIdentifiers,\n },\n parseReceipt,\n true, // a policy Deny did not apply the mutation\n );\n }\n\n /**\n * Verifies a receipt with the pinned receipt public key.\n *\n * @param receipt - Receipt returned by the escrow.\n * @param mutation - Mutation the receipt must acknowledge.\n * @param expectedEscrowId - Escrow identity expected by the caller.\n * @returns Whether the receipt is valid for this escrow.\n */\n async verifyReceipt(\n receipt: MutationReceipt,\n mutation: Mutation,\n expectedEscrowId: string,\n ): Promise<boolean> {\n if (\n receipt.mutationId !== mutation.id ||\n receipt.requestHash !== mutation.requestHash ||\n receipt.escrowId !== expectedEscrowId ||\n expectedEscrowId !== this.id ||\n receipt.version !== mutation.newVersion ||\n receipt.receiptKeyId !== (await receiptKeyId(this.#receiptPublicKey))\n ) {\n return false;\n }\n return await verifySignature(\n this.#receiptPublicKey,\n receipt.signature,\n await hashMutationReceipt({\n escrowId: receipt.escrowId,\n mutationId: receipt.mutationId,\n receiptKeyId: receipt.receiptKeyId,\n requestHash: receipt.requestHash,\n version: receipt.version,\n }),\n );\n }\n\n async #invoke<Result>(\n cmd: string,\n body: Record<string, unknown>,\n parse: (value: unknown) => Result,\n denyIsRejection = false,\n ): Promise<Result> {\n const client = await this.#getClient();\n let invocation: InvokePolicyResponse;\n try {\n invocation = await client.apiClient.policyInvoke(\n this.#functionId,\n this.#version,\n { request: toWire({ cmd, ...body }) },\n );\n } catch (error) {\n throw new MfaRecoveryError(\n error instanceof Error ? error.message : 'Escrow request failed',\n 'escrow_request_failed',\n );\n }\n const policy = invocation.response;\n if (policy.response !== 'Allow') {\n if (denyIsRejection && policy.response === 'Deny') {\n throw new MutationRejectedError(policy.reason);\n }\n throw new MfaRecoveryError(\n policy.response === 'Deny' ? policy.reason : policy.error,\n 'escrow_request_failed',\n );\n }\n let json: unknown;\n try {\n json = JSON.parse(bytesToString(hexToBytes(invocation.stdout)));\n } catch {\n throw new MfaRecoveryError(\n 'Escrow response was not JSON',\n 'escrow_response_invalid',\n );\n }\n return parse(json);\n }\n}\n\n// `type()` is non-strict, so unknown fields are ignored. The explicit field\n// mapping below still strips them from the returned value.\nconst PingStruct = type({ ok: literal(true) });\n\nconst ChallengeStruct = type({\n id: string(),\n escrowId: literal(ESCROW_ID),\n expiresAt: integer(),\n});\n\nconst IdentifiersStruct = type({\n identifiers: array(\n type({\n type: string(),\n namespace: string(),\n value: string(),\n verifier: unknown(),\n }),\n ),\n version: min(integer(), 0),\n});\n\nconst GetSecretResponseStruct = type({\n recoverySecret: type({ ciphertext: string() }),\n version: min(integer(), 0),\n lastMutationId: string(),\n wrapKeyId: string(),\n});\n\nfunction parsePing(value: unknown): void {\n if (!is(value, PingStruct)) {\n throw new MfaRecoveryError(\n 'Invalid ping response',\n 'escrow_response_invalid',\n );\n }\n}\n\nfunction parseChallenge(value: unknown): PoPChallenge {\n if (!is(value, ChallengeStruct)) {\n throw new MfaRecoveryError(\n 'Invalid PoP challenge',\n 'escrow_response_invalid',\n );\n }\n return {\n id: value.id,\n escrowId: value.escrowId,\n expiresAt: value.expiresAt,\n };\n}\n\nfunction parseIdentifiers(value: unknown): GetIdentifiersResponse {\n if (!is(value, IdentifiersStruct)) {\n throw new MfaRecoveryError(\n 'Invalid identifiers response',\n 'escrow_response_invalid',\n );\n }\n return {\n identifiers: value.identifiers.map((identifier) => ({\n type: identifier.type,\n namespace: identifier.namespace,\n value: identifier.value,\n ...(identifier.verifier === undefined\n ? {}\n : { verifier: identifier.verifier }),\n })),\n version: value.version,\n };\n}\n\n/**\n * Encodes a request for the escrow wire: every byte field becomes a 0x-prefixed\n * hex string. Typed arrays have no `toJSON`, so the replacer sees them directly.\n *\n * @param value - Request body.\n * @returns JSON-compatible request body.\n */\nfunction toWire(value: Record<string, unknown>): Record<string, unknown> {\n return JSON.parse(\n JSON.stringify(value, (_key, node: unknown) =>\n node instanceof Uint8Array ? bytesToHex(node) : node,\n ),\n ) as Record<string, unknown>;\n}\n\nfunction parseGetSecretResponse(value: unknown): GetRecoverySecretResponse {\n if (\n !is(value, GetSecretResponseStruct) ||\n !isStrictHexString(value.recoverySecret.ciphertext)\n ) {\n throw new MfaRecoveryError(\n 'Invalid recovery secret response',\n 'escrow_response_invalid',\n );\n }\n return {\n recoverySecret: {\n ciphertext: hexToBytes(value.recoverySecret.ciphertext),\n },\n version: value.version,\n lastMutationId: value.lastMutationId,\n wrapKeyId: value.wrapKeyId,\n };\n}\n\nfunction parseReceipt(value: unknown): MutationReceipt {\n if (!isMutationReceipt(value)) {\n throw new MfaRecoveryError(\n 'Invalid mutation receipt',\n 'escrow_response_invalid',\n );\n }\n return value;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/mfa-recovery-controller",
3
- "version": "0.0.0-preview-c33f4af3a",
3
+ "version": "0.0.0-preview-e1e83cae0",
4
4
  "description": "Manages MFA recovery flows across MetaMask clients",
5
5
  "keywords": [
6
6
  "Ethereum",