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E,OAAO,CAAC,GAAG,cAAc,CAAC,IAAI,EAAE,CAAC;CACxC,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;IAC3C,GAAG,gBAAgB,CAAC,MAAM;IAE1B;;OAEG;IACH,OAAO,EAAE,sBAAsB;IAE/B;;OAEG;IACH,IAAI,EAAE,OAAO,CAAC,GAAG,cAAc,CAAC,MAAM,EAAE,CAAC;CAC1C,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC;IACzC,GAAG,gBAAgB,CAAC,MAAM;IAE1B;;OAEG;IACH,OAAO,EAAE,oBAAoB;IAE7B;;OAEG;IACH,IAAI,EAAE,OAAO,CAAC,GAAG,cAAc,CAAC,IAAI,EAAE,CAAC;CACxC,CAAC,CAAC","sourcesContent":["import { object } from '@metamask/keyring-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n  string,\n  array,\n  enums,\n  refine,\n  literal,\n  nonempty,\n} from '@metamask/superstruct';\nimport { AddressType, getAddressInfo } from 'bitcoin-address-validation';\n\nimport {\n  BtcAccountType,\n  CaipChainIdStruct,\n  KeyringAccountStruct,\n} from '../api';\n\nconst validateAddress = (\n  address: string,\n  type: AddressType,\n): boolean | Error => {\n  try {\n    const addressInfo = getAddressInfo(address);\n    if (addressInfo.type === type) {\n      return true;\n    }\n    return new Error(`Invalid ${type} address`);\n  } catch (error) {\n    return new Error(\n      `Failed to decode ${type} address: ${(error as Error).message}`,\n    );\n  }\n};\n\nexport const BtcP2pkhAddressStruct = refine(\n  string(),\n  'BtcP2pkhAddressStruct',\n  (address: string) => {\n    return validateAddress(address, AddressType.p2pkh);\n  },\n);\n\nexport const BtcP2shAddressStruct = refine(\n  string(),\n  'BtcP2shAddressStruct',\n  (address: string) => {\n    return validateAddress(address, AddressType.p2sh);\n  },\n);\n\nexport const BtcP2wpkhAddressStruct = refine(\n  string(),\n  'BtcP2wpkhAddressStruct',\n  (address: string) => {\n    return validateAddress(address, AddressType.p2wpkh);\n  },\n);\n\nexport const BtcP2trAddressStruct = refine(\n  string(),\n  'BtcP2trAddressStruct',\n  (address: string) => {\n    return validateAddress(address, AddressType.p2tr);\n  },\n);\n\n/**\n * Supported Bitcoin methods.\n */\nexport enum BtcMethod {\n  SignPsbt = 'signPsbt',\n  ComputeFee = 'computeFee',\n  FillPsbt = 'fillPsbt',\n  BroadcastPsbt = 'broadcastPsbt',\n  SendTransfer = 'sendTransfer',\n  GetUtxo = 'getUtxo',\n  ListUtxos = 'listUtxos',\n  PublicDescriptor = 'publicDescriptor',\n  SignMessage = 'signMessage',\n}\n\nconst BtcAccountStruct = object({\n  ...KeyringAccountStruct.schema,\n\n  /**\n   * Account supported scopes (CAIP-2 chain ID).\n   */\n  scopes: nonempty(array(CaipChainIdStruct)),\n\n  /**\n   * Account supported methods.\n   */\n  methods: array(enums(Object.values(BtcMethod))),\n});\n\nexport const BtcP2pkhAccountStruct = object({\n  ...BtcAccountStruct.schema,\n\n  /**\n   * Account P2PKH address.\n   */\n  address: BtcP2pkhAddressStruct,\n\n  /**\n   * Account type.\n   */\n  type: literal(`${BtcAccountType.P2pkh}`),\n});\n\nexport const BtcP2shAccountStruct = object({\n  ...BtcAccountStruct.schema,\n\n  /**\n   * Account P2SH address.\n   */\n  address: BtcP2shAddressStruct,\n\n  /**\n   * Account type.\n   */\n  type: literal(`${BtcAccountType.P2sh}`),\n});\n\nexport const BtcP2wpkhAccountStruct = object({\n  ...BtcAccountStruct.schema,\n\n  /**\n   * Account P2WPKH address.\n   */\n  address: BtcP2wpkhAddressStruct,\n\n  /**\n   * Account type.\n   */\n  type: literal(`${BtcAccountType.P2wpkh}`),\n});\n\nexport const BtcP2trAccountStruct = object({\n  ...BtcAccountStruct.schema,\n\n  /**\n   * Account P2TR address.\n   */\n  address: BtcP2trAddressStruct,\n\n  /**\n   * Account type.\n   */\n  type: literal(`${BtcAccountType.P2tr}`),\n});\n\nexport type BtcP2pkhAccount = Infer<typeof BtcP2pkhAccountStruct>;\nexport type BtcP2shAccount = Infer<typeof BtcP2shAccountStruct>;\nexport type BtcP2wpkhAccount = Infer<typeof BtcP2wpkhAccountStruct>;\nexport type BtcP2trAccount = Infer<typeof BtcP2trAccountStruct>;\n"]}