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C,IAAI;YACjB,oBAAoB;YACpB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;QAEH,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,mBAAmB,CAAC;QAClC,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC;AA3DD,0EA2DC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n  PermissionSpecificationBuilder,\n  PermissionValidatorConstraint,\n  RestrictedMethodOptions,\n  ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n  GetBip32PublicKeyParams,\n  GetBip32PublicKeyResult,\n} from '@metamask/snaps-sdk';\nimport {\n  bip32entropy,\n  Bip32PathStruct,\n  CurveStruct,\n  SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport { boolean, object, optional, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport {\n  getValueFromEntropySource,\n  getNodeFromMnemonic,\n  getNodeFromSeed,\n} from '../utils';\n\nconst targetName = 'snap_getBip32PublicKey';\n\nexport type GetBip32PublicKeyMethodHooks = {\n  /**\n   * Get the mnemonic of the provided source. If no source is provided, the\n   * mnemonic of the primary keyring will be returned.\n   *\n   * @param source - The optional ID of the source to get the mnemonic of.\n   * @returns The mnemonic of the provided source, or the default source if no\n   * source is provided.\n   */\n  getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;\n\n  /**\n   * Get the mnemonic seed of the provided source. If no source is provided, the\n   * mnemonic seed of the primary keyring will be returned.\n   *\n   * @param source - The optional ID of the source to get the mnemonic of.\n   * @returns The mnemonic seed of the provided source, or the default source if no\n   * source is provided.\n   */\n  getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;\n\n  /**\n   * Waits for the extension to be unlocked.\n   *\n   * @returns A promise that resolves once the extension is unlocked.\n   */\n  getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n  /**\n   * Get the cryptographic functions to use for the client. This may return an\n   * empty object or `undefined` to fall back to the default cryptographic\n   * functions.\n   *\n   * @returns The cryptographic functions to use for the client.\n   */\n  getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip32PublicKeySpecificationBuilderOptions = {\n  methodHooks: GetBip32PublicKeyMethodHooks;\n};\n\ntype GetBip32PublicKeySpecification = ValidPermissionSpecification<{\n  permissionType: PermissionType.RestrictedMethod;\n  targetName: typeof targetName;\n  methodImplementation: ReturnType<typeof getBip32PublicKeyImplementation>;\n  allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n  validator: PermissionValidatorConstraint;\n}>;\n\nexport const Bip32PublicKeyArgsStruct = bip32entropy(\n  object({\n    path: Bip32PathStruct,\n    curve: CurveStruct,\n    compressed: optional(boolean()),\n    source: optional(string()),\n  }),\n);\n\n/**\n * The specification builder for the `snap_getBip32PublicKey` permission.\n * `snap_getBip32PublicKey` lets the Snap retrieve public keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32PublicKey` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n  PermissionType.RestrictedMethod,\n  GetBip32PublicKeySpecificationBuilderOptions,\n  GetBip32PublicKeySpecification\n> = ({ methodHooks }: GetBip32PublicKeySpecificationBuilderOptions) => {\n  return {\n    permissionType: PermissionType.RestrictedMethod,\n    targetName,\n    allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n    methodImplementation: getBip32PublicKeyImplementation(methodHooks),\n    validator: ({ caveats }) => {\n      if (\n        caveats?.length !== 1 ||\n        caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n      ) {\n        throw rpcErrors.invalidParams({\n          message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n        });\n      }\n    },\n    subjectTypes: [SubjectType.Snap],\n  };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32PublicKeyMethodHooks> = {\n  getMnemonic: true,\n  getMnemonicSeed: true,\n  getUnlockPromise: true,\n  getClientCryptography: true,\n};\n\nexport const getBip32PublicKeyBuilder = Object.freeze({\n  targetName,\n  specificationBuilder,\n  methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32PublicKey`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a public key.\n * @throws If the params are invalid.\n */\nexport function getBip32PublicKeyImplementation({\n  getMnemonic,\n  getMnemonicSeed,\n  getUnlockPromise,\n  getClientCryptography,\n}: GetBip32PublicKeyMethodHooks) {\n  return async function getBip32PublicKey(\n    args: RestrictedMethodOptions<GetBip32PublicKeyParams>,\n  ): Promise<GetBip32PublicKeyResult> {\n    await getUnlockPromise(true);\n\n    assertStruct(\n      args.params,\n      Bip32PublicKeyArgsStruct,\n      'Invalid BIP-32 public key params',\n      rpcErrors.invalidParams,\n    );\n\n    const { params } = args;\n\n    // Using the seed is much faster, but we can only do it for these specific curves.\n    if (params.curve === 'secp256k1' || params.curve === 'ed25519') {\n      const seed = await getValueFromEntropySource(\n        getMnemonicSeed,\n        params.source,\n      );\n\n      const node = await getNodeFromSeed({\n        curve: params.curve,\n        path: params.path,\n        seed,\n        cryptographicFunctions: getClientCryptography(),\n      });\n\n      if (params.compressed) {\n        return node.compressedPublicKey;\n      }\n\n      return node.publicKey;\n    }\n\n    const secretRecoveryPhrase = await getValueFromEntropySource(\n      getMnemonic,\n      params.source,\n    );\n\n    const node = await getNodeFromMnemonic({\n      curve: params.curve,\n      path: params.path,\n      secretRecoveryPhrase,\n      cryptographicFunctions: getClientCryptography(),\n    });\n\n    if (params.compressed) {\n      return node.compressedPublicKey;\n    }\n\n    return node.publicKey;\n  };\n}\n"]}