@metamask/keyring-utils
Version:
MetaMask Keyring utils
46 lines • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ETH_SCOPE_EOA = void 0;
exports.isScopeEqual = isScopeEqual;
exports.isScopeEqualToAny = isScopeEqualToAny;
const utils_1 = require("@metamask/utils");
// We do not use the `EthScope` from `@metamask/keyring-api` here to avoid pulling it
// as a dependency.
exports.ETH_SCOPE_EOA = `${utils_1.KnownCaipNamespace.Eip155}:0`;
// We use a string prefix when comparing EOAs to avoid unecessary splits.
const ETH_SCOPE_PREFIX = `${utils_1.KnownCaipNamespace.Eip155}:`;
/**
* Check if scope matches another scope. It also supports the special
* case of `eip155:0` for EVM EOA chain ID which is compatible with any EVM chain
* ID (`eip155:*`).
*
* @param scope - The scope (CAIP-2 chain ID) to check.
* @param other - Another scope to compare to.
* @returns True if both scope are compatible, false otherwise.
*/
function isScopeEqual(scope, other) {
const isScopeEoa = scope === exports.ETH_SCOPE_EOA;
const isOtherEoa = other === exports.ETH_SCOPE_EOA;
// Special case for EOA scopes (we check on both sides).
if (isScopeEoa) {
return other.startsWith(ETH_SCOPE_PREFIX);
}
if (isOtherEoa) {
return scope.startsWith(ETH_SCOPE_PREFIX);
}
// Normal case, if both scopes strictly match, then they are compatible.
return scope === other;
}
/**
* Check if `scope` matches any scope from `scopes`. It also supports the special
* case of `eip155:0` for EVM EOA chain ID which is compatible with any EVM chain
* ID (`eip155:*`).
*
* @param scope - The scope (CAIP-2 chain ID) to check.
* @param scopes - The list of scopes to check against.
* @returns True if `scope` matches any scope from `scopes`, false otherwise.
*/
function isScopeEqualToAny(scope, scopes) {
return scopes.some((other) => isScopeEqual(scope, other));
}
//# sourceMappingURL=scopes.cjs.map