Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions common/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ export class ByteArray extends Uint8Array {
return self
}

/**
* Returns bytes in little-endian order.
*/
static fromU32(x: u32): ByteArray {
let self = new ByteArray(4)
self[0] = x as u8
self[1] = (x >> 8) as u8
self[2] = (x >> 16) as u8
self[3] = (x >> 24) as u8
return self
}

/**
* Returns bytes in little-endian order.
*/
static fromI64(x: i64): ByteArray {
let self = new ByteArray(8)
self[0] = x as u8
self[1] = (x >> 8) as u8
self[2] = (x >> 16) as u8
self[3] = (x >> 24) as u8
self[4] = (x >> 32) as u8
self[5] = (x >> 40) as u8
self[6] = (x >> 48) as u8
self[7] = (x >> 56) as u8
return self
}

/**
* Returns bytes in little-endian order.
*/
static fromU64(x: u64): ByteArray {
let self = new ByteArray(8)
self[0] = x as u8
self[1] = (x >> 8) as u8
self[2] = (x >> 16) as u8
self[3] = (x >> 24) as u8
self[4] = (x >> 32) as u8
self[5] = (x >> 40) as u8
self[6] = (x >> 48) as u8
self[7] = (x >> 56) as u8
return self
}

static empty(): ByteArray {
return ByteArray.fromI32(0)
}
Expand Down Expand Up @@ -121,6 +165,81 @@ export class ByteArray extends Uint8Array {
return x
}


/**
* Interprets the byte array as a little-endian I64.
* Throws in case of overflow.
*/

toI64(): i64 {
let isNeg = this.length > 0 && this[this.length - 1] >> 7 == 1
let padding = isNeg ? 255 : 0
for (let i = 8; i < this.length; i++) {
if (this[i] != padding) {
assert(false, 'overflow converting ' + this.toHexString() + ' to i64')
}
}
let paddedBytes = new Bytes(8)
paddedBytes[0] = padding
paddedBytes[1] = padding
paddedBytes[2] = padding
paddedBytes[3] = padding
paddedBytes[4] = padding
paddedBytes[5] = padding
paddedBytes[6] = padding
paddedBytes[7] = padding
let minLen = paddedBytes.length < this.length ? paddedBytes.length : this.length
for (let i = 0; i < minLen; i++) {
paddedBytes[i] = this[i]
}
let x: i64 = 0
x = (x | paddedBytes[7]) << 8
x = (x | paddedBytes[6]) << 8
x = (x | paddedBytes[5]) << 8
x = (x | paddedBytes[4]) << 8
x = (x | paddedBytes[3]) << 8
x = (x | paddedBytes[2]) << 8
x = (x | paddedBytes[1]) << 8
x = x | paddedBytes[0]
return x
}

/**
* Interprets the byte array as a little-endian U64.
* Throws in case of overflow.
*/

toU64(): u64 {
for (let i = 8; i < this.length; i++) {
if (this[i] != 0) {
assert(false, 'overflow converting ' + this.toHexString() + ' to u64')
}
}
let paddedBytes = new Bytes(8)
paddedBytes[0] = 0
paddedBytes[1] = 0
paddedBytes[2] = 0
paddedBytes[3] = 0
paddedBytes[4] = 0
paddedBytes[5] = 0
paddedBytes[6] = 0
paddedBytes[7] = 0
let minLen = paddedBytes.length < this.length ? paddedBytes.length : this.length
for (let i = 0; i < minLen; i++) {
paddedBytes[i] = this[i]
}
let x: u64 = 0
x = (x | paddedBytes[7]) << 8
x = (x | paddedBytes[6]) << 8
x = (x | paddedBytes[5]) << 8
x = (x | paddedBytes[4]) << 8
x = (x | paddedBytes[3]) << 8
x = (x | paddedBytes[2]) << 8
x = (x | paddedBytes[1]) << 8
x = x | paddedBytes[0]
return x
}

