@okxweb3/coin-stellar
Version:
@ok/coin-stellar is a Stellar SDK for building Web3 wallets and applications. It supports Stellar and PI blockchains, enabling private key management, address generation, transaction signing, trustline creation, and asset transfers
171 lines • 5.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.XdrLargeInt = void 0;
const js_xdr_1 = require("@stellar/js-xdr");
const uint128_1 = require("./uint128");
const uint256_1 = require("./uint256");
const int128_1 = require("./int128");
const int256_1 = require("./int256");
const xdr_1 = __importDefault(require("../xdr"));
class XdrLargeInt {
constructor(type, values) {
if (!(values instanceof Array)) {
values = [values];
}
values = values.map((i) => {
if (typeof i === 'bigint') {
return i;
}
if (i instanceof XdrLargeInt) {
return i.toBigInt();
}
return BigInt(i);
});
switch (type) {
case 'i64':
this.int = new js_xdr_1.Hyper(values);
break;
case 'i128':
this.int = new int128_1.Int128(values);
break;
case 'i256':
this.int = new int256_1.Int256(values);
break;
case 'u64':
this.int = new js_xdr_1.UnsignedHyper(values);
break;
case 'u128':
this.int = new uint128_1.Uint128(values);
break;
case 'u256':
this.int = new uint256_1.Uint256(values);
break;
default:
throw TypeError(`invalid type: ${type}`);
}
this.type = type;
}
toNumber() {
const bi = this.int.toBigInt();
if (bi > Number.MAX_SAFE_INTEGER || bi < Number.MIN_SAFE_INTEGER) {
throw RangeError(`value ${bi} not in range for Number ` +
`[${Number.MAX_SAFE_INTEGER}, ${Number.MIN_SAFE_INTEGER}]`);
}
return Number(bi);
}
toBigInt() {
return this.int.toBigInt();
}
toI64() {
this._sizeCheck(64);
const v = this.toBigInt();
if (BigInt.asIntN(64, v) !== v) {
throw RangeError(`value too large for i64: ${v}`);
}
return xdr_1.default.ScVal.scvI64(new xdr_1.default.Int64(v));
}
toU64() {
this._sizeCheck(64);
return xdr_1.default.ScVal.scvU64(new xdr_1.default.Uint64(BigInt.asUintN(64, this.toBigInt())));
}
toI128() {
this._sizeCheck(128);
const v = this.int.toBigInt();
const hi64 = BigInt.asIntN(64, v >> 64n);
const lo64 = BigInt.asUintN(64, v);
return xdr_1.default.ScVal.scvI128(new xdr_1.default.Int128Parts({
hi: new xdr_1.default.Int64(hi64),
lo: new xdr_1.default.Uint64(lo64)
}));
}
toU128() {
this._sizeCheck(128);
const v = this.int.toBigInt();
return xdr_1.default.ScVal.scvU128(new xdr_1.default.UInt128Parts({
hi: new xdr_1.default.Uint64(BigInt.asUintN(64, v >> 64n)),
lo: new xdr_1.default.Uint64(BigInt.asUintN(64, v))
}));
}
toI256() {
const v = this.int.toBigInt();
const hiHi64 = BigInt.asIntN(64, v >> 192n);
const hiLo64 = BigInt.asUintN(64, v >> 128n);
const loHi64 = BigInt.asUintN(64, v >> 64n);
const loLo64 = BigInt.asUintN(64, v);
return xdr_1.default.ScVal.scvI256(new xdr_1.default.Int256Parts({
hiHi: new xdr_1.default.Int64(hiHi64),
hiLo: new xdr_1.default.Uint64(hiLo64),
loHi: new xdr_1.default.Uint64(loHi64),
loLo: new xdr_1.default.Uint64(loLo64)
}));
}
toU256() {
const v = this.int.toBigInt();
const hiHi64 = BigInt.asUintN(64, v >> 192n);
const hiLo64 = BigInt.asUintN(64, v >> 128n);
const loHi64 = BigInt.asUintN(64, v >> 64n);
const loLo64 = BigInt.asUintN(64, v);
return xdr_1.default.ScVal.scvU256(new xdr_1.default.UInt256Parts({
hiHi: new xdr_1.default.Uint64(hiHi64),
hiLo: new xdr_1.default.Uint64(hiLo64),
loHi: new xdr_1.default.Uint64(loHi64),
loLo: new xdr_1.default.Uint64(loLo64)
}));
}
toScVal() {
switch (this.type) {
case 'i64':
return this.toI64();
case 'i128':
return this.toI128();
case 'i256':
return this.toI256();
case 'u64':
return this.toU64();
case 'u128':
return this.toU128();
case 'u256':
return this.toU256();
default:
throw TypeError(`invalid type: ${this.type}`);
}
}
valueOf() {
return this.int.valueOf();
}
toString() {
return this.int.toString();
}
toJSON() {
return {
value: this.toBigInt().toString(),
type: this.type
};
}
_sizeCheck(bits) {
if (this.int.size > bits) {
throw RangeError(`value too large for ${bits} bits (${this.type})`);
}
}
static isType(type) {
switch (type) {
case 'i64':
case 'i128':
case 'i256':
case 'u64':
case 'u128':
case 'u256':
return true;
default:
return false;
}
}
static getType(scvType) {
return scvType.slice(3).toLowerCase();
}
}
exports.XdrLargeInt = XdrLargeInt;
//# sourceMappingURL=xdr_large_int.js.map