-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Added calltable serialization for TransactionV1, Changing Tr…
…ansactionV1 structure
- Loading branch information
1 parent
829463f
commit 332a002
Showing
14 changed files
with
641 additions
and
861 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { concat } from '@ethersproject/bytes'; | ||
import { toBytesU16, toBytesU32 } from './ByteConverters'; | ||
|
||
export class Field { | ||
readonly index: number; | ||
readonly offset: number; | ||
readonly value: Uint8Array; | ||
|
||
constructor(index: number, offset: number, value: Uint8Array) { | ||
this.index = index; | ||
this.offset = offset; | ||
this.value = value; | ||
} | ||
|
||
/** | ||
* Calculates the serialized vector size for the given number of fields. | ||
* @returns The size of the serialized vector. | ||
*/ | ||
static serializedVecSize(): number { | ||
return 4 + 4 * 2; | ||
} | ||
} | ||
|
||
export class CalltableSerialization { | ||
private fields: Field[] = []; | ||
private currentOffset = 0; | ||
|
||
/** | ||
* Adds a field to the call table. | ||
* @param index The field index. | ||
* @param value The field value as a byte array. | ||
* @returns The current instance of CalltableSerialization. | ||
*/ | ||
addField(index: number, value: Uint8Array): CalltableSerialization { | ||
if (this.fields.length !== index) { | ||
throw new Error('Add fields in correct index order.'); | ||
} | ||
|
||
const field = new Field(index, this.currentOffset, value); | ||
this.fields.push(field); | ||
this.currentOffset += value.length; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Serializes the call table to a byte array. | ||
* @returns A Uint8Array representing the serialized call table. | ||
*/ | ||
toBytes(): Uint8Array { | ||
const calltableBytes: Uint8Array[] = []; | ||
const payloadBytes: Uint8Array[] = []; | ||
|
||
calltableBytes.push(toBytesU32(this.fields.length)); | ||
|
||
for (const field of this.fields) { | ||
calltableBytes.push(toBytesU16(field.index)); | ||
calltableBytes.push(toBytesU32(field.offset)); | ||
payloadBytes.push(field.value); | ||
} | ||
|
||
calltableBytes.push(toBytesU32(this.currentOffset)); | ||
|
||
return concat([...calltableBytes, ...payloadBytes]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.