@operator('==')
equals(other: ByteArray): boolean {
if (this.length != other.length) {
Expand Down
35 changes: 34 additions & 1 deletion common/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ export class BigInt extends Uint8Array {
return BigInt.fromByteArray(byteArray)
}

static fromU32(x: u32): BigInt {
let byteArray = ByteArray.fromU32(x)
return BigInt.fromUnsignedBytes(byteArray)
}

static fromI64(x: i64): BigInt {
let byteArray = ByteArray.fromI64(x)
return BigInt.fromByteArray(byteArray)
}

static fromU64(x: u64): BigInt {
let byteArray = ByteArray.fromU64(x)
return BigInt.fromUnsignedBytes(byteArray)
}

static zero(): BigInt {
return BigInt.fromI32(0)
}
Expand All @@ -74,7 +89,7 @@ export class BigInt extends Uint8Array {
* `bytes` assumed to be little-endian. If your input is big-endian, call `.reverse()` first.
*/

static fromUnsignedBytes(bytes: Bytes): BigInt {
static fromUnsignedBytes(bytes: ByteArray): BigInt {
let signedBytes = new BigInt(bytes.length + 1)
for (let i = 0; i < bytes.length; i++) {
signedBytes[i] = bytes[i]
Expand Down Expand Up @@ -105,6 +120,24 @@ export class BigInt extends Uint8Array {
return byteArray.toI32()
}

toU32(): u32 {
let uint8Array = changetype<Uint8Array>(this)
let byteArray = changetype<ByteArray>(uint8Array)
return byteArray.toU32()
}

toI64(): i64 {
let uint8Array = changetype<Uint8Array>(this)
let byteArray = changetype<ByteArray>(uint8Array)
return byteArray.toI64()
}

toU64(): u64 {
let uint8Array = changetype<Uint8Array>(this)
let byteArray = changetype<ByteArray>(uint8Array)
return byteArray.toU64()
}

toBigDecimal(): BigDecimal {
return new BigDecimal(this)
}
Expand Down
27 changes: 27 additions & 0 deletions test/bigInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ export function testBigInt(): void {
assert(a.toI32() == -2147483648)
assert(b.toI32() == 2147483647)

a = BigInt.fromU32(U32.MIN_VALUE)
b = BigInt.fromU32(U32.MAX_VALUE)
let c = BigInt.fromU32(0)
assert(a < b && a <= b, `a: ${a.toU32()}, b: ${b.toU32()}`)
assert(b > a && b >= a, `a: ${a.toU32()}, b: ${b.toU32()}`)
assert(a.toU32() == 0, `Actual value ${a.toU32()}`)
assert(b.toU32() == 4294967295, `Actual value ${b.toU32()}`)
assert(c.toU32() == 0, `Actual value ${c.toU32()}`)

a = BigInt.fromI64(I64.MIN_VALUE)
b = BigInt.fromI64(I64.MAX_VALUE)
c = BigInt.fromI64(0)
assert(a < b && a <= b, `a: ${a.toU64()}, b: ${b.toU64()}`)
assert(b > a && b >= a, `a: ${a.toU64()}, b: ${b.toU64()}`)
assert(a.toI64() == -9223372036854775808, `Actual value ${a.toI64()}`)
assert(b.toI64() == 9223372036854775807, `Actual value ${b.toI64()}`)
assert(c.toI64() == 0, `Actual value ${c.toI64()}`)

a = BigInt.fromU64(U64.MIN_VALUE)
b = BigInt.fromU64(U64.MAX_VALUE)
c = BigInt.fromU64(0)
assert(a < b && a <= b, `a: ${a.toU64()}, b: ${b.toU64()}`)
assert(b > a && b >= a, `a: ${a.toU64()}, b: ${b.toU64()}`)
assert(a.toU64() == 0, `Actual value ${a.toU64()}`)
assert(b.toU64() == 18446744073709551615, `Actual value ${b.toU64()}`)
assert(c.toU64() == 0, `Actual value ${c.toU64()}`)

// This is 8071860 in binary.
let blockNumber = new ByteArray(3)
blockNumber[0] = 180
Expand Down
Loading