ton3-core
Version:
TON low-level API tools
96 lines • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Builder = exports.Slice = exports.HashmapE = exports.Hashmap = exports.CellType = exports.Cell = exports.Mask = exports.BOC = void 0;
const builder_1 = require("./builder");
Object.defineProperty(exports, "Builder", { enumerable: true, get: function () { return builder_1.Builder; } });
const slice_1 = require("./slice");
Object.defineProperty(exports, "Slice", { enumerable: true, get: function () { return slice_1.Slice; } });
const mask_1 = require("./mask");
Object.defineProperty(exports, "Mask", { enumerable: true, get: function () { return mask_1.Mask; } });
const cell_1 = require("./cell");
Object.defineProperty(exports, "Cell", { enumerable: true, get: function () { return cell_1.Cell; } });
Object.defineProperty(exports, "CellType", { enumerable: true, get: function () { return cell_1.CellType; } });
const hashmap_1 = require("./hashmap");
Object.defineProperty(exports, "Hashmap", { enumerable: true, get: function () { return hashmap_1.Hashmap; } });
Object.defineProperty(exports, "HashmapE", { enumerable: true, get: function () { return hashmap_1.HashmapE; } });
const helpers_1 = require("../utils/helpers");
const serializer_1 = require("./serializer");
class BOC {
constructor(cells) {
if (cells.length === 0 || cells.length > 4) {
throw new Error('BOC: root cells length must be from 1 to 4');
}
this._root = cells;
}
static isHex(data) {
const re = /^[a-fA-F0-9]+$/;
return typeof data === 'string' && re.test(data);
}
static isBase64(data) {
const re = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/;
return typeof data === 'string' && re.test(data);
}
static isFift(data) {
const re = /^x\{/;
return typeof data === 'string' && re.test(data);
}
static isBytes(data) {
return ArrayBuffer.isView(data);
}
static toBase64(cells, options) {
const bytes = (0, serializer_1.serialize)(cells, options);
return (0, helpers_1.bytesToBase64)(bytes);
}
static toFiftHex(cells) {
const fift = cells.map(cell => cell.print());
return fift.join('\n');
}
static toHex(cells, options) {
const bytes = (0, serializer_1.serialize)(cells, options);
return (0, helpers_1.bytesToHex)(bytes);
}
[Symbol.iterator]() {
return this.root.values();
}
static from(data, checkMerkleProofs = false) {
if (BOC.isBytes(data)) {
return new BOC((0, serializer_1.deserialize)(data, checkMerkleProofs));
}
const value = data.trim();
if (BOC.isFift(value)) {
if (checkMerkleProofs) {
throw new Error('BOC: cheking Merkle Proofs is not currently implemented for fift hex');
}
return new BOC((0, serializer_1.deserializeFift)(value));
}
if (BOC.isHex(value)) {
return new BOC((0, serializer_1.deserialize)((0, helpers_1.hexToBytes)(value), checkMerkleProofs));
}
if (BOC.isBase64(value)) {
return new BOC((0, serializer_1.deserialize)((0, helpers_1.base64ToBytes)(value), checkMerkleProofs));
}
throw new Error('BOC: can\'t deserialize. Bad data.');
}
toBytes(options) {
return (0, serializer_1.serialize)(this.root, options);
}
toString(encoding = 'hex', options) {
if (encoding !== 'base64' && encoding !== 'hex' && encoding !== 'fift') {
throw new Error('BOC: unknown encoding');
}
if (encoding === 'base64') {
return BOC.toBase64(this.root, options);
}
if (encoding === 'hex') {
return BOC.toHex(this.root, options);
}
if (encoding === 'fift') {
return BOC.toFiftHex(this.root);
}
}
get root() {
return this._root.slice(0, this._root.length);
}
}
exports.BOC = BOC;
//# sourceMappingURL=index.js.map