UNPKG

ox

Version:

Ethereum Standard Library

174 lines 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.type = exports.serializedType = void 0; exports.assert = assert; exports.deserialize = deserialize; exports.from = from; exports.getSignPayload = getSignPayload; exports.hash = hash; exports.serialize = serialize; exports.toRpc = toRpc; exports.validate = validate; const AccessList = require("./AccessList.js"); const Address = require("./Address.js"); const Hash = require("./Hash.js"); const Hex = require("./Hex.js"); const Rlp = require("./Rlp.js"); const Signature = require("./Signature.js"); const TransactionEnvelope = require("./TransactionEnvelope.js"); exports.serializedType = '0x02'; exports.type = 'eip1559'; function assert(envelope) { const { chainId, maxPriorityFeePerGas, maxFeePerGas, to } = envelope; if (chainId <= 0) throw new TransactionEnvelope.InvalidChainIdError({ chainId }); if (to) Address.assert(to, { strict: false }); if (maxFeePerGas && BigInt(maxFeePerGas) > 2n ** 256n - 1n) throw new TransactionEnvelope.FeeCapTooHighError({ feeCap: maxFeePerGas }); if (maxPriorityFeePerGas && maxFeePerGas && maxPriorityFeePerGas > maxFeePerGas) throw new TransactionEnvelope.TipAboveFeeCapError({ maxFeePerGas, maxPriorityFeePerGas, }); } function deserialize(serialized) { const transactionArray = Rlp.toHex(Hex.slice(serialized, 1)); const [chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gas, to, value, data, accessList, yParity, r, s,] = transactionArray; if (!(transactionArray.length === 9 || transactionArray.length === 12)) throw new TransactionEnvelope.InvalidSerializedError({ attributes: { chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gas, to, value, data, accessList, ...(transactionArray.length > 9 ? { yParity, r, s, } : {}), }, serialized, type: exports.type, }); let transaction = { chainId: Number(chainId), type: exports.type, }; if (Hex.validate(to) && to !== '0x') transaction.to = to; if (Hex.validate(gas) && gas !== '0x') transaction.gas = BigInt(gas); if (Hex.validate(data) && data !== '0x') transaction.data = data; if (Hex.validate(nonce)) transaction.nonce = nonce === '0x' ? 0n : BigInt(nonce); if (Hex.validate(value) && value !== '0x') transaction.value = BigInt(value); if (Hex.validate(maxFeePerGas) && maxFeePerGas !== '0x') transaction.maxFeePerGas = BigInt(maxFeePerGas); if (Hex.validate(maxPriorityFeePerGas) && maxPriorityFeePerGas !== '0x') transaction.maxPriorityFeePerGas = BigInt(maxPriorityFeePerGas); if (accessList.length !== 0 && accessList !== '0x') transaction.accessList = AccessList.fromTupleList(accessList); const signature = r && s && yParity ? Signature.fromTuple([yParity, r, s]) : undefined; if (signature) transaction = { ...transaction, ...signature, }; assert(transaction); return transaction; } function from(envelope, options = {}) { const { signature } = options; const envelope_ = (typeof envelope === 'string' ? deserialize(envelope) : envelope); assert(envelope_); return { ...envelope_, ...(signature ? Signature.from(signature) : {}), type: 'eip1559', }; } function getSignPayload(envelope) { return hash(envelope, { presign: true }); } function hash(envelope, options = {}) { const { presign } = options; return Hash.keccak256(serialize({ ...envelope, ...(presign ? { r: undefined, s: undefined, yParity: undefined, v: undefined, } : {}), })); } function serialize(envelope, options = {}) { const { chainId, gas, nonce, to, value, maxFeePerGas, maxPriorityFeePerGas, accessList, data, input, } = envelope; assert(envelope); const accessTupleList = AccessList.toTupleList(accessList); const signature = Signature.extract(options.signature || envelope); const serialized = [ Hex.fromNumber(chainId), nonce ? Hex.fromNumber(nonce) : '0x', maxPriorityFeePerGas ? Hex.fromNumber(maxPriorityFeePerGas) : '0x', maxFeePerGas ? Hex.fromNumber(maxFeePerGas) : '0x', gas ? Hex.fromNumber(gas) : '0x', to ?? '0x', value ? Hex.fromNumber(value) : '0x', data ?? input ?? '0x', accessTupleList, ...(signature ? Signature.toTuple(signature) : []), ]; return Hex.concat(exports.serializedType, Rlp.fromHex(serialized)); } function toRpc(envelope) { const signature = Signature.extract(envelope); return { ...envelope, chainId: Hex.fromNumber(envelope.chainId), data: envelope.data ?? envelope.input, type: '0x2', ...(typeof envelope.gas === 'bigint' ? { gas: Hex.fromNumber(envelope.gas) } : {}), ...(typeof envelope.nonce === 'bigint' ? { nonce: Hex.fromNumber(envelope.nonce) } : {}), ...(typeof envelope.value === 'bigint' ? { value: Hex.fromNumber(envelope.value) } : {}), ...(typeof envelope.maxFeePerGas === 'bigint' ? { maxFeePerGas: Hex.fromNumber(envelope.maxFeePerGas) } : {}), ...(typeof envelope.maxPriorityFeePerGas === 'bigint' ? { maxPriorityFeePerGas: Hex.fromNumber(envelope.maxPriorityFeePerGas), } : {}), ...(signature ? Signature.toRpc(signature) : {}), }; } function validate(envelope) { try { assert(envelope); return true; } catch { return false; } } //# sourceMappingURL=TransactionEnvelopeEip1559.js.map