UNPKG

@okxweb3/coin-ethereum

Version:

An Ethereum SDK for building Web3 wallets and applications.

160 lines 7.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ethereumjs_util_1 = require("../ethereumjs-util"); const crypto_lib_1 = require("@okxweb3/crypto-lib"); const baseTransaction_1 = require("./baseTransaction"); const util_1 = require("./util"); const TRANSACTION_TYPE = 1; const TRANSACTION_TYPE_BUFFER = Buffer.from(TRANSACTION_TYPE.toString(16).padStart(2, '0'), 'hex'); class AccessListEIP2930Transaction extends baseTransaction_1.BaseTransaction { static fromTxData(txData) { return new AccessListEIP2930Transaction(txData); } static fromSerializedTx(serialized) { if (!serialized.slice(0, 1).equals(TRANSACTION_TYPE_BUFFER)) { throw new Error(`Invalid serialized tx input: not an EIP-2930 transaction (wrong tx type, expected: ${TRANSACTION_TYPE}, received: ${serialized .slice(0, 1) .toString('hex')}`); } const values = ethereumjs_util_1.rlp.decode(serialized.slice(1)); if (!Array.isArray(values)) { throw new Error('Invalid serialized tx input: must be array'); } return AccessListEIP2930Transaction.fromValuesArray(values); } static fromValuesArray(values) { if (values.length !== 8 && values.length !== 11) { throw new Error('Invalid EIP-2930 transaction. Only expecting 8 values (for unsigned tx) or 11 values (for signed tx).'); } const [chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s] = values; (0, ethereumjs_util_1.validateNoLeadingZeroes)({ nonce, gasPrice, gasLimit, value, v, r, s }); const emptyAccessList = []; return new AccessListEIP2930Transaction({ chainId: new crypto_lib_1.BN(chainId), nonce, gasPrice, gasLimit, to, value, data, accessList: accessList ?? emptyAccessList, v: v !== undefined ? new crypto_lib_1.BN(v) : undefined, r, s, }); } constructor(txData) { super({ ...txData, type: TRANSACTION_TYPE }); const { chainId, accessList, gasPrice } = txData; this.chainId = (0, ethereumjs_util_1.toType)(chainId, ethereumjs_util_1.TypeOutput.BN); this.activeCapabilities = this.activeCapabilities.concat([2718, 2930]); const accessListData = util_1.AccessLists.getAccessListData(accessList ?? []); this.accessList = accessListData.accessList; this.AccessListJSON = accessListData.AccessListJSON; util_1.AccessLists.verifyAccessList(this.accessList); this.gasPrice = new crypto_lib_1.BN((0, ethereumjs_util_1.toBuffer)(gasPrice === '' ? '0x' : gasPrice)); this._validateCannotExceedMaxInteger({ gasPrice: this.gasPrice, }); if (this.gasPrice.mul(this.gasLimit).gt(ethereumjs_util_1.MAX_INTEGER)) { const msg = this._errorMsg('gasLimit * gasPrice cannot exceed MAX_INTEGER'); throw new Error(msg); } if (this.v && !this.v.eqn(0) && !this.v.eqn(1)) { const msg = this._errorMsg('The y-parity of the transaction should either be 0 or 1'); throw new Error(msg); } } raw() { return [ (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.chainId), (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.nonce), (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.gasPrice), (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.gasLimit), this.to !== undefined ? this.to.buf : Buffer.from([]), (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.value), this.data, this.accessList, this.v !== undefined ? (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.v) : Buffer.from([]), this.r !== undefined ? (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.r) : Buffer.from([]), this.s !== undefined ? (0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.s) : Buffer.from([]), ]; } serialize() { const base = this.raw(); return Buffer.concat([TRANSACTION_TYPE_BUFFER, ethereumjs_util_1.rlp.encode(base)]); } getMessageToSign(hashMessage = true) { const base = this.raw().slice(0, 8); const message = Buffer.concat([TRANSACTION_TYPE_BUFFER, ethereumjs_util_1.rlp.encode(base)]); if (hashMessage) { return (0, ethereumjs_util_1.keccak256)(message); } else { return message; } } hash() { if (!this.isSigned()) { const msg = this._errorMsg('Cannot call hash method if transaction is not signed'); throw new Error(msg); } return (0, ethereumjs_util_1.keccak256)(this.serialize()); } _processSignature(v, r, s) { return AccessListEIP2930Transaction.fromTxData({ chainId: this.chainId, nonce: this.nonce, gasPrice: this.gasPrice, gasLimit: this.gasLimit, to: this.to, value: this.value, data: this.data, accessList: this.accessList, v: new crypto_lib_1.BN(v - 27), r: new crypto_lib_1.BN(r), s: new crypto_lib_1.BN(s), }); } _processSignatureWithRawV(v, r, s) { return AccessListEIP2930Transaction.fromTxData({ chainId: this.chainId, nonce: this.nonce, gasPrice: this.gasPrice, gasLimit: this.gasLimit, to: this.to, value: this.value, data: this.data, accessList: this.accessList, v: new crypto_lib_1.BN(v), r: new crypto_lib_1.BN(r), s: new crypto_lib_1.BN(s), }); } toJSON() { const accessListJSON = util_1.AccessLists.getAccessListJSON(this.accessList); return { chainId: (0, ethereumjs_util_1.bnToHex)(this.chainId), nonce: (0, ethereumjs_util_1.bnToHex)(this.nonce), gasPrice: (0, ethereumjs_util_1.bnToHex)(this.gasPrice), gasLimit: (0, ethereumjs_util_1.bnToHex)(this.gasLimit), to: this.to !== undefined ? this.to.toString() : undefined, value: (0, ethereumjs_util_1.bnToHex)(this.value), data: '0x' + this.data.toString('hex'), accessList: accessListJSON, v: this.v !== undefined ? (0, ethereumjs_util_1.bnToHex)(this.v) : undefined, r: this.r !== undefined ? (0, ethereumjs_util_1.bnToHex)(this.r) : undefined, s: this.s !== undefined ? (0, ethereumjs_util_1.bnToHex)(this.s) : undefined, }; } errorStr() { let errorStr = this._getSharedErrorPostfix(); errorStr += ` gasPrice=${this.gasPrice} accessListCount=${this.accessList?.length ?? 0}`; return errorStr; } _errorMsg(msg) { return `${msg} (${this.errorStr()})`; } } exports.default = AccessListEIP2930Transaction; //# sourceMappingURL=eip2930Transaction.js.map