Skip to content

Commit

Permalink
Extend EIP 4844 transaction toJson data to include additional fields (#…
Browse files Browse the repository at this point in the history
…3365)

* Add networkWrapperToJson with extended eip 4844 data included

* Test networkWrapperToJson function

* Fix linting issue

* Add docstring

* Update packages/tx/test/eip4844.spec.ts

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>

* Update packages/tx/src/eip4844Transaction.ts

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>

* Update packages/tx/src/eip4844Transaction.ts

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>

* Rename ExtendedJsonTx to JsonBlobTxNetworkWrapper

---------

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
  • Loading branch information
scorbajio and acolytec3 authored Apr 24, 2024
1 parent 6b24349 commit 3125eac
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/tx/src/eip4844Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
TxData as AllTypesTxData,
TxValuesArray as AllTypesTxValuesArray,
BlobEIP4844NetworkValuesArray,
JsonBlobTxNetworkWrapper,
JsonTx,
TxOptions,
} from './types.js'
Expand Down Expand Up @@ -365,7 +366,6 @@ export class BlobEIP4844Transaction extends BaseTransaction<TransactionType.Blob
* @param opts any TxOptions defined
* @returns a BlobEIP4844Transaction
*/

public static fromSerializedBlobTxNetworkWrapper(
serialized: Uint8Array,
opts?: TxOptions
Expand Down Expand Up @@ -553,6 +553,36 @@ export class BlobEIP4844Transaction extends BaseTransaction<TransactionType.Blob
return Legacy.getSenderPublicKey(this)
}

/**
* Returns the EIP 4844 transaction network wrapper in JSON format similar to toJSON, including
* blobs, commitments, and proofs fields
* @param serialized a buffer representing a serialized BlobTransactionNetworkWrapper
* @param opts any TxOptions defined
* @returns JsonBlobTxNetworkWrapper with blobs, KZG commitments, and KZG proofs fields
*/
public static networkWrapperToJson(
serialized: Uint8Array,
opts?: TxOptions
): JsonBlobTxNetworkWrapper {
const tx = this.fromSerializedBlobTxNetworkWrapper(serialized, opts)

const accessListJSON = AccessLists.getAccessListJSON(tx.accessList)
const baseJson = tx.toJSON()

return {
...baseJson,
chainId: bigIntToHex(tx.chainId),
maxPriorityFeePerGas: bigIntToHex(tx.maxPriorityFeePerGas),
maxFeePerGas: bigIntToHex(tx.maxFeePerGas),
accessList: accessListJSON,
maxFeePerBlobGas: bigIntToHex(tx.maxFeePerBlobGas),
blobVersionedHashes: tx.blobVersionedHashes.map((hash) => bytesToHex(hash)),
blobs: tx.blobs!.map((bytes) => bytesToHex(bytes)),
kzgCommitments: tx.kzgCommitments!.map((bytes) => bytesToHex(bytes)),
kzgProofs: tx.kzgProofs!.map((bytes) => bytesToHex(bytes)),
}
}

toJSON(): JsonTx {
const accessListJSON = AccessLists.getAccessListJSON(this.accessList)
const baseJson = super.toJSON()
Expand Down
6 changes: 6 additions & 0 deletions packages/tx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ export interface JsonTx {
blobVersionedHashes?: PrefixedHexString[]
}

export type JsonBlobTxNetworkWrapper = JsonTx & {
blobs: PrefixedHexString[]
kzgCommitments: PrefixedHexString[]
kzgProofs: PrefixedHexString[]
}

/*
* Based on https://ethereum.org/en/developers/docs/apis/json-rpc/
*/
Expand Down
29 changes: 29 additions & 0 deletions packages/tx/test/eip4844.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,35 @@ describe('Network wrapper tests', () => {
const signedTx = unsignedTx.sign(pk)
const sender = signedTx.getSenderAddress().toString()
const wrapper = signedTx.serializeNetworkWrapper()

const jsonData = BlobEIP4844Transaction.networkWrapperToJson(wrapper, { common })
assert.equal(jsonData.blobs?.length, blobs.length, 'contains the correct number of blobs')
for (let i = 0; i < jsonData.blobs.length; i++) {
const b1 = jsonData.blobs[i]
const b2 = bytesToHex(signedTx.blobs![i])
assert.equal(b1, b2, 'contains the same blobs')
}
assert.equal(
jsonData.kzgCommitments.length,
signedTx.kzgCommitments!.length,
'contains the correct number of commitments'
)
for (let i = 0; i < jsonData.kzgCommitments.length; i++) {
const c1 = jsonData.kzgCommitments[i]
const c2 = bytesToHex(signedTx.kzgCommitments![i])
assert.equal(c1, c2, 'contains the same commitments')
}
assert.equal(
jsonData.kzgProofs?.length,
signedTx.kzgProofs!.length,
'contains the correct number of proofs'
)
for (let i = 0; i < jsonData.kzgProofs.length; i++) {
const p1 = jsonData.kzgProofs[i]
const p2 = bytesToHex(signedTx.kzgProofs![i])
assert.equal(p1, p2, 'contains the same proofs')
}

const deserializedTx = BlobEIP4844Transaction.fromSerializedBlobTxNetworkWrapper(wrapper, {
common,
})
Expand Down

0 comments on commit 3125eac

Please sign in to comment.