Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend EIP 4844 transaction toJson data to include additional fields #3365

Merged
merged 9 commits into from
Apr 24, 2024
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
Loading