-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add
ZigZag
and VLQ
encoding for BigInt
- Loading branch information
1 parent
255f116
commit 1271bfd
Showing
11 changed files
with
715 additions
and
514 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
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,40 @@ | ||
import { HexString, isEmpty } from "@fleet-sdk/common"; | ||
import { hexToBytes } from "@noble/hashes/utils"; | ||
import { vlqDecode, vlqDecodeBigInt } from "../vlq"; | ||
import { zigZagDecode, zigZagDecodeBigInt } from "../zigZag"; | ||
import { SigmaTypeCode } from "./sigmaTypeCode"; | ||
|
||
export class SigmaReader { | ||
private _bytes!: Uint8Array; | ||
private _cursor!: number; | ||
|
||
public get isEmpty(): boolean { | ||
return isEmpty(this._bytes); | ||
} | ||
|
||
constructor(bytes: HexString | Uint8Array) { | ||
if (typeof bytes === "string") { | ||
this._bytes = hexToBytes(bytes); | ||
} else { | ||
this._bytes = bytes; | ||
} | ||
|
||
this._cursor = 0; | ||
} | ||
|
||
public readByte(): number { | ||
return this._bytes[this._cursor++]; | ||
} | ||
|
||
public readType(): SigmaTypeCode { | ||
return this.readByte(); | ||
} | ||
|
||
public readNumber(): number { | ||
return Number(zigZagDecode(vlqDecode(this))); | ||
} | ||
|
||
public readLong(): bigint { | ||
return zigZagDecodeBigInt(vlqDecodeBigInt(this)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import { SigmaTypeCode } from "./sigmaTypeCode"; | ||
import { IPrimitiveSigmaType, ISigmaCollection, ISigmaType } from "./sigmaTypes"; | ||
|
||
export function isEmbeddable(typeCode: number): boolean { | ||
return typeCode >= 0x01 && typeCode <= 0x0b; | ||
} | ||
|
||
export function isPrimitiveType<T>(data: ISigmaType): data is IPrimitiveSigmaType<T> { | ||
return !isConstructorType(data); | ||
return !isConstructorTypeCode(data.type); | ||
} | ||
|
||
export function isColl<T>(data: ISigmaType): data is ISigmaCollection<T> { | ||
return data.type >= 0x0c && data.type <= 0x23; | ||
} | ||
|
||
export function isConstructorType(data: ISigmaType): boolean { | ||
return data.type >= 0x0c && data.type <= 0x60; | ||
export function isEmbeddableTypeCode(typeCode: number): boolean { | ||
return typeCode >= 0x01 && typeCode <= 0x0b; | ||
} | ||
|
||
export function isPrimitiveTypeCode(typeCode: SigmaTypeCode): boolean { | ||
return !isConstructorTypeCode(typeCode); | ||
} | ||
|
||
export function isConstructorTypeCode(type: SigmaTypeCode): boolean { | ||
return type >= 0x0c && type <= 0x60; | ||
} |
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.