UNPKG

@tevm/tx

Version:

A custom implementation of ethereumjs tx

1 lines 4.18 kB
{"version":3,"sources":["../src/createImpersonatedTx.js"],"names":["tx","FeeMarket1559Tx","InternalError","InvalidGasLimitError","keccak256"],"mappings":";;;;;;;AAoBa,IAAA,oBAAA,GAAuB,CAAC,MAAA,EAAQ,IAAS,KAAA;AAIrD,EAAI,IAAAA,IAAA;AACJ,EAAI,IAAA;AACH,IAAKA,IAAA,GAAA,IAAIC,kBAAgB,CAAA,MAAA,EAAQ,IAAI,CAAA;AAAA,WAC7B,CAAG,EAAA;AACX,IAAI,IAAA,EAAE,aAAa,KAAQ,CAAA,EAAA;AAC1B,MAAM,MAAA,IAAIC,oBAAc,CAAA,eAAA,EAAiB,EAAE,KAAA;AAAA;AAAA,QAAwB;AAAA,OAAA,EAAI,CAAA;AAAA;AAExE,IAAA,IAAI,CAAE,CAAA,OAAA,CAAQ,QAAS,CAAA,gCAAgC,CAAG,EAAA;AACzD,MAAA,MAAM,IAAIA,oBAAA;AAAA,QACT,0GAAA;AAAA,QACA,EAAE,OAAO,CAAE;AAAA,OACZ;AAAA;AAED,IACC,IAAA,CAAA,CAAE,QAAQ,QAAS,CAAA,4CAA4C,KAC/D,CAAE,CAAA,OAAA,CAAQ,QAAS,CAAA,6DAA6D,CAC/E,EAAA;AACD,MAAA,MAAM,IAAIC,2BAAqB,CAAA,CAAA,CAAE,SAAS,EAAE,KAAA,EAAO,GAAG,CAAA;AAAA;AAEvD,IAAA,IACC,EAAE,OAAQ,CAAA,QAAA;AAAA,MACT;AAAA,KAEA,EAAA;AACD,MAAA,MAAM,IAAIA,2BAAqB,CAAA,CAAA,CAAE,SAAS,EAAE,KAAA,EAAO,GAAG,CAAA;AAAA;AAEvD,IAAA,MAAM,IAAID,oBAAc,CAAA,CAAA,CAAE,SAAS,EAAE,KAAA,EAAO,GAAG,CAAA;AAAA;AAEhD,EAAA;AAAA;AAAA,IACC,IAAI,MAAMF,IAAI,EAAA;AAAA,MACb,GAAA,CAAI,QAAQ,IAAM,EAAA;AACjB,QAAA,IAAI,SAAS,gBAAkB,EAAA;AAC9B,UAAO,OAAA,IAAA;AAAA;AAER,QAAA,IAAI,SAAS,MAAQ,EAAA;AACpB,UAAA,OAAO,MAAM;AACZ,YAAI,IAAA;AACH,cAAA,OAAO,OAAO,IAAK,EAAA;AAAA,qBACX,CAAG,EAAA;AACX,cAAA,OAAOI,eAAU,CAAA,MAAA,CAAO,sBAAuB,EAAA,EAAG,OAAO,CAAA;AAAA;AAC1D,WACD;AAAA;AAED,QAAA,IAAI,SAAS,UAAY,EAAA;AACxB,UAAA,OAAO,MAAM,IAAA;AAAA;AAEd,QAAA,IAAI,SAAS,kBAAoB,EAAA;AAChC,UAAA,OAAO,MAAM,MAAO,CAAA,mBAAA;AAAA;AAErB,QAAO,OAAA,OAAA,CAAQ,GAAI,CAAA,MAAA,EAAQ,IAAI,CAAA;AAAA;AAChC,KACA;AAAA;AAEH","file":"index.cjs","sourcesContent":["import { FeeMarket1559Tx } from '@ethereumjs/tx'\nimport { InternalError, InvalidGasLimitError } from '@tevm/errors'\nimport { EthjsAddress, keccak256 } from '@tevm/utils'\n\n/**\n * @typedef {InternalError | InvalidGasLimitError} CreateImpersonatedTxError\n */\n\n/**\n * Creates an impersonated tx that wraps {@link FeeMarket1559Tx}.\n * Wraps following methods\n * - 'isImpersonated'\n * - 'hash'\n * - 'isSigned'\n * - 'getSenderAddress'\n * @throws {CreateImpersonatedTxError} Error if the constructor for {@link FeeMarket1559Tx} throws\n * @param {import(\"@ethereumjs/tx\").FeeMarketEIP1559TxData & {impersonatedAddress: EthjsAddress}} txData\n * @param {import(\"@ethereumjs/tx\").TxOptions} [opts]\n * @returns {import('./ImpersonatedTx.js').ImpersonatedTx}\n */\nexport const createImpersonatedTx = (txData, opts) => {\n\t/**\n\t * @type {FeeMarket1559Tx}\n\t */\n\tlet tx\n\ttry {\n\t\ttx = new FeeMarket1559Tx(txData, opts)\n\t} catch (e) {\n\t\tif (!(e instanceof Error)) {\n\t\t\tthrow new InternalError('Unknown Error', { cause: /** @type any*/ (e) })\n\t\t}\n\t\tif (e.message.includes('EIP-1559 not enabled on Common')) {\n\t\t\tthrow new InternalError(\n\t\t\t\t'EIP-1559 is not enabled on Common. Tevm currently only supports 1559 and it should be enabled by default',\n\t\t\t\t{ cause: e },\n\t\t\t)\n\t\t}\n\t\tif (\n\t\t\te.message.includes('gasLimit cannot exceed MAX_UINT64 (2^64-1)') ||\n\t\t\te.message.includes('gasLimit * maxFeePerGas cannot exceed MAX_INTEGER (2^256-1)')\n\t\t) {\n\t\t\tthrow new InvalidGasLimitError(e.message, { cause: e })\n\t\t}\n\t\tif (\n\t\t\te.message.includes(\n\t\t\t\t'maxFeePerGas cannot be less than maxPriorityFeePerGas (The total must be the larger of the two)',\n\t\t\t)\n\t\t) {\n\t\t\tthrow new InvalidGasLimitError(e.message, { cause: e })\n\t\t}\n\t\tthrow new InternalError(e.message, { cause: e })\n\t}\n\treturn /** @type {import('./ImpersonatedTx.js').ImpersonatedTx}*/ (\n\t\tnew Proxy(tx, {\n\t\t\tget(target, prop) {\n\t\t\t\tif (prop === 'isImpersonated') {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t\tif (prop === 'hash') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn target.hash()\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\treturn keccak256(target.getHashedMessageToSign(), 'bytes')\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (prop === 'isSigned') {\n\t\t\t\t\treturn () => true\n\t\t\t\t}\n\t\t\t\tif (prop === 'getSenderAddress') {\n\t\t\t\t\treturn () => txData.impersonatedAddress\n\t\t\t\t}\n\t\t\t\treturn Reflect.get(target, prop)\n\t\t\t},\n\t\t})\n\t)\n}\n"]}