@yubing744/rooch-sdk
Version:
97 lines (96 loc) • 2.82 kB
JavaScript
import * as util from "@kayahr/text-encoding";
const _BinaryDeserializer = class {
constructor(data) {
this.buffer = new ArrayBuffer(data.length);
new Uint8Array(this.buffer).set(data, 0);
this.offset = 0;
}
read(length) {
const bytes = this.buffer.slice(this.offset, this.offset + length);
this.offset += length;
return bytes;
}
deserializeStr() {
const value = this.deserializeBytes();
return _BinaryDeserializer.textDecoder.decode(value);
}
deserializeBytes() {
const len = this.deserializeLen();
if (len < 0) {
throw new Error("Length of a bytes array can't be negative");
}
return new Uint8Array(this.read(len));
}
deserializeBool() {
const bool = new Uint8Array(this.read(1))[0];
return bool == 1;
}
deserializeUnit() {
return null;
}
deserializeU8() {
return new DataView(this.read(1)).getUint8(0);
}
deserializeU16() {
return new DataView(this.read(2)).getUint16(0, true);
}
deserializeU32() {
return new DataView(this.read(4)).getUint32(0, true);
}
deserializeU64() {
const low = this.deserializeU32();
const high = this.deserializeU32();
return BigInt(
BigInt(high.toString()) << _BinaryDeserializer.BIG_32 | BigInt(low.toString())
);
}
deserializeU128() {
const low = this.deserializeU64();
const high = this.deserializeU64();
return BigInt(
BigInt(high.toString()) << _BinaryDeserializer.BIG_64 | BigInt(low.toString())
);
}
deserializeI8() {
return new DataView(this.read(1)).getInt8(0);
}
deserializeI16() {
return new DataView(this.read(2)).getInt16(0, true);
}
deserializeI32() {
return new DataView(this.read(4)).getInt32(0, true);
}
deserializeI64() {
const low = this.deserializeI32();
const high = this.deserializeI32();
return BigInt(high.toString()) << _BinaryDeserializer.BIG_32 | BigInt(low.toString());
}
deserializeI128() {
const low = this.deserializeI64();
const high = this.deserializeI64();
return BigInt(high.toString()) << _BinaryDeserializer.BIG_64 | BigInt(low.toString());
}
deserializeOptionTag() {
return this.deserializeBool();
}
getBufferOffset() {
return this.offset;
}
deserializeChar() {
throw new Error("Method deserializeChar not implemented.");
}
deserializeF32() {
return new DataView(this.read(4)).getFloat32(0, true);
}
deserializeF64() {
return new DataView(this.read(8)).getFloat64(0, true);
}
};
let BinaryDeserializer = _BinaryDeserializer;
BinaryDeserializer.BIG_32 = BigInt(32);
BinaryDeserializer.BIG_64 = BigInt(64);
BinaryDeserializer.textDecoder = typeof window === "undefined" ? new util.TextDecoder() : new TextDecoder();
export {
BinaryDeserializer
};
//# sourceMappingURL=binaryDeserializer.js.map