-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
843 additions
and
1,819 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
111 changes: 57 additions & 54 deletions
111
platforms/algorand/protocols/tokenBridge/src/bigVarint.ts
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,57 +1,60 @@ | ||
// Forever grateful to https://github.com/joeltg/big-varint/blob/main/src/unsigned.ts | ||
const LIMIT = BigInt(0x7f); | ||
|
||
export function encodingLength(value: bigint): number { | ||
let i = 0; | ||
|
||
for (; value >= BigInt(0x80); i++) { | ||
value >>= BigInt(7); | ||
} | ||
|
||
return i + 1; | ||
} | ||
|
||
export function encode(i: bigint, buffer?: ArrayBuffer, byteOffset?: number): Uint8Array { | ||
if (i < BigInt(0)) { | ||
throw new RangeError("value must be unsigned"); | ||
} | ||
|
||
const byteLength = encodingLength(i); | ||
buffer = buffer || new ArrayBuffer(byteLength); | ||
byteOffset = byteOffset || 0; | ||
if (buffer.byteLength < byteOffset + byteLength) { | ||
throw new RangeError("the buffer is too small to encode the number at the offset"); | ||
} | ||
|
||
const array = new Uint8Array(buffer, byteOffset); | ||
|
||
let offset = 0; | ||
while (LIMIT < i) { | ||
array[offset++] = Number(i & LIMIT) | 0x80; | ||
i >>= BigInt(7); | ||
} | ||
|
||
array[offset] = Number(i); | ||
|
||
return array; | ||
} | ||
|
||
export function encodeHex(i: bigint, buffer?: ArrayBuffer, byteOffset?: number): string { | ||
return Buffer.from(encode(i, buffer, byteOffset)).toString("hex"); | ||
} | ||
|
||
export function decode(data: Uint8Array, offset = 0): bigint { | ||
let i = BigInt(0); | ||
let n = 0; | ||
let b: number | undefined; | ||
do { | ||
b = data[offset + n]; | ||
if (b === undefined) { | ||
throw new RangeError("offset out of range"); | ||
|
||
import { encoding } from "@wormhole-foundation/connect-sdk"; | ||
|
||
// Useful for encoding numbers as varints to patch TEAL binary | ||
export const varint = { | ||
encodingLength: (value: bigint) => { | ||
let i = 0; | ||
for (; value >= BigInt(0x80); i++) { | ||
value >>= BigInt(7); | ||
} | ||
return i + 1; | ||
}, | ||
|
||
encode: (i: bigint | number, buffer?: ArrayBuffer, byteOffset?: number) => { | ||
if (typeof i === "number") i = BigInt(i); | ||
|
||
const LIMIT = BigInt(0x7f); | ||
if (i < BigInt(0)) { | ||
throw new RangeError("value must be unsigned"); | ||
} | ||
|
||
const byteLength = varint.encodingLength(i); | ||
buffer = buffer || new ArrayBuffer(byteLength); | ||
byteOffset = byteOffset || 0; | ||
if (buffer.byteLength < byteOffset + byteLength) { | ||
throw new RangeError("the buffer is too small to encode the number at the offset"); | ||
} | ||
|
||
const array = new Uint8Array(buffer, byteOffset); | ||
|
||
let offset = 0; | ||
while (LIMIT < i) { | ||
array[offset++] = Number(i & LIMIT) | 0x80; | ||
i >>= BigInt(7); | ||
} | ||
|
||
i += BigInt(b & 0x7f) << BigInt(n * 7); | ||
n++; | ||
} while (0x80 <= b); | ||
return i; | ||
} | ||
array[offset] = Number(i); | ||
|
||
return array; | ||
}, | ||
decode: (data: Uint8Array, offset = 0) => { | ||
let i = BigInt(0); | ||
let n = 0; | ||
let b: number | undefined; | ||
do { | ||
b = data[offset + n]; | ||
if (b === undefined) { | ||
throw new RangeError("offset out of range"); | ||
} | ||
|
||
i += BigInt(b & 0x7f) << BigInt(n * 7); | ||
n++; | ||
} while (0x80 <= b); | ||
return i; | ||
}, | ||
encodeHex: (i: bigint | number, buffer?: ArrayBuffer, byteOffset?: number) => { | ||
return encoding.hex.encode(varint.encode(i, buffer, byteOffset)); | ||
}, | ||
}; |
Oops, something went wrong.