Skip to content

Commit 7ee0e8e

Browse files
committed
tx: added additional code documentation
1 parent 54fa1f9 commit 7ee0e8e

File tree

3 files changed

+100
-20
lines changed

3 files changed

+100
-20
lines changed

packages/tx/src/baseTransaction.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,14 @@ export abstract class BaseTransaction<TransactionObject> {
173173
return new Address(publicToAddress(this.getSenderPublicKey()))
174174
}
175175

176+
/**
177+
* Returns the public key of the sender
178+
*/
176179
abstract getSenderPublicKey(): Buffer
177180

181+
/**
182+
* Signs a tx and returns a new signed tx object
183+
*/
178184
sign(privateKey: Buffer): TransactionObject {
179185
if (privateKey.length !== 32) {
180186
throw new Error('Private key must be 32 bytes in length.')
@@ -189,14 +195,14 @@ export abstract class BaseTransaction<TransactionObject> {
189195
return this._processSignature(v, r, s)
190196
}
191197

192-
// Accept the v,r,s values from the `sign` method, and convert this into a TransactionObject
193-
protected abstract _processSignature(v: number, r: Buffer, s: Buffer): TransactionObject
194-
195198
/**
196199
* Returns an object with the JSON representation of the transaction
197200
*/
198201
abstract toJSON(): JsonTx
199202

203+
// Accept the v,r,s values from the `sign` method, and convert this into a TransactionObject
204+
protected abstract _processSignature(v: number, r: Buffer, s: Buffer): TransactionObject
205+
200206
protected _validateExceedsMaxInteger(validateCannotExceedMaxInteger: { [key: string]: BN }) {
201207
for (const [key, value] of Object.entries(validateCannotExceedMaxInteger)) {
202208
if (value && value.gt(MAX_INTEGER)) {

packages/tx/src/eip2930Transaction.ts

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ type EIP2930ValuesArray = [
4141
export default class AccessListEIP2930Transaction extends BaseTransaction<AccessListEIP2930Transaction> {
4242
public readonly chainId: BN
4343
public readonly accessList: AccessListBuffer
44+
public readonly AccessListJSON: AccessList
4445

4546
get transactionType(): number {
4647
return 1
4748
}
4849

49-
public readonly AccessListJSON: AccessList
50-
5150
// EIP-2930 alias for `s`
5251
get senderS() {
5352
return this.s
@@ -59,16 +58,22 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
5958
}
6059

6160
// EIP-2930 alias for `v`
62-
6361
get yParity() {
6462
return this.v
6563
}
6664

65+
/**
66+
* Instantiate a transaction from a data dictionary
67+
*/
6768
public static fromTxData(txData: AccessListEIP2930TxData, opts: TxOptions = {}) {
6869
return new AccessListEIP2930Transaction(txData, opts)
6970
}
7071

71-
// Instantiate a transaction from the serialized tx. This means that the Buffer should start with 0x01.
72+
/**
73+
* Instantiate a transaction from the serialized tx.
74+
*
75+
* Note: this means that the Buffer should start with 0x01.
76+
*/
7277
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
7378
if (serialized[0] !== 1) {
7479
throw new Error(
@@ -84,14 +89,25 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
8489
return AccessListEIP2930Transaction.fromValuesArray(values, opts)
8590
}
8691

87-
// Instantiate a transaction from the serialized tx. This means that the Buffer should start with 0x01.
88-
// Alias of fromSerializedTx
92+
/**
93+
* Instantiate a transaction from the serialized tx.
94+
* (alias of fromSerializedTx())
95+
*
96+
* Note: This means that the Buffer should start with 0x01.
97+
*
98+
* @deprecated this constructor alias is deprecated and will be removed
99+
* in favor of the from SerializedTx() constructor
100+
*/
89101
public static fromRlpSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
90102
return AccessListEIP2930Transaction.fromSerializedTx(serialized, opts)
91103
}
92104

93-
// Create a transaction from a values array.
94-
// The format is: chainId, nonce, gasPrice, gasLimit, to, value, data, access_list, [yParity, senderR, senderS]
105+
/**
106+
* Create a transaction from a values array.
107+
*
108+
* The format is:
109+
* chainId, nonce, gasPrice, gasLimit, to, value, data, access_list, yParity (v), senderR (r), senderS (s)
110+
*/
95111
public static fromValuesArray(values: (Buffer | AccessListBuffer)[], opts: TxOptions = {}) {
96112
if (values.length == 8 || values.length == 11) {
97113
const [chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s] = <
@@ -122,6 +138,10 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
122138
}
123139
}
124140

141+
/**
142+
* This constructor takes the values, validates them, assigns them and freezes the object.
143+
* Use the static factory methods to assist in creating a Transaction object from varying data types.
144+
*/
125145
public constructor(txData: AccessListEIP2930TxData, opts: TxOptions = {}) {
126146
const { chainId, accessList } = txData
127147

@@ -236,6 +256,16 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
236256

237257
/**
238258
* Returns a Buffer Array of the raw Buffers of this transaction, in order.
259+
*
260+
* Note that if you want to use this function in a tx type independent way
261+
* to then use the raw data output for tx instantiation with
262+
* `Tx.fromValuesArray()` you should set the `asList` parameter to `true` -
263+
* which is ignored on a legacy tx but provides the correct format on
264+
* a typed tx.
265+
*
266+
* To prepare a tx to be added as block data with `Block.fromValuesArray()`
267+
* just use the plain `raw()` method.
268+
*
239269
* @param asList - By default, this method returns a concatenated Buffer
240270
* If this is not desired, then set this to `true`, to get a Buffer array.
241271
*/
@@ -268,11 +298,17 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
268298
return <Buffer>this.raw()
269299
}
270300

301+
/**
302+
* Computes a sha3-256 hash of the serialized unsigned tx, which is used to sign the transaction.
303+
*/
271304
getMessageToSign() {
272305
const base = this.raw(true).slice(0, 8)
273306
return keccak256(Buffer.concat([Buffer.from('01', 'hex'), rlp.encode(base)]))
274307
}
275308

309+
/**
310+
* Computes a sha3-256 hash of the serialized tx
311+
*/
276312
public hash(): Buffer {
277313
if (!this.isSigned()) {
278314
throw new Error('Cannot call hash method if transaction is not signed')
@@ -281,10 +317,16 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
281317
return keccak256(this.serialize())
282318
}
283319

320+
/**
321+
* Computes a sha3-256 hash which can be used to verify the signature
322+
*/
284323
public getMessageToVerifySignature(): Buffer {
285324
return this.getMessageToSign()
286325
}
287326

327+
/**
328+
* Returns the public key of the sender
329+
*/
288330
public getSenderPublicKey(): Buffer {
289331
if (!this.isSigned()) {
290332
throw new Error('Cannot call this method if transaction is not signed')

packages/tx/src/legacyTransaction.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ import { BaseTransaction } from './baseTransaction'
1919
const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
2020

2121
/**
22-
* An Ethereum transaction.
22+
* An Ethereum non-typed (legacy) transaction
2323
*/
2424
export default class Transaction extends BaseTransaction<Transaction> {
2525
get transactionType(): number {
2626
return 0
2727
}
2828

29+
/**
30+
* Instantiate a transaction from a data dictionary
31+
*/
2932
public static fromTxData(txData: TxData, opts: TxOptions = {}) {
3033
return new Transaction(txData, opts)
3134
}
3235

33-
public static fromRlpSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
36+
/**
37+
* Instantiate a transaction from the serialized tx.
38+
*/
39+
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
3440
const values = rlp.decode(serialized)
3541

3642
if (!Array.isArray(values)) {
@@ -40,11 +46,23 @@ export default class Transaction extends BaseTransaction<Transaction> {
4046
return this.fromValuesArray(values, opts)
4147
}
4248

43-
// alias of fromRlpSerializedTx
44-
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
45-
return Transaction.fromRlpSerializedTx(serialized, opts)
49+
/**
50+
* Instantiate a transaction from the serialized tx.
51+
* (alias of fromSerializedTx())
52+
*
53+
* @deprecated this constructor alias is deprecated and will be removed
54+
* in favor of the from SerializedTx() constructor
55+
*/
56+
public static fromRlpSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
57+
return Transaction.fromSerializedTx(serialized, opts)
4658
}
4759

60+
/**
61+
* Create a transaction from a values array.
62+
*
63+
* The format is:
64+
* nonce, gasPrice, gasLimit, to, value, data, v, r, s
65+
*/
4866
public static fromValuesArray(values: Buffer[], opts: TxOptions = {}) {
4967
if (values.length !== 6 && values.length !== 9) {
5068
throw new Error(
@@ -83,7 +101,9 @@ export default class Transaction extends BaseTransaction<Transaction> {
83101
/**
84102
* This constructor takes the values, validates them, assigns them and freezes the object.
85103
* Use the static factory methods to assist in creating a Transaction object from varying data types.
86-
* @note Transaction objects implement EIP155 by default. To disable it, pass in an `@ethereumjs/common` object set before EIP155 activation (i.e. before Spurious Dragon).
104+
*
105+
* @note Transaction objects implement EIP155 by default. To disable it, pass in an `@ethereumjs/common`
106+
* object set before EIP155 activation (i.e. before Spurious Dragon).
87107
*/
88108
public constructor(txData: TxData, opts: TxOptions = {}) {
89109
super(txData, opts)
@@ -109,6 +129,15 @@ export default class Transaction extends BaseTransaction<Transaction> {
109129

110130
/**
111131
* Returns a Buffer Array of the raw Buffers of this transaction, in order.
132+
*
133+
* Note that if you want to use this function in a tx type independent way
134+
* to then use the raw data output for tx instantiation with
135+
* `Tx.fromValuesArray()` you should set the `asList` parameter to `true` -
136+
* which is ignored on a legacy tx but provides the correct format on
137+
* a typed tx.
138+
*
139+
* To prepare a tx to be added as block data with `Block.fromValuesArray()`
140+
* just use the plain `raw()` method.
112141
*/
113142
raw(): Buffer[] {
114143
return [
@@ -154,6 +183,9 @@ export default class Transaction extends BaseTransaction<Transaction> {
154183
return rlphash(values)
155184
}
156185

186+
/**
187+
* Computes a sha3-256 hash of the serialized unsigned tx, which is used to sign the transaction.
188+
*/
157189
getMessageToSign() {
158190
return this._getMessageToSign(this._unsignedTxImplementsEIP155())
159191
}
@@ -165,14 +197,14 @@ export default class Transaction extends BaseTransaction<Transaction> {
165197
return rlphash(this.raw())
166198
}
167199

200+
/**
201+
* Computes a sha3-256 hash which can be used to verify the signature
202+
*/
168203
getMessageToVerifySignature() {
169204
const withEIP155 = this._signedTxImplementsEIP155()
170205
return this._getMessageToSign(withEIP155)
171206
}
172207

173-
/**
174-
* Returns the public key of the sender
175-
*/
176208
/**
177209
* Returns the public key of the sender
178210
*/

0 commit comments

Comments
 (0)