@okxweb3/coin-ethereum
Version:
An Ethereum SDK for building Web3 wallets and applications.
178 lines • 8.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ethereumjs_util_1 = require("../ethereumjs-util");
const coin_base_1 = require("@okxweb3/coin-base");
const baseTransaction_1 = require("./baseTransaction");
const util_1 = require("./util");
const TRANSACTION_TYPE = 4;
const TRANSACTION_TYPE_BUFFER = Buffer.from(TRANSACTION_TYPE.toString(16).padStart(2, '0'), 'hex');
class EOACodeEIP7702Transaction extends baseTransaction_1.BaseTransaction {
static fromTxData(txData) {
return new EOACodeEIP7702Transaction(txData);
}
static fromSerializedTx(serialized) {
if (!serialized.slice(0, 1).equals(TRANSACTION_TYPE_BUFFER)) {
throw new Error(`Invalid serialized tx input: not an EIP-1559 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 EOACodeEIP7702Transaction.fromValuesArray(values);
}
static fromValuesArray(values) {
if (values.length !== 10 && values.length !== 13) {
throw new Error('Invalid EIP-7702 transaction. Only expecting 10 values (for unsigned tx) or 13 values (for signed tx).');
}
const [chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, authorityList, v, r, s,] = values;
(0, ethereumjs_util_1.validateNoLeadingZeroes)({ nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, value, v, r, s });
return new EOACodeEIP7702Transaction({
chainId: new coin_base_1.BN(chainId),
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value,
data,
accessList: accessList ?? [],
authorizationList: authorityList ?? [],
v: v !== undefined ? new coin_base_1.BN(v) : undefined,
r,
s,
});
}
constructor(txData) {
super({ ...txData, type: TRANSACTION_TYPE });
const { chainId, accessList, authorizationList, maxFeePerGas, maxPriorityFeePerGas } = txData;
this.chainId = (0, ethereumjs_util_1.toType)(chainId, ethereumjs_util_1.TypeOutput.BN);
this.activeCapabilities = this.activeCapabilities.concat([1559, 2718, 2930, 7702]);
const accessListData = util_1.AccessLists.getAccessListData(accessList ?? []);
this.accessList = accessListData.accessList;
this.AccessListJSON = accessListData.AccessListJSON;
util_1.AccessLists.verifyAccessList(this.accessList);
this.maxFeePerGas = new coin_base_1.BN((0, ethereumjs_util_1.toBuffer)(maxFeePerGas === '' ? '0x' : maxFeePerGas));
this.maxPriorityFeePerGas = new coin_base_1.BN((0, ethereumjs_util_1.toBuffer)(maxPriorityFeePerGas === '' ? '0x' : maxPriorityFeePerGas));
this._validateCannotExceedMaxInteger({
maxFeePerGas: this.maxFeePerGas,
maxPriorityFeePerGas: this.maxPriorityFeePerGas,
});
if (this.gasLimit.mul(this.maxFeePerGas).gt(ethereumjs_util_1.MAX_INTEGER)) {
const msg = this._errorMsg('gasLimit * maxFeePerGas cannot exceed MAX_INTEGER (2^256-1)');
throw new Error(msg);
}
if (this.maxFeePerGas.lt(this.maxPriorityFeePerGas)) {
const msg = this._errorMsg('maxFeePerGas cannot be less than maxPriorityFeePerGas (The total must be the larger of the two)');
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);
}
const authorizationListData = util_1.AuthorizationLists.getAuthorizationListData(authorizationList ?? []);
this.authorizationList = authorizationListData.authorizationList;
this.AuthorizationListJSON = authorizationListData.AuthorizationListJSON;
util_1.AuthorizationLists.verifyAuthorizationList(this.authorizationList);
}
raw() {
return [
(0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.chainId),
(0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.nonce),
(0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.maxPriorityFeePerGas),
(0, ethereumjs_util_1.bnToUnpaddedBuffer)(this.maxFeePerGas),
(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.authorizationList,
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, 10);
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 EOACodeEIP7702Transaction.fromTxData({
chainId: this.chainId,
nonce: this.nonce,
maxPriorityFeePerGas: this.maxPriorityFeePerGas,
maxFeePerGas: this.maxFeePerGas,
gasLimit: this.gasLimit,
to: this.to,
value: this.value,
data: this.data,
accessList: this.accessList,
authorizationList: this.authorizationList,
v: new coin_base_1.BN(v - 27),
r: new coin_base_1.BN(r),
s: new coin_base_1.BN(s),
});
}
_processSignatureWithRawV(v, r, s) {
return EOACodeEIP7702Transaction.fromTxData({
chainId: this.chainId,
nonce: this.nonce,
maxPriorityFeePerGas: this.maxPriorityFeePerGas,
maxFeePerGas: this.maxFeePerGas,
gasLimit: this.gasLimit,
to: this.to,
value: this.value,
data: this.data,
accessList: this.accessList,
v: new coin_base_1.BN(v),
r: new coin_base_1.BN(r),
s: new coin_base_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),
maxPriorityFeePerGas: (0, ethereumjs_util_1.bnToHex)(this.maxPriorityFeePerGas),
maxFeePerGas: (0, ethereumjs_util_1.bnToHex)(this.maxFeePerGas),
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,
authorizationList: this.AuthorizationListJSON,
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 += ` maxFeePerGas=${this.maxFeePerGas} maxPriorityFeePerGas=${this.maxPriorityFeePerGas}`;
return errorStr;
}
_errorMsg(msg) {
return `${msg} (${this.errorStr()})`;
}
}
exports.default = EOACodeEIP7702Transaction;
//# sourceMappingURL=eip7702Transaction.js.map