@@ -36,43 +36,51 @@ export abstract class BaseTransaction<TransactionObject> {
36
36
value : this . value ,
37
37
}
38
38
39
- this . validateExceedsMaxInteger ( validateCannotExceedMaxInteger )
39
+ this . _validateExceedsMaxInteger ( validateCannotExceedMaxInteger )
40
40
41
41
this . common =
42
42
( txOptions . common &&
43
43
Object . assign ( Object . create ( Object . getPrototypeOf ( txOptions . common ) ) , txOptions . common ) ) ??
44
44
new Common ( { chain : 'mainnet' } )
45
45
}
46
46
47
- protected validateExceedsMaxInteger ( validateCannotExceedMaxInteger : { [ key : string ] : BN } ) {
48
- for ( const [ key , value ] of Object . entries ( validateCannotExceedMaxInteger ) ) {
49
- if ( value && value . gt ( MAX_INTEGER ) ) {
50
- throw new Error ( `${ key } cannot exceed MAX_INTEGER, given ${ value } ` )
51
- }
52
- }
53
- }
54
-
55
47
/**
56
- * If the tx's `to` is to the creation address
48
+ * Checks if the transaction has the minimum amount of gas required
49
+ * (DataFee + TxFee + Creation Fee).
57
50
*/
58
- toCreationAddress ( ) : boolean {
59
- return this . to === undefined || this . to . buf . length === 0
60
- }
61
-
62
51
/**
63
- * Computes a sha3-256 hash of the serialized unsigned tx, which is used to sign the transaction.
52
+ * Checks if the transaction has the minimum amount of gas required
53
+ * (DataFee + TxFee + Creation Fee).
64
54
*/
65
- rawTxHash ( ) : Buffer {
66
- return this . getMessageToSign ( )
67
- }
55
+ validate ( ) : boolean
56
+ /* eslint-disable-next-line no-dupe-class-members */
57
+ validate ( stringError : false ) : boolean
58
+ /* eslint-disable-next-line no-dupe-class-members */
59
+ validate ( stringError : true ) : string [ ]
60
+ /* eslint-disable-next-line no-dupe-class-members */
61
+ validate ( stringError : boolean = false ) : boolean | string [ ] {
62
+ const errors = [ ]
68
63
69
- abstract getMessageToSign ( ) : Buffer
64
+ if ( this . getBaseFee ( ) . gt ( this . gasLimit ) ) {
65
+ errors . push ( `gasLimit is too low. given ${ this . gasLimit } , need at least ${ this . getBaseFee ( ) } ` )
66
+ }
67
+
68
+ if ( this . isSigned ( ) && ! this . verifySignature ( ) ) {
69
+ errors . push ( 'Invalid Signature' )
70
+ }
71
+
72
+ return stringError ? errors : errors . length === 0
73
+ }
70
74
71
75
/**
72
- * Returns chain ID
76
+ * The minimum amount of gas the tx must have (DataFee + TxFee + Creation Fee)
73
77
*/
74
- getChainId ( ) : number {
75
- return this . common . chainId ( )
78
+ getBaseFee ( ) : BN {
79
+ const fee = this . getDataFee ( ) . addn ( this . common . param ( 'gasPrices' , 'tx' ) )
80
+ if ( this . common . gteHardfork ( 'homestead' ) && this . toCreationAddress ( ) ) {
81
+ fee . iaddn ( this . common . param ( 'gasPrices' , 'txCreation' ) )
82
+ }
83
+ return fee
76
84
}
77
85
78
86
/**
@@ -89,17 +97,6 @@ export abstract class BaseTransaction<TransactionObject> {
89
97
return new BN ( cost )
90
98
}
91
99
92
- /**
93
- * The minimum amount of gas the tx must have (DataFee + TxFee + Creation Fee)
94
- */
95
- getBaseFee ( ) : BN {
96
- const fee = this . getDataFee ( ) . addn ( this . common . param ( 'gasPrices' , 'tx' ) )
97
- if ( this . common . gteHardfork ( 'homestead' ) && this . toCreationAddress ( ) ) {
98
- fee . iaddn ( this . common . param ( 'gasPrices' , 'txCreation' ) )
99
- }
100
- return fee
101
- }
102
-
103
100
/**
104
101
* The up front amount that an account must have for this transaction to be valid
105
102
*/
@@ -108,42 +105,42 @@ export abstract class BaseTransaction<TransactionObject> {
108
105
}
109
106
110
107
/**
111
- * Checks if the transaction has the minimum amount of gas required
112
- * (DataFee + TxFee + Creation Fee).
108
+ * If the tx's `to` is to the creation address
113
109
*/
110
+ toCreationAddress ( ) : boolean {
111
+ return this . to === undefined || this . to . buf . length === 0
112
+ }
113
+
114
114
/**
115
- * Checks if the transaction has the minimum amount of gas required
116
- * (DataFee + TxFee + Creation Fee).
115
+ * Returns the raw `Buffer[]` (Transaction) or `Buffer` (typed transaction).
116
+ * This is the data which is found in the transactions of the block body.
117
+ *
118
+ * Note that if you want to use this function in a tx type independent way
119
+ * to then use the raw data output for tx instantiation with
120
+ * `Tx.fromValuesArray()` you should set the `asList` parameter to `true` -
121
+ * which is ignored on a legacy tx but provides the correct format on
122
+ * a typed tx.
123
+ *
124
+ * To prepare a tx to be added as block data with `Block.fromValuesArray()`
125
+ * just use the plain `raw()` method.
117
126
*/
118
- validate ( ) : boolean
119
- /* eslint-disable-next-line no-dupe-class-members */
120
- validate ( stringError : false ) : boolean
121
- /* eslint-disable-next-line no-dupe-class-members */
122
- validate ( stringError : true ) : string [ ]
123
- /* eslint-disable-next-line no-dupe-class-members */
124
- validate ( stringError : boolean = false ) : boolean | string [ ] {
125
- const errors = [ ]
126
-
127
- if ( this . getBaseFee ( ) . gt ( this . gasLimit ) ) {
128
- errors . push ( `gasLimit is too low. given ${ this . gasLimit } , need at least ${ this . getBaseFee ( ) } ` )
129
- }
130
-
131
- if ( this . isSigned ( ) && ! this . verifySignature ( ) ) {
132
- errors . push ( 'Invalid Signature' )
133
- }
134
-
135
- return stringError ? errors : errors . length === 0
136
- }
127
+ abstract raw ( asList : boolean ) : Buffer [ ] | Buffer
137
128
138
129
/**
139
130
* Returns the encoding of the transaction.
140
131
*/
141
132
abstract serialize ( ) : Buffer
142
133
143
134
/**
144
- * Returns an object with the JSON representation of the transaction
135
+ * Computes a sha3-256 hash of the serialized unsigned tx, which is used to sign the transaction.
145
136
*/
146
- abstract toJSON ( ) : JsonTx
137
+ rawTxHash ( ) : Buffer {
138
+ return this . getMessageToSign ( )
139
+ }
140
+
141
+ abstract getMessageToSign ( ) : Buffer
142
+
143
+ abstract hash ( ) : Buffer
147
144
148
145
abstract isSigned ( ) : boolean
149
146
@@ -160,14 +157,8 @@ export abstract class BaseTransaction<TransactionObject> {
160
157
}
161
158
}
162
159
163
- /**
164
- * Returns the raw `Buffer[]` (Transaction) or `Buffer` (typed transaction).
165
- * This is the data which is found in the transactions of the block body.
166
- */
167
- abstract raw ( ) : Buffer [ ] | Buffer
168
- abstract hash ( ) : Buffer
169
-
170
160
abstract getMessageToVerifySignature ( ) : Buffer
161
+
171
162
/**
172
163
* Returns the sender's address
173
164
*/
@@ -187,9 +178,22 @@ export abstract class BaseTransaction<TransactionObject> {
187
178
/* eslint-disable-next-line prefer-const */
188
179
let { v, r, s } = ecsign ( msgHash , privateKey )
189
180
190
- return this . processSignature ( v , r , s )
181
+ return this . _processSignature ( v , r , s )
191
182
}
192
183
193
184
// Accept the v,r,s values from the `sign` method, and convert this into a TransactionObject
194
- protected abstract processSignature ( v : number , r : Buffer , s : Buffer ) : TransactionObject
185
+ protected abstract _processSignature ( v : number , r : Buffer , s : Buffer ) : TransactionObject
186
+
187
+ /**
188
+ * Returns an object with the JSON representation of the transaction
189
+ */
190
+ abstract toJSON ( ) : JsonTx
191
+
192
+ protected _validateExceedsMaxInteger ( validateCannotExceedMaxInteger : { [ key : string ] : BN } ) {
193
+ for ( const [ key , value ] of Object . entries ( validateCannotExceedMaxInteger ) ) {
194
+ if ( value && value . gt ( MAX_INTEGER ) ) {
195
+ throw new Error ( `${ key } cannot exceed MAX_INTEGER, given ${ value } ` )
196
+ }
197
+ }
198
+ }
195
199
}
0 commit comments