UNPKG

navio-blsct

Version:

TypeScript bindings for the `libblsct` library used by the [Navio](https://nav.io/) blockchain to construct confidential transactions based on the BLS12-381 curve.

96 lines (95 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenId = void 0; const blsct_1 = require("./blsct"); const managedObj_1 = require("./managedObj"); /** Represents a token ID. A token ID consists of two parameters: token and subid, both of which are optional. When omitted, default values are used instead of random values. * * Examples: * ```ts * const { TokenId } = require('navio-blsct') * const tid1 = TokenId.default() * const tid2 = TokenId.fromToken(123) * const tid3 = TokenId.fromTokenAndSubid(123, 456) * tid3.getToken() // 123 * tid3.getSubid() // 456 * tid3.equals(tid2) // false * tid3.equals(tid3) // true * ser = tid3.serialize() * deser = TokenId.deserialize(ser) * deser.serialize() === ser // true * ``` */ class TokenId extends managedObj_1.ManagedObj { constructor(obj) { super(obj); } /** Generates a new default `TokenId` instance. * @returns A new `TokenId` instance with default values for token and subid. */ static default() { const rv = (0, blsct_1.genDefaultTokenId)(); return new TokenId(rv.value); } /** Generates a `TokenId` from the provided token. * * @param token - The token number to use for the `TokenId`. * @return A new `TokenId` instance with the specified token and a default subid. */ static fromToken(token) { const rv = (0, blsct_1.genTokenId)(token); const tokenId = TokenId.fromObj(rv.value); (0, blsct_1.freeObj)(rv); return tokenId; } /** Generates a `TokenId` from the provided token and subid. * * @param token - The token number to use for the `TokenId`. * @param subid - The subid number to use for the `TokenId`. * @return A new `TokenId` instance with the specified token and subid. */ static fromTokenAndSubid(token, subid) { const rv = (0, blsct_1.genTokenIdWithSubid)(token, subid); const tokenId = TokenId.fromObj(rv.value); (0, blsct_1.freeObj)(rv); return tokenId; } /** Returns the token number of the `TokenId`. * * @return The token number of the `TokenId`. */ getToken() { return (0, blsct_1.getTokenIdToken)(this.value()); } /** Returns the subid number of the `TokenId`. * * @return The subid number of the `TokenId`. */ getSubid() { return (0, blsct_1.getTokenIdSubid)(this.value()); } /** Checks if the current `TokenId` is equal to another `TokenId`. * * @param other - The `TokenId` to compare with. * @return `true` if both token and subid are equal, `false` otherwise. */ equals(other) { return this.getToken() === other.getToken() && this.getSubid() === other.getSubid(); } value() { return (0, blsct_1.castToTokenId)(this.obj); } serialize() { return (0, blsct_1.serializeTokenId)(this.value()); } /** Deserializes a hexadecimal string into a `TokenId` instance. * * @param hex - The hexadecimal string to convert. * @returns The `TokenId` instance represented by the input string. */ static deserialize(hex) { return TokenId._deserialize(hex, blsct_1.deserializeTokenId); } } exports.TokenId = TokenId;