@metamask-previews/eth-json-rpc-middleware 22.0.1-preview-cb4a07d5 → 22.0.1-preview-eb60826c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -94,8 +94,8 @@ class BlockCacheStrategy {
94
94
  // clear old caches
95
95
  Object.keys(__classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f"))
96
96
  .map(Number)
97
- .filter((num) => num < oldBlockNumber)
98
- .forEach((num) => delete __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[num]);
97
+ .filter((value) => value < oldBlockNumber)
98
+ .forEach((value) => delete __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[value]);
99
99
  }
100
100
  }
101
101
  _BlockCacheStrategy_cache = new WeakMap();
@@ -1 +1 @@
1
- {"version":3,"file":"block-cache.cjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,uDAAoE;AAOpE,6CAMuB;AAEvB,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAc,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMpE,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QAFA,4CAAc;QAGZ,uBAAA,IAAI,6BAAU,EAAE,MAAA,CAAC;IACnB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,IAAI,UAAU,GAAe,uBAAA,IAAI,iCAAO,CAAC,WAAW,CAAC,CAAC;QACtD,+BAA+B;QAC/B,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,QAAQ,GAAe,EAAE,CAAC;YAChC,uBAAA,IAAI,iCAAO,CAAC,WAAW,CA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ClB,CAAC;YACF,UAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;YAC1C,oBAAoB,GAAG,iBAAiB,CAAC;QAC3C,CAAC;aAAM,CAAC;YACN,uBAAuB;YACvB,oBAAoB,GAAG,QAAQ,CAAC;QAClC,CAAC;QAED,mCAAmC;QACnC,MAAM,WAAW,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;QACtE,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC9B,aAAa;YACb,8CAA8C;YAC9C,GAAG,CACD,iEAAiE,EACjE,oBAAoB,CACrB,CAAC;YACF,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;YAE5B,sBAAsB;YACtB,oEAAoE;YACpE,wCAAwC;YACxC,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;YACrC,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAe,CAAC,CAAC;YACnE,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAzFD,gEAyFC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n #cache: Cache;\n\n constructor() {\n this.#cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.#cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.#cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.#cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.#cache[num]);\n }\n}\n\n/**\n * Creates a middleware that caches block-related requests.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The block cache middleware.\n */\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
1
+ {"version":3,"file":"block-cache.cjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,uDAAoE;AAOpE,6CAMuB;AAEvB,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAc,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMpE,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QAFA,4CAAc;QAGZ,uBAAA,IAAI,6BAAU,EAAE,MAAA,CAAC;IACnB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,IAAI,UAAU,GAAe,uBAAA,IAAI,iCAAO,CAAC,WAAW,CAAC,CAAC;QACtD,+BAA+B;QAC/B,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,QAAQ,GAAe,EAAE,CAAC;YAChC,uBAAA,IAAI,iCAAO,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC;YACpC,UAAU,GAAG,QAAQ,CAAC;QACxB,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,GAAG,CACP,OAAuB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vB,oBAAoB,GAAG,QAAQ,CAAC;QAClC,CAAC;QAED,mCAAmC;QACnC,MAAM,WAAW,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;QACtE,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC9B,aAAa;YACb,8CAA8C;YAC9C,GAAG,CACD,iEAAiE,EACjE,oBAAoB,CACrB,CAAC;YACF,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;YAE5B,sBAAsB;YACtB,oEAAoE;YACpE,wCAAwC;YACxC,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;YACrC,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAe,CAAC,CAAC;YACnE,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAzFD,gEAyFC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n #cache: Cache;\n\n constructor() {\n this.#cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.#cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.#cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.#cache)\n .map(Number)\n .filter((value) => value < oldBlockNumber)\n .forEach((value) => delete this.#cache[value]);\n }\n}\n\n/**\n * Creates a middleware that caches block-related requests.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The block cache middleware.\n */\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
@@ -91,8 +91,8 @@ class BlockCacheStrategy {
91
91
  // clear old caches
92
92
  Object.keys(__classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f"))
93
93
  .map(Number)
94
- .filter((num) => num < oldBlockNumber)
95
- .forEach((num) => delete __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[num]);
94
+ .filter((value) => value < oldBlockNumber)
95
+ .forEach((value) => delete __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[value]);
96
96
  }
97
97
  }
98
98
  _BlockCacheStrategy_cache = new WeakMap();
@@ -1 +1 @@
1
- {"version":3,"file":"block-cache.mjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAOpE,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,aAAa,EACd,0BAAsB;AAEvB,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAc,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMpE,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QAFA,4CAAc;QAGZ,uBAAA,IAAI,6BAAU,EAAE,MAAA,CAAC;IACnB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,I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GAAG,CAAC,qDAAqD,CAAC,CAAC;YAC3D,MAAM,iBAAiB,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;YAE9D,sCAAsC;YACtC,GAAG,CACD,sDAAsD,EACtD,iBAAiB,CAClB,CAAC;YACF,UAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;YAC1C,oBAAoB,GAAG,iBAAiB,CAAC;QAC3C,CAAC;aAAM,CAAC;YACN,uBAAuB;YACvB,oBAAoB,GAAG,QAAQ,CAAC;QAClC,CAAC;QAED,mCAAmC;QACnC,MAAM,WAAW,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;QACtE,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC9B,aAAa;YACb,8CAA8C;YAC9C,GAAG,CACD,iEAAiE,EACjE,oBAAoB,CACrB,CAAC;YACF,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;YAE5B,sBAAsB;YACtB,oEAAoE;YACpE,wCAAwC;YACxC,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;YACrC,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAe,CAAC,CAAC;YACnE,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n #cache: Cache;\n\n constructor() {\n this.#cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.#cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.#cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.#cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.#cache[num]);\n }\n}\n\n/**\n * Creates a middleware that caches block-related requests.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The block cache middleware.\n */\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
1
+ {"version":3,"file":"block-cache.mjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAOpE,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,aAAa,EACd,0BAAsB;AAEvB,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAc,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMpE,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QAFA,4CAAc;QAGZ,uBAAA,IAAI,6BAAU,EAAE,MAAA,CAAC;IACnB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,IAAI,UAAU,GAAe,uBAAA,IAAI,iCAAO,CAAC,WAAW,CAAC,CAAC;QACtD,+BAA+B;QAC/B,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EAAE,CAAC;YAE9D,sCAAsC;YACtC,GAAG,CACD,sDAAsD,EACtD,iBAAiB,CAClB,CAAC;YACF,UAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;YAC1C,oBAAoB,GAAG,iBAAiB,CAAC;QAC3C,CAAC;aAAM,CAAC;YACN,uBAAuB;YACvB,oBAAoB,GAAG,QAAQ,CAAC;QAClC,CAAC;QAED,mCAAmC;QACnC,MAAM,WAAW,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;QACtE,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC9B,aAAa;YACb,8CAA8C;YAC9C,GAAG,CACD,iEAAiE,EACjE,oBAAoB,CACrB,CAAC;YACF,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;YAE5B,sBAAsB;YACtB,oEAAoE;YACpE,wCAAwC;YACxC,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;YACrC,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAe,CAAC,CAAC;YACnE,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n #cache: Cache;\n\n constructor() {\n this.#cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.#cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.#cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.#cache)\n .map(Number)\n .filter((value) => value < oldBlockNumber)\n .forEach((value) => delete this.#cache[value]);\n }\n}\n\n/**\n * Creates a middleware that caches block-related requests.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The block cache middleware.\n */\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"inflight-cache.cjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":";;;AAIA,2CAAwD;AAGxD,uDAAoE;AACpE,6CAA0D;AAW1D,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,SAAgB,6BAA6B;IAK3C,MAAM,gBAAgB,GAAoB,EAAE,CAAC;IAE7C,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QAC1C,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC;YAC7B,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,MAAM,OAAO,GAAkB,IAAA,iCAAyB,EAAC,OAAO,CAAC,CAAC;QAClE,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,GAAG,CAAC,gDAAgD,EAAE,OAAO,CAAC,CAAC;YAC/D,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,8BAA8B;QAC9B,IAAI,qBAAqB,GAAqB,gBAAgB,CAAC,OAAO,CAAC,CAAC;QACxE,sDAAsD;QACtD,IAAI,qBAAqB,EAAE,CAAC;YAC1B,2DAA2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jB,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CACzB,aAA8D,EAC9D,qBAAuC;IAEvC,6DAA6D;IAC7D,UAAU,CAAC,GAAG,EAAE;QACd,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,EAAE,EAAE;YACrD,IAAI,QAAQ,IAAI,aAAa,EAAE,CAAC;gBAC9B,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YAClC,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport { createDeferredPromise } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandler = [\n (result: Readonly<Json>) => void,\n (error: unknown) => void,\n];\n\ntype InflightRequest = {\n [cacheId: string]: RequestHandler[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\n/**\n * Creates a middleware that caches inflight requests.\n * If a request is already in flight, the middleware will wait for the request to complete\n * and then return the result.\n *\n * @returns A middleware that caches inflight requests.\n */\nexport function createInflightCacheMiddleware(): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache: boolean }>\n> {\n const inflightRequests: InflightRequest = {};\n\n return async ({ request, context, next }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const cacheId: string | null = cacheIdentifierForRequest(request);\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', request);\n return next();\n }\n\n // check for matching requests\n let activeRequestHandlers: RequestHandler[] = inflightRequests[cacheId];\n // if found, wait for the active request to be handled\n if (activeRequestHandlers) {\n // setup the response listener and wait for it to be called\n // it will handle copying the result and request fields\n log(\n 'Running %i handler(s) for request %o',\n activeRequestHandlers.length,\n request,\n );\n return await createActiveRequestHandler(activeRequestHandlers);\n }\n\n // setup response handler array for subsequent requests\n activeRequestHandlers = [];\n inflightRequests[cacheId] = activeRequestHandlers;\n // allow request to be handled normally\n log('Carrying original request forward %o', request);\n try {\n const result = (await next()) as Readonly<Json>;\n log(\n 'Running %i collected handler(s) for successful request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ result }, activeRequestHandlers);\n return result;\n } catch (error) {\n log(\n 'Running %i collected handler(s) for failed request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ error }, activeRequestHandlers);\n throw error;\n } finally {\n delete inflightRequests[cacheId];\n }\n };\n}\n\n/**\n * Creates a new request handler for the active request.\n *\n * @param activeRequestHandlers - The active request handlers.\n * @returns A promise that resolves to the result of the request.\n */\nfunction createActiveRequestHandler(\n activeRequestHandlers: RequestHandler[],\n): Promise<Readonly<Json>> {\n const { resolve, promise, reject } = createDeferredPromise<Readonly<Json>>();\n activeRequestHandlers.push([\n (result: Readonly<Json>) => resolve(result),\n (error: unknown) => reject(error),\n ]);\n return promise;\n}\n\n/**\n * Runs the request handlers for the given result or error.\n *\n * @param resultOrError - The result or error of the request.\n * @param activeRequestHandlers - The active request handlers.\n */\nfunction runRequestHandlers(\n resultOrError: { result: Readonly<Json> } | { error: unknown },\n activeRequestHandlers: RequestHandler[],\n): void {\n // use setTimeout so we can handle the original request first\n setTimeout(() => {\n activeRequestHandlers.forEach(([onSuccess, onError]) => {\n if ('result' in resultOrError) {\n onSuccess(resultOrError.result);\n } else {\n onError(resultOrError.error);\n }\n });\n });\n}\n"]}
1
+ {"version":3,"file":"inflight-cache.cjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":";;;AAIA,2CAAwD;AAGxD,uDAAoE;AACpE,6CAA0D;AAW1D,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,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AG,CACD,uDAAuD,EACvD,qBAAqB,CAAC,MAAM,EAC5B,OAAO,CACR,CAAC;YACF,kBAAkB,CAAC,EAAE,KAAK,EAAE,EAAE,qBAAqB,CAAC,CAAC;YACrD,MAAM,KAAK,CAAC;QACd,CAAC;gBAAS,CAAC;YACT,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAC;QACnC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AA1DD,sEA0DC;AAED;;;;;GAKG;AACH,SAAS,0BAA0B,CACjC,qBAAuC;IAEvC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAA,6BAAqB,GAAkB,CAAC;IAC7E,qBAAqB,CAAC,IAAI,CAAC;QACzB,CAAC,MAAsB,EAAQ,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC;QACjD,CAAC,KAAc,EAAQ,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC;KACxC,CAAC,CAAC;IACH,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CACzB,aAA8D,EAC9D,qBAAuC;IAEvC,6DAA6D;IAC7D,UAAU,CAAC,GAAG,EAAE;QACd,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,EAAE,EAAE;YACrD,IAAI,QAAQ,IAAI,aAAa,EAAE,CAAC;gBAC9B,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YAClC,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport { createDeferredPromise } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandler = [\n (result: Readonly<Json>) => void,\n (error: unknown) => void,\n];\n\ntype InflightRequest = {\n [cacheId: string]: RequestHandler[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\n/**\n * Creates a middleware that caches inflight requests.\n * If a request is already in flight, the middleware will wait for the request to complete\n * and then return the result.\n *\n * @returns A middleware that caches inflight requests.\n */\nexport function createInflightCacheMiddleware(): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache: boolean }>\n> {\n const inflightRequests: InflightRequest = {};\n\n return async ({ request, context, next }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const cacheId: string | null = cacheIdentifierForRequest(request);\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', request);\n return next();\n }\n\n // check for matching requests\n let activeRequestHandlers: RequestHandler[] = inflightRequests[cacheId];\n // if found, wait for the active request to be handled\n if (activeRequestHandlers) {\n // setup the response listener and wait for it to be called\n // it will handle copying the result and request fields\n log(\n 'Running %i handler(s) for request %o',\n activeRequestHandlers.length,\n request,\n );\n return await createActiveRequestHandler(activeRequestHandlers);\n }\n\n // setup response handler array for subsequent requests\n activeRequestHandlers = [];\n inflightRequests[cacheId] = activeRequestHandlers;\n // allow request to be handled normally\n log('Carrying original request forward %o', request);\n try {\n const result = (await next()) as Readonly<Json>;\n log(\n 'Running %i collected handler(s) for successful request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ result }, activeRequestHandlers);\n return result;\n } catch (error) {\n log(\n 'Running %i collected handler(s) for failed request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ error }, activeRequestHandlers);\n throw error;\n } finally {\n delete inflightRequests[cacheId];\n }\n };\n}\n\n/**\n * Creates a new request handler for the active request.\n *\n * @param activeRequestHandlers - The active request handlers.\n * @returns A promise that resolves to the result of the request.\n */\nfunction createActiveRequestHandler(\n activeRequestHandlers: RequestHandler[],\n): Promise<Readonly<Json>> {\n const { resolve, promise, reject } = createDeferredPromise<Readonly<Json>>();\n activeRequestHandlers.push([\n (result: Readonly<Json>): void => resolve(result),\n (error: unknown): void => reject(error),\n ]);\n return promise;\n}\n\n/**\n * Runs the request handlers for the given result or error.\n *\n * @param resultOrError - The result or error of the request.\n * @param activeRequestHandlers - The active request handlers.\n */\nfunction runRequestHandlers(\n resultOrError: { result: Readonly<Json> } | { error: unknown },\n activeRequestHandlers: RequestHandler[],\n): void {\n // use setTimeout so we can handle the original request first\n setTimeout(() => {\n activeRequestHandlers.forEach(([onSuccess, onError]) => {\n if ('result' in resultOrError) {\n onSuccess(resultOrError.result);\n } else {\n onError(resultOrError.error);\n }\n });\n });\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"inflight-cache.mjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,qBAAqB,EAAE,wBAAwB;AAGxD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AACpE,OAAO,EAAE,yBAAyB,EAAE,0BAAsB;AAW1D,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,MAAM,UAAU,6BAA6B;IAK3C,MAAM,gBAAgB,GAAoB,EAAE,CAAC;IAE7C,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QAC1C,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC;YAC7B,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,MAAM,OAAO,GAAkB,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAClE,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,GAAG,CAAC,gDAAgD,EAAE,OAAO,CAAC,CAAC;YAC/D,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,8BAA8B;QAC9B,IAAI,qBAAqB,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AAE,CAAC,MAAM,CAAC,KAAK,CAAC;KAClC,CAAC,CAAC;IACH,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CACzB,aAA8D,EAC9D,qBAAuC;IAEvC,6DAA6D;IAC7D,UAAU,CAAC,GAAG,EAAE;QACd,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,EAAE,EAAE;YACrD,IAAI,QAAQ,IAAI,aAAa,EAAE,CAAC;gBAC9B,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YAClC,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport { createDeferredPromise } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandler = [\n (result: Readonly<Json>) => void,\n (error: unknown) => void,\n];\n\ntype InflightRequest = {\n [cacheId: string]: RequestHandler[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\n/**\n * Creates a middleware that caches inflight requests.\n * If a request is already in flight, the middleware will wait for the request to complete\n * and then return the result.\n *\n * @returns A middleware that caches inflight requests.\n */\nexport function createInflightCacheMiddleware(): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache: boolean }>\n> {\n const inflightRequests: InflightRequest = {};\n\n return async ({ request, context, next }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const cacheId: string | null = cacheIdentifierForRequest(request);\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', request);\n return next();\n }\n\n // check for matching requests\n let activeRequestHandlers: RequestHandler[] = inflightRequests[cacheId];\n // if found, wait for the active request to be handled\n if (activeRequestHandlers) {\n // setup the response listener and wait for it to be called\n // it will handle copying the result and request fields\n log(\n 'Running %i handler(s) for request %o',\n activeRequestHandlers.length,\n request,\n );\n return await createActiveRequestHandler(activeRequestHandlers);\n }\n\n // setup response handler array for subsequent requests\n activeRequestHandlers = [];\n inflightRequests[cacheId] = activeRequestHandlers;\n // allow request to be handled normally\n log('Carrying original request forward %o', request);\n try {\n const result = (await next()) as Readonly<Json>;\n log(\n 'Running %i collected handler(s) for successful request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ result }, activeRequestHandlers);\n return result;\n } catch (error) {\n log(\n 'Running %i collected handler(s) for failed request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ error }, activeRequestHandlers);\n throw error;\n } finally {\n delete inflightRequests[cacheId];\n }\n };\n}\n\n/**\n * Creates a new request handler for the active request.\n *\n * @param activeRequestHandlers - The active request handlers.\n * @returns A promise that resolves to the result of the request.\n */\nfunction createActiveRequestHandler(\n activeRequestHandlers: RequestHandler[],\n): Promise<Readonly<Json>> {\n const { resolve, promise, reject } = createDeferredPromise<Readonly<Json>>();\n activeRequestHandlers.push([\n (result: Readonly<Json>) => resolve(result),\n (error: unknown) => reject(error),\n ]);\n return promise;\n}\n\n/**\n * Runs the request handlers for the given result or error.\n *\n * @param resultOrError - The result or error of the request.\n * @param activeRequestHandlers - The active request handlers.\n */\nfunction runRequestHandlers(\n resultOrError: { result: Readonly<Json> } | { error: unknown },\n activeRequestHandlers: RequestHandler[],\n): void {\n // use setTimeout so we can handle the original request first\n setTimeout(() => {\n activeRequestHandlers.forEach(([onSuccess, onError]) => {\n if ('result' in resultOrError) {\n onSuccess(resultOrError.result);\n } else {\n onError(resultOrError.error);\n }\n });\n });\n}\n"]}
1
+ {"version":3,"file":"inflight-cache.mjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,qBA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Context,\n} from '@metamask/json-rpc-engine/v2';\nimport { createDeferredPromise } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandler = [\n (result: Readonly<Json>) => void,\n (error: unknown) => void,\n];\n\ntype InflightRequest = {\n [cacheId: string]: RequestHandler[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\n/**\n * Creates a middleware that caches inflight requests.\n * If a request is already in flight, the middleware will wait for the request to complete\n * and then return the result.\n *\n * @returns A middleware that caches inflight requests.\n */\nexport function createInflightCacheMiddleware(): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache: boolean }>\n> {\n const inflightRequests: InflightRequest = {};\n\n return async ({ request, context, next }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const cacheId: string | null = cacheIdentifierForRequest(request);\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', request);\n return next();\n }\n\n // check for matching requests\n let activeRequestHandlers: RequestHandler[] = inflightRequests[cacheId];\n // if found, wait for the active request to be handled\n if (activeRequestHandlers) {\n // setup the response listener and wait for it to be called\n // it will handle copying the result and request fields\n log(\n 'Running %i handler(s) for request %o',\n activeRequestHandlers.length,\n request,\n );\n return await createActiveRequestHandler(activeRequestHandlers);\n }\n\n // setup response handler array for subsequent requests\n activeRequestHandlers = [];\n inflightRequests[cacheId] = activeRequestHandlers;\n // allow request to be handled normally\n log('Carrying original request forward %o', request);\n try {\n const result = (await next()) as Readonly<Json>;\n log(\n 'Running %i collected handler(s) for successful request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ result }, activeRequestHandlers);\n return result;\n } catch (error) {\n log(\n 'Running %i collected handler(s) for failed request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ error }, activeRequestHandlers);\n throw error;\n } finally {\n delete inflightRequests[cacheId];\n }\n };\n}\n\n/**\n * Creates a new request handler for the active request.\n *\n * @param activeRequestHandlers - The active request handlers.\n * @returns A promise that resolves to the result of the request.\n */\nfunction createActiveRequestHandler(\n activeRequestHandlers: RequestHandler[],\n): Promise<Readonly<Json>> {\n const { resolve, promise, reject } = createDeferredPromise<Readonly<Json>>();\n activeRequestHandlers.push([\n (result: Readonly<Json>): void => resolve(result),\n (error: unknown): void => reject(error),\n ]);\n return promise;\n}\n\n/**\n * Runs the request handlers for the given result or error.\n *\n * @param resultOrError - The result or error of the request.\n * @param activeRequestHandlers - The active request handlers.\n */\nfunction runRequestHandlers(\n resultOrError: { result: Readonly<Json> } | { error: unknown },\n activeRequestHandlers: RequestHandler[],\n): void {\n // use setTimeout so we can handle the original request first\n setTimeout(() => {\n activeRequestHandlers.forEach(([onSuccess, onError]) => {\n if ('result' in resultOrError) {\n onSuccess(resultOrError.result);\n } else {\n onError(resultOrError.error);\n }\n });\n });\n}\n"]}
@@ -42,9 +42,7 @@ function createRetryOnEmptyMiddleware({ provider, blockTracker, } = {}) {
42
42
  ? request.params[blockRefIndex]
43
43
  : undefined;
44
44
  // omitted blockRef implies "latest"
45
- if (blockRef === undefined) {
46
- blockRef = 'latest';
47
- }
45
+ blockRef ?? (blockRef = 'latest');
48
46
  // skip if non-number block reference
49
47
  if (['latest', 'pending'].includes(blockRef)) {
50
48
  return next();
@@ -94,9 +92,9 @@ async function retry(maxRetries, asyncFn) {
94
92
  try {
95
93
  return await asyncFn();
96
94
  }
97
- catch (err) {
98
- if ((0, error_1.isExecutionRevertedError)(err)) {
99
- throw err;
95
+ catch (error) {
96
+ if ((0, error_1.isExecutionRevertedError)(error)) {
97
+ throw error;
100
98
  }
101
99
  log('(call %i) Request failed, waiting 1s to retry again...', index + 1);
102
100
  await (0, timeout_1.timeout)(1000);
@@ -1 +1 @@
1
- {"version":3,"file":"retryOnEmpty.cjs","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":";;;AAIA,iCAA8B;AAE9B,uDA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,CAAC,YAAY,CAAC,GAAG,CACnD,CAAC;YACJ,CAAC;YACD,OAAO,aAAa,CAAC;QACvB,CAAC,CAAC,CAAC;QACH,GAAG,CAAC,mBAAmB,EAAE,WAAW,CAAC,CAAC;QACtC,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AA3FD,oEA2FC;AAED;;;;;;GAMG;AACH,KAAK,UAAU,KAAK,CAClB,UAAkB,EAClB,OAA8B;IAE9B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC;QAChD,IAAI,CAAC;YACH,OAAO,MAAM,OAAO,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,GAAY,EAAE,CAAC;YACtB,IAAI,IAAA,gCAAwB,EAAC,GAAG,CAAC,EAAE,CAAC;gBAClC,MAAM,GAAc,CAAC;YACvB,CAAC;YACD,GAAG,CAAC,wDAAwD,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACzE,MAAM,IAAA,iBAAO,EAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IACD,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACzB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;AAChE,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { InternalProvider } from '@metamask/eth-json-rpc-provider';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\nimport { klona } from 'klona';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { Block } from './types';\nimport { blockTagParamIndex } from './utils/cache';\nimport { isExecutionRevertedError } from './utils/error';\nimport { timeout } from './utils/timeout';\n\n//\n// RetryOnEmptyMiddleware will retry any request with an empty response that has\n// a numbered block reference at or lower than the blockTracker's latest block.\n// Its useful for dealing with load-balanced ethereum JSON RPC\n// nodes that are not always in sync with each other.\n//\n\nconst log = createModuleLogger(projectLogger, 'retry-on-empty');\n// empty values used to determine if a request should be retried\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [null, '\\u003cnil\\u003e'];\n\n/**\n * Creates a middleware that retries requests with empty responses.\n *\n * @param options - The options for the middleware.\n * @param options.provider - The provider to use.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createRetryOnEmptyMiddleware({\n provider,\n blockTracker,\n}: {\n provider?: InternalProvider;\n blockTracker?: PollingBlockTracker;\n} = {}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n if (!provider) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"provider\" option is missing.',\n );\n }\n\n if (!blockTracker) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return async ({ request, next }) => {\n const blockRefIndex: number | undefined = blockTagParamIndex(\n request.method,\n );\n // skip if method does not include blockRef\n if (blockRefIndex === undefined) {\n return next();\n }\n // skip if not exact block references\n let blockRef: string | undefined =\n Array.isArray(request.params) && request.params[blockRefIndex]\n ? (request.params[blockRefIndex] as string)\n : undefined;\n // omitted blockRef implies \"latest\"\n if (blockRef === undefined) {\n blockRef = 'latest';\n }\n\n // skip if non-number block reference\n if (['latest', 'pending'].includes(blockRef)) {\n return next();\n }\n // skip if block refernce is not a valid number\n const blockRefNumber: number = Number.parseInt(blockRef.slice(2), 16);\n if (Number.isNaN(blockRefNumber)) {\n return next();\n }\n // lookup latest block\n const latestBlockNumberHex: string = await blockTracker.getLatestBlock();\n const latestBlockNumber: number = Number.parseInt(\n latestBlockNumberHex.slice(2),\n 16,\n );\n // skip if request block number is higher than current\n if (blockRefNumber > latestBlockNumber) {\n log(\n 'Requested block number %o is higher than latest block number %o, falling through to original request',\n blockRefNumber,\n latestBlockNumber,\n );\n return next();\n }\n\n log(\n 'Requested block number %o is not higher than latest block number %o, trying request until non-empty response is received',\n blockRefNumber,\n latestBlockNumber,\n );\n\n // create child request with specific block-ref\n const childRequest = klona(request);\n // attempt child request until non-empty response is received\n const childResult = await retry(10, async () => {\n log('Performing request %o', childRequest);\n const attemptResult = await provider.request<JsonRpcParams, Block>(\n childRequest,\n );\n log('Result is %o', attemptResult);\n // verify result\n const allEmptyValues: unknown[] = emptyValues;\n if (allEmptyValues.includes(attemptResult)) {\n throw new Error(\n `RetryOnEmptyMiddleware - empty result \"${JSON.stringify(\n attemptResult,\n )}\" for request \"${JSON.stringify(childRequest)}\"`,\n );\n }\n return attemptResult;\n });\n log('Copying result %o', childResult);\n return childResult;\n };\n}\n\n/**\n * Retries an asynchronous function up to a maximum number of times.\n *\n * @param maxRetries - The maximum number of retries.\n * @param asyncFn - The asynchronous function to retry.\n * @returns The result of the asynchronous function.\n */\nasync function retry<Result>(\n maxRetries: number,\n asyncFn: () => Promise<Result>,\n): Promise<Result> {\n for (let index = 0; index < maxRetries; index++) {\n try {\n return await asyncFn();\n } catch (err: unknown) {\n if (isExecutionRevertedError(err)) {\n throw err as unknown;\n }\n log('(call %i) Request failed, waiting 1s to retry again...', index + 1);\n await timeout(1000);\n }\n }\n log('Retries exhausted');\n throw new Error('RetryOnEmptyMiddleware - retries exhausted');\n}\n"]}
1
+ {"version":3,"file":"retryOnEmpty.cjs","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":";;;AAIA,iCAA8B;AAE9B,uDAAoE;AAEpE,6CAAmD;AACnD,6CAAyD;AACzD,iDAA0C;AAE1C,EAAE;AACF,gFAAgF;AAChF,+EAA+E;AAC/E,8DAA8D;AAC9D,qDAAqD;AACrD,EAAE;AAEF,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,gBAAgB,CAAC,CAAC;AAChE,gEAAgE;AAChE,0EAA0E;AAC1E,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAE9C;;;;;;;GAOG;AACH,SAAgB,4BAA4B,CAA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;YACH,OAAO,MAAM,OAAO,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACxB,IAAI,IAAA,gCAAwB,EAAC,KAAK,CAAC,EAAE,CAAC;gBACpC,MAAM,KAAgB,CAAC;YACzB,CAAC;YACD,GAAG,CAAC,wDAAwD,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACzE,MAAM,IAAA,iBAAO,EAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IACD,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACzB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;AAChE,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { InternalProvider } from '@metamask/eth-json-rpc-provider';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\nimport { klona } from 'klona';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { Block } from './types';\nimport { blockTagParamIndex } from './utils/cache';\nimport { isExecutionRevertedError } from './utils/error';\nimport { timeout } from './utils/timeout';\n\n//\n// RetryOnEmptyMiddleware will retry any request with an empty response that has\n// a numbered block reference at or lower than the blockTracker's latest block.\n// Its useful for dealing with load-balanced ethereum JSON RPC\n// nodes that are not always in sync with each other.\n//\n\nconst log = createModuleLogger(projectLogger, 'retry-on-empty');\n// empty values used to determine if a request should be retried\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [null, '\\u003cnil\\u003e'];\n\n/**\n * Creates a middleware that retries requests with empty responses.\n *\n * @param options - The options for the middleware.\n * @param options.provider - The provider to use.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createRetryOnEmptyMiddleware({\n provider,\n blockTracker,\n}: {\n provider?: InternalProvider;\n blockTracker?: PollingBlockTracker;\n} = {}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n if (!provider) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"provider\" option is missing.',\n );\n }\n\n if (!blockTracker) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return async ({ request, next }) => {\n const blockRefIndex: number | undefined = blockTagParamIndex(\n request.method,\n );\n // skip if method does not include blockRef\n if (blockRefIndex === undefined) {\n return next();\n }\n // skip if not exact block references\n let blockRef: string | undefined =\n Array.isArray(request.params) && request.params[blockRefIndex]\n ? (request.params[blockRefIndex] as string)\n : undefined;\n // omitted blockRef implies \"latest\"\n blockRef ??= 'latest';\n\n // skip if non-number block reference\n if (['latest', 'pending'].includes(blockRef)) {\n return next();\n }\n // skip if block refernce is not a valid number\n const blockRefNumber: number = Number.parseInt(blockRef.slice(2), 16);\n if (Number.isNaN(blockRefNumber)) {\n return next();\n }\n // lookup latest block\n const latestBlockNumberHex: string = await blockTracker.getLatestBlock();\n const latestBlockNumber: number = Number.parseInt(\n latestBlockNumberHex.slice(2),\n 16,\n );\n // skip if request block number is higher than current\n if (blockRefNumber > latestBlockNumber) {\n log(\n 'Requested block number %o is higher than latest block number %o, falling through to original request',\n blockRefNumber,\n latestBlockNumber,\n );\n return next();\n }\n\n log(\n 'Requested block number %o is not higher than latest block number %o, trying request until non-empty response is received',\n blockRefNumber,\n latestBlockNumber,\n );\n\n // create child request with specific block-ref\n const childRequest = klona(request);\n // attempt child request until non-empty response is received\n const childResult = await retry(10, async () => {\n log('Performing request %o', childRequest);\n const attemptResult = await provider.request<JsonRpcParams, Block>(\n childRequest,\n );\n log('Result is %o', attemptResult);\n // verify result\n const allEmptyValues: unknown[] = emptyValues;\n if (allEmptyValues.includes(attemptResult)) {\n throw new Error(\n `RetryOnEmptyMiddleware - empty result \"${JSON.stringify(\n attemptResult,\n )}\" for request \"${JSON.stringify(childRequest)}\"`,\n );\n }\n return attemptResult;\n });\n log('Copying result %o', childResult);\n return childResult;\n };\n}\n\n/**\n * Retries an asynchronous function up to a maximum number of times.\n *\n * @param maxRetries - The maximum number of retries.\n * @param asyncFn - The asynchronous function to retry.\n * @returns The result of the asynchronous function.\n */\nasync function retry<Result>(\n maxRetries: number,\n asyncFn: () => Promise<Result>,\n): Promise<Result> {\n for (let index = 0; index < maxRetries; index++) {\n try {\n return await asyncFn();\n } catch (error: unknown) {\n if (isExecutionRevertedError(error)) {\n throw error as unknown;\n }\n log('(call %i) Request failed, waiting 1s to retry again...', index + 1);\n await timeout(1000);\n }\n }\n log('Retries exhausted');\n throw new Error('RetryOnEmptyMiddleware - retries exhausted');\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"retryOnEmpty.d.cts","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAiB,cAAc,EAAE,wBAAwB;AAqB3E;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,QAAQ,EACR,YAAY,GACb,GAAE;IACD,QAAQ,CAAC,EAAE,gBAAgB,CAAC;IAC5B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CAC/B,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CAqF/C"}
1
+ {"version":3,"file":"retryOnEmpty.d.cts","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAiB,cAAc,EAAE,wBAAwB;AAqB3E;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,QAAQ,EACR,YAAY,GACb,GAAE;IACD,QAAQ,CAAC,EAAE,gBAAgB,CAAC;IAC5B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CAC/B,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CAmF/C"}
@@ -1 +1 @@
1
- {"version":3,"file":"retryOnEmpty.d.mts","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAiB,cAAc,EAAE,wBAAwB;AAqB3E;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,QAAQ,EACR,YAAY,GACb,GAAE;IACD,QAAQ,CAAC,EAAE,gBAAgB,CAAC;IAC5B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CAC/B,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CAqF/C"}
1
+ {"version":3,"file":"retryOnEmpty.d.mts","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAiB,cAAc,EAAE,wBAAwB;AAqB3E;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,QAAQ,EACR,YAAY,GACb,GAAE;IACD,QAAQ,CAAC,EAAE,gBAAgB,CAAC;IAC5B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CAC/B,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CAmF/C"}
@@ -39,9 +39,7 @@ export function createRetryOnEmptyMiddleware({ provider, blockTracker, } = {}) {
39
39
  ? request.params[blockRefIndex]
40
40
  : undefined;
41
41
  // omitted blockRef implies "latest"
42
- if (blockRef === undefined) {
43
- blockRef = 'latest';
44
- }
42
+ blockRef ?? (blockRef = 'latest');
45
43
  // skip if non-number block reference
46
44
  if (['latest', 'pending'].includes(blockRef)) {
47
45
  return next();
@@ -90,9 +88,9 @@ async function retry(maxRetries, asyncFn) {
90
88
  try {
91
89
  return await asyncFn();
92
90
  }
93
- catch (err) {
94
- if (isExecutionRevertedError(err)) {
95
- throw err;
91
+ catch (error) {
92
+ if (isExecutionRevertedError(error)) {
93
+ throw error;
96
94
  }
97
95
  log('(call %i) Request failed, waiting 1s to retry again...', index + 1);
98
96
  await timeout(1000);
@@ -1 +1 @@
1
- {"version":3,"file":"retryOnEmpty.mjs","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,cAAc;AAE9B,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAEpE,OAAO,EAAE,kBAAkB,EAAE,0BAAsB;AACnD,OAAO,EAAE,wBAAwB,EAAE,0BAAsB;AACzD,OAAO,EAAE,OAAO,EAAE,4BAAwB;AAE1C,EAAE;AACF,gFAAgF;AAChF,+EAA+E;AAC/E,8DAA8D;AAC9D,qDAAqD;AACrD,EAAE;AAEF,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAChE,gEAAgE;AAChE,0EAA0E;AAC1E,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAE9C;;;;;;;GAOG;AACH,MAAM,UAAU,4BAA4B,CAAC,EAC3C,QAAQ,EACR,YAAY,MAIV,EAAE;IACJ,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,KAAK,CACT,kEAAkE,CACnE,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,MA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AAC;YACD,GAAG,CAAC,wDAAwD,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACzE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IACD,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACzB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;AAChE,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { InternalProvider } from '@metamask/eth-json-rpc-provider';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\nimport { klona } from 'klona';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { Block } from './types';\nimport { blockTagParamIndex } from './utils/cache';\nimport { isExecutionRevertedError } from './utils/error';\nimport { timeout } from './utils/timeout';\n\n//\n// RetryOnEmptyMiddleware will retry any request with an empty response that has\n// a numbered block reference at or lower than the blockTracker's latest block.\n// Its useful for dealing with load-balanced ethereum JSON RPC\n// nodes that are not always in sync with each other.\n//\n\nconst log = createModuleLogger(projectLogger, 'retry-on-empty');\n// empty values used to determine if a request should be retried\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [null, '\\u003cnil\\u003e'];\n\n/**\n * Creates a middleware that retries requests with empty responses.\n *\n * @param options - The options for the middleware.\n * @param options.provider - The provider to use.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createRetryOnEmptyMiddleware({\n provider,\n blockTracker,\n}: {\n provider?: InternalProvider;\n blockTracker?: PollingBlockTracker;\n} = {}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n if (!provider) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"provider\" option is missing.',\n );\n }\n\n if (!blockTracker) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return async ({ request, next }) => {\n const blockRefIndex: number | undefined = blockTagParamIndex(\n request.method,\n );\n // skip if method does not include blockRef\n if (blockRefIndex === undefined) {\n return next();\n }\n // skip if not exact block references\n let blockRef: string | undefined =\n Array.isArray(request.params) && request.params[blockRefIndex]\n ? (request.params[blockRefIndex] as string)\n : undefined;\n // omitted blockRef implies \"latest\"\n if (blockRef === undefined) {\n blockRef = 'latest';\n }\n\n // skip if non-number block reference\n if (['latest', 'pending'].includes(blockRef)) {\n return next();\n }\n // skip if block refernce is not a valid number\n const blockRefNumber: number = Number.parseInt(blockRef.slice(2), 16);\n if (Number.isNaN(blockRefNumber)) {\n return next();\n }\n // lookup latest block\n const latestBlockNumberHex: string = await blockTracker.getLatestBlock();\n const latestBlockNumber: number = Number.parseInt(\n latestBlockNumberHex.slice(2),\n 16,\n );\n // skip if request block number is higher than current\n if (blockRefNumber > latestBlockNumber) {\n log(\n 'Requested block number %o is higher than latest block number %o, falling through to original request',\n blockRefNumber,\n latestBlockNumber,\n );\n return next();\n }\n\n log(\n 'Requested block number %o is not higher than latest block number %o, trying request until non-empty response is received',\n blockRefNumber,\n latestBlockNumber,\n );\n\n // create child request with specific block-ref\n const childRequest = klona(request);\n // attempt child request until non-empty response is received\n const childResult = await retry(10, async () => {\n log('Performing request %o', childRequest);\n const attemptResult = await provider.request<JsonRpcParams, Block>(\n childRequest,\n );\n log('Result is %o', attemptResult);\n // verify result\n const allEmptyValues: unknown[] = emptyValues;\n if (allEmptyValues.includes(attemptResult)) {\n throw new Error(\n `RetryOnEmptyMiddleware - empty result \"${JSON.stringify(\n attemptResult,\n )}\" for request \"${JSON.stringify(childRequest)}\"`,\n );\n }\n return attemptResult;\n });\n log('Copying result %o', childResult);\n return childResult;\n };\n}\n\n/**\n * Retries an asynchronous function up to a maximum number of times.\n *\n * @param maxRetries - The maximum number of retries.\n * @param asyncFn - The asynchronous function to retry.\n * @returns The result of the asynchronous function.\n */\nasync function retry<Result>(\n maxRetries: number,\n asyncFn: () => Promise<Result>,\n): Promise<Result> {\n for (let index = 0; index < maxRetries; index++) {\n try {\n return await asyncFn();\n } catch (err: unknown) {\n if (isExecutionRevertedError(err)) {\n throw err as unknown;\n }\n log('(call %i) Request failed, waiting 1s to retry again...', index + 1);\n await timeout(1000);\n }\n }\n log('Retries exhausted');\n throw new Error('RetryOnEmptyMiddleware - retries exhausted');\n}\n"]}
1
+ {"version":3,"file":"retryOnEmpty.mjs","sourceRoot":"","sources":["../src/retryOnEmpty.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,cAAc;AAE9B,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAEpE,OAAO,EAAE,kBAAkB,EAAE,0BAAsB;AACnD,OAAO,EAAE,wBAAwB,EAAE,0BAAsB;AACzD,OAAO,EAAE,OAAO,EAAE,4BAAwB;AAE1C,EAAE;AACF,gFAAgF;AAChF,+EAA+E;AAC/E,8DAA8D;AAC9D,qDAAqD;AACrD,EAAE;AAEF,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAChE,gEAAgE;AAChE,0EAA0E;AAC1E,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAE9C;;;;;;;GAOG;AACH,MAAM,UAAU,4BAA4B,CAAC,EAC3C,QAAQ,EACR,YAAY,MAIV,EAAE;IACJ,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,KAAK,CACT,kEAAkE,CACnE,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,MAAM,KAAK,CACT,sEAAsE,CACvE,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,aAAa,GAAuB,kBAAkB,CAC1D,OAAO,CAAC,MAAM,CACf,CAAC;QACF,2CAA2C;QAC3C,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChC,OAAO,IAAI,E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IAAI,EAAE;YAC7C,GAAG,CAAC,uBAAuB,EAAE,YAAY,CAAC,CAAC;YAC3C,MAAM,aAAa,GAAG,MAAM,QAAQ,CAAC,OAAO,CAC1C,YAAY,CACb,CAAC;YACF,GAAG,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;YACnC,gBAAgB;YAChB,MAAM,cAAc,GAAc,WAAW,CAAC;YAC9C,IAAI,cAAc,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;gBAC3C,MAAM,IAAI,KAAK,CACb,0CAA0C,IAAI,CAAC,SAAS,CACtD,aAAa,CACd,kBAAkB,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG,CACnD,CAAC;YACJ,CAAC;YACD,OAAO,aAAa,CAAC;QACvB,CAAC,CAAC,CAAC;QACH,GAAG,CAAC,mBAAmB,EAAE,WAAW,CAAC,CAAC;QACtC,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,KAAK,UAAU,KAAK,CAClB,UAAkB,EAClB,OAA8B;IAE9B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC;QAChD,IAAI,CAAC;YACH,OAAO,MAAM,OAAO,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACxB,IAAI,wBAAwB,CAAC,KAAK,CAAC,EAAE,CAAC;gBACpC,MAAM,KAAgB,CAAC;YACzB,CAAC;YACD,GAAG,CAAC,wDAAwD,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACzE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IACD,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACzB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;AAChE,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { InternalProvider } from '@metamask/eth-json-rpc-provider';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\nimport { klona } from 'klona';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { Block } from './types';\nimport { blockTagParamIndex } from './utils/cache';\nimport { isExecutionRevertedError } from './utils/error';\nimport { timeout } from './utils/timeout';\n\n//\n// RetryOnEmptyMiddleware will retry any request with an empty response that has\n// a numbered block reference at or lower than the blockTracker's latest block.\n// Its useful for dealing with load-balanced ethereum JSON RPC\n// nodes that are not always in sync with each other.\n//\n\nconst log = createModuleLogger(projectLogger, 'retry-on-empty');\n// empty values used to determine if a request should be retried\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [null, '\\u003cnil\\u003e'];\n\n/**\n * Creates a middleware that retries requests with empty responses.\n *\n * @param options - The options for the middleware.\n * @param options.provider - The provider to use.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createRetryOnEmptyMiddleware({\n provider,\n blockTracker,\n}: {\n provider?: InternalProvider;\n blockTracker?: PollingBlockTracker;\n} = {}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n if (!provider) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"provider\" option is missing.',\n );\n }\n\n if (!blockTracker) {\n throw Error(\n 'RetryOnEmptyMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return async ({ request, next }) => {\n const blockRefIndex: number | undefined = blockTagParamIndex(\n request.method,\n );\n // skip if method does not include blockRef\n if (blockRefIndex === undefined) {\n return next();\n }\n // skip if not exact block references\n let blockRef: string | undefined =\n Array.isArray(request.params) && request.params[blockRefIndex]\n ? (request.params[blockRefIndex] as string)\n : undefined;\n // omitted blockRef implies \"latest\"\n blockRef ??= 'latest';\n\n // skip if non-number block reference\n if (['latest', 'pending'].includes(blockRef)) {\n return next();\n }\n // skip if block refernce is not a valid number\n const blockRefNumber: number = Number.parseInt(blockRef.slice(2), 16);\n if (Number.isNaN(blockRefNumber)) {\n return next();\n }\n // lookup latest block\n const latestBlockNumberHex: string = await blockTracker.getLatestBlock();\n const latestBlockNumber: number = Number.parseInt(\n latestBlockNumberHex.slice(2),\n 16,\n );\n // skip if request block number is higher than current\n if (blockRefNumber > latestBlockNumber) {\n log(\n 'Requested block number %o is higher than latest block number %o, falling through to original request',\n blockRefNumber,\n latestBlockNumber,\n );\n return next();\n }\n\n log(\n 'Requested block number %o is not higher than latest block number %o, trying request until non-empty response is received',\n blockRefNumber,\n latestBlockNumber,\n );\n\n // create child request with specific block-ref\n const childRequest = klona(request);\n // attempt child request until non-empty response is received\n const childResult = await retry(10, async () => {\n log('Performing request %o', childRequest);\n const attemptResult = await provider.request<JsonRpcParams, Block>(\n childRequest,\n );\n log('Result is %o', attemptResult);\n // verify result\n const allEmptyValues: unknown[] = emptyValues;\n if (allEmptyValues.includes(attemptResult)) {\n throw new Error(\n `RetryOnEmptyMiddleware - empty result \"${JSON.stringify(\n attemptResult,\n )}\" for request \"${JSON.stringify(childRequest)}\"`,\n );\n }\n return attemptResult;\n });\n log('Copying result %o', childResult);\n return childResult;\n };\n}\n\n/**\n * Retries an asynchronous function up to a maximum number of times.\n *\n * @param maxRetries - The maximum number of retries.\n * @param asyncFn - The asynchronous function to retry.\n * @returns The result of the asynchronous function.\n */\nasync function retry<Result>(\n maxRetries: number,\n asyncFn: () => Promise<Result>,\n): Promise<Result> {\n for (let index = 0; index < maxRetries; index++) {\n try {\n return await asyncFn();\n } catch (error: unknown) {\n if (isExecutionRevertedError(error)) {\n throw error as unknown;\n }\n log('(call %i) Request failed, waiting 1s to retry again...', index + 1);\n await timeout(1000);\n }\n }\n log('Retries exhausted');\n throw new Error('RetryOnEmptyMiddleware - retries exhausted');\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"common.cjs","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CAAC,UAAkB,EAAE,EAAE;IAC5D,IAAI,UAAU,EAAE,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC;QACrC,OAAO,UAAU,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACrD,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AALW,QAAA,uBAAuB,2BAKlC","sourcesContent":["/**\n * Function to stripe array brackets if string defining the type has it.\n *\n * @param typeString - String defining type from which array brackets are required to be removed.\n * @returns Parameter string with array brackets [] removed.\n */\nexport const stripArrayTypeIfPresent = (typeString: string) => {\n if (typeString?.match(/\\S\\[\\d*\\]$/u)) {\n return typeString.replace(/\\[\\d*\\]$/gu, '').trim();\n }\n return typeString;\n};\n"]}
1
+ {"version":3,"file":"common.cjs","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CAAC,UAAkB,EAAU,EAAE;IACpE,IAAI,UAAU,EAAE,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC;QACrC,OAAO,UAAU,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACrD,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AALW,QAAA,uBAAuB,2BAKlC","sourcesContent":["/**\n * Function to stripe array brackets if string defining the type has it.\n *\n * @param typeString - String defining type from which array brackets are required to be removed.\n * @returns Parameter string with array brackets [] removed.\n */\nexport const stripArrayTypeIfPresent = (typeString: string): string => {\n if (typeString?.match(/\\S\\[\\d*\\]$/u)) {\n return typeString.replace(/\\[\\d*\\]$/gu, '').trim();\n }\n return typeString;\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"common.d.cts","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,eAAgB,MAAM,WAKzD,CAAC"}
1
+ {"version":3,"file":"common.d.cts","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,eAAgB,MAAM,KAAG,MAK5D,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"common.d.mts","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,eAAgB,MAAM,WAKzD,CAAC"}
1
+ {"version":3,"file":"common.d.mts","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,eAAgB,MAAM,KAAG,MAK5D,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"common.mjs","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,UAAkB,EAAE,EAAE;IAC5D,IAAI,UAAU,EAAE,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC;QACrC,OAAO,UAAU,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACrD,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC","sourcesContent":["/**\n * Function to stripe array brackets if string defining the type has it.\n *\n * @param typeString - String defining type from which array brackets are required to be removed.\n * @returns Parameter string with array brackets [] removed.\n */\nexport const stripArrayTypeIfPresent = (typeString: string) => {\n if (typeString?.match(/\\S\\[\\d*\\]$/u)) {\n return typeString.replace(/\\[\\d*\\]$/gu, '').trim();\n }\n return typeString;\n};\n"]}
1
+ {"version":3,"file":"common.mjs","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,UAAkB,EAAU,EAAE;IACpE,IAAI,UAAU,EAAE,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC;QACrC,OAAO,UAAU,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACrD,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC","sourcesContent":["/**\n * Function to stripe array brackets if string defining the type has it.\n *\n * @param typeString - String defining type from which array brackets are required to be removed.\n * @returns Parameter string with array brackets [] removed.\n */\nexport const stripArrayTypeIfPresent = (typeString: string): string => {\n if (typeString?.match(/\\S\\[\\d*\\]$/u)) {\n return typeString.replace(/\\[\\d*\\]$/gu, '').trim();\n }\n return typeString;\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.cjs","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":";;;AAaA;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,WAAmB;IACvD,IAAI,IAAI,CAAC;IACT,IAAI,CAAC;QACH,IAAI,GAAG,iBAAiB,CAAC,WAAW,CAAC,CAAC;IACxC,CAAC;IAAC,MAAM,CAAC;QACP,yDAAyD;QACzD,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC;IAEhD,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,GAAG,IAAI;QACP,MAAM,EAAE;YACN,GAAG,IAAI,CAAC,MAAM;YACd,iBAAiB,EAAE,wBAAwB,CAAC,iBAAiB,CAAC;SAC/D;KACF,CAAC,CAAC;AACL,CAAC;AAtBD,sDAsBC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,IAAY;IAC5C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAwC,CAAC;AACjE,CAAC;AAND,8CAMC;AAED;;;;;GAKG;AACH,SAAS,wBAAwB,CAAC,OAAY;IAC5C,IAAI,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,OAAO,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IACjC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\ntype EIP712Domain = {\n verifyingContract: Hex;\n};\n\ntype SignTypedMessageDataV3V4 = {\n types: Record<string, unknown>;\n domain: EIP712Domain;\n primaryType: string;\n message: unknown;\n};\n\n/**\n * Normalizes the messageData for the eth_signTypedData\n *\n * @param messageData - The messageData to normalize.\n * @returns The normalized messageData.\n */\nexport function normalizeTypedMessage(messageData: string) {\n let data;\n try {\n data = parseTypedMessage(messageData);\n } catch {\n // Ignore normalization errors and pass the message as is\n return messageData;\n }\n\n const { verifyingContract } = data.domain ?? {};\n\n if (!verifyingContract) {\n return messageData;\n }\n\n return JSON.stringify({\n ...data,\n domain: {\n ...data.domain,\n verifyingContract: normalizeContractAddress(verifyingContract),\n },\n });\n}\n\n/**\n * Parses the messageData to obtain the data object for EIP712 normalization\n *\n * @param data - The messageData to parse.\n * @returns The data object for EIP712 normalization.\n */\nexport function parseTypedMessage(data: string) {\n if (typeof data !== 'string') {\n return data;\n }\n\n return JSON.parse(data) as unknown as SignTypedMessageDataV3V4;\n}\n\n/**\n * Normalizes the address to standard hexadecimal format\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\nfunction normalizeContractAddress(address: Hex): Hex {\n if (address.startsWith('0X')) {\n return `0x${address.slice(2)}`;\n }\n return address;\n}\n"]}
1
+ {"version":3,"file":"normalize.cjs","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":";;;AAaA;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,WAAmB;IACvD,IAAI,IAAI,CAAC;IACT,IAAI,CAAC;QACH,IAAI,GAAG,iBAAiB,CAAC,WAAW,CAAC,CAAC;IACxC,CAAC;IAAC,MAAM,CAAC;QACP,yDAAyD;QACzD,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC;IAEhD,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,GAAG,IAAI;QACP,MAAM,EAAE;YACN,GAAG,IAAI,CAAC,MAAM;YACd,iBAAiB,EAAE,wBAAwB,CAAC,iBAAiB,CAAC;SAC/D;KACF,CAAC,CAAC;AACL,CAAC;AAtBD,sDAsBC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,IAAY;IAC5C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAwC,CAAC;AACjE,CAAC;AAND,8CAMC;AAED;;;;;GAKG;AACH,SAAS,wBAAwB,CAAC,OAAY;IAC5C,IAAI,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,OAAO,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IACjC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\ntype EIP712Domain = {\n verifyingContract: Hex;\n};\n\ntype SignTypedMessageDataV3V4 = {\n types: Record<string, unknown>;\n domain: EIP712Domain;\n primaryType: string;\n message: unknown;\n};\n\n/**\n * Normalizes the messageData for the eth_signTypedData\n *\n * @param messageData - The messageData to normalize.\n * @returns The normalized messageData.\n */\nexport function normalizeTypedMessage(messageData: string): string {\n let data;\n try {\n data = parseTypedMessage(messageData);\n } catch {\n // Ignore normalization errors and pass the message as is\n return messageData;\n }\n\n const { verifyingContract } = data.domain ?? {};\n\n if (!verifyingContract) {\n return messageData;\n }\n\n return JSON.stringify({\n ...data,\n domain: {\n ...data.domain,\n verifyingContract: normalizeContractAddress(verifyingContract),\n },\n });\n}\n\n/**\n * Parses the messageData to obtain the data object for EIP712 normalization\n *\n * @param data - The messageData to parse.\n * @returns The data object for EIP712 normalization.\n */\nexport function parseTypedMessage(data: string): SignTypedMessageDataV3V4 {\n if (typeof data !== 'string') {\n return data;\n }\n\n return JSON.parse(data) as unknown as SignTypedMessageDataV3V4;\n}\n\n/**\n * Normalizes the address to standard hexadecimal format\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\nfunction normalizeContractAddress(address: Hex): Hex {\n if (address.startsWith('0X')) {\n return `0x${address.slice(2)}`;\n }\n return address;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.d.cts","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,KAAK,YAAY,GAAG;IAClB,iBAAiB,EAAE,GAAG,CAAC;CACxB,CAAC;AAEF,KAAK,wBAAwB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,MAAM,EAAE,YAAY,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,MAAM,UAsBxD;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,4BAM7C"}
1
+ {"version":3,"file":"normalize.d.cts","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,KAAK,YAAY,GAAG;IAClB,iBAAiB,EAAE,GAAG,CAAC;CACxB,CAAC;AAEF,KAAK,wBAAwB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,MAAM,EAAE,YAAY,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAsBjE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,wBAAwB,CAMxE"}
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.d.mts","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,KAAK,YAAY,GAAG;IAClB,iBAAiB,EAAE,GAAG,CAAC;CACxB,CAAC;AAEF,KAAK,wBAAwB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,MAAM,EAAE,YAAY,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,MAAM,UAsBxD;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,4BAM7C"}
1
+ {"version":3,"file":"normalize.d.mts","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,KAAK,YAAY,GAAG;IAClB,iBAAiB,EAAE,GAAG,CAAC;CACxB,CAAC;AAEF,KAAK,wBAAwB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,MAAM,EAAE,YAAY,CAA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AAK,CAAC,CAAC,CAAC,EAAE,CAAC;IACjC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\ntype EIP712Domain = {\n verifyingContract: Hex;\n};\n\ntype SignTypedMessageDataV3V4 = {\n types: Record<string, unknown>;\n domain: EIP712Domain;\n primaryType: string;\n message: unknown;\n};\n\n/**\n * Normalizes the messageData for the eth_signTypedData\n *\n * @param messageData - The messageData to normalize.\n * @returns The normalized messageData.\n */\nexport function normalizeTypedMessage(messageData: string) {\n let data;\n try {\n data = parseTypedMessage(messageData);\n } catch {\n // Ignore normalization errors and pass the message as is\n return messageData;\n }\n\n const { verifyingContract } = data.domain ?? {};\n\n if (!verifyingContract) {\n return messageData;\n }\n\n return JSON.stringify({\n ...data,\n domain: {\n ...data.domain,\n verifyingContract: normalizeContractAddress(verifyingContract),\n },\n });\n}\n\n/**\n * Parses the messageData to obtain the data object for EIP712 normalization\n *\n * @param data - The messageData to parse.\n * @returns The data object for EIP712 normalization.\n */\nexport function parseTypedMessage(data: string) {\n if (typeof data !== 'string') {\n return data;\n }\n\n return JSON.parse(data) as unknown as SignTypedMessageDataV3V4;\n}\n\n/**\n * Normalizes the address to standard hexadecimal format\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\nfunction normalizeContractAddress(address: Hex): Hex {\n if (address.startsWith('0X')) {\n return `0x${address.slice(2)}`;\n }\n return address;\n}\n"]}
1
+ {"version":3,"file":"normalize.mjs","sourceRoot":"","sources":["../../src/utils/normalize.ts"],"names":[],"mappings":"AAaA;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,WAAmB;IACvD,IAAI,IAAI,CAAC;IACT,IAAI,CAAC;QACH,IAAI,GAAG,iBAAiB,CAAC,WAAW,CAAC,CAAC;IACxC,CAAC;IAAC,MAAM,CAAC;QACP,yDAAyD;QACzD,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC;IAEhD,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,GAAG,IAAI;QACP,MAAM,EAAE;YACN,GAAG,IAAI,CAAC,MAAM;YACd,iBAAiB,EAAE,wBAAwB,CAAC,iBAAiB,CAAC;SAC/D;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAwC,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,SAAS,wBAAwB,CAAC,OAAY;IAC5C,IAAI,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,OAAO,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IACjC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\ntype EIP712Domain = {\n verifyingContract: Hex;\n};\n\ntype SignTypedMessageDataV3V4 = {\n types: Record<string, unknown>;\n domain: EIP712Domain;\n primaryType: string;\n message: unknown;\n};\n\n/**\n * Normalizes the messageData for the eth_signTypedData\n *\n * @param messageData - The messageData to normalize.\n * @returns The normalized messageData.\n */\nexport function normalizeTypedMessage(messageData: string): string {\n let data;\n try {\n data = parseTypedMessage(messageData);\n } catch {\n // Ignore normalization errors and pass the message as is\n return messageData;\n }\n\n const { verifyingContract } = data.domain ?? {};\n\n if (!verifyingContract) {\n return messageData;\n }\n\n return JSON.stringify({\n ...data,\n domain: {\n ...data.domain,\n verifyingContract: normalizeContractAddress(verifyingContract),\n },\n });\n}\n\n/**\n * Parses the messageData to obtain the data object for EIP712 normalization\n *\n * @param data - The messageData to parse.\n * @returns The data object for EIP712 normalization.\n */\nexport function parseTypedMessage(data: string): SignTypedMessageDataV3V4 {\n if (typeof data !== 'string') {\n return data;\n }\n\n return JSON.parse(data) as unknown as SignTypedMessageDataV3V4;\n}\n\n/**\n * Normalizes the address to standard hexadecimal format\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\nfunction normalizeContractAddress(address: Hex): Hex {\n if (address.startsWith('0X')) {\n return `0x${address.slice(2)}`;\n }\n return address;\n}\n"]}
@@ -69,7 +69,7 @@ exports.resemblesAddress = resemblesAddress;
69
69
  function formatValidationError(error, message) {
70
70
  return `${message}\n\n${error
71
71
  .failures()
72
- .map((f) => `${f.path.join(' > ')}${f.path.length ? ' - ' : ''}${f.message}`)
72
+ .map((failure) => `${failure.path.join(' > ')}${failure.path.length ? ' - ' : ''}${failure.message}`)
73
73
  .join('\n')}`;
74
74
  }
75
75
  //# sourceMappingURL=validation.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"validation.cjs","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":";;;AAAA,qDAAiE;AAEjE,uDAAiD;AAKjD;;;;;;;;;;GAUG;AACI,KAAK,UAAU,6BAA6B,CACjD,OAAY,EACZ,OAAgC,EAChC,EAAE,WAAW,EAA0D;IAEvE,IACE,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,CAAC,MAAM,GAAG,CAAC;QAClB,gBAAgB,CAAC,OAAO,CAAC,EACzB,CAAC;QACD,iEAAiE;QACjE,+CAA+C;QAC/C,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QAEhE,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,CAAC;YACnD,OAAO,iBAAiB,CAAC;QAC3B,CAAC;QAED,MAAM,2BAAc,CAAC,YAAY,EAAE,CAAC;IACtC,CAAC;IAED,MAAM,sBAAS,CAAC,aAAa,CAAC;QAC5B,OAAO,EAAE,uDAAuD;KACjE,CAAC,CAAC;AACL,CAAC;AA9BD,sEA8BC;AAED;;;;;;;GAOG;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,CAAC;QACV,MAAM,sBAAS,CAAC,aAAa,CAC3B,qBAAqB,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAC/C,CAAC;IACJ,CAAC;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 { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Struct, StructError } from '@metamask/superstruct';\nimport { validate } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\n\nimport type { WalletMiddlewareContext } from '../wallet';\n\n/**\n * Validates and normalizes a keyholder address for transaction- and\n * signature-related operations.\n *\n * @param address - The Ethereum address to validate and normalize.\n * @param context - The context of the request.\n * @param options - The options for the validation.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\nexport async function validateAndNormalizeKeyholder(\n address: Hex,\n context: WalletMiddlewareContext,\n { getAccounts }: { getAccounts: (origin: string) => 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(context.assertGet('origin'));\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 the parameters of a request against a Superstruct schema.\n * Throws a JSON-RPC error if the parameters are invalid.\n *\n * @param value - The value to validate.\n * @param struct - The Superstruct schema to validate against.\n * @throws An error if the parameters are invalid.\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.\n *\n * @param str - The string to check.\n * @returns True if the string resembles an Ethereum address, false otherwise.\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.\n * @param message - The base error message to prepend to the formatted details.\n * @returns The formatted error.\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"]}
1
+ {"version":3,"file":"validation.cjs","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":";;;AAAA,qDAAiE;AAEjE,uDAAiD;AAKjD;;;;;;;;;;GAUG;AACI,KAAK,UAAU,6BAA6B,CACjD,OAAY,EACZ,OAAgC,EAChC,EAAE,WAAW,EAA0D;IAEvE,IACE,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,CAAC,MAAM,GAAG,CAAC;QAClB,gBAAgB,CAAC,OAAO,CAAC,EACzB,CAAC;QACD,iEAAiE;QACjE,+CAA+C;QAC/C,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QAEhE,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,CAAC;YACnD,OAAO,iBAAiB,CAAC;QAC3B,CAAC;QAED,MAAM,2BAAc,CAAC,YAAY,EAAE,CAAC;IACtC,CAAC;IAED,MAAM,sBAAS,CAAC,aAAa,CAAC;QAC5B,OAAO,EAAE,uDAAuD;KACjE,CAAC,CAAC;AACL,CAAC;AA9BD,sEA8BC;AAED;;;;;;;GAOG;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,CAAC;QACV,MAAM,sBAAS,CAAC,aAAa,CAC3B,qBAAqB,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAC/C,CAAC;IACJ,CAAC;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,OAAO,EAAE,EAAE,CACV,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CACrF;SACA,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Struct, StructError } from '@metamask/superstruct';\nimport { validate } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\n\nimport type { WalletMiddlewareContext } from '../wallet';\n\n/**\n * Validates and normalizes a keyholder address for transaction- and\n * signature-related operations.\n *\n * @param address - The Ethereum address to validate and normalize.\n * @param context - The context of the request.\n * @param options - The options for the validation.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\nexport async function validateAndNormalizeKeyholder(\n address: Hex,\n context: WalletMiddlewareContext,\n { getAccounts }: { getAccounts: (origin: string) => 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(context.assertGet('origin'));\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 the parameters of a request against a Superstruct schema.\n * Throws a JSON-RPC error if the parameters are invalid.\n *\n * @param value - The value to validate.\n * @param struct - The Superstruct schema to validate against.\n * @throws An error if the parameters are invalid.\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.\n *\n * @param str - The string to check.\n * @returns True if the string resembles an Ethereum address, false otherwise.\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.\n * @param message - The base error message to prepend to the formatted details.\n * @returns The formatted error.\n */\nfunction formatValidationError(error: StructError, message: string): string {\n return `${message}\\n\\n${error\n .failures()\n .map(\n (failure) =>\n `${failure.path.join(' > ')}${failure.path.length ? ' - ' : ''}${failure.message}`,\n )\n .join('\\n')}`;\n}\n"]}
@@ -63,7 +63,7 @@ export function resemblesAddress(str) {
63
63
  function formatValidationError(error, message) {
64
64
  return `${message}\n\n${error
65
65
  .failures()
66
- .map((f) => `${f.path.join(' > ')}${f.path.length ? ' - ' : ''}${f.message}`)
66
+ .map((failure) => `${failure.path.join(' > ')}${failure.path.length ? ' - ' : ''}${failure.message}`)
67
67
  .join('\n')}`;
68
68
  }
69
69
  //# sourceMappingURL=validation.mj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C;IACJ,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,0BAA0B;IAC1B,OAAO,GAAG,CAAC,MAAM,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AACnC,CAAC;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 { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Struct, StructError } from '@metamask/superstruct';\nimport { validate } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\n\nimport type { WalletMiddlewareContext } from '../wallet';\n\n/**\n * Validates and normalizes a keyholder address for transaction- and\n * signature-related operations.\n *\n * @param address - The Ethereum address to validate and normalize.\n * @param context - The context of the request.\n * @param options - The options for the validation.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\nexport async function validateAndNormalizeKeyholder(\n address: Hex,\n context: WalletMiddlewareContext,\n { getAccounts }: { getAccounts: (origin: string) => 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(context.assertGet('origin'));\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 the parameters of a request against a Superstruct schema.\n * Throws a JSON-RPC error if the parameters are invalid.\n *\n * @param value - The value to validate.\n * @param struct - The Superstruct schema to validate against.\n * @throws An error if the parameters are invalid.\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.\n *\n * @param str - The string to check.\n * @returns True if the string resembles an Ethereum address, false otherwise.\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.\n * @param message - The base error message to prepend to the formatted details.\n * @returns The formatted error.\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"]}
1
+ {"version":3,"file":"validation.mjs","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAEjE,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AAKjD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,OAAY,EACZ,OAAgC,EAChC,EAAE,WAAW,EAA0D;IAEvE,IACE,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,CAAC,MAAM,GAAG,CAAC;QAClB,gBAAgB,CAAC,OAAO,CAAC,EACzB,CAAC;Q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,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CACrF;SACA,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Struct, StructError } from '@metamask/superstruct';\nimport { validate } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\n\nimport type { WalletMiddlewareContext } from '../wallet';\n\n/**\n * Validates and normalizes a keyholder address for transaction- and\n * signature-related operations.\n *\n * @param address - The Ethereum address to validate and normalize.\n * @param context - The context of the request.\n * @param options - The options for the validation.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\nexport async function validateAndNormalizeKeyholder(\n address: Hex,\n context: WalletMiddlewareContext,\n { getAccounts }: { getAccounts: (origin: string) => 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(context.assertGet('origin'));\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 the parameters of a request against a Superstruct schema.\n * Throws a JSON-RPC error if the parameters are invalid.\n *\n * @param value - The value to validate.\n * @param struct - The Superstruct schema to validate against.\n * @throws An error if the parameters are invalid.\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.\n *\n * @param str - The string to check.\n * @returns True if the string resembles an Ethereum address, false otherwise.\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.\n * @param message - The base error message to prepend to the formatted details.\n * @returns The formatted error.\n */\nfunction formatValidationError(error: StructError, message: string): string {\n return `${message}\\n\\n${error\n .failures()\n .map(\n (failure) =>\n `${failure.path.join(' > ')}${failure.path.length ? ' - ' : ''}${failure.message}`,\n )\n .join('\\n')}`;\n}\n"]}
package/dist/wallet.cjs CHANGED
@@ -126,6 +126,8 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
126
126
  const params = request.params[0];
127
127
  const txParams = {
128
128
  ...params,
129
+ // Not using nullish coalescing, since `params` may be `null`.
130
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
129
131
  from: await validateAndNormalizeKeyholder(params?.from || '', context),
130
132
  };
131
133
  return await processTransaction(txParams, request, context);
@@ -150,6 +152,8 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
150
152
  const params = request.params[0];
151
153
  const txParams = {
152
154
  ...params,
155
+ // Not using nullish coalescing, since `params` may be `null`.
156
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
153
157
  from: await validateAndNormalizeKeyholder(params?.from || '', context),
154
158
  };
155
159
  return await processSignTransaction(txParams, request, context);
@@ -178,6 +182,8 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
178
182
  const message = params[0];
179
183
  const address = await validateAndNormalizeKeyholder(params[1], context);
180
184
  const version = 'V1';
185
+ // Not using nullish coalescing, since `params` may be `null`.
186
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
181
187
  const extraParams = params[2] || {};
182
188
  const msgParams = {
183
189
  ...extraParams,
@@ -272,6 +278,8 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
272
278
  const firstParam = params[0];
273
279
  const secondParam = params[1];
274
280
  // non-standard "extraParams" to be appended to our "msgParams" obj
281
+ // Not using nullish coalescing, since `params` may be `null`.
282
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
275
283
  const extraParams = params[2] || {};
276
284
  // We initially incorrectly ordered these parameters.
277
285
  // To gracefully respect users who adopted this API early,
@@ -365,6 +373,8 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
365
373
  const params = request.params;
366
374
  const ciphertext = params[0];
367
375
  const address = await validateAndNormalizeKeyholder(params[1], context);
376
+ // Not using nullish coalescing, since `params` may be `null`.
377
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
368
378
  const extraParams = params[2] || {};
369
379
  const msgParams = {
370
380
  ...extraParams,
@@ -1 +1 @@
1
- {"version":3,"file":"wallet.cjs","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gEAAkD;AAMlD,qDAAwE;AAExE,qDAAiD;AACjD,2CAAoD;AAGpD,6GAAgH;AAEhH,yGAA4G;AAE5G,+CAAyD;AACzD,qDAA6E;AAC7E,uDAG4B;AAiF5B;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,sBAAsB,CAAC,EACrC,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,qBAAqB,EACrB,kCAAkC,EAClC,gCAAgC,GACR;IAKxB,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAClD,CAAC;IAED,OAAO,IAAA,6BAAwB,EAA0B;QACvD,kBAAkB;QAClB,YAAY,EAAE,cAAc;QAC5B,YAAY,EAAE,oBAAoB;QAElC,gBAAgB;QAChB,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sBAAS,CAAC,YAAY,EAAE,CAAC;IACjC,CAAC;AACH,CAAC","sourcesContent":["import * as sigUtil from '@metamask/eth-sig-util';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n MiddlewareParams,\n} from '@metamask/json-rpc-engine/v2';\nimport { createScaffoldMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { MessageRequest } from '@metamask/message-manager';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isValidHexAddress } from '@metamask/utils';\nimport type { JsonRpcRequest, Json, Hex } from '@metamask/utils';\n\nimport { createWalletRequestExecutionPermissionsHandler } from './methods/wallet-request-execution-permissions';\nimport type { ProcessRequestExecutionPermissionsHook } from './methods/wallet-request-execution-permissions';\nimport { createWalletRevokeExecutionPermissionHandler } from './methods/wallet-revoke-execution-permission';\nimport type { ProcessRevokeExecutionPermissionHook } from './methods/wallet-revoke-execution-permission';\nimport { stripArrayTypeIfPresent } from './utils/common';\nimport { normalizeTypedMessage, parseTypedMessage } from './utils/normalize';\nimport {\n resemblesAddress,\n validateAndNormalizeKeyholder as validateKeyholder,\n} from './utils/validation';\n\nexport type TransactionParams = {\n from: string;\n};\n\nexport type MessageParams = TransactionParams & {\n data: string;\n signatureMethod?: string;\n};\n\nexport type TypedMessageParams = MessageParams & {\n version: string;\n};\n\nexport type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {\n data: Record<string, unknown>[];\n};\n\nexport type WalletMiddlewareOptions = {\n getAccounts: (origin: string) => Promise<string[]>;\n processDecryptMessage?: (\n msgParams: MessageParams,\n req: MessageRequest,\n ) => Promise<string>;\n processEncryptionPublicKey?: (\n address: string,\n req: MessageRequest,\n ) => Promise<string>;\n processPersonalMessage?: (\n msgParams: MessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processSignTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTypedMessage?: (\n msgParams: TypedMessageV1Params,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV3?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV4?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;\n processRevokeExecutionPermission?: ProcessRevokeExecutionPermissionHook;\n};\n\nexport type WalletMiddlewareKeyValues = {\n networkClientId: string;\n origin: string;\n securityAlertResponse?: Record<string, Json>;\n traceContext?: unknown;\n};\n\nexport type WalletMiddlewareContext =\n MiddlewareContext<WalletMiddlewareKeyValues>;\n\nexport type WalletMiddlewareParams = MiddlewareParams<\n JsonRpcRequest,\n WalletMiddlewareContext\n>;\n\n/**\n * Creates a JSON-RPC middleware that handles \"wallet\"-related JSON-RPC methods.\n * \"Wallet\" may have had a specific meaning at some point in the distant past,\n * but at this point it's just an arbitrary label.\n *\n * @param options - The options for the middleware.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @param options.processDecryptMessage - The function to process the decrypt message request.\n * @param options.processEncryptionPublicKey - The function to process the encryption public key request.\n * @param options.processPersonalMessage - The function to process the personal message request.\n * @param options.processTransaction - The function to process the transaction request.\n * @param options.processSignTransaction - The function to process the sign transaction request.\n * @param options.processTypedMessage - The function to process the typed message request.\n * @param options.processTypedMessageV3 - The function to process the typed message v3 request.\n * @param options.processTypedMessageV4 - The function to process the typed message v4 request.\n * @param options.processRequestExecutionPermissions - The function to process the request execution permissions request.\n * @param options.processRevokeExecutionPermission - The function to process the revoke execution permission request.\n * @returns A JSON-RPC middleware that handles wallet-related JSON-RPC methods.\n */\nexport function createWalletMiddleware({\n getAccounts,\n processDecryptMessage,\n processEncryptionPublicKey,\n processPersonalMessage,\n processTransaction,\n processSignTransaction,\n processTypedMessage,\n processTypedMessageV3,\n processTypedMessageV4,\n processRequestExecutionPermissions,\n processRevokeExecutionPermission,\n}: WalletMiddlewareOptions): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n WalletMiddlewareContext\n> {\n if (!getAccounts) {\n throw new Error('opts.getAccounts is required');\n }\n\n return createScaffoldMiddleware<WalletMiddlewareContext>({\n // account lookups\n eth_accounts: lookupAccounts,\n eth_coinbase: lookupDefaultAccount,\n\n // tx signatures\n eth_sendTransaction: sendTransaction,\n eth_signTransaction: signTransaction,\n\n // message signatures\n eth_signTypedData: signTypedData,\n eth_signTypedData_v3: signTypedDataV3,\n eth_signTypedData_v4: signTypedDataV4,\n personal_sign: personalSign,\n eth_getEncryptionPublicKey: encryptionPublicKey,\n eth_decrypt: decryptMessage,\n personal_ecRecover: personalRecover,\n\n // EIP-7715\n wallet_requestExecutionPermissions:\n createWalletRequestExecutionPermissionsHandler({\n processRequestExecutionPermissions,\n }),\n wallet_revokeExecutionPermission:\n createWalletRevokeExecutionPermissionHandler({\n processRevokeExecutionPermission,\n }),\n });\n\n //\n // account lookups\n //\n\n /**\n * Gets the accounts for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The accounts for the origin.\n */\n async function lookupAccounts({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n return await getAccounts(context.assertGet('origin'));\n }\n\n /**\n * Gets the default account (i.e. first in the list) for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The default account for the origin.\n */\n async function lookupDefaultAccount({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n const accounts = await getAccounts(context.assertGet('origin'));\n return accounts[0] || null;\n }\n\n //\n // transaction signatures\n //\n\n /**\n * Sends a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The transaction hash.\n */\n async function sendTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processTransaction(txParams, request, context);\n }\n\n /**\n * Signs a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed transaction.\n */\n async function signTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processSignTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processSignTransaction(txParams, request, context);\n }\n\n //\n // message signatures\n //\n\n /**\n * Signs a `eth_signTypedData` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedData({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [\n Record<string, unknown>[],\n string,\n Record<string, string>?,\n ];\n const message = params[0];\n const address = await validateAndNormalizeKeyholder(params[1], context);\n const version = 'V1';\n const extraParams = params[2] || {};\n const msgParams: TypedMessageV1Params = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'eth_signTypedData',\n version,\n };\n\n return await processTypedMessage(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v3` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV3({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV3) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V3';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v3',\n };\n\n return await processTypedMessageV3(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v4` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV4({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV4) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V4';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v4',\n };\n\n return await processTypedMessageV4(msgParams, request, context, version);\n }\n\n /**\n * Signs a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function personalSign({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processPersonalMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string, TransactionParams?];\n\n // process normally\n const firstParam = params[0];\n const secondParam = params[1];\n // non-standard \"extraParams\" to be appended to our \"msgParams\" obj\n const extraParams = params[2] || {};\n\n // We initially incorrectly ordered these parameters.\n // To gracefully respect users who adopted this API early,\n // we are currently gracefully recovering from the wrong param order\n // when it is clearly identifiable.\n //\n // That means when the first param is definitely an address,\n // and the second param is definitely not, but is hex.\n let address: string, message: string;\n if (resemblesAddress(firstParam) && !resemblesAddress(secondParam)) {\n address = firstParam;\n message = secondParam;\n } else {\n message = firstParam;\n address = secondParam;\n }\n address = await validateAndNormalizeKeyholder(address, context);\n\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'personal_sign',\n };\n\n return await processPersonalMessage(msgParams, request, context);\n }\n\n /**\n * Recovers the signer address from a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @returns The recovered signer address.\n */\n async function personalRecover({\n request,\n }: WalletMiddlewareParams): Promise<Json> {\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n const message = params[0];\n const signature = params[1];\n const signerAddress = sigUtil.recoverPersonalSignature({\n data: message,\n signature,\n });\n\n return signerAddress;\n }\n\n /**\n * Gets the encryption public key for an address.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The encryption public key.\n */\n async function encryptionPublicKey({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processEncryptionPublicKey) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n\n return await processEncryptionPublicKey(address, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n /**\n * Decrypts a message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The decrypted message.\n */\n async function decryptMessage({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processDecryptMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n const params = request.params as [string, string, Record<string, Json>?];\n\n const ciphertext: string = params[0];\n const address: string = await validateAndNormalizeKeyholder(\n params[1],\n context,\n );\n const extraParams = params[2] || {};\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: ciphertext,\n };\n\n return await processDecryptMessage(msgParams, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n //\n // utility\n //\n\n /**\n * Validates the keyholder address, and returns a normalized (i.e. lowercase)\n * copy of it.\n *\n * @param address - The address to validate and normalize.\n * @param context - The context of the request.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\n async function validateAndNormalizeKeyholder(\n address: string,\n context: WalletMiddlewareContext,\n ): Promise<string> {\n return validateKeyholder(address as Hex, context, { getAccounts });\n }\n}\n\n/**\n * Validates primary of typedSignMessage, to ensure that it's type definition is present in message.\n *\n * @param data - The data passed in typedSign request.\n */\nfunction validatePrimaryType(data: string) {\n const { primaryType, types } = parseTypedMessage(data);\n if (!types) {\n throw rpcErrors.invalidInput();\n }\n\n // Primary type can be an array.\n const baseType = stripArrayTypeIfPresent(primaryType);\n\n // Return if the base type is not defined in the types\n const baseTypeDefinitions = types[baseType];\n if (!baseTypeDefinitions) {\n throw rpcErrors.invalidInput();\n }\n}\n\n/**\n * Validates verifyingContract of typedSignMessage.\n *\n * @param data - The data passed in typedSign request.\n * This function allows the verifyingContract to be either:\n * - A valid hex address\n * - The string \"cosmos\" (as it is hard-coded in some Cosmos ecosystem's EVM adapters)\n * - An empty string\n */\nfunction validateVerifyingContract(data: string) {\n const { domain: { verifyingContract } = {} } = parseTypedMessage(data);\n // Explicit check for cosmos here has been added to address this issue\n // https://github.com/MetaMask/eth-json-rpc-middleware/issues/337\n if (\n verifyingContract &&\n (verifyingContract as string) !== 'cosmos' &&\n !isValidHexAddress(verifyingContract)\n ) {\n throw rpcErrors.invalidInput();\n }\n}\n"]}
1
+ {"version":3,"file":"wallet.cjs","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gEAAkD;AAMlD,qDAAwE;AAExE,qDAAiD;AACjD,2CAAoD;AAGpD,6GAAgH;AAEhH,yGAA4G;AAE5G,+CAAyD;AACzD,qDAA6E;AAC7E,uDAG4B;AAiF5B;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,sBAAsB,CAAC,EACrC,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,qBAAqB,EACrB,kCAAkC,EAClC,gCAAgC,GACR;IAKxB,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAClD,CAAC;IAED,OAAO,IAAA,6BAAwB,EAA0B;QACvD,kBAAkB;QAClB,YAAY,EAAE,cAAc;QAC5B,YAAY,EAAE,oBAAoB;QAElC,gBAAgB;QAChB,mBAAmB,EAAE,eAAe;QACpC,mBAAmB,EAAE,eAAe;QAEpC,qBAAqB;QACrB,iBAAiB,EAAE,aAAa;QAChC,oBAAoB,EAAE,eAAe;QACrC,oBAAoB,EAAE,eAAe;QACrC,aAAa,EAAE,YAAY;QAC3B,0BAA0B,EAAE,mBAAmB;QAC/C,WAAW,EAAE,cAAc;QAC3B,kBAAkB,EAAE,eAAe;QAEnC,WAAW;QACX,kCAAkC,EAChC,IAAA,qFAA8C,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AAO,CAAC,GAAG,CAAC,uBAAuB,CAAC;SAC5D,CAAC,CAAC;IACL,CAAC;IAED,EAAE;IACF,UAAU;IACV,EAAE;IAEF;;;;;;;;OAQG;IACH,KAAK,UAAU,6BAA6B,CAC1C,OAAe,EACf,OAAgC;QAEhC,OAAO,IAAA,0CAAiB,EAAC,OAAc,EAAE,OAAO,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;IACrE,CAAC;AACH,CAAC;AAndD,wDAmdC;AAED;;;;GAIG;AACH,SAAS,mBAAmB,CAAC,IAAY;IACvC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,GAAG,IAAA,6BAAiB,EAAC,IAAI,CAAC,CAAC;IACvD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,sBAAS,CAAC,YAAY,EAAE,CAAC;IACjC,CAAC;IAED,gCAAgC;IAChC,MAAM,QAAQ,GAAG,IAAA,gCAAuB,EAAC,WAAW,CAAC,CAAC;IAEtD,sDAAsD;IACtD,MAAM,mBAAmB,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC5C,IAAI,CAAC,mBAAmB,EAAE,CAAC;QACzB,MAAM,sBAAS,CAAC,YAAY,EAAE,CAAC;IACjC,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,yBAAyB,CAAC,IAAY;IAC7C,MAAM,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,GAAG,EAAE,EAAE,GAAG,IAAA,6BAAiB,EAAC,IAAI,CAAC,CAAC;IACvE,sEAAsE;IACtE,iEAAiE;IACjE,IACE,iBAAiB;QAChB,iBAA4B,KAAK,QAAQ;QAC1C,CAAC,IAAA,yBAAiB,EAAC,iBAAiB,CAAC,EACrC,CAAC;QACD,MAAM,sBAAS,CAAC,YAAY,EAAE,CAAC;IACjC,CAAC;AACH,CAAC","sourcesContent":["import * as sigUtil from '@metamask/eth-sig-util';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n MiddlewareParams,\n} from '@metamask/json-rpc-engine/v2';\nimport { createScaffoldMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { MessageRequest } from '@metamask/message-manager';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isValidHexAddress } from '@metamask/utils';\nimport type { JsonRpcRequest, Json, Hex } from '@metamask/utils';\n\nimport { createWalletRequestExecutionPermissionsHandler } from './methods/wallet-request-execution-permissions';\nimport type { ProcessRequestExecutionPermissionsHook } from './methods/wallet-request-execution-permissions';\nimport { createWalletRevokeExecutionPermissionHandler } from './methods/wallet-revoke-execution-permission';\nimport type { ProcessRevokeExecutionPermissionHook } from './methods/wallet-revoke-execution-permission';\nimport { stripArrayTypeIfPresent } from './utils/common';\nimport { normalizeTypedMessage, parseTypedMessage } from './utils/normalize';\nimport {\n resemblesAddress,\n validateAndNormalizeKeyholder as validateKeyholder,\n} from './utils/validation';\n\nexport type TransactionParams = {\n from: string;\n};\n\nexport type MessageParams = TransactionParams & {\n data: string;\n signatureMethod?: string;\n};\n\nexport type TypedMessageParams = MessageParams & {\n version: string;\n};\n\nexport type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {\n data: Record<string, unknown>[];\n};\n\nexport type WalletMiddlewareOptions = {\n getAccounts: (origin: string) => Promise<string[]>;\n processDecryptMessage?: (\n msgParams: MessageParams,\n req: MessageRequest,\n ) => Promise<string>;\n processEncryptionPublicKey?: (\n address: string,\n req: MessageRequest,\n ) => Promise<string>;\n processPersonalMessage?: (\n msgParams: MessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processSignTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTypedMessage?: (\n msgParams: TypedMessageV1Params,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV3?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV4?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;\n processRevokeExecutionPermission?: ProcessRevokeExecutionPermissionHook;\n};\n\nexport type WalletMiddlewareKeyValues = {\n networkClientId: string;\n origin: string;\n securityAlertResponse?: Record<string, Json>;\n traceContext?: unknown;\n};\n\nexport type WalletMiddlewareContext =\n MiddlewareContext<WalletMiddlewareKeyValues>;\n\nexport type WalletMiddlewareParams = MiddlewareParams<\n JsonRpcRequest,\n WalletMiddlewareContext\n>;\n\n/**\n * Creates a JSON-RPC middleware that handles \"wallet\"-related JSON-RPC methods.\n * \"Wallet\" may have had a specific meaning at some point in the distant past,\n * but at this point it's just an arbitrary label.\n *\n * @param options - The options for the middleware.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @param options.processDecryptMessage - The function to process the decrypt message request.\n * @param options.processEncryptionPublicKey - The function to process the encryption public key request.\n * @param options.processPersonalMessage - The function to process the personal message request.\n * @param options.processTransaction - The function to process the transaction request.\n * @param options.processSignTransaction - The function to process the sign transaction request.\n * @param options.processTypedMessage - The function to process the typed message request.\n * @param options.processTypedMessageV3 - The function to process the typed message v3 request.\n * @param options.processTypedMessageV4 - The function to process the typed message v4 request.\n * @param options.processRequestExecutionPermissions - The function to process the request execution permissions request.\n * @param options.processRevokeExecutionPermission - The function to process the revoke execution permission request.\n * @returns A JSON-RPC middleware that handles wallet-related JSON-RPC methods.\n */\nexport function createWalletMiddleware({\n getAccounts,\n processDecryptMessage,\n processEncryptionPublicKey,\n processPersonalMessage,\n processTransaction,\n processSignTransaction,\n processTypedMessage,\n processTypedMessageV3,\n processTypedMessageV4,\n processRequestExecutionPermissions,\n processRevokeExecutionPermission,\n}: WalletMiddlewareOptions): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n WalletMiddlewareContext\n> {\n if (!getAccounts) {\n throw new Error('opts.getAccounts is required');\n }\n\n return createScaffoldMiddleware<WalletMiddlewareContext>({\n // account lookups\n eth_accounts: lookupAccounts,\n eth_coinbase: lookupDefaultAccount,\n\n // tx signatures\n eth_sendTransaction: sendTransaction,\n eth_signTransaction: signTransaction,\n\n // message signatures\n eth_signTypedData: signTypedData,\n eth_signTypedData_v3: signTypedDataV3,\n eth_signTypedData_v4: signTypedDataV4,\n personal_sign: personalSign,\n eth_getEncryptionPublicKey: encryptionPublicKey,\n eth_decrypt: decryptMessage,\n personal_ecRecover: personalRecover,\n\n // EIP-7715\n wallet_requestExecutionPermissions:\n createWalletRequestExecutionPermissionsHandler({\n processRequestExecutionPermissions,\n }),\n wallet_revokeExecutionPermission:\n createWalletRevokeExecutionPermissionHandler({\n processRevokeExecutionPermission,\n }),\n });\n\n //\n // account lookups\n //\n\n /**\n * Gets the accounts for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The accounts for the origin.\n */\n async function lookupAccounts({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n return await getAccounts(context.assertGet('origin'));\n }\n\n /**\n * Gets the default account (i.e. first in the list) for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The default account for the origin.\n */\n async function lookupDefaultAccount({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n const accounts = await getAccounts(context.assertGet('origin'));\n return accounts[0] || null;\n }\n\n //\n // transaction signatures\n //\n\n /**\n * Sends a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The transaction hash.\n */\n async function sendTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processTransaction(txParams, request, context);\n }\n\n /**\n * Signs a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed transaction.\n */\n async function signTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processSignTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processSignTransaction(txParams, request, context);\n }\n\n //\n // message signatures\n //\n\n /**\n * Signs a `eth_signTypedData` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedData({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [\n Record<string, unknown>[],\n string,\n Record<string, string>?,\n ];\n const message = params[0];\n const address = await validateAndNormalizeKeyholder(params[1], context);\n const version = 'V1';\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const extraParams = params[2] || {};\n const msgParams: TypedMessageV1Params = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'eth_signTypedData',\n version,\n };\n\n return await processTypedMessage(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v3` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV3({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV3) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V3';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v3',\n };\n\n return await processTypedMessageV3(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v4` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV4({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV4) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V4';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v4',\n };\n\n return await processTypedMessageV4(msgParams, request, context, version);\n }\n\n /**\n * Signs a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function personalSign({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processPersonalMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string, TransactionParams?];\n\n // process normally\n const firstParam = params[0];\n const secondParam = params[1];\n // non-standard \"extraParams\" to be appended to our \"msgParams\" obj\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const extraParams = params[2] || {};\n\n // We initially incorrectly ordered these parameters.\n // To gracefully respect users who adopted this API early,\n // we are currently gracefully recovering from the wrong param order\n // when it is clearly identifiable.\n //\n // That means when the first param is definitely an address,\n // and the second param is definitely not, but is hex.\n let address: string, message: string;\n if (resemblesAddress(firstParam) && !resemblesAddress(secondParam)) {\n address = firstParam;\n message = secondParam;\n } else {\n message = firstParam;\n address = secondParam;\n }\n address = await validateAndNormalizeKeyholder(address, context);\n\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'personal_sign',\n };\n\n return await processPersonalMessage(msgParams, request, context);\n }\n\n /**\n * Recovers the signer address from a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @returns The recovered signer address.\n */\n async function personalRecover({\n request,\n }: WalletMiddlewareParams): Promise<Json> {\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n const message = params[0];\n const signature = params[1];\n const signerAddress = sigUtil.recoverPersonalSignature({\n data: message,\n signature,\n });\n\n return signerAddress;\n }\n\n /**\n * Gets the encryption public key for an address.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The encryption public key.\n */\n async function encryptionPublicKey({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processEncryptionPublicKey) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n\n return await processEncryptionPublicKey(address, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n /**\n * Decrypts a message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The decrypted message.\n */\n async function decryptMessage({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processDecryptMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n const params = request.params as [string, string, Record<string, Json>?];\n\n const ciphertext: string = params[0];\n const address: string = await validateAndNormalizeKeyholder(\n params[1],\n context,\n );\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const extraParams = params[2] || {};\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: ciphertext,\n };\n\n return await processDecryptMessage(msgParams, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n //\n // utility\n //\n\n /**\n * Validates the keyholder address, and returns a normalized (i.e. lowercase)\n * copy of it.\n *\n * @param address - The address to validate and normalize.\n * @param context - The context of the request.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\n async function validateAndNormalizeKeyholder(\n address: string,\n context: WalletMiddlewareContext,\n ): Promise<string> {\n return validateKeyholder(address as Hex, context, { getAccounts });\n }\n}\n\n/**\n * Validates primary of typedSignMessage, to ensure that it's type definition is present in message.\n *\n * @param data - The data passed in typedSign request.\n */\nfunction validatePrimaryType(data: string): void {\n const { primaryType, types } = parseTypedMessage(data);\n if (!types) {\n throw rpcErrors.invalidInput();\n }\n\n // Primary type can be an array.\n const baseType = stripArrayTypeIfPresent(primaryType);\n\n // Return if the base type is not defined in the types\n const baseTypeDefinitions = types[baseType];\n if (!baseTypeDefinitions) {\n throw rpcErrors.invalidInput();\n }\n}\n\n/**\n * Validates verifyingContract of typedSignMessage.\n *\n * @param data - The data passed in typedSign request.\n * This function allows the verifyingContract to be either:\n * - A valid hex address\n * - The string \"cosmos\" (as it is hard-coded in some Cosmos ecosystem's EVM adapters)\n * - An empty string\n */\nfunction validateVerifyingContract(data: string): void {\n const { domain: { verifyingContract } = {} } = parseTypedMessage(data);\n // Explicit check for cosmos here has been added to address this issue\n // https://github.com/MetaMask/eth-json-rpc-middleware/issues/337\n if (\n verifyingContract &&\n (verifyingContract as string) !== 'cosmos' &&\n !isValidHexAddress(verifyingContract)\n ) {\n throw rpcErrors.invalidInput();\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"wallet.d.cts","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EACjB,qCAAqC;AAEtC,OAAO,KAAK,EAAE,cAAc,EAAE,kCAAkC;AAGhE,OAAO,KAAK,EAAE,cAAc,EAAE,IAAI,EAAO,wBAAwB;AAGjE,OAAO,KAAK,EAAE,sCAAsC,EAAE,2DAAuD;AAE7G,OAAO,KAAK,EAAE,oCAAoC,EAAE,yDAAqD;AAQzG,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,iBAAiB,GAAG;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,aAAa,GAAG;IAC/C,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,kBAAkB,EAAE,MAAM,CAAC,GAAG;IACpE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO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AC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,qBAAqB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC7C,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GACjC,iBAAiB,CAAC,yBAAyB,CAAC,CAAC;AAE/C,MAAM,MAAM,sBAAsB,GAAG,gBAAgB,CACnD,cAAc,EACd,uBAAuB,CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,sBAAsB,CAAC,EACrC,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,qBAAqB,EACrB,kCAAkC,EAClC,gCAAgC,GACjC,EAAE,uBAAuB,GAAG,iBAAiB,CAC5C,cAAc,EACd,IAAI,EACJ,uBAAuB,CACxB,CAmcA"}
package/dist/wallet.mjs CHANGED
@@ -100,6 +100,8 @@ export function createWalletMiddleware({ getAccounts, processDecryptMessage, pro
100
100
  const params = request.params[0];
101
101
  const txParams = {
102
102
  ...params,
103
+ // Not using nullish coalescing, since `params` may be `null`.
104
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
103
105
  from: await validateAndNormalizeKeyholder(params?.from || '', context),
104
106
  };
105
107
  return await processTransaction(txParams, request, context);
@@ -124,6 +126,8 @@ export function createWalletMiddleware({ getAccounts, processDecryptMessage, pro
124
126
  const params = request.params[0];
125
127
  const txParams = {
126
128
  ...params,
129
+ // Not using nullish coalescing, since `params` may be `null`.
130
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
127
131
  from: await validateAndNormalizeKeyholder(params?.from || '', context),
128
132
  };
129
133
  return await processSignTransaction(txParams, request, context);
@@ -152,6 +156,8 @@ export function createWalletMiddleware({ getAccounts, processDecryptMessage, pro
152
156
  const message = params[0];
153
157
  const address = await validateAndNormalizeKeyholder(params[1], context);
154
158
  const version = 'V1';
159
+ // Not using nullish coalescing, since `params` may be `null`.
160
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
155
161
  const extraParams = params[2] || {};
156
162
  const msgParams = {
157
163
  ...extraParams,
@@ -246,6 +252,8 @@ export function createWalletMiddleware({ getAccounts, processDecryptMessage, pro
246
252
  const firstParam = params[0];
247
253
  const secondParam = params[1];
248
254
  // non-standard "extraParams" to be appended to our "msgParams" obj
255
+ // Not using nullish coalescing, since `params` may be `null`.
256
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
249
257
  const extraParams = params[2] || {};
250
258
  // We initially incorrectly ordered these parameters.
251
259
  // To gracefully respect users who adopted this API early,
@@ -339,6 +347,8 @@ export function createWalletMiddleware({ getAccounts, processDecryptMessage, pro
339
347
  const params = request.params;
340
348
  const ciphertext = params[0];
341
349
  const address = await validateAndNormalizeKeyholder(params[1], context);
350
+ // Not using nullish coalescing, since `params` may be `null`.
351
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
342
352
  const extraParams = params[2] || {};
343
353
  const msgParams = {
344
354
  ...extraParams,
@@ -1 +1 @@
1
- {"version":3,"file":"wallet.mjs","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,+BAA+B;AAMlD,OAAO,EAAE,wBAAwB,EAAE,qCAAqC;AAExE,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,iBAAiB,EAAE,wBAAwB;AAGpD,OAAO,EAAE,8CAA8C,EAAE,2DAAuD;AAEhH,OAAO,EAAE,4CAA4C,EAAE,yDAAqD;AAE5G,OAAO,EAAE,uBAAuB,EAAE,2BAAuB;AACzD,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,8BAA0B;AAC7E,OAAO,EACL,gBAAgB,EAChB,6BAA6B,IAAI,iBAAiB,EACnD,+BAA2B;AAiF5B;;;;;;;;;;;;;;;;;;GAkBG;A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;AAED;;;;;;;;GAQG;AACH,SAAS,yBAAyB,CAAC,IAAY;IAC7C,MAAM,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,GAAG,EAAE,EAAE,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACvE,sEAAsE;IACtE,iEAAiE;IACjE,IACE,iBAAiB;QAChB,iBAA4B,KAAK,QAAQ;QAC1C,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,EACrC,CAAC;QACD,MAAM,SAAS,CAAC,YAAY,EAAE,CAAC;IACjC,CAAC;AACH,CAAC","sourcesContent":["import * as sigUtil from '@metamask/eth-sig-util';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n MiddlewareParams,\n} from '@metamask/json-rpc-engine/v2';\nimport { createScaffoldMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { MessageRequest } from '@metamask/message-manager';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isValidHexAddress } from '@metamask/utils';\nimport type { JsonRpcRequest, Json, Hex } from '@metamask/utils';\n\nimport { createWalletRequestExecutionPermissionsHandler } from './methods/wallet-request-execution-permissions';\nimport type { ProcessRequestExecutionPermissionsHook } from './methods/wallet-request-execution-permissions';\nimport { createWalletRevokeExecutionPermissionHandler } from './methods/wallet-revoke-execution-permission';\nimport type { ProcessRevokeExecutionPermissionHook } from './methods/wallet-revoke-execution-permission';\nimport { stripArrayTypeIfPresent } from './utils/common';\nimport { normalizeTypedMessage, parseTypedMessage } from './utils/normalize';\nimport {\n resemblesAddress,\n validateAndNormalizeKeyholder as validateKeyholder,\n} from './utils/validation';\n\nexport type TransactionParams = {\n from: string;\n};\n\nexport type MessageParams = TransactionParams & {\n data: string;\n signatureMethod?: string;\n};\n\nexport type TypedMessageParams = MessageParams & {\n version: string;\n};\n\nexport type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {\n data: Record<string, unknown>[];\n};\n\nexport type WalletMiddlewareOptions = {\n getAccounts: (origin: string) => Promise<string[]>;\n processDecryptMessage?: (\n msgParams: MessageParams,\n req: MessageRequest,\n ) => Promise<string>;\n processEncryptionPublicKey?: (\n address: string,\n req: MessageRequest,\n ) => Promise<string>;\n processPersonalMessage?: (\n msgParams: MessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processSignTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTypedMessage?: (\n msgParams: TypedMessageV1Params,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV3?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV4?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;\n processRevokeExecutionPermission?: ProcessRevokeExecutionPermissionHook;\n};\n\nexport type WalletMiddlewareKeyValues = {\n networkClientId: string;\n origin: string;\n securityAlertResponse?: Record<string, Json>;\n traceContext?: unknown;\n};\n\nexport type WalletMiddlewareContext =\n MiddlewareContext<WalletMiddlewareKeyValues>;\n\nexport type WalletMiddlewareParams = MiddlewareParams<\n JsonRpcRequest,\n WalletMiddlewareContext\n>;\n\n/**\n * Creates a JSON-RPC middleware that handles \"wallet\"-related JSON-RPC methods.\n * \"Wallet\" may have had a specific meaning at some point in the distant past,\n * but at this point it's just an arbitrary label.\n *\n * @param options - The options for the middleware.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @param options.processDecryptMessage - The function to process the decrypt message request.\n * @param options.processEncryptionPublicKey - The function to process the encryption public key request.\n * @param options.processPersonalMessage - The function to process the personal message request.\n * @param options.processTransaction - The function to process the transaction request.\n * @param options.processSignTransaction - The function to process the sign transaction request.\n * @param options.processTypedMessage - The function to process the typed message request.\n * @param options.processTypedMessageV3 - The function to process the typed message v3 request.\n * @param options.processTypedMessageV4 - The function to process the typed message v4 request.\n * @param options.processRequestExecutionPermissions - The function to process the request execution permissions request.\n * @param options.processRevokeExecutionPermission - The function to process the revoke execution permission request.\n * @returns A JSON-RPC middleware that handles wallet-related JSON-RPC methods.\n */\nexport function createWalletMiddleware({\n getAccounts,\n processDecryptMessage,\n processEncryptionPublicKey,\n processPersonalMessage,\n processTransaction,\n processSignTransaction,\n processTypedMessage,\n processTypedMessageV3,\n processTypedMessageV4,\n processRequestExecutionPermissions,\n processRevokeExecutionPermission,\n}: WalletMiddlewareOptions): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n WalletMiddlewareContext\n> {\n if (!getAccounts) {\n throw new Error('opts.getAccounts is required');\n }\n\n return createScaffoldMiddleware<WalletMiddlewareContext>({\n // account lookups\n eth_accounts: lookupAccounts,\n eth_coinbase: lookupDefaultAccount,\n\n // tx signatures\n eth_sendTransaction: sendTransaction,\n eth_signTransaction: signTransaction,\n\n // message signatures\n eth_signTypedData: signTypedData,\n eth_signTypedData_v3: signTypedDataV3,\n eth_signTypedData_v4: signTypedDataV4,\n personal_sign: personalSign,\n eth_getEncryptionPublicKey: encryptionPublicKey,\n eth_decrypt: decryptMessage,\n personal_ecRecover: personalRecover,\n\n // EIP-7715\n wallet_requestExecutionPermissions:\n createWalletRequestExecutionPermissionsHandler({\n processRequestExecutionPermissions,\n }),\n wallet_revokeExecutionPermission:\n createWalletRevokeExecutionPermissionHandler({\n processRevokeExecutionPermission,\n }),\n });\n\n //\n // account lookups\n //\n\n /**\n * Gets the accounts for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The accounts for the origin.\n */\n async function lookupAccounts({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n return await getAccounts(context.assertGet('origin'));\n }\n\n /**\n * Gets the default account (i.e. first in the list) for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The default account for the origin.\n */\n async function lookupDefaultAccount({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n const accounts = await getAccounts(context.assertGet('origin'));\n return accounts[0] || null;\n }\n\n //\n // transaction signatures\n //\n\n /**\n * Sends a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The transaction hash.\n */\n async function sendTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processTransaction(txParams, request, context);\n }\n\n /**\n * Signs a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed transaction.\n */\n async function signTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processSignTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processSignTransaction(txParams, request, context);\n }\n\n //\n // message signatures\n //\n\n /**\n * Signs a `eth_signTypedData` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedData({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [\n Record<string, unknown>[],\n string,\n Record<string, string>?,\n ];\n const message = params[0];\n const address = await validateAndNormalizeKeyholder(params[1], context);\n const version = 'V1';\n const extraParams = params[2] || {};\n const msgParams: TypedMessageV1Params = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'eth_signTypedData',\n version,\n };\n\n return await processTypedMessage(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v3` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV3({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV3) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V3';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v3',\n };\n\n return await processTypedMessageV3(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v4` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV4({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV4) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V4';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v4',\n };\n\n return await processTypedMessageV4(msgParams, request, context, version);\n }\n\n /**\n * Signs a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function personalSign({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processPersonalMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string, TransactionParams?];\n\n // process normally\n const firstParam = params[0];\n const secondParam = params[1];\n // non-standard \"extraParams\" to be appended to our \"msgParams\" obj\n const extraParams = params[2] || {};\n\n // We initially incorrectly ordered these parameters.\n // To gracefully respect users who adopted this API early,\n // we are currently gracefully recovering from the wrong param order\n // when it is clearly identifiable.\n //\n // That means when the first param is definitely an address,\n // and the second param is definitely not, but is hex.\n let address: string, message: string;\n if (resemblesAddress(firstParam) && !resemblesAddress(secondParam)) {\n address = firstParam;\n message = secondParam;\n } else {\n message = firstParam;\n address = secondParam;\n }\n address = await validateAndNormalizeKeyholder(address, context);\n\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'personal_sign',\n };\n\n return await processPersonalMessage(msgParams, request, context);\n }\n\n /**\n * Recovers the signer address from a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @returns The recovered signer address.\n */\n async function personalRecover({\n request,\n }: WalletMiddlewareParams): Promise<Json> {\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n const message = params[0];\n const signature = params[1];\n const signerAddress = sigUtil.recoverPersonalSignature({\n data: message,\n signature,\n });\n\n return signerAddress;\n }\n\n /**\n * Gets the encryption public key for an address.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The encryption public key.\n */\n async function encryptionPublicKey({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processEncryptionPublicKey) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n\n return await processEncryptionPublicKey(address, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n /**\n * Decrypts a message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The decrypted message.\n */\n async function decryptMessage({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processDecryptMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n const params = request.params as [string, string, Record<string, Json>?];\n\n const ciphertext: string = params[0];\n const address: string = await validateAndNormalizeKeyholder(\n params[1],\n context,\n );\n const extraParams = params[2] || {};\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: ciphertext,\n };\n\n return await processDecryptMessage(msgParams, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n //\n // utility\n //\n\n /**\n * Validates the keyholder address, and returns a normalized (i.e. lowercase)\n * copy of it.\n *\n * @param address - The address to validate and normalize.\n * @param context - The context of the request.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\n async function validateAndNormalizeKeyholder(\n address: string,\n context: WalletMiddlewareContext,\n ): Promise<string> {\n return validateKeyholder(address as Hex, context, { getAccounts });\n }\n}\n\n/**\n * Validates primary of typedSignMessage, to ensure that it's type definition is present in message.\n *\n * @param data - The data passed in typedSign request.\n */\nfunction validatePrimaryType(data: string) {\n const { primaryType, types } = parseTypedMessage(data);\n if (!types) {\n throw rpcErrors.invalidInput();\n }\n\n // Primary type can be an array.\n const baseType = stripArrayTypeIfPresent(primaryType);\n\n // Return if the base type is not defined in the types\n const baseTypeDefinitions = types[baseType];\n if (!baseTypeDefinitions) {\n throw rpcErrors.invalidInput();\n }\n}\n\n/**\n * Validates verifyingContract of typedSignMessage.\n *\n * @param data - The data passed in typedSign request.\n * This function allows the verifyingContract to be either:\n * - A valid hex address\n * - The string \"cosmos\" (as it is hard-coded in some Cosmos ecosystem's EVM adapters)\n * - An empty string\n */\nfunction validateVerifyingContract(data: string) {\n const { domain: { verifyingContract } = {} } = parseTypedMessage(data);\n // Explicit check for cosmos here has been added to address this issue\n // https://github.com/MetaMask/eth-json-rpc-middleware/issues/337\n if (\n verifyingContract &&\n (verifyingContract as string) !== 'cosmos' &&\n !isValidHexAddress(verifyingContract)\n ) {\n throw rpcErrors.invalidInput();\n }\n}\n"]}
1
+ {"version":3,"file":"wallet.mjs","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,+BAA+B;AAMlD,OAAO,EAAE,wBAAwB,EAAE,qCAAqC;AAExE,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,iBAAiB,EAAE,wBAAwB;AAGpD,OAAO,EAAE,8CAA8C,EAAE,2DAAuD;AAEhH,OAAO,EAAE,4CAA4C,EAAE,yDAAqD;AAE5G,OAAO,EAAE,uBAAuB,EAAE,2BAAuB;AACzD,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,8BAA0B;AAC7E,OAAO,EACL,gBAAgB,EAChB,6BAA6B,IAAI,iBAAiB,EACnD,+BAA2B;AAiF5B;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,sBAAsB,CAAC,EACrC,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,qBAAqB,EACrB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;;GAQG;AACH,SAAS,yBAAyB,CAAC,IAAY;IAC7C,MAAM,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,GAAG,EAAE,EAAE,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACvE,sEAAsE;IACtE,iEAAiE;IACjE,IACE,iBAAiB;QAChB,iBAA4B,KAAK,QAAQ;QAC1C,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,EACrC,CAAC;QACD,MAAM,SAAS,CAAC,YAAY,EAAE,CAAC;IACjC,CAAC;AACH,CAAC","sourcesContent":["import * as sigUtil from '@metamask/eth-sig-util';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n MiddlewareParams,\n} from '@metamask/json-rpc-engine/v2';\nimport { createScaffoldMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { MessageRequest } from '@metamask/message-manager';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isValidHexAddress } from '@metamask/utils';\nimport type { JsonRpcRequest, Json, Hex } from '@metamask/utils';\n\nimport { createWalletRequestExecutionPermissionsHandler } from './methods/wallet-request-execution-permissions';\nimport type { ProcessRequestExecutionPermissionsHook } from './methods/wallet-request-execution-permissions';\nimport { createWalletRevokeExecutionPermissionHandler } from './methods/wallet-revoke-execution-permission';\nimport type { ProcessRevokeExecutionPermissionHook } from './methods/wallet-revoke-execution-permission';\nimport { stripArrayTypeIfPresent } from './utils/common';\nimport { normalizeTypedMessage, parseTypedMessage } from './utils/normalize';\nimport {\n resemblesAddress,\n validateAndNormalizeKeyholder as validateKeyholder,\n} from './utils/validation';\n\nexport type TransactionParams = {\n from: string;\n};\n\nexport type MessageParams = TransactionParams & {\n data: string;\n signatureMethod?: string;\n};\n\nexport type TypedMessageParams = MessageParams & {\n version: string;\n};\n\nexport type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {\n data: Record<string, unknown>[];\n};\n\nexport type WalletMiddlewareOptions = {\n getAccounts: (origin: string) => Promise<string[]>;\n processDecryptMessage?: (\n msgParams: MessageParams,\n req: MessageRequest,\n ) => Promise<string>;\n processEncryptionPublicKey?: (\n address: string,\n req: MessageRequest,\n ) => Promise<string>;\n processPersonalMessage?: (\n msgParams: MessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processSignTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processTypedMessage?: (\n msgParams: TypedMessageV1Params,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV3?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processTypedMessageV4?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n version: string,\n ) => Promise<string>;\n processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;\n processRevokeExecutionPermission?: ProcessRevokeExecutionPermissionHook;\n};\n\nexport type WalletMiddlewareKeyValues = {\n networkClientId: string;\n origin: string;\n securityAlertResponse?: Record<string, Json>;\n traceContext?: unknown;\n};\n\nexport type WalletMiddlewareContext =\n MiddlewareContext<WalletMiddlewareKeyValues>;\n\nexport type WalletMiddlewareParams = MiddlewareParams<\n JsonRpcRequest,\n WalletMiddlewareContext\n>;\n\n/**\n * Creates a JSON-RPC middleware that handles \"wallet\"-related JSON-RPC methods.\n * \"Wallet\" may have had a specific meaning at some point in the distant past,\n * but at this point it's just an arbitrary label.\n *\n * @param options - The options for the middleware.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @param options.processDecryptMessage - The function to process the decrypt message request.\n * @param options.processEncryptionPublicKey - The function to process the encryption public key request.\n * @param options.processPersonalMessage - The function to process the personal message request.\n * @param options.processTransaction - The function to process the transaction request.\n * @param options.processSignTransaction - The function to process the sign transaction request.\n * @param options.processTypedMessage - The function to process the typed message request.\n * @param options.processTypedMessageV3 - The function to process the typed message v3 request.\n * @param options.processTypedMessageV4 - The function to process the typed message v4 request.\n * @param options.processRequestExecutionPermissions - The function to process the request execution permissions request.\n * @param options.processRevokeExecutionPermission - The function to process the revoke execution permission request.\n * @returns A JSON-RPC middleware that handles wallet-related JSON-RPC methods.\n */\nexport function createWalletMiddleware({\n getAccounts,\n processDecryptMessage,\n processEncryptionPublicKey,\n processPersonalMessage,\n processTransaction,\n processSignTransaction,\n processTypedMessage,\n processTypedMessageV3,\n processTypedMessageV4,\n processRequestExecutionPermissions,\n processRevokeExecutionPermission,\n}: WalletMiddlewareOptions): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n WalletMiddlewareContext\n> {\n if (!getAccounts) {\n throw new Error('opts.getAccounts is required');\n }\n\n return createScaffoldMiddleware<WalletMiddlewareContext>({\n // account lookups\n eth_accounts: lookupAccounts,\n eth_coinbase: lookupDefaultAccount,\n\n // tx signatures\n eth_sendTransaction: sendTransaction,\n eth_signTransaction: signTransaction,\n\n // message signatures\n eth_signTypedData: signTypedData,\n eth_signTypedData_v3: signTypedDataV3,\n eth_signTypedData_v4: signTypedDataV4,\n personal_sign: personalSign,\n eth_getEncryptionPublicKey: encryptionPublicKey,\n eth_decrypt: decryptMessage,\n personal_ecRecover: personalRecover,\n\n // EIP-7715\n wallet_requestExecutionPermissions:\n createWalletRequestExecutionPermissionsHandler({\n processRequestExecutionPermissions,\n }),\n wallet_revokeExecutionPermission:\n createWalletRevokeExecutionPermissionHandler({\n processRevokeExecutionPermission,\n }),\n });\n\n //\n // account lookups\n //\n\n /**\n * Gets the accounts for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The accounts for the origin.\n */\n async function lookupAccounts({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n return await getAccounts(context.assertGet('origin'));\n }\n\n /**\n * Gets the default account (i.e. first in the list) for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The default account for the origin.\n */\n async function lookupDefaultAccount({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n const accounts = await getAccounts(context.assertGet('origin'));\n return accounts[0] || null;\n }\n\n //\n // transaction signatures\n //\n\n /**\n * Sends a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The transaction hash.\n */\n async function sendTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processTransaction(txParams, request, context);\n }\n\n /**\n * Signs a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed transaction.\n */\n async function signTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processSignTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processSignTransaction(txParams, request, context);\n }\n\n //\n // message signatures\n //\n\n /**\n * Signs a `eth_signTypedData` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedData({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [\n Record<string, unknown>[],\n string,\n Record<string, string>?,\n ];\n const message = params[0];\n const address = await validateAndNormalizeKeyholder(params[1], context);\n const version = 'V1';\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const extraParams = params[2] || {};\n const msgParams: TypedMessageV1Params = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'eth_signTypedData',\n version,\n };\n\n return await processTypedMessage(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v3` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV3({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV3) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V3';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v3',\n };\n\n return await processTypedMessageV3(msgParams, request, context, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v4` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV4({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV4) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V4';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v4',\n };\n\n return await processTypedMessageV4(msgParams, request, context, version);\n }\n\n /**\n * Signs a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function personalSign({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processPersonalMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string, TransactionParams?];\n\n // process normally\n const firstParam = params[0];\n const secondParam = params[1];\n // non-standard \"extraParams\" to be appended to our \"msgParams\" obj\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const extraParams = params[2] || {};\n\n // We initially incorrectly ordered these parameters.\n // To gracefully respect users who adopted this API early,\n // we are currently gracefully recovering from the wrong param order\n // when it is clearly identifiable.\n //\n // That means when the first param is definitely an address,\n // and the second param is definitely not, but is hex.\n let address: string, message: string;\n if (resemblesAddress(firstParam) && !resemblesAddress(secondParam)) {\n address = firstParam;\n message = secondParam;\n } else {\n message = firstParam;\n address = secondParam;\n }\n address = await validateAndNormalizeKeyholder(address, context);\n\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'personal_sign',\n };\n\n return await processPersonalMessage(msgParams, request, context);\n }\n\n /**\n * Recovers the signer address from a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @returns The recovered signer address.\n */\n async function personalRecover({\n request,\n }: WalletMiddlewareParams): Promise<Json> {\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n const message = params[0];\n const signature = params[1];\n const signerAddress = sigUtil.recoverPersonalSignature({\n data: message,\n signature,\n });\n\n return signerAddress;\n }\n\n /**\n * Gets the encryption public key for an address.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The encryption public key.\n */\n async function encryptionPublicKey({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processEncryptionPublicKey) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n\n return await processEncryptionPublicKey(address, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n /**\n * Decrypts a message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The decrypted message.\n */\n async function decryptMessage({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processDecryptMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n const params = request.params as [string, string, Record<string, Json>?];\n\n const ciphertext: string = params[0];\n const address: string = await validateAndNormalizeKeyholder(\n params[1],\n context,\n );\n // Not using nullish coalescing, since `params` may be `null`.\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const extraParams = params[2] || {};\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: ciphertext,\n };\n\n return await processDecryptMessage(msgParams, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.get('securityAlertResponse'),\n });\n }\n\n //\n // utility\n //\n\n /**\n * Validates the keyholder address, and returns a normalized (i.e. lowercase)\n * copy of it.\n *\n * @param address - The address to validate and normalize.\n * @param context - The context of the request.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\n async function validateAndNormalizeKeyholder(\n address: string,\n context: WalletMiddlewareContext,\n ): Promise<string> {\n return validateKeyholder(address as Hex, context, { getAccounts });\n }\n}\n\n/**\n * Validates primary of typedSignMessage, to ensure that it's type definition is present in message.\n *\n * @param data - The data passed in typedSign request.\n */\nfunction validatePrimaryType(data: string): void {\n const { primaryType, types } = parseTypedMessage(data);\n if (!types) {\n throw rpcErrors.invalidInput();\n }\n\n // Primary type can be an array.\n const baseType = stripArrayTypeIfPresent(primaryType);\n\n // Return if the base type is not defined in the types\n const baseTypeDefinitions = types[baseType];\n if (!baseTypeDefinitions) {\n throw rpcErrors.invalidInput();\n }\n}\n\n/**\n * Validates verifyingContract of typedSignMessage.\n *\n * @param data - The data passed in typedSign request.\n * This function allows the verifyingContract to be either:\n * - A valid hex address\n * - The string \"cosmos\" (as it is hard-coded in some Cosmos ecosystem's EVM adapters)\n * - An empty string\n */\nfunction validateVerifyingContract(data: string): void {\n const { domain: { verifyingContract } = {} } = parseTypedMessage(data);\n // Explicit check for cosmos here has been added to address this issue\n // https://github.com/MetaMask/eth-json-rpc-middleware/issues/337\n if (\n verifyingContract &&\n (verifyingContract as string) !== 'cosmos' &&\n !isValidHexAddress(verifyingContract)\n ) {\n throw rpcErrors.invalidInput();\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/eth-json-rpc-middleware",
3
- "version": "22.0.1-preview-cb4a07d5",
3
+ "version": "22.0.1-preview-eb60826c",
4
4
  "description": "Ethereum-related json-rpc-engine middleware",
5
5
  "keywords": [
6
6
  "MetaMask",