2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
import { getAddress } from "../address/index.js";
|
2022-11-28 05:52:21 +03:00
|
|
|
import { keccak256, Signature, SigningKey } from "../crypto/index.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
import {
|
2022-11-28 05:52:21 +03:00
|
|
|
concat, decodeRlp, encodeRlp, getBytes, getBigInt, getNumber, hexlify,
|
2022-12-10 02:21:45 +03:00
|
|
|
assert, assertArgument, toBeArray, zeroPadValue
|
2022-09-05 23:14:43 +03:00
|
|
|
} from "../utils/index.js";
|
|
|
|
|
|
|
|
import { accessListify } from "./accesslist.js";
|
|
|
|
import { recoverAddress } from "./address.js";
|
|
|
|
|
2022-11-28 05:52:21 +03:00
|
|
|
import type { BigNumberish, BytesLike } from "../utils/index.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
import type { SignatureLike } from "../crypto/index.js";
|
|
|
|
|
|
|
|
import type { AccessList, AccessListish } from "./index.js";
|
|
|
|
|
|
|
|
|
|
|
|
const BN_0 = BigInt(0);
|
|
|
|
const BN_2 = BigInt(2);
|
|
|
|
const BN_27 = BigInt(27)
|
|
|
|
const BN_28 = BigInt(28)
|
|
|
|
const BN_35 = BigInt(35);
|
|
|
|
const BN_MAX_UINT = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
|
|
|
|
export interface TransactionLike<A = string> {
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The type.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
type?: null | number;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The recipient address or ``null`` for an ``init`` transaction.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
to?: null | A;
|
2022-12-03 05:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The sender.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
from?: null | A;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The nonce.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
nonce?: null | number;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The maximum amount of gas that can be used.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
gasLimit?: null | BigNumberish;
|
2022-12-03 05:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The gas price for legacy and berlin transactions.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
gasPrice?: null | BigNumberish;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The maximum priority fee per gas for london transactions.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
maxPriorityFeePerGas?: null | BigNumberish;
|
2022-12-03 05:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum total fee per gas for london transactions.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
maxFeePerGas?: null | BigNumberish;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The data.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
data?: null | string;
|
2022-12-03 05:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The value (in wei) to send.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
value?: null | BigNumberish;
|
2022-12-03 05:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The chain ID the transaction is valid on.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
chainId?: null | BigNumberish;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The transaction hash.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
hash?: null | string;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The signature provided by the sender.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
signature?: null | SignatureLike;
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The access list for berlin and london transactions.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
accessList?: null | AccessListish;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleAddress(value: string): null | string {
|
|
|
|
if (value === "0x") { return null; }
|
|
|
|
return getAddress(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleAccessList(value: any, param: string): AccessList {
|
|
|
|
try {
|
|
|
|
return accessListify(value);
|
2022-11-28 05:52:21 +03:00
|
|
|
} catch (error: any) {
|
|
|
|
assertArgument(false, error.message, param, value);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleNumber(_value: string, param: string): number {
|
|
|
|
if (_value === "0x") { return 0; }
|
2022-09-09 06:21:08 +03:00
|
|
|
return getNumber(_value, param);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleUint(_value: string, param: string): bigint {
|
|
|
|
if (_value === "0x") { return BN_0; }
|
2022-09-09 06:21:08 +03:00
|
|
|
const value = getBigInt(_value, param);
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(value <= BN_MAX_UINT, "value exceeds uint size", param, value);
|
2022-09-05 23:14:43 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatNumber(_value: BigNumberish, name: string): Uint8Array {
|
2022-09-09 06:21:08 +03:00
|
|
|
const value = getBigInt(_value, "value");
|
2022-12-10 02:21:45 +03:00
|
|
|
const result = toBeArray(value);
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(result.length <= 32, `value too large`, `tx.${ name }`, value);
|
2022-09-05 23:14:43 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatAccessList(value: AccessListish): Array<[ string, Array<string> ]> {
|
|
|
|
return accessListify(value).map((set) => [ set.address, set.storageKeys ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _parseLegacy(data: Uint8Array): TransactionLike {
|
|
|
|
const fields: any = decodeRlp(data);
|
|
|
|
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(Array.isArray(fields) && (fields.length === 9 || fields.length === 6),
|
|
|
|
"invalid field count for legacy transaction", "data", data);
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
const tx: TransactionLike = {
|
|
|
|
type: 0,
|
|
|
|
nonce: handleNumber(fields[0], "nonce"),
|
|
|
|
gasPrice: handleUint(fields[1], "gasPrice"),
|
|
|
|
gasLimit: handleUint(fields[2], "gasLimit"),
|
|
|
|
to: handleAddress(fields[3]),
|
|
|
|
value: handleUint(fields[4], "value"),
|
2022-11-28 05:52:21 +03:00
|
|
|
data: hexlify(fields[5]),
|
2022-09-05 23:14:43 +03:00
|
|
|
chainId: BN_0
|
|
|
|
};
|
|
|
|
|
|
|
|
// Legacy unsigned transaction
|
|
|
|
if (fields.length === 6) { return tx; }
|
|
|
|
|
|
|
|
const v = handleUint(fields[6], "v");
|
|
|
|
const r = handleUint(fields[7], "r");
|
|
|
|
const s = handleUint(fields[8], "s");
|
|
|
|
|
|
|
|
if (r === BN_0 && s === BN_0) {
|
|
|
|
// EIP-155 unsigned transaction
|
|
|
|
tx.chainId = v;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Compute the EIP-155 chain ID (or 0 for legacy)
|
|
|
|
let chainId = (v - BN_35) / BN_2;
|
|
|
|
if (chainId < BN_0) { chainId = BN_0; }
|
|
|
|
tx.chainId = chainId
|
|
|
|
|
|
|
|
// Signed Legacy Transaction
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(chainId !== BN_0 || (v === BN_27 || v === BN_28), "non-canonical legacy v", "v", fields[6]);
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
tx.signature = Signature.from({
|
|
|
|
r: zeroPadValue(fields[7], 32),
|
|
|
|
s: zeroPadValue(fields[8], 32),
|
|
|
|
v
|
|
|
|
});
|
|
|
|
|
|
|
|
tx.hash = keccak256(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _serializeLegacy(tx: Transaction, sig?: Signature): string {
|
|
|
|
const fields: Array<any> = [
|
|
|
|
formatNumber(tx.nonce || 0, "nonce"),
|
|
|
|
formatNumber(tx.gasPrice || 0, "gasPrice"),
|
|
|
|
formatNumber(tx.gasLimit || 0, "gasLimit"),
|
|
|
|
((tx.to != null) ? getAddress(tx.to): "0x"),
|
|
|
|
formatNumber(tx.value || 0, "value"),
|
|
|
|
(tx.data || "0x"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let chainId = BN_0;
|
2023-03-16 19:11:46 +03:00
|
|
|
if (tx.chainId != BN_0) {
|
2022-09-05 23:14:43 +03:00
|
|
|
// A chainId was provided; if non-zero we'll use EIP-155
|
2022-09-09 06:21:08 +03:00
|
|
|
chainId = getBigInt(tx.chainId, "tx.chainId");
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
// We have a chainId in the tx and an EIP-155 v in the signature,
|
|
|
|
// make sure they agree with each other
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(!sig || sig.networkV == null || sig.legacyChainId === chainId,
|
|
|
|
"tx.chainId/sig.v mismatch", "sig", sig);
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2023-03-16 19:11:46 +03:00
|
|
|
} else if (tx.signature) {
|
|
|
|
// No explicit chainId, but EIP-155 have a derived implicit chainId
|
|
|
|
const legacy = tx.signature.legacyChainId;
|
2022-09-05 23:14:43 +03:00
|
|
|
if (legacy != null) { chainId = legacy; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requesting an unsigned transaction
|
|
|
|
if (!sig) {
|
|
|
|
// We have an EIP-155 transaction (chainId was specified and non-zero)
|
|
|
|
if (chainId !== BN_0) {
|
2022-12-10 02:21:45 +03:00
|
|
|
fields.push(toBeArray(chainId));
|
2022-09-05 23:14:43 +03:00
|
|
|
fields.push("0x");
|
|
|
|
fields.push("0x");
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodeRlp(fields);
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:11:46 +03:00
|
|
|
// @TODO: We should probably check that tx.signature, chainId, and sig
|
|
|
|
// match but that logic could break existing code, so schedule
|
|
|
|
// this for the next major bump.
|
|
|
|
|
|
|
|
// Compute the EIP-155 v
|
2022-09-05 23:14:43 +03:00
|
|
|
let v = BigInt(27 + sig.yParity);
|
|
|
|
if (chainId !== BN_0) {
|
|
|
|
v = Signature.getChainIdV(chainId, sig.v);
|
|
|
|
} else if (BigInt(sig.v) !== v) {
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(false, "tx.chainId/sig.v mismatch", "sig", sig);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:11:46 +03:00
|
|
|
// Add the signature
|
2022-12-10 02:21:45 +03:00
|
|
|
fields.push(toBeArray(v));
|
|
|
|
fields.push(toBeArray(sig.r));
|
|
|
|
fields.push(toBeArray(sig.s));
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
return encodeRlp(fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _parseEipSignature(tx: TransactionLike, fields: Array<string>, serialize: (tx: TransactionLike) => string): void {
|
|
|
|
let yParity: number;
|
|
|
|
try {
|
|
|
|
yParity = handleNumber(fields[0], "yParity");
|
|
|
|
if (yParity !== 0 && yParity !== 1) { throw new Error("bad yParity"); }
|
|
|
|
} catch (error) {
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(false, "invalid yParity", "yParity", fields[0]);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const r = zeroPadValue(fields[1], 32);
|
|
|
|
const s = zeroPadValue(fields[2], 32);
|
|
|
|
|
|
|
|
const signature = Signature.from({ r, s, yParity });
|
|
|
|
tx.signature = signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _parseEip1559(data: Uint8Array): TransactionLike {
|
2022-09-09 06:21:08 +03:00
|
|
|
const fields: any = decodeRlp(getBytes(data).slice(1));
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(Array.isArray(fields) && (fields.length === 9 || fields.length === 12),
|
|
|
|
"invalid field count for transaction type: 2", "data", hexlify(data));
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
const maxPriorityFeePerGas = handleUint(fields[2], "maxPriorityFeePerGas");
|
|
|
|
const maxFeePerGas = handleUint(fields[3], "maxFeePerGas");
|
|
|
|
const tx: TransactionLike = {
|
|
|
|
type: 2,
|
|
|
|
chainId: handleUint(fields[0], "chainId"),
|
|
|
|
nonce: handleNumber(fields[1], "nonce"),
|
|
|
|
maxPriorityFeePerGas: maxPriorityFeePerGas,
|
|
|
|
maxFeePerGas: maxFeePerGas,
|
|
|
|
gasPrice: null,
|
|
|
|
gasLimit: handleUint(fields[4], "gasLimit"),
|
|
|
|
to: handleAddress(fields[5]),
|
|
|
|
value: handleUint(fields[6], "value"),
|
2022-11-28 05:52:21 +03:00
|
|
|
data: hexlify(fields[7]),
|
2022-09-05 23:14:43 +03:00
|
|
|
accessList: handleAccessList(fields[8], "accessList"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unsigned EIP-1559 Transaction
|
|
|
|
if (fields.length === 9) { return tx; }
|
|
|
|
|
|
|
|
tx.hash = keccak256(data);
|
|
|
|
|
|
|
|
_parseEipSignature(tx, fields.slice(9), _serializeEip1559);
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _serializeEip1559(tx: TransactionLike, sig?: Signature): string {
|
|
|
|
const fields: Array<any> = [
|
|
|
|
formatNumber(tx.chainId || 0, "chainId"),
|
|
|
|
formatNumber(tx.nonce || 0, "nonce"),
|
|
|
|
formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
|
|
|
|
formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
|
|
|
|
formatNumber(tx.gasLimit || 0, "gasLimit"),
|
|
|
|
((tx.to != null) ? getAddress(tx.to): "0x"),
|
|
|
|
formatNumber(tx.value || 0, "value"),
|
|
|
|
(tx.data || "0x"),
|
|
|
|
(formatAccessList(tx.accessList || []))
|
|
|
|
];
|
|
|
|
|
|
|
|
if (sig) {
|
|
|
|
fields.push(formatNumber(sig.yParity, "yParity"));
|
2022-12-10 02:21:45 +03:00
|
|
|
fields.push(toBeArray(sig.r));
|
|
|
|
fields.push(toBeArray(sig.s));
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return concat([ "0x02", encodeRlp(fields)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _parseEip2930(data: Uint8Array): TransactionLike {
|
2022-09-09 06:21:08 +03:00
|
|
|
const fields: any = decodeRlp(getBytes(data).slice(1));
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(Array.isArray(fields) && (fields.length === 8 || fields.length === 11),
|
|
|
|
"invalid field count for transaction type: 1", "data", hexlify(data));
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
const tx: TransactionLike = {
|
|
|
|
type: 1,
|
|
|
|
chainId: handleUint(fields[0], "chainId"),
|
|
|
|
nonce: handleNumber(fields[1], "nonce"),
|
|
|
|
gasPrice: handleUint(fields[2], "gasPrice"),
|
|
|
|
gasLimit: handleUint(fields[3], "gasLimit"),
|
|
|
|
to: handleAddress(fields[4]),
|
|
|
|
value: handleUint(fields[5], "value"),
|
2022-11-28 05:52:21 +03:00
|
|
|
data: hexlify(fields[6]),
|
2022-09-05 23:14:43 +03:00
|
|
|
accessList: handleAccessList(fields[7], "accessList")
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unsigned EIP-2930 Transaction
|
|
|
|
if (fields.length === 8) { return tx; }
|
|
|
|
|
|
|
|
tx.hash = keccak256(data);
|
|
|
|
|
|
|
|
_parseEipSignature(tx, fields.slice(8), _serializeEip2930);
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _serializeEip2930(tx: TransactionLike, sig?: Signature): string {
|
|
|
|
const fields: any = [
|
|
|
|
formatNumber(tx.chainId || 0, "chainId"),
|
|
|
|
formatNumber(tx.nonce || 0, "nonce"),
|
|
|
|
formatNumber(tx.gasPrice || 0, "gasPrice"),
|
|
|
|
formatNumber(tx.gasLimit || 0, "gasLimit"),
|
|
|
|
((tx.to != null) ? getAddress(tx.to): "0x"),
|
|
|
|
formatNumber(tx.value || 0, "value"),
|
|
|
|
(tx.data || "0x"),
|
|
|
|
(formatAccessList(tx.accessList || []))
|
|
|
|
];
|
|
|
|
|
|
|
|
if (sig) {
|
|
|
|
fields.push(formatNumber(sig.yParity, "recoveryParam"));
|
2022-12-10 02:21:45 +03:00
|
|
|
fields.push(toBeArray(sig.r));
|
|
|
|
fields.push(toBeArray(sig.s));
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return concat([ "0x01", encodeRlp(fields)]);
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* A **Transaction** describes an operation to be executed on
|
|
|
|
* Ethereum by an Externally Owned Account (EOA). It includes
|
|
|
|
* who (the [[to]] address), what (the [[data]]) and how much (the
|
|
|
|
* [[value]] in ether) the operation should entail.
|
2022-12-10 02:21:45 +03:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* tx = new Transaction()
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* tx.data = "0x1234";
|
|
|
|
* //_result:
|
2022-12-03 05:23:13 +03:00
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
export class Transaction implements TransactionLike<string> {
|
|
|
|
#type: null | number;
|
|
|
|
#to: null | string;
|
|
|
|
#data: string;
|
|
|
|
#nonce: number;
|
|
|
|
#gasLimit: bigint;
|
|
|
|
#gasPrice: null | bigint;
|
|
|
|
#maxPriorityFeePerGas: null | bigint;
|
|
|
|
#maxFeePerGas: null | bigint;
|
|
|
|
#value: bigint;
|
|
|
|
#chainId: bigint;
|
|
|
|
#sig: null | Signature;
|
|
|
|
#accessList: null | AccessList;
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The transaction type.
|
|
|
|
*
|
|
|
|
* If null, the type will be automatically inferred based on
|
|
|
|
* explicit properties.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get type(): null | number { return this.#type; }
|
2022-09-05 23:14:43 +03:00
|
|
|
set type(value: null | number | string) {
|
|
|
|
switch (value) {
|
|
|
|
case null:
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#type = null;
|
2022-09-05 23:14:43 +03:00
|
|
|
break;
|
|
|
|
case 0: case "legacy":
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#type = 0;
|
2022-09-05 23:14:43 +03:00
|
|
|
break;
|
|
|
|
case 1: case "berlin": case "eip-2930":
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#type = 1;
|
2022-09-05 23:14:43 +03:00
|
|
|
break;
|
|
|
|
case 2: case "london": case "eip-1559":
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#type = 2;
|
2022-09-05 23:14:43 +03:00
|
|
|
break;
|
|
|
|
default:
|
2022-11-28 05:52:21 +03:00
|
|
|
assertArgument(false, "unsupported transaction type", "type", value);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The name of the transaction type.
|
|
|
|
*/
|
|
|
|
get typeName(): null | string {
|
|
|
|
switch (this.type) {
|
|
|
|
case 0: return "legacy";
|
|
|
|
case 1: return "eip-2930";
|
|
|
|
case 2: return "eip-1559";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ``to`` address for the transaction or ``null`` if the
|
|
|
|
* transaction is an ``init`` transaction.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get to(): null | string { return this.#to; }
|
2022-09-05 23:14:43 +03:00
|
|
|
set to(value: null | string) {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#to = (value == null) ? null: getAddress(value);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The transaction nonce.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get nonce(): number { return this.#nonce; }
|
|
|
|
set nonce(value: BigNumberish) { this.#nonce = getNumber(value, "value"); }
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The gas limit.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get gasLimit(): bigint { return this.#gasLimit; }
|
|
|
|
set gasLimit(value: BigNumberish) { this.#gasLimit = getBigInt(value); }
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The gas price.
|
|
|
|
*
|
|
|
|
* On legacy networks this defines the fee that will be paid. On
|
|
|
|
* EIP-1559 networks, this should be ``null``.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get gasPrice(): null | bigint {
|
2022-11-28 05:52:21 +03:00
|
|
|
const value = this.#gasPrice;
|
2022-09-05 23:14:43 +03:00
|
|
|
if (value == null && (this.type === 0 || this.type === 1)) { return BN_0; }
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
set gasPrice(value: null | BigNumberish) {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#gasPrice = (value == null) ? null: getBigInt(value, "gasPrice");
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The maximum priority fee per unit of gas to pay. On legacy
|
|
|
|
* networks this should be ``null``.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get maxPriorityFeePerGas(): null | bigint {
|
2022-11-28 05:52:21 +03:00
|
|
|
const value = this.#maxPriorityFeePerGas;
|
|
|
|
if (value == null) {
|
|
|
|
if (this.type === 2) { return BN_0; }
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
set maxPriorityFeePerGas(value: null | BigNumberish) {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#maxPriorityFeePerGas = (value == null) ? null: getBigInt(value, "maxPriorityFeePerGas");
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The maximum total fee per unit of gas to pay. On legacy
|
|
|
|
* networks this should be ``null``.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get maxFeePerGas(): null | bigint {
|
2022-11-28 05:52:21 +03:00
|
|
|
const value = this.#maxFeePerGas;
|
|
|
|
if (value == null) {
|
|
|
|
if (this.type === 2) { return BN_0; }
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
set maxFeePerGas(value: null | BigNumberish) {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#maxFeePerGas = (value == null) ? null: getBigInt(value, "maxFeePerGas");
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The transaction data. For ``init`` transactions this is the
|
|
|
|
* deployment code.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get data(): string { return this.#data; }
|
|
|
|
set data(value: BytesLike) { this.#data = hexlify(value); }
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The amount of ether to send in this transactions.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get value(): bigint { return this.#value; }
|
2022-09-05 23:14:43 +03:00
|
|
|
set value(value: BigNumberish) {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#value = getBigInt(value, "value");
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The chain ID this transaction is valid on.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get chainId(): bigint { return this.#chainId; }
|
|
|
|
set chainId(value: BigNumberish) { this.#chainId = getBigInt(value); }
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* If signed, the signature for this transaction.
|
|
|
|
*/
|
2022-11-28 05:52:21 +03:00
|
|
|
get signature(): null | Signature { return this.#sig || null; }
|
2022-09-05 23:14:43 +03:00
|
|
|
set signature(value: null | SignatureLike) {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#sig = (value == null) ? null: Signature.from(value);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The access list.
|
|
|
|
*
|
|
|
|
* An access list permits discounted (but pre-paid) access to
|
|
|
|
* bytecode and state variable access within contract execution.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get accessList(): null | AccessList {
|
2022-11-28 05:52:21 +03:00
|
|
|
const value = this.#accessList || null;
|
|
|
|
if (value == null) {
|
|
|
|
if (this.type === 1 || this.type === 2) { return [ ]; }
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
set accessList(value: null | AccessListish) {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#accessList = (value == null) ? null: accessListify(value);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* Creates a new Transaction with default values.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
constructor() {
|
2022-11-28 05:52:21 +03:00
|
|
|
this.#type = null;
|
|
|
|
this.#to = null;
|
|
|
|
this.#nonce = 0;
|
|
|
|
this.#gasLimit = BigInt(0);
|
|
|
|
this.#gasPrice = null;
|
|
|
|
this.#maxPriorityFeePerGas = null;
|
|
|
|
this.#maxFeePerGas = null;
|
|
|
|
this.#data = "0x";
|
|
|
|
this.#value = BigInt(0);
|
|
|
|
this.#chainId = BigInt(0);
|
|
|
|
this.#sig = null;
|
|
|
|
this.#accessList = null;
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The transaction hash, if signed. Otherwise, ``null``.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get hash(): null | string {
|
2022-10-20 11:48:52 +03:00
|
|
|
if (this.signature == null) { return null; }
|
2022-09-05 23:14:43 +03:00
|
|
|
return keccak256(this.serialized);
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The pre-image hash of this transaction.
|
|
|
|
*
|
|
|
|
* This is the digest that a [[Signer]] must sign to authorize
|
|
|
|
* this transaction.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get unsignedHash(): string {
|
|
|
|
return keccak256(this.unsignedSerialized);
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The sending address, if signed. Otherwise, ``null``.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get from(): null | string {
|
|
|
|
if (this.signature == null) { return null; }
|
2022-10-20 11:48:52 +03:00
|
|
|
return recoverAddress(this.unsignedHash, this.signature);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The public key of the sender, if signed. Otherwise, ``null``.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get fromPublicKey(): null | string {
|
|
|
|
if (this.signature == null) { return null; }
|
2022-11-28 05:52:21 +03:00
|
|
|
return SigningKey.recoverPublicKey(this.unsignedHash, this.signature);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* Returns true if signed.
|
|
|
|
*
|
|
|
|
* This provides a Type Guard that properties requiring a signed
|
|
|
|
* transaction are non-null.
|
|
|
|
*/
|
|
|
|
isSigned(): this is (Transaction & { type: number, typeName: string, from: string, signature: Signature }) {
|
|
|
|
//isSigned(): this is SignedTransaction {
|
2022-09-05 23:14:43 +03:00
|
|
|
return this.signature != null;
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The serialized transaction.
|
|
|
|
*
|
|
|
|
* This throws if the transaction is unsigned. For the pre-image,
|
|
|
|
* use [[unsignedSerialized]].
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get serialized(): string {
|
2022-11-28 05:52:21 +03:00
|
|
|
assert(this.signature != null, "cannot serialize unsigned transaction; maybe you meant .unsignedSerialized", "UNSUPPORTED_OPERATION", { operation: ".serialized"});
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-11-28 05:52:21 +03:00
|
|
|
switch (this.inferType()) {
|
2022-09-05 23:14:43 +03:00
|
|
|
case 0:
|
|
|
|
return _serializeLegacy(this, this.signature);
|
|
|
|
case 1:
|
|
|
|
return _serializeEip2930(this, this.signature);
|
|
|
|
case 2:
|
|
|
|
return _serializeEip1559(this, this.signature);
|
|
|
|
}
|
|
|
|
|
2022-11-28 05:52:21 +03:00
|
|
|
assert(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: ".serialized" });
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* The transaction pre-image.
|
|
|
|
*
|
|
|
|
* The hash of this is the digest which needs to be signed to
|
|
|
|
* authorize this transaction.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
get unsignedSerialized(): string {
|
2022-11-28 05:52:21 +03:00
|
|
|
switch (this.inferType()) {
|
2022-09-05 23:14:43 +03:00
|
|
|
case 0:
|
|
|
|
return _serializeLegacy(this);
|
|
|
|
case 1:
|
|
|
|
return _serializeEip2930(this);
|
|
|
|
case 2:
|
|
|
|
return _serializeEip1559(this);
|
|
|
|
}
|
|
|
|
|
2022-11-28 05:52:21 +03:00
|
|
|
assert(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: ".unsignedSerialized" });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the most "likely" type; currently the highest
|
2022-12-03 05:23:13 +03:00
|
|
|
* supported transaction type.
|
2022-11-28 05:52:21 +03:00
|
|
|
*/
|
|
|
|
inferType(): number {
|
|
|
|
return <number>(this.inferTypes().pop());
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* Validates the explicit properties and returns a list of compatible
|
|
|
|
* transaction types.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
inferTypes(): Array<number> {
|
|
|
|
|
|
|
|
// Checks that there are no conflicting properties set
|
|
|
|
const hasGasPrice = this.gasPrice != null;
|
|
|
|
const hasFee = (this.maxFeePerGas != null || this.maxPriorityFeePerGas != null);
|
|
|
|
const hasAccessList = (this.accessList != null);
|
|
|
|
|
|
|
|
//if (hasGasPrice && hasFee) {
|
|
|
|
// throw new Error("transaction cannot have gasPrice and maxFeePerGas");
|
|
|
|
//}
|
|
|
|
|
|
|
|
if (this.maxFeePerGas != null && this.maxPriorityFeePerGas != null) {
|
2022-11-28 05:52:21 +03:00
|
|
|
assert(this.maxFeePerGas >= this.maxPriorityFeePerGas, "priorityFee cannot be more than maxFee", "BAD_DATA", { value: this });
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//if (this.type === 2 && hasGasPrice) {
|
|
|
|
// throw new Error("eip-1559 transaction cannot have gasPrice");
|
|
|
|
//}
|
|
|
|
|
2022-11-28 05:52:21 +03:00
|
|
|
assert(!hasFee || (this.type !== 0 && this.type !== 1), "transaction type cannot have maxFeePerGas or maxPriorityFeePerGas", "BAD_DATA", { value: this });
|
|
|
|
assert(this.type !== 0 || !hasAccessList, "legacy transaction cannot have accessList", "BAD_DATA", { value: this })
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
const types: Array<number> = [ ];
|
|
|
|
|
|
|
|
// Explicit type
|
|
|
|
if (this.type != null) {
|
|
|
|
types.push(this.type);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (hasFee) {
|
|
|
|
types.push(2);
|
|
|
|
} else if (hasGasPrice) {
|
|
|
|
types.push(1);
|
|
|
|
if (!hasAccessList) { types.push(0); }
|
|
|
|
} else if (hasAccessList) {
|
|
|
|
types.push(1);
|
|
|
|
types.push(2);
|
|
|
|
} else {
|
|
|
|
types.push(0);
|
|
|
|
types.push(1);
|
|
|
|
types.push(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
types.sort();
|
|
|
|
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* Returns true if this transaction is a legacy transaction (i.e.
|
|
|
|
* ``type === 0``).
|
|
|
|
*
|
|
|
|
* This provides a Type Guard that the related properties are
|
|
|
|
* non-null.
|
|
|
|
*/
|
2022-09-10 01:33:36 +03:00
|
|
|
isLegacy(): this is (Transaction & { type: 0, gasPrice: bigint }) {
|
|
|
|
return (this.type === 0);
|
|
|
|
}
|
2022-12-03 05:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this transaction is berlin hardform transaction (i.e.
|
|
|
|
* ``type === 1``).
|
|
|
|
*
|
|
|
|
* This provides a Type Guard that the related properties are
|
|
|
|
* non-null.
|
|
|
|
*/
|
2022-09-10 01:33:36 +03:00
|
|
|
isBerlin(): this is (Transaction & { type: 1, gasPrice: bigint, accessList: AccessList }) {
|
|
|
|
return (this.type === 1);
|
|
|
|
}
|
2022-12-03 05:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this transaction is london hardform transaction (i.e.
|
|
|
|
* ``type === 2``).
|
|
|
|
*
|
|
|
|
* This provides a Type Guard that the related properties are
|
|
|
|
* non-null.
|
|
|
|
*/
|
2022-09-10 01:33:36 +03:00
|
|
|
isLondon(): this is (Transaction & { type: 2, accessList: AccessList, maxFeePerGas: bigint, maxPriorityFeePerGas: bigint}) {
|
|
|
|
return (this.type === 2);
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* Create a copy of this transaciton.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
clone(): Transaction {
|
|
|
|
return Transaction.from(this);
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* Return a JSON-friendly object.
|
|
|
|
*/
|
2022-10-20 11:48:52 +03:00
|
|
|
toJSON(): any {
|
|
|
|
const s = (v: null | bigint) => {
|
|
|
|
if (v == null) { return null; }
|
|
|
|
return v.toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: this.type,
|
|
|
|
to: this.to,
|
2022-11-28 05:52:21 +03:00
|
|
|
// from: this.from,
|
2022-10-20 11:48:52 +03:00
|
|
|
data: this.data,
|
|
|
|
nonce: this.nonce,
|
|
|
|
gasLimit: s(this.gasLimit),
|
|
|
|
gasPrice: s(this.gasPrice),
|
|
|
|
maxPriorityFeePerGas: s(this.maxPriorityFeePerGas),
|
|
|
|
maxFeePerGas: s(this.maxFeePerGas),
|
|
|
|
value: s(this.value),
|
|
|
|
chainId: s(this.chainId),
|
|
|
|
sig: this.signature ? this.signature.toJSON(): null,
|
|
|
|
accessList: this.accessList
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:23:13 +03:00
|
|
|
/**
|
|
|
|
* Create a **Transaction** from a serialized transaction or a
|
|
|
|
* Transaction-like object.
|
|
|
|
*/
|
|
|
|
static from(tx?: string | TransactionLike<string>): Transaction {
|
|
|
|
if (tx == null) { return new Transaction(); }
|
|
|
|
|
2022-09-05 23:14:43 +03:00
|
|
|
if (typeof(tx) === "string") {
|
2022-09-09 06:21:08 +03:00
|
|
|
const payload = getBytes(tx);
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
if (payload[0] >= 0x7f) { // @TODO: > vs >= ??
|
|
|
|
return Transaction.from(_parseLegacy(payload));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(payload[0]) {
|
|
|
|
case 1: return Transaction.from(_parseEip2930(payload));
|
|
|
|
case 2: return Transaction.from(_parseEip1559(payload));
|
|
|
|
}
|
2022-11-28 05:52:21 +03:00
|
|
|
assert(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: "from" });
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = new Transaction();
|
|
|
|
if (tx.type != null) { result.type = tx.type; }
|
|
|
|
if (tx.to != null) { result.to = tx.to; }
|
|
|
|
if (tx.nonce != null) { result.nonce = tx.nonce; }
|
|
|
|
if (tx.gasLimit != null) { result.gasLimit = tx.gasLimit; }
|
|
|
|
if (tx.gasPrice != null) { result.gasPrice = tx.gasPrice; }
|
|
|
|
if (tx.maxPriorityFeePerGas != null) { result.maxPriorityFeePerGas = tx.maxPriorityFeePerGas; }
|
|
|
|
if (tx.maxFeePerGas != null) { result.maxFeePerGas = tx.maxFeePerGas; }
|
|
|
|
if (tx.data != null) { result.data = tx.data; }
|
|
|
|
if (tx.value != null) { result.value = tx.value; }
|
|
|
|
if (tx.chainId != null) { result.chainId = tx.chainId; }
|
|
|
|
if (tx.signature != null) { result.signature = Signature.from(tx.signature); }
|
|
|
|
if (tx.accessList != null) { result.accessList = tx.accessList; }
|
|
|
|
|
|
|
|
if (tx.hash != null) {
|
2023-01-17 21:51:52 +03:00
|
|
|
assertArgument(result.isSigned(), "unsigned transaction cannot define hash", "tx", tx);
|
2022-11-28 05:52:21 +03:00
|
|
|
assertArgument(result.hash === tx.hash, "hash mismatch", "tx", tx);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tx.from != null) {
|
2023-01-17 21:51:52 +03:00
|
|
|
assertArgument(result.isSigned(), "unsigned transaction cannot define from", "tx", tx);
|
2022-11-28 05:52:21 +03:00
|
|
|
assertArgument(result.from.toLowerCase() === (tx.from || "").toLowerCase(), "from mismatch", "tx", tx);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|