2022-09-05 23:57:11 +03:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
exports . Transaction = void 0 ;
const index _js _1 = require ( "../address/index.js" ) ;
const index _js _2 = require ( "../crypto/index.js" ) ;
const index _js _3 = require ( "../utils/index.js" ) ;
const accesslist _js _1 = require ( "./accesslist.js" ) ;
const address _js _1 = require ( "./address.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" ) ;
function handleAddress ( value ) {
if ( value === "0x" ) {
return null ;
}
return ( 0 , index _js _1 . getAddress ) ( value ) ;
}
function handleData ( value , param ) {
try {
return ( 0 , index _js _3 . hexlify ) ( value ) ;
}
catch ( error ) {
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( false , "invalid data" , param , value ) ;
2022-09-05 23:57:11 +03:00
}
}
function handleAccessList ( value , param ) {
try {
return ( 0 , accesslist _js _1 . accessListify ) ( value ) ;
}
catch ( error ) {
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( false , "invalid accessList" , param , value ) ;
2022-09-05 23:57:11 +03:00
}
}
function handleNumber ( _value , param ) {
if ( _value === "0x" ) {
return 0 ;
}
2022-09-16 05:58:45 +03:00
return ( 0 , index _js _3 . getNumber ) ( _value , param ) ;
2022-09-05 23:57:11 +03:00
}
function handleUint ( _value , param ) {
if ( _value === "0x" ) {
return BN _0 ;
}
2022-09-16 05:58:45 +03:00
const value = ( 0 , index _js _3 . getBigInt ) ( _value , param ) ;
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( value <= BN _MAX _UINT , "value exceeds uint size" , param , value ) ;
2022-09-05 23:57:11 +03:00
return value ;
}
function formatNumber ( _value , name ) {
2022-09-16 05:58:45 +03:00
const value = ( 0 , index _js _3 . getBigInt ) ( _value , "value" ) ;
2022-09-05 23:57:11 +03:00
const result = ( 0 , index _js _3 . toArray ) ( value ) ;
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( result . length <= 32 , ` value too large ` , ` tx. ${ name } ` , value ) ;
2022-09-05 23:57:11 +03:00
return result ;
}
function formatAccessList ( value ) {
return ( 0 , accesslist _js _1 . accessListify ) ( value ) . map ( ( set ) => [ set . address , set . storageKeys ] ) ;
}
function _parseLegacy ( data ) {
const fields = ( 0 , index _js _3 . decodeRlp ) ( data ) ;
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( Array . isArray ( fields ) && ( fields . length === 9 || fields . length === 6 ) , "invalid field count for legacy transaction" , "data" , data ) ;
2022-09-05 23:57:11 +03:00
const tx = {
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" ) ,
data : handleData ( fields [ 5 ] , "dta" ) ,
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-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( chainId !== BN _0 || ( v === BN _27 || v === BN _28 ) , "non-canonical legacy v" , "v" , fields [ 6 ] ) ;
2022-09-05 23:57:11 +03:00
tx . signature = index _js _2 . Signature . from ( {
r : ( 0 , index _js _3 . zeroPadValue ) ( fields [ 7 ] , 32 ) ,
s : ( 0 , index _js _3 . zeroPadValue ) ( fields [ 8 ] , 32 ) ,
v
} ) ;
tx . hash = ( 0 , index _js _2 . keccak256 ) ( data ) ;
}
return tx ;
}
function _serializeLegacy ( tx , sig ) {
const fields = [
formatNumber ( tx . nonce || 0 , "nonce" ) ,
formatNumber ( tx . gasPrice || 0 , "gasPrice" ) ,
formatNumber ( tx . gasLimit || 0 , "gasLimit" ) ,
( ( tx . to != null ) ? ( 0 , index _js _1 . getAddress ) ( tx . to ) : "0x" ) ,
formatNumber ( tx . value || 0 , "value" ) ,
( tx . data || "0x" ) ,
] ;
let chainId = BN _0 ;
if ( tx . chainId != null ) {
// A chainId was provided; if non-zero we'll use EIP-155
2022-09-16 05:58:45 +03:00
chainId = ( 0 , index _js _3 . getBigInt ) ( tx . chainId , "tx.chainId" ) ;
2022-09-05 23:57:11 +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-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( ! sig || sig . networkV == null || sig . legacyChainId === chainId , "tx.chainId/sig.v mismatch" , "sig" , sig ) ;
2022-09-05 23:57:11 +03:00
}
else if ( sig ) {
// No chainId provided, but the signature is signing with EIP-155; derive chainId
const legacy = sig . legacyChainId ;
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 ) {
fields . push ( ( 0 , index _js _3 . toArray ) ( chainId ) ) ;
fields . push ( "0x" ) ;
fields . push ( "0x" ) ;
}
return ( 0 , index _js _3 . encodeRlp ) ( fields ) ;
}
// We pushed a chainId and null r, s on for hashing only; remove those
let v = BigInt ( 27 + sig . yParity ) ;
if ( chainId !== BN _0 ) {
v = index _js _2 . Signature . getChainIdV ( chainId , sig . v ) ;
}
else if ( BigInt ( sig . v ) !== v ) {
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( false , "tx.chainId/sig.v mismatch" , "sig" , sig ) ;
2022-09-05 23:57:11 +03:00
}
fields . push ( ( 0 , index _js _3 . toArray ) ( v ) ) ;
fields . push ( ( 0 , index _js _3 . toArray ) ( sig . r ) ) ;
fields . push ( ( 0 , index _js _3 . toArray ) ( sig . s ) ) ;
return ( 0 , index _js _3 . encodeRlp ) ( fields ) ;
}
function _parseEipSignature ( tx , fields , serialize ) {
let yParity ;
try {
yParity = handleNumber ( fields [ 0 ] , "yParity" ) ;
if ( yParity !== 0 && yParity !== 1 ) {
throw new Error ( "bad yParity" ) ;
}
}
catch ( error ) {
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( false , "invalid yParity" , "yParity" , fields [ 0 ] ) ;
2022-09-05 23:57:11 +03:00
}
const r = ( 0 , index _js _3 . zeroPadValue ) ( fields [ 1 ] , 32 ) ;
const s = ( 0 , index _js _3 . zeroPadValue ) ( fields [ 2 ] , 32 ) ;
const signature = index _js _2 . Signature . from ( { r , s , yParity } ) ;
tx . signature = signature ;
}
function _parseEip1559 ( data ) {
2022-09-16 05:58:45 +03:00
const fields = ( 0 , index _js _3 . decodeRlp ) ( ( 0 , index _js _3 . getBytes ) ( data ) . slice ( 1 ) ) ;
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( Array . isArray ( fields ) && ( fields . length === 9 || fields . length === 12 ) , "invalid field count for transaction type: 2" , "data" , ( 0 , index _js _3 . hexlify ) ( data ) ) ;
2022-09-05 23:57:11 +03:00
const maxPriorityFeePerGas = handleUint ( fields [ 2 ] , "maxPriorityFeePerGas" ) ;
const maxFeePerGas = handleUint ( fields [ 3 ] , "maxFeePerGas" ) ;
const tx = {
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" ) ,
data : handleData ( fields [ 7 ] , "data" ) ,
accessList : handleAccessList ( fields [ 8 ] , "accessList" ) ,
} ;
// Unsigned EIP-1559 Transaction
if ( fields . length === 9 ) {
return tx ;
}
tx . hash = ( 0 , index _js _2 . keccak256 ) ( data ) ;
_parseEipSignature ( tx , fields . slice ( 9 ) , _serializeEip1559 ) ;
return tx ;
}
function _serializeEip1559 ( tx , sig ) {
const fields = [
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 ) ? ( 0 , index _js _1 . getAddress ) ( tx . to ) : "0x" ) ,
formatNumber ( tx . value || 0 , "value" ) ,
( tx . data || "0x" ) ,
( formatAccessList ( tx . accessList || [ ] ) )
] ;
if ( sig ) {
fields . push ( formatNumber ( sig . yParity , "yParity" ) ) ;
fields . push ( ( 0 , index _js _3 . toArray ) ( sig . r ) ) ;
fields . push ( ( 0 , index _js _3 . toArray ) ( sig . s ) ) ;
}
return ( 0 , index _js _3 . concat ) ( [ "0x02" , ( 0 , index _js _3 . encodeRlp ) ( fields ) ] ) ;
}
function _parseEip2930 ( data ) {
2022-09-16 05:58:45 +03:00
const fields = ( 0 , index _js _3 . decodeRlp ) ( ( 0 , index _js _3 . getBytes ) ( data ) . slice ( 1 ) ) ;
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( Array . isArray ( fields ) && ( fields . length === 8 || fields . length === 11 ) , "invalid field count for transaction type: 1" , "data" , ( 0 , index _js _3 . hexlify ) ( data ) ) ;
2022-09-05 23:57:11 +03:00
const tx = {
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" ) ,
data : handleData ( fields [ 6 ] , "data" ) ,
accessList : handleAccessList ( fields [ 7 ] , "accessList" )
} ;
// Unsigned EIP-2930 Transaction
if ( fields . length === 8 ) {
return tx ;
}
tx . hash = ( 0 , index _js _2 . keccak256 ) ( data ) ;
_parseEipSignature ( tx , fields . slice ( 8 ) , _serializeEip2930 ) ;
return tx ;
}
function _serializeEip2930 ( tx , sig ) {
const fields = [
formatNumber ( tx . chainId || 0 , "chainId" ) ,
formatNumber ( tx . nonce || 0 , "nonce" ) ,
formatNumber ( tx . gasPrice || 0 , "gasPrice" ) ,
formatNumber ( tx . gasLimit || 0 , "gasLimit" ) ,
( ( tx . to != null ) ? ( 0 , index _js _1 . getAddress ) ( tx . to ) : "0x" ) ,
formatNumber ( tx . value || 0 , "value" ) ,
( tx . data || "0x" ) ,
( formatAccessList ( tx . accessList || [ ] ) )
] ;
if ( sig ) {
fields . push ( formatNumber ( sig . yParity , "recoveryParam" ) ) ;
fields . push ( ( 0 , index _js _3 . toArray ) ( sig . r ) ) ;
fields . push ( ( 0 , index _js _3 . toArray ) ( sig . s ) ) ;
}
return ( 0 , index _js _3 . concat ) ( [ "0x01" , ( 0 , index _js _3 . encodeRlp ) ( fields ) ] ) ;
}
class Transaction {
# props ;
// A type of null indicates the type will be populated automatically
get type ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "type" ) ; }
get typeName ( ) {
switch ( this . type ) {
case 0 : return "legacy" ;
case 1 : return "eip-2930" ;
case 2 : return "eip-1559" ;
}
return null ;
}
set type ( value ) {
switch ( value ) {
case null :
( 0 , index _js _3 . setStore ) ( this . # props , "type" , null ) ;
break ;
case 0 :
case "legacy" :
( 0 , index _js _3 . setStore ) ( this . # props , "type" , 0 ) ;
break ;
case 1 :
case "berlin" :
case "eip-2930" :
( 0 , index _js _3 . setStore ) ( this . # props , "type" , 1 ) ;
break ;
case 2 :
case "london" :
case "eip-1559" :
( 0 , index _js _3 . setStore ) ( this . # props , "type" , 2 ) ;
break ;
default :
throw new Error ( ` unsupported transaction type ` ) ;
}
}
get to ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "to" ) ; }
set to ( value ) {
( 0 , index _js _3 . setStore ) ( this . # props , "to" , ( value == null ) ? null : ( 0 , index _js _1 . getAddress ) ( value ) ) ;
}
get nonce ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "nonce" ) ; }
2022-09-16 05:58:45 +03:00
set nonce ( value ) { ( 0 , index _js _3 . setStore ) ( this . # props , "nonce" , ( 0 , index _js _3 . getNumber ) ( value , "value" ) ) ; }
2022-09-05 23:57:11 +03:00
get gasLimit ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "gasLimit" ) ; }
2022-09-16 05:58:45 +03:00
set gasLimit ( value ) { ( 0 , index _js _3 . setStore ) ( this . # props , "gasLimit" , ( 0 , index _js _3 . getBigInt ) ( value ) ) ; }
2022-09-05 23:57:11 +03:00
get gasPrice ( ) {
const value = ( 0 , index _js _3 . getStore ) ( this . # props , "gasPrice" ) ;
if ( value == null && ( this . type === 0 || this . type === 1 ) ) {
return BN _0 ;
}
return value ;
}
set gasPrice ( value ) {
2022-09-16 05:58:45 +03:00
( 0 , index _js _3 . setStore ) ( this . # props , "gasPrice" , ( value == null ) ? null : ( 0 , index _js _3 . getBigInt ) ( value , "gasPrice" ) ) ;
2022-09-05 23:57:11 +03:00
}
get maxPriorityFeePerGas ( ) {
const value = ( 0 , index _js _3 . getStore ) ( this . # props , "maxPriorityFeePerGas" ) ;
if ( value == null && this . type === 2 ) {
return BN _0 ;
}
return value ;
}
set maxPriorityFeePerGas ( value ) {
2022-09-16 05:58:45 +03:00
( 0 , index _js _3 . setStore ) ( this . # props , "maxPriorityFeePerGas" , ( value == null ) ? null : ( 0 , index _js _3 . getBigInt ) ( value , "maxPriorityFeePerGas" ) ) ;
2022-09-05 23:57:11 +03:00
}
get maxFeePerGas ( ) {
const value = ( 0 , index _js _3 . getStore ) ( this . # props , "maxFeePerGas" ) ;
if ( value == null && this . type === 2 ) {
return BN _0 ;
}
return value ;
}
set maxFeePerGas ( value ) {
2022-09-16 05:58:45 +03:00
( 0 , index _js _3 . setStore ) ( this . # props , "maxFeePerGas" , ( value == null ) ? null : ( 0 , index _js _3 . getBigInt ) ( value , "maxFeePerGas" ) ) ;
2022-09-05 23:57:11 +03:00
}
get data ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "data" ) ; }
set data ( value ) { ( 0 , index _js _3 . setStore ) ( this . # props , "data" , ( 0 , index _js _3 . hexlify ) ( value ) ) ; }
get value ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "value" ) ; }
set value ( value ) {
2022-09-16 05:58:45 +03:00
( 0 , index _js _3 . setStore ) ( this . # props , "value" , ( 0 , index _js _3 . getBigInt ) ( value , "value" ) ) ;
2022-09-05 23:57:11 +03:00
}
get chainId ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "chainId" ) ; }
2022-09-16 05:58:45 +03:00
set chainId ( value ) { ( 0 , index _js _3 . setStore ) ( this . # props , "chainId" , ( 0 , index _js _3 . getBigInt ) ( value ) ) ; }
2022-09-05 23:57:11 +03:00
get signature ( ) { return ( 0 , index _js _3 . getStore ) ( this . # props , "sig" ) || null ; }
set signature ( value ) {
( 0 , index _js _3 . setStore ) ( this . # props , "sig" , ( value == null ) ? null : index _js _2 . Signature . from ( value ) ) ;
}
get accessList ( ) {
const value = ( 0 , index _js _3 . getStore ) ( this . # props , "accessList" ) || null ;
if ( value == null && ( this . type === 1 || this . type === 2 ) ) {
return [ ] ;
}
return value ;
}
set accessList ( value ) {
( 0 , index _js _3 . setStore ) ( this . # props , "accessList" , ( value == null ) ? null : ( 0 , accesslist _js _1 . accessListify ) ( value ) ) ;
}
constructor ( ) {
this . # props = {
type : null ,
to : null ,
nonce : 0 ,
gasLimit : BigInt ( 0 ) ,
gasPrice : null ,
maxPriorityFeePerGas : null ,
maxFeePerGas : null ,
data : "0x" ,
value : BigInt ( 0 ) ,
chainId : BigInt ( 0 ) ,
sig : null ,
accessList : null
} ;
}
get hash ( ) {
if ( this . signature == null ) {
2022-10-20 12:03:32 +03:00
return null ;
2022-09-05 23:57:11 +03:00
}
return ( 0 , index _js _2 . keccak256 ) ( this . serialized ) ;
}
get unsignedHash ( ) {
return ( 0 , index _js _2 . keccak256 ) ( this . unsignedSerialized ) ;
}
get from ( ) {
if ( this . signature == null ) {
return null ;
}
2022-10-20 12:03:32 +03:00
return ( 0 , address _js _1 . recoverAddress ) ( this . unsignedHash , this . signature ) ;
2022-09-05 23:57:11 +03:00
}
get fromPublicKey ( ) {
if ( this . signature == null ) {
return null ;
}
2022-10-20 12:03:32 +03:00
throw new Error ( "@TODO" ) ;
2022-09-05 23:57:11 +03:00
// use ecrecover
return "" ;
}
isSigned ( ) {
return this . signature != null ;
}
get serialized ( ) {
if ( this . signature == null ) {
throw new Error ( "cannot serialize unsigned transaction; maybe you meant .unsignedSerialized" ) ;
}
const types = this . inferTypes ( ) ;
if ( types . length !== 1 ) {
throw new Error ( "cannot determine transaction type; specify type manually" ) ;
}
switch ( types [ 0 ] ) {
case 0 :
return _serializeLegacy ( this , this . signature ) ;
case 1 :
return _serializeEip2930 ( this , this . signature ) ;
case 2 :
return _serializeEip1559 ( this , this . signature ) ;
}
throw new Error ( "unsupported type" ) ;
}
get unsignedSerialized ( ) {
const types = this . inferTypes ( ) ;
if ( types . length !== 1 ) {
throw new Error ( "cannot determine transaction type; specify type manually" ) ;
}
switch ( types [ 0 ] ) {
case 0 :
return _serializeLegacy ( this ) ;
case 1 :
return _serializeEip2930 ( this ) ;
case 2 :
return _serializeEip1559 ( this ) ;
}
throw new Error ( "unsupported type" ) ;
}
// Validates properties and lists possible types this transaction adheres to
inferTypes ( ) {
// 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 ) {
if ( this . maxFeePerGas < this . maxPriorityFeePerGas ) {
throw new Error ( "priorityFee cannot be more than maxFee" ) ;
}
}
//if (this.type === 2 && hasGasPrice) {
// throw new Error("eip-1559 transaction cannot have gasPrice");
//}
if ( ( this . type === 0 || this . type === 1 ) && hasFee ) {
throw new Error ( "transaction type cannot have maxFeePerGas or maxPriorityFeePerGas" ) ;
}
if ( this . type === 0 && hasAccessList ) {
throw new Error ( "legacy transaction cannot have accessList" ) ;
}
const types = [ ] ;
// 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-09-16 05:58:45 +03:00
isLegacy ( ) {
return ( this . type === 0 ) ;
}
isBerlin ( ) {
return ( this . type === 1 ) ;
}
isLondon ( ) {
return ( this . type === 2 ) ;
}
2022-09-05 23:57:11 +03:00
clone ( ) {
return Transaction . from ( this ) ;
}
freeze ( ) {
if ( this . # props . sig ) {
this . # props . sig = ( this . # props . sig . clone ( ) . freeze ( ) ) ;
}
if ( this . # props . accessList ) {
this . # props . accessList = Object . freeze ( this . # props . accessList . map ( ( set ) => {
Object . freeze ( set . storageKeys ) ;
return Object . freeze ( set ) ;
} ) ) ;
}
Object . freeze ( this . # props ) ;
return this ;
}
isFrozen ( ) {
return Object . isFrozen ( this . # props ) ;
}
2022-10-20 12:03:32 +03:00
toJSON ( ) {
const s = ( v ) => {
if ( v == null ) {
return null ;
}
return v . toString ( ) ;
} ;
return {
type : this . type ,
to : this . to ,
from : this . from ,
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-09-05 23:57:11 +03:00
static from ( tx ) {
if ( typeof ( tx ) === "string" ) {
2022-09-16 05:58:45 +03:00
const payload = ( 0 , index _js _3 . getBytes ) ( tx ) ;
2022-09-05 23:57:11 +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 ) ) ;
}
throw new Error ( "unsupported transaction type" ) ;
}
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 = index _js _2 . Signature . from ( tx . signature ) ;
}
if ( tx . accessList != null ) {
result . accessList = tx . accessList ;
}
if ( tx . hash != null ) {
if ( result . isSigned ( ) ) {
if ( result . hash !== tx . hash ) {
throw new Error ( "hash mismatch" ) ;
}
}
else {
throw new Error ( "unsigned transaction cannot have a hashs" ) ;
}
}
if ( tx . from != null ) {
if ( result . isSigned ( ) ) {
if ( result . from . toLowerCase ( ) !== ( tx . from || "" ) . toLowerCase ( ) ) {
throw new Error ( "from mismatch" ) ;
}
}
else {
throw new Error ( "unsigned transaction cannot have a from" ) ;
}
}
return result ;
}
}
exports . Transaction = Transaction ;
//# sourceMappingURL=transaction.js.map