avail-js-sdk
Version:
Avail library of functions to interact with blockchain and manipulate transactions
144 lines (143 loc) • 5.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Decoder = exports.Hasher = void 0;
exports.decodeBlake2_128Concat = decodeBlake2_128Concat;
exports.decodeTwox64Concat = decodeTwox64Concat;
exports.partiallyDecodeKey = partiallyDecodeKey;
const _1 = require("./.");
const util_1 = require("@polkadot/util");
var Hasher;
(function (Hasher) {
Hasher[Hasher["BLAKE2_128_CONCAT"] = 0] = "BLAKE2_128_CONCAT";
Hasher[Hasher["TWOX64_CONCAT"] = 1] = "TWOX64_CONCAT";
})(Hasher || (exports.Hasher = Hasher = {}));
function decodeBlake2_128Concat(input) {
// Blake2_128Concat keys are in the format:
// [16-byte Blake2_128 hash | original key bytes]
if (input.byteLength <= 16) {
throw new Error("Invalid Blake2_128Concat key format");
}
return input.slice(16); // Return the original key bytes
}
function decodeTwox64Concat(input) {
// Twox64Concat keys are in the format:
// [8-byte Twox64Concat | original key bytes]
if (input.byteLength <= 8) {
throw new Error("Invalid Twox64Concat key format");
}
return input.slice(8);
}
function partiallyDecodeKey(input, hasher) {
if (hasher == Hasher.BLAKE2_128_CONCAT) {
return new Uint8Array(decodeBlake2_128Concat(input.slice(32)));
}
else if (hasher == Hasher.TWOX64_CONCAT) {
return new Uint8Array(decodeTwox64Concat(input.slice(32)));
}
throw new Error("Unknown Hasher");
}
class Decoder {
constructor(array, offset) {
this.array = array;
this.offset = offset;
}
len() {
return this.array.length;
}
remainingLen() {
return this.array.length - this.offset;
}
hasAtLeast(count) {
return this.remainingLen() >= count;
}
decodeU8() {
if (!this.hasAtLeast(1)) {
throw new Error("Not enough bytes to decode u8");
}
const arrayValue = this.array.slice(this.offset, this.offset + 1);
const value = new _1.BN(arrayValue, "hex", "le");
this.offset += 1;
return value.toNumber();
}
decodeU16() {
if (!this.hasAtLeast(2)) {
throw new Error("Not enough bytes to decode u16");
}
const arrayValue = this.array.slice(this.offset, this.offset + 2);
const value = new _1.BN(arrayValue, "hex", "le");
this.offset += 2;
return value.toNumber();
}
decodeU32(compact) {
compact !== null && compact !== void 0 ? compact : (compact = false);
if (compact) {
const [offset, value] = (0, util_1.compactFromU8a)(this.array.slice(this.offset));
this.offset += offset;
return value.toNumber();
}
if (!this.hasAtLeast(4)) {
throw new Error("Not enough bytes to decode u32");
}
const arrayValue = this.array.slice(this.offset, this.offset + 4);
const value = new _1.BN(arrayValue, "hex", "le");
this.offset += 4;
return value.toNumber();
}
decodeU64(compact) {
compact !== null && compact !== void 0 ? compact : (compact = false);
if (compact) {
const [offset, value] = (0, util_1.compactFromU8a)(this.array.slice(this.offset));
this.offset += offset;
return value;
}
if (!this.hasAtLeast(8)) {
throw new Error("Not enough bytes to decode u64");
}
const arrayValue = this.array.slice(this.offset, this.offset + 8);
const value = new _1.BN(arrayValue, "hex", "le");
this.offset += 8;
return value;
}
decodeU128(compact) {
compact !== null && compact !== void 0 ? compact : (compact = false);
if (compact) {
const [offset, value] = (0, util_1.compactFromU8a)(this.array.slice(this.offset));
this.offset += offset;
return value;
}
if (!this.hasAtLeast(16)) {
throw new Error("Not enough bytes to decode u128");
}
const arrayValue = this.array.slice(this.offset, this.offset + 16);
const value = new _1.BN(arrayValue, "hex", "le");
this.offset += 16;
return value;
}
// Fixed Array
bytes(count) {
if (!this.hasAtLeast(count)) {
throw new Error("Not enough bytes to decode bytes");
}
const value = this.array.slice(this.offset, this.offset + count);
this.offset += count;
return value;
}
// Dynamic Array like Vec
bytesWLen() {
// Read Compact length
const [offset, length] = (0, util_1.compactFromU8a)(this.array.slice(this.offset));
this.offset += offset;
if (length.toNumber() == 0) {
return new Uint8Array();
}
const value = this.array.slice(this.offset, this.offset + length.toNumber());
this.offset += length.toNumber();
return value;
}
throwOnRemLength() {
/* if (this.remainingLen() > 0) {
throw Error(`RemainingLen is not zero. Length: ${this.remainingLen()}`)
} */
}
}
exports.Decoder = Decoder;