UNPKG

@metamask/keyring-utils

Version:
1 lines 2.68 kB
{"version":3,"file":"scopes.cjs","sourceRoot":"","sources":["../src/scopes.ts"],"names":[],"mappings":";;;AAmBA,oCAcC;AAWD,8CAKC;AAhDD,2CAAqD;AAErD,qFAAqF;AACrF,mBAAmB;AACN,QAAA,aAAa,GAAG,GAAG,0BAAkB,CAAC,MAAM,IAAI,CAAC;AAE9D,yEAAyE;AACzE,MAAM,gBAAgB,GAAG,GAAG,0BAAkB,CAAC,MAAM,GAAG,CAAC;AAEzD;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAC,KAAkB,EAAE,KAAkB;IACjE,MAAM,UAAU,GAAG,KAAK,KAAK,qBAAa,CAAC;IAC3C,MAAM,UAAU,GAAG,KAAK,KAAK,qBAAa,CAAC;IAE3C,wDAAwD;IACxD,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAC5C,CAAC;IAED,wEAAwE;IACxE,OAAO,KAAK,KAAK,KAAK,CAAC;AACzB,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,iBAAiB,CAC/B,KAAkB,EAClB,MAAqB;IAErB,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["import type { CaipChainId } from '@metamask/utils';\nimport { KnownCaipNamespace } from '@metamask/utils';\n\n// We do not use the `EthScope` from `@metamask/keyring-api` here to avoid pulling it\n// as a dependency.\nexport const ETH_SCOPE_EOA = `${KnownCaipNamespace.Eip155}:0`;\n\n// We use a string prefix when comparing EOAs to avoid unecessary splits.\nconst ETH_SCOPE_PREFIX = `${KnownCaipNamespace.Eip155}:`;\n\n/**\n * Check if scope matches another scope. It also supports the special\n * case of `eip155:0` for EVM EOA chain ID which is compatible with any EVM chain\n * ID (`eip155:*`).\n *\n * @param scope - The scope (CAIP-2 chain ID) to check.\n * @param other - Another scope to compare to.\n * @returns True if both scope are compatible, false otherwise.\n */\nexport function isScopeEqual(scope: CaipChainId, other: CaipChainId): boolean {\n const isScopeEoa = scope === ETH_SCOPE_EOA;\n const isOtherEoa = other === ETH_SCOPE_EOA;\n\n // Special case for EOA scopes (we check on both sides).\n if (isScopeEoa) {\n return other.startsWith(ETH_SCOPE_PREFIX);\n }\n if (isOtherEoa) {\n return scope.startsWith(ETH_SCOPE_PREFIX);\n }\n\n // Normal case, if both scopes strictly match, then they are compatible.\n return scope === other;\n}\n\n/**\n * Check if `scope` matches any scope from `scopes`. It also supports the special\n * case of `eip155:0` for EVM EOA chain ID which is compatible with any EVM chain\n * ID (`eip155:*`).\n *\n * @param scope - The scope (CAIP-2 chain ID) to check.\n * @param scopes - The list of scopes to check against.\n * @returns True if `scope` matches any scope from `scopes`, false otherwise.\n */\nexport function isScopeEqualToAny(\n scope: CaipChainId,\n scopes: CaipChainId[],\n): boolean {\n return scopes.some((other) => isScopeEqual(scope, other));\n}\n"]}