UNPKG

@metamask/eip-5792-middleware

Version:

Implements the JSON-RPC methods for sending multiple calls from the user's wallet, and checking their status, as referenced in EIP-5792

1 lines 6.53 kB
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,qDAA+E;AAE/E,uDAAiD;AAGjD,+CAA+C;AAG/C;;;;;;;GAOG;AACH,SAAgB,qBAAqB,CACnC,cAAmB,EACnB,SAA2B;IAE3B,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,IAAI,CACjC,6BAA6B,CAC9B,CAAC,gBAAgB,CAAC;IAEnB,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,CAC1C,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,cAAc,CAAC,WAAW,EAAE,CACpE,CAAC;IAEF,MAAM,WAAW,GAAG,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC;IAErD,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,IAAI,yBAAY,CACpB,4BAAgB,CAAC,eAAe,EAChC,2DAA2D,CAC5D,CAAC;KACH;IAED,OAAO,WAA2B,CAAC;AACrC,CAAC;AAtBD,sDAsBC;AAED;;;;;;;;;;GAUG;AACI,KAAK,UAAU,6BAA6B,CACjD,OAAY,EACZ,GAAmB,EACnB,EAAE,WAAW,EAA+D;IAE5E,IACE,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,CAAC,MAAM,GAAG,CAAC;QAClB,gBAAgB,CAAC,OAAO,CAAC,EACzB;QACA,iEAAiE;QACjE,+CAA+C;QAC/C,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,GAAG,CAAC,CAAC;QAExC,MAAM,kBAAkB,GAAa,QAAQ,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAC7D,QAAQ,CAAC,WAAW,EAAE,CACvB,CAAC;QAEF,MAAM,iBAAiB,GAAG,OAAO,CAAC,WAAW,EAAS,CAAC;QAEvD,IAAI,kBAAkB,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE;YAClD,OAAO,iBAAiB,CAAC;SAC1B;QAED,MAAM,2BAAc,CAAC,YAAY,EAAE,CAAC;KACrC;IAED,MAAM,sBAAS,CAAC,aAAa,CAAC;QAC5B,OAAO,EAAE,uDAAuD;KACjE,CAAC,CAAC;AACL,CAAC;AA9BD,sEA8BC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAC5B,KAA2B,EAC3B,MAA0B;IAE1B,MAAM,CAAC,KAAK,CAAC,GAAG,IAAA,sBAAQ,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAExC,IAAI,KAAK,EAAE;QACT,MAAM,sBAAS,CAAC,aAAa,CAC3B,qBAAqB,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAC/C,CAAC;KACH;AACH,CAAC;AAXD,wCAWC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,GAAW;IAC1C,0BAA0B;IAC1B,OAAO,GAAG,CAAC,MAAM,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AACnC,CAAC;AAHD,4CAGC;AAED;;;;;;GAMG;AACH,SAAS,qBAAqB,CAAC,KAAkB,EAAE,OAAe;IAChE,OAAO,GAAG,OAAO,OAAO,KAAK;SAC1B,QAAQ,EAAE;SACV,GAAG,CACF,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,OAAO,EAAE,CACxE;SACA,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import type { KeyringTypes } from '@metamask/keyring-controller';\nimport { JsonRpcError, providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Struct, StructError } from '@metamask/superstruct';\nimport { validate } from '@metamask/superstruct';\nimport type { Hex, JsonRpcRequest } from '@metamask/utils';\n\nimport { EIP5792ErrorCode } from './constants';\nimport type { EIP5792Messenger } from './types';\n\n/**\n * Retrieves the keyring type for a given account address.\n *\n * @param accountAddress - The account address to look up.\n * @param messenger - Messenger instance for controller communication.\n * @returns The keyring type associated with the account.\n * @throws JsonRpcError if the account type is unknown or not found.\n */\nexport function getAccountKeyringType(\n accountAddress: Hex,\n messenger: EIP5792Messenger,\n): KeyringTypes {\n const { accounts } = messenger.call(\n 'AccountsController:getState',\n ).internalAccounts;\n\n const account = Object.values(accounts).find(\n (acc) => acc.address.toLowerCase() === accountAddress.toLowerCase(),\n );\n\n const keyringType = account?.metadata?.keyring?.type;\n\n if (!keyringType) {\n throw new JsonRpcError(\n EIP5792ErrorCode.RejectedUpgrade,\n 'EIP-7702 upgrade not supported as account type is unknown',\n );\n }\n\n return keyringType as KeyringTypes;\n}\n\n/**\n * Validates and normalizes a keyholder address for EIP-5792 operations.\n *\n * @param address - The Ethereum address to validate and normalize.\n * @param req - The JSON-RPC request object for permission checking.\n * @param options - Configuration object containing the getAccounts function.\n * @param options.getAccounts - Function to retrieve accounts for the requester.\n * @returns A normalized (lowercase) hex address if valid and authorized.\n * @throws JsonRpcError with unauthorized error if the requester doesn't have permission to access the address.\n * @throws JsonRpcError with invalid params if the address format is invalid.\n */\nexport async function validateAndNormalizeKeyholder(\n address: Hex,\n req: JsonRpcRequest,\n { getAccounts }: { getAccounts: (req: JsonRpcRequest) => Promise<string[]> },\n): Promise<Hex> {\n if (\n typeof address === 'string' &&\n address.length > 0 &&\n resemblesAddress(address)\n ) {\n // Ensure that an \"unauthorized\" error is thrown if the requester\n // does not have the `eth_accounts` permission.\n const accounts = await getAccounts(req);\n\n const normalizedAccounts: string[] = accounts.map((_address) =>\n _address.toLowerCase(),\n );\n\n const normalizedAddress = address.toLowerCase() as Hex;\n\n if (normalizedAccounts.includes(normalizedAddress)) {\n return normalizedAddress;\n }\n\n throw providerErrors.unauthorized();\n }\n\n throw rpcErrors.invalidParams({\n message: `Invalid parameters: must provide an Ethereum address.`,\n });\n}\n\n/**\n * Validates parameters against a Superstruct schema and throws an error if validation fails.\n *\n * @param value - The value to validate against the struct schema.\n * @param struct - The Superstruct schema to validate against.\n * @throws JsonRpcError with invalid params if the value doesn't match the struct schema.\n */\nexport function validateParams<ParamsType>(\n value: unknown | ParamsType,\n struct: Struct<ParamsType>,\n): asserts value is ParamsType {\n const [error] = validate(value, struct);\n\n if (error) {\n throw rpcErrors.invalidParams(\n formatValidationError(error, `Invalid params`),\n );\n }\n}\n\n/**\n * Checks if a string resembles an Ethereum address format.\n *\n * @param str - The string to check for address-like format.\n * @returns True if the string has the correct length for an Ethereum address.\n */\nexport function resemblesAddress(str: string): boolean {\n // hex prefix 2 + 20 bytes\n return str.length === 2 + 20 * 2;\n}\n\n/**\n * Formats a Superstruct validation error into a human-readable string.\n *\n * @param error - The Superstruct validation error to format.\n * @param message - The base error message to prepend to the formatted details.\n * @returns A formatted error message string with validation failure details.\n */\nfunction formatValidationError(error: StructError, message: string): string {\n return `${message}\\n\\n${error\n .failures()\n .map(\n (f) => `${f.path.join(' > ')}${f.path.length ? ' - ' : ''}${f.message}`,\n )\n .join('\\n')}`;\n}\n"]}