@mysten/bcs
Version:
BCS - Canonical Binary Serialization implementation for JavaScript
137 lines (136 loc) • 4.19 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var reader_exports = {};
__export(reader_exports, {
BcsReader: () => BcsReader
});
module.exports = __toCommonJS(reader_exports);
var import_uleb = require("./uleb.js");
class BcsReader {
/**
* @param {Uint8Array} data Data to use as a buffer.
*/
constructor(data) {
this.bytePosition = 0;
this.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
}
/**
* Shift current cursor position by `bytes`.
*
* @param {Number} bytes Number of bytes to
* @returns {this} Self for possible chaining.
*/
shift(bytes) {
this.bytePosition += bytes;
return this;
}
/**
* Read U8 value from the buffer and shift cursor by 1.
* @returns
*/
read8() {
const value = this.dataView.getUint8(this.bytePosition);
this.shift(1);
return value;
}
/**
* Read U16 value from the buffer and shift cursor by 2.
* @returns
*/
read16() {
const value = this.dataView.getUint16(this.bytePosition, true);
this.shift(2);
return value;
}
/**
* Read U32 value from the buffer and shift cursor by 4.
* @returns
*/
read32() {
const value = this.dataView.getUint32(this.bytePosition, true);
this.shift(4);
return value;
}
/**
* Read U64 value from the buffer and shift cursor by 8.
* @returns
*/
read64() {
const value1 = this.read32();
const value2 = this.read32();
const result = value2.toString(16) + value1.toString(16).padStart(8, "0");
return BigInt("0x" + result).toString(10);
}
/**
* Read U128 value from the buffer and shift cursor by 16.
*/
read128() {
const value1 = BigInt(this.read64());
const value2 = BigInt(this.read64());
const result = value2.toString(16) + value1.toString(16).padStart(16, "0");
return BigInt("0x" + result).toString(10);
}
/**
* Read U128 value from the buffer and shift cursor by 32.
* @returns
*/
read256() {
const value1 = BigInt(this.read128());
const value2 = BigInt(this.read128());
const result = value2.toString(16) + value1.toString(16).padStart(32, "0");
return BigInt("0x" + result).toString(10);
}
/**
* Read `num` number of bytes from the buffer and shift cursor by `num`.
* @param num Number of bytes to read.
*/
readBytes(num) {
const start = this.bytePosition + this.dataView.byteOffset;
const value = new Uint8Array(this.dataView.buffer, start, num);
this.shift(num);
return value;
}
/**
* Read ULEB value - an integer of varying size. Used for enum indexes and
* vector lengths.
* @returns {Number} The ULEB value.
*/
readULEB() {
const start = this.bytePosition + this.dataView.byteOffset;
const buffer = new Uint8Array(this.dataView.buffer, start);
const { value, length } = (0, import_uleb.ulebDecode)(buffer);
this.shift(length);
return value;
}
/**
* Read a BCS vector: read a length and then apply function `cb` X times
* where X is the length of the vector, defined as ULEB in BCS bytes.
* @param cb Callback to process elements of vector.
* @returns {Array<Any>} Array of the resulting values, returned by callback.
*/
readVec(cb) {
const length = this.readULEB();
const result = [];
for (let i = 0; i < length; i++) {
result.push(cb(this, i, length));
}
return result;
}
}
//# sourceMappingURL=reader.js.map