@truenetworkio/sdk
Version:
True Network SDK is the abstracted interface for interacting with True Network nodes.
101 lines (100 loc) • 3.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertBytesToSerialize = exports.convertHexToString = exports.toHexString = void 0;
exports.decodeBytesToNumber = decodeBytesToNumber;
exports.stringToBlakeTwo256Hash = stringToBlakeTwo256Hash;
exports.bytesToBlakeTwo256Hash = bytesToBlakeTwo256Hash;
exports.numberToUint8Array = numberToUint8Array;
exports.toLittleEndianHex = toLittleEndianHex;
const blake = __importStar(require("blakejs"));
function decodeBytesToNumber(bytes) {
let value = 0;
for (let i = 0; i < bytes.length; i++) {
value += bytes[i] * 256 ** i;
}
return value;
}
function stringToBlakeTwo256Hash(inputString) {
// Convert string to Uint8Array
const inputBytes = new TextEncoder().encode(inputString);
return bytesToBlakeTwo256Hash(inputBytes);
}
function bytesToBlakeTwo256Hash(bytes) {
const hashBytes = blake.blake2b(bytes, undefined, 32);
const hashHex = Array.from(hashBytes).map(byte => ('0' + (byte & 0xFF).toString(16)).slice(-2)).join('');
return hashHex;
}
const toHexString = (input) => {
if (typeof input === 'number') {
// Convert number to hexadecimal
return `0x0${input.toString(16)}`;
}
else {
return input;
}
};
exports.toHexString = toHexString;
function numberToUint8Array(number) {
const buffer = new ArrayBuffer(1); // Assuming a 32-bit integer
const view = new DataView(buffer);
view.setUint8(0, (number)); // Little-endian byte order
return new Uint8Array(buffer);
}
function toLittleEndianHex(num, byteLength) {
const hexString = num.toString(16).padStart(byteLength * 2, '0');
const littleEndianHex = [];
for (let i = 0; i < byteLength; i++) {
littleEndianHex.push(hexString.substr(i * 2, 2));
}
return `0x${littleEndianHex.reverse().join('')}`;
}
const convertHexToString = (str) => {
return Buffer.from(str.slice(2), 'hex').toString('utf8');
};
exports.convertHexToString = convertHexToString;
const convertBytesToSerialize = (input) => {
// Use regular expression to check if the input string is a valid numerical value
const isValidNumber = /^-?\d+(\.\d+)?$/.test(input);
if (isValidNumber) {
// Use parseFloat to convert the string to a floating-point number
return parseInt(input);
}
else {
// Return null if the input string is not a valid numerical value
return input;
}
};
exports.convertBytesToSerialize = convertBytesToSerialize;