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AI,CAAC,CAAC;QAE7B,4EAA4E;QAC5E,MAAM,MAAM,GAAG,IAAI,CAAC,MAA+B,CAAC;QACpD,MAAM,IAAI,GAAG,MAAM,IAAA,iCAAyB,EAC1C,eAAe,EACf,MAAM,CAAC,MAAM,CACd,CAAC;QAEF,MAAM,IAAI,GAAG,MAAM,4BAAiB,CAAC,QAAQ,CAC3C;YACE,cAAc,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,SAAS,MAAM,CAAC,QAAQ,GAAG,CAAC;YAChE,OAAO,EAAE,SAAS;SACnB,EACD,qBAAqB,EAAE,CACxB,CAAC;QAEF,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AA3BD,sEA2BC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport { BIP44CoinTypeNode } 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  GetBip44EntropyParams,\n  GetBip44EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { getValueFromEntropySource } from '../utils';\n\nconst targetName = 'snap_getBip44Entropy';\n\nexport type GetBip44EntropyMethodHooks = {\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 GetBip44EntropySpecificationBuilderOptions = {\n  methodHooks: GetBip44EntropyMethodHooks;\n};\n\ntype GetBip44EntropySpecification = ValidPermissionSpecification<{\n  permissionType: PermissionType.RestrictedMethod;\n  targetName: typeof targetName;\n  methodImplementation: ReturnType<typeof getBip44EntropyImplementation>;\n  allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n  validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip44Entropy` permission.\n * `snap_getBip44Entropy_*` lets the Snap control private keys for a particular\n * BIP-32 coin type.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_getBip44Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n  PermissionType.RestrictedMethod,\n  GetBip44EntropySpecificationBuilderOptions,\n  GetBip44EntropySpecification\n> = ({ methodHooks }: GetBip44EntropySpecificationBuilderOptions) => {\n  return {\n    permissionType: PermissionType.RestrictedMethod,\n    targetName,\n    allowedCaveats: [SnapCaveatType.PermittedCoinTypes],\n    methodImplementation: getBip44EntropyImplementation(methodHooks),\n    validator: ({ caveats }) => {\n      if (\n        caveats?.length !== 1 ||\n        caveats[0].type !== SnapCaveatType.PermittedCoinTypes\n      ) {\n        throw rpcErrors.invalidParams({\n          message: `Expected a single \"${SnapCaveatType.PermittedCoinTypes}\" caveat.`,\n        });\n      }\n    },\n    subjectTypes: [SubjectType.Snap],\n  };\n};\n\nconst methodHooks: MethodHooksObject<GetBip44EntropyMethodHooks> = {\n  getMnemonicSeed: true,\n  getUnlockPromise: true,\n  getClientCryptography: true,\n};\n\nexport const getBip44EntropyBuilder = Object.freeze({\n  targetName,\n  specificationBuilder,\n  methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip44Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed\n * of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * 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 `BIP44CoinTypeNode`.\n * @throws If the params are invalid.\n */\nexport function getBip44EntropyImplementation({\n  getMnemonicSeed,\n  getUnlockPromise,\n  getClientCryptography,\n}: GetBip44EntropyMethodHooks) {\n  return async function getBip44Entropy(\n    args: RestrictedMethodOptions<GetBip44EntropyParams>,\n  ): Promise<GetBip44EntropyResult> {\n    await getUnlockPromise(true);\n\n    // `args.params` is validated by the decorator, so it's safe to assert here.\n    const params = args.params as GetBip44EntropyParams;\n    const seed = await getValueFromEntropySource(\n      getMnemonicSeed,\n      params.source,\n    );\n\n    const node = await BIP44CoinTypeNode.fromSeed(\n      {\n        derivationPath: [seed, `bip32:44'`, `bip32:${params.coinType}'`],\n        network: 'mainnet',\n      },\n      getClientCryptography(),\n    );\n\n    return node.toJSON();\n  };\n}\n"]}