Skip to content
This repository was archived by the owner on Jan 18, 2024. It is now read-only.
This repository was archived by the owner on Jan 18, 2024. It is now read-only.

Create New ABI Serialization Deserialization Classes #38

@ericpassmore

Description

@ericpassmore

Four or more classes

  • ABIEncoder
  • ABIDecoder
  • ABIEncodeError
  • ABIDecodeError

One or more interfaces

  • ABISerializableObject
interface ABISerializableObject {
    toABI?(encoder: ABIEncoder): void
    toJSON(): any
    equals(other: any): boolean
}
class ABIDecoder {
    private pos = 0
    private data: DataView
    private textDecoder = new TextDecoder('utf-8', {fatal: true})
    /* declared references */
    metadata: Record<string, any> = {}

    /* constructor */ 
    constructor(private array: Uint8Array)
    /* functions */
    canRead(bytes = 1): boolean
    /* check not to exceed buffer */
    private ensure(bytes: number) 
    setPosition(pos: number)
    getPosition(): number 
    advance(bytes: number) 
    /** Read one byte. */
    readByte(): number 
  
    /** Read floating point as JavaScript number, 32, 64 bits. Will extend later to 106 bits */
    readFloat(byteWidth: number) 
    readVaruint32() 
    readVarint32() 
    /** will need to add readVarint64() later with BigInt **/
    readArray(length: number) 
    readString() 
    decodeBinary(type: ABI.ResolvedType, decoder: ABIDecoder, ctx: DecodingContext): any
    /** no decode object as we don't have proper types in eosjs (note: could be wrong **/
}
export class ABIEncoder {
    private pos = 0
    private data: DataView
    private array: Uint8Array
    private textEncoder = new TextEncoder()

    /** User declared metadata, can be used to pass info to instances when encoding.  */
    metadata: Record<string, any> = {}

    /** constructor **/
    constructor(private pageSize = 1024) 

    /** functions **
    private ensure(bytes: number) 
    /** Write a single byte. */
    writeByte(byte: number) 
    /** Write an array of bytes. */
    writeArray(bytes: ArrayLike<number>) 
    /** need to extend this later to handle 106 bits **/
    writeFloat(value: number, byteWidth: number) 
    writeVaruint32(v: number) 
    writeVarint32(v: number) 
   /** need to extend writeVarint64 with BigInt **/
    writeString(v: string) 
    getData(): Uint8Array 
    getBytes(): Bytes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions