UNPKG

@firmachain/firma-js

Version:

The Official FirmaChain Javascript SDK written in Typescript

355 lines (354 loc) 21.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var chai_1 = require("chai"); var FirmaUtil_1 = require("../sdk/FirmaUtil"); var FirmaSDK_1 = require("../sdk/FirmaSDK"); var config_test_1 = require("./config_test"); var FirmaConfig_1 = require("../sdk/FirmaConfig"); describe('[18. util Test]', function () { var firma; var aliceWallet; var aliceAddress; beforeEach(function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: firma = new FirmaSDK_1.FirmaSDK(config_test_1.TestChainConfig); return [4 /*yield*/, firma.Wallet.fromMnemonic(config_test_1.aliceMnemonic)]; case 1: aliceWallet = _a.sent(); return [4 /*yield*/, aliceWallet.getAddress()]; case 2: aliceAddress = _a.sent(); return [2 /*return*/]; } }); }); }); // getHashFromString it('getSha1HashFromString test', function () { return __awaiter(void 0, void 0, void 0, function () { var contractName, result; return __generator(this, function (_a) { contractName = "testContract1234"; result = FirmaUtil_1.FirmaUtil.getSha1HashFromString(contractName); (0, chai_1.expect)(result).to.be.equal("c88753a797d1310b36673e3494005bc7485746b7"); return [2 /*return*/]; }); }); }); it('getHashFromString test', function () { return __awaiter(void 0, void 0, void 0, function () { var contractName, result; return __generator(this, function (_a) { contractName = "testContract1234"; result = FirmaUtil_1.FirmaUtil.getHashFromString(contractName); (0, chai_1.expect)(result).to.be.equal("95e55f6b55ccf6b3988a6f9ee6d9c3c0011ea93a2489e7f05d10cada2613c17f"); return [2 /*return*/]; }); }); }); it('isValidAddress test', function () { return __awaiter(void 0, void 0, void 0, function () { var validAddress, wrongAddress1, wrongAddress2, result; return __generator(this, function (_a) { validAddress = "firma134pp6s2nv7pl4mxu58aeufdd6fv5s2zujrrmsa"; wrongAddress1 = "firma134pp6s2nv7pl4mxu58aeufdd6fv5s2zujrrmsb"; wrongAddress2 = "firma134pp6s2nv7pl4mxu58aeufdd6fv5s2zujrrmsa1"; result = FirmaUtil_1.FirmaUtil.isValidAddress(validAddress); (0, chai_1.expect)(result).to.be.equal(true); result = FirmaUtil_1.FirmaUtil.isValidAddress(wrongAddress1); (0, chai_1.expect)(result).to.be.equal(false); result = FirmaUtil_1.FirmaUtil.isValidAddress(wrongAddress2); (0, chai_1.expect)(result).to.be.equal(false); return [2 /*return*/]; }); }); }); it('getValOperAddressFromAccAddress test', function () { return __awaiter(void 0, void 0, void 0, function () { var accAddress, valoperAddress, result; return __generator(this, function (_a) { accAddress = "firma1a85hxs97rxsrf0yzdn72vhfu39sa0dwxv90ghy"; valoperAddress = "firmavaloper1a85hxs97rxsrf0yzdn72vhfu39sa0dwxjkynh2"; result = FirmaUtil_1.FirmaUtil.getValOperAddressFromAccAddress(accAddress); (0, chai_1.expect)(result).to.be.equal(valoperAddress); return [2 /*return*/]; }); }); }); it('getValConsAddressFromConsensusPubKey test', function () { return __awaiter(void 0, void 0, void 0, function () { var consensusPubkey, valconsAddress, result; return __generator(this, function (_a) { consensusPubkey = "InWhZBMP3wKQkwIBCrVqQ+BNoPhV5mTjpwiYHKHCZ/k="; valconsAddress = "firmavalcons1fh73gr3f9df7yc390ykdnmeedetlw5ll3dqwje"; result = FirmaUtil_1.FirmaUtil.getValConsAddressFromAccAddress(consensusPubkey); (0, chai_1.expect)(result).to.be.equal(valconsAddress); return [2 /*return*/]; }); }); }); it('getAccAddressFromValOperAddress test', function () { return __awaiter(void 0, void 0, void 0, function () { var accAddress, valoperAddress, result; return __generator(this, function (_a) { accAddress = "firma1a85hxs97rxsrf0yzdn72vhfu39sa0dwxv90ghy"; valoperAddress = "firmavaloper1a85hxs97rxsrf0yzdn72vhfu39sa0dwxjkynh2"; result = FirmaUtil_1.FirmaUtil.getAccAddressFromValOperAddress(valoperAddress); (0, chai_1.expect)(result).to.be.equal(accAddress); return [2 /*return*/]; }); }); }); it('getFCTFromUFCTString test', function () { return __awaiter(void 0, void 0, void 0, function () { var amountUFCT, fct; return __generator(this, function (_a) { amountUFCT = 1000000; fct = FirmaUtil_1.FirmaUtil.getFCTStringFromUFCT(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1"); amountUFCT = 1234000; fct = FirmaUtil_1.FirmaUtil.getFCTStringFromUFCT(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1.234"); return [2 /*return*/]; }); }); }); it('getUFCTStringFromFCT test', function () { return __awaiter(void 0, void 0, void 0, function () { var amountUFCT, fct; return __generator(this, function (_a) { amountUFCT = 1; fct = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCT(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1000000"); amountUFCT = 1.23; fct = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCT(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1230000"); return [2 /*return*/]; }); }); }); it('getFCTFromUFCTString test', function () { return __awaiter(void 0, void 0, void 0, function () { var amountUFCT, fct; return __generator(this, function (_a) { amountUFCT = "1000000"; fct = FirmaUtil_1.FirmaUtil.getFCTStringFromUFCTStr(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1"); amountUFCT = "1234000"; fct = FirmaUtil_1.FirmaUtil.getFCTStringFromUFCTStr(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1.234"); return [2 /*return*/]; }); }); }); it('getUFCTStringFromFCT test', function () { return __awaiter(void 0, void 0, void 0, function () { var amountUFCT, fct; return __generator(this, function (_a) { amountUFCT = "1"; fct = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCTStr(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1000000"); amountUFCT = "1.23"; fct = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCTStr(amountUFCT); (0, chai_1.expect)(fct).to.be.equal("1230000"); return [2 /*return*/]; }); }); }); it('getTokenFromUTokenString test', function () { return __awaiter(void 0, void 0, void 0, function () { var decimal, amountUToken, token; return __generator(this, function (_a) { decimal = 6; amountUToken = 1000000; token = FirmaUtil_1.FirmaUtil.getTokenStringFromUToken(amountUToken, decimal); (0, chai_1.expect)(token).to.be.equal("1"); amountUToken = 1234000; token = FirmaUtil_1.FirmaUtil.getTokenStringFromUToken(amountUToken, decimal); (0, chai_1.expect)(token).to.be.equal("1.234"); return [2 /*return*/]; }); }); }); it('getUTokenStringFromToken test', function () { return __awaiter(void 0, void 0, void 0, function () { var decimal, amountUToken, token; return __generator(this, function (_a) { decimal = 6; amountUToken = 1; token = FirmaUtil_1.FirmaUtil.getUTokenStringFromToken(amountUToken, decimal); (0, chai_1.expect)(token).to.be.equal("1000000"); amountUToken = 1.23; token = FirmaUtil_1.FirmaUtil.getUTokenStringFromToken(amountUToken, decimal); (0, chai_1.expect)(token).to.be.equal("1230000"); return [2 /*return*/]; }); }); }); it('cutting the ufct decimal point test', function () { return __awaiter(void 0, void 0, void 0, function () { var decimal, testUTokenValue, testUTokenValueStr, testTokenValueStr; return __generator(this, function (_a) { decimal = 6; testUTokenValue = 533.827284; testUTokenValueStr = "533.827284"; testTokenValueStr = "0.000533"; (0, chai_1.expect)(FirmaUtil_1.FirmaUtil.getTokenStringFromUTokenStr(testUTokenValueStr, decimal)).to.be.equal(testTokenValueStr); (0, chai_1.expect)(FirmaUtil_1.FirmaUtil.getTokenStringFromUToken(testUTokenValue, decimal)).to.be.equal(testTokenValueStr); return [2 /*return*/]; }); }); }); it('cutting the decimal point test', function () { return __awaiter(void 0, void 0, void 0, function () { var decimal, testTokenValue, testTokenValueStr, testUTokenValue, testUTokenValueStr; return __generator(this, function (_a) { decimal = 6; testTokenValue = 533.827284; testTokenValueStr = "533.827284"; testUTokenValue = 533827284; testUTokenValueStr = "533827284"; (0, chai_1.expect)(FirmaUtil_1.FirmaUtil.getUTokenFromToken(testTokenValue, decimal)).to.be.equal(testUTokenValue); (0, chai_1.expect)(FirmaUtil_1.FirmaUtil.getUTokenStringFromTokenStr(testTokenValueStr, decimal)).to.be.equal(testUTokenValueStr); (0, chai_1.expect)(FirmaUtil_1.FirmaUtil.getTokenStringFromUTokenStr(testUTokenValueStr, decimal)).to.be.equal(testTokenValueStr); (0, chai_1.expect)(FirmaUtil_1.FirmaUtil.getTokenStringFromUToken(testUTokenValue, decimal)).to.be.equal(testTokenValueStr); return [2 /*return*/]; }); }); }); it('getTokenFromUTokenString test', function () { return __awaiter(void 0, void 0, void 0, function () { var decimal, amountUToken, token; return __generator(this, function (_a) { decimal = 6; amountUToken = "1000000"; token = FirmaUtil_1.FirmaUtil.getTokenStringFromUTokenStr(amountUToken, decimal); (0, chai_1.expect)(token).to.be.equal("1"); amountUToken = "1234000"; token = FirmaUtil_1.FirmaUtil.getTokenStringFromUTokenStr(amountUToken, decimal); (0, chai_1.expect)(token).to.be.equal("1.234"); return [2 /*return*/]; }); }); }); it('getUTokenStringFromToken test', function () { return __awaiter(void 0, void 0, void 0, function () { var decimal, amountUToken, token; return __generator(this, function (_a) { decimal = 6; amountUToken = "1"; token = FirmaUtil_1.FirmaUtil.getUTokenStringFromTokenStr(amountUToken, decimal); (0, chai_1.expect)(token).to.be.equal("1000000"); amountUToken = "1.23"; token = FirmaUtil_1.FirmaUtil.getUFCTStringFromFCTStr(amountUToken); (0, chai_1.expect)(token).to.be.equal("1230000"); return [2 /*return*/]; }); }); }); it('getFileHashFromBuffer test', function () { return __awaiter(void 0, void 0, void 0, function () { var testString, enc, buffer, hash; return __generator(this, function (_a) { testString = "hello world"; enc = new TextEncoder(); buffer = enc.encode(testString); hash = FirmaUtil_1.FirmaUtil.getFileHashFromBuffer(buffer); // hash from sha256 online (0, chai_1.expect)(hash).to.be.equal("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"); return [2 /*return*/]; }); }); }); it('parseDurationString test', function () { return __awaiter(void 0, void 0, void 0, function () { var testString, duration; return __generator(this, function (_a) { testString = "1800s"; duration = FirmaUtil_1.FirmaUtil.parseDurationString(testString); // Correct way to test object properties (0, chai_1.expect)(duration.seconds).to.be.equal(BigInt(1800)); (0, chai_1.expect)(duration.nanos).to.be.equal(0); return [2 /*return*/]; }); }); }); it('createDurationFromString test', function () { return __awaiter(void 0, void 0, void 0, function () { var testString, duration; return __generator(this, function (_a) { testString = "1800s"; duration = FirmaUtil_1.FirmaUtil.createDurationFromString(testString); (0, chai_1.expect)(duration.seconds).to.be.equal(BigInt(1800)); (0, chai_1.expect)(duration.nanos).to.be.equal(0); return [2 /*return*/]; }); }); }); it('processCommissionRateAsDecimal test - success case', function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.000000000000000000"); (0, chai_1.expect)(result).to.equal("0"); result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1"); (0, chai_1.expect)(result).to.equal("1000000000000000000"); result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.99999999999999999999999"); (0, chai_1.expect)(result).to.equal("999999999999999999"); result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.3719281729181018373290120300000831"); (0, chai_1.expect)(result).to.equal("371928172918101837"); result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.000000000000000000"); (0, chai_1.expect)(result).to.equal("1000000000000000000"); result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(" 0.75 "); (0, chai_1.expect)(result).to.equal("750000000000000000"); return [2 /*return*/]; }); }); }); it('processCommissionRateAsDecimal test - failure cases', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("."); }).to.throw("Invalid commission rate format: ."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(""); }).to.throw("Invalid commission rate format: "); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(" "); }).to.throw("Invalid commission rate format: "); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("null"); }).to.throw("Invalid commission rate format: null"); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.1abc"); }).to.throw("Invalid commission rate format: 0.1abc"); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.1.2"); }).to.throw("Invalid commission rate format: 0.1.2"); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("--0.5"); }).to.throw("Invalid commission rate format: --0.5"); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.01"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("2"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.1"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-0.1"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-1"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-0.01"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-0.000000000000000001"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.00000000000000000001"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive."); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.5%"); }).to.throw("Invalid commission rate format: 0.5%"); (0, chai_1.expect)(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(" 0.5 extra"); }).to.throw("Invalid commission rate format: 0.5 extra"); return [2 /*return*/]; }); }); }); it('getAccountInfo test', function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, FirmaUtil_1.FirmaUtil.getAccountInfo(aliceAddress)]; case 1: result = _a.sent(); (0, chai_1.expect)(result).to.have.property('account_number'); (0, chai_1.expect)(result).to.have.property('sequence'); return [2 /*return*/]; } }); }); }); it('getChainId test', function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, FirmaUtil_1.FirmaUtil.getChainId()]; case 1: result = _a.sent(); (0, chai_1.expect)(result).to.be.equal(FirmaConfig_1.FirmaConfig.DevNetConfig.chainID); return [2 /*return*/]; } }); }); }); });