UNPKG

eth-janus

Version:

Indirection service

196 lines 9.83 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(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 (_) 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 }; } }; var _this = this; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("../services/utils"); var chai = require("chai"); var models_1 = require("../common/models"); var assert = require('assert'); var expect = chai.expect; var should = chai.should(); var mochaAsync = function (fn) { return function () { return __awaiter(_this, void 0, void 0, function () { var err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, fn()]; case 1: _a.sent(); return [2 /*return*/]; case 2: err_1 = _a.sent(); throw err_1; case 3: return [2 /*return*/]; } }); }); }; }; var mochaAsyncBeforeHook = function (fn) { return function () { return __awaiter(_this, void 0, void 0, function () { var err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, fn()]; case 1: _a.sent(); return [2 /*return*/]; case 2: err_2 = _a.sent(); return [2 /*return*/]; case 3: return [2 /*return*/]; } }); }); }; }; describe('utils tests', function () { before(function () { var _this = this; this.timeout(0); var fn = mochaAsyncBeforeHook(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }); return fn(); }); it('test verifySignature method', function () { return __awaiter(this, void 0, void 0, function () { var testMessage, signature, signerAddress, response; return __generator(this, function (_a) { switch (_a.label) { case 0: testMessage = "test message"; signature = "0x769451f7a13d8d41b529f1c14585a67e8bdab3402aa3c1a006fc3d125c7c24e437f1a3c9f5f2fb1263f3c7c3ad5897ffa71243bca3be5ed21fc90ecae76a2d211c"; signerAddress = "0x54C57ae841886D815e054225b9075C87058F366c"; return [4 /*yield*/, (new utils_1.Utils()).verifySignature(testMessage, signature)]; case 1: response = _a.sent(); assert.notEqual(response, null, "failed to verify signature"); assert.equal(response.error, null, response.error); assert.equal(response.isValid, true, "signature not valid"); assert.equal(response.signerAddress, signerAddress, "signer account not matching"); return [2 /*return*/]; } }); }); }); it('test buildTransaction method case 1', function () { var txn = { to: "0x54C57ae841886D815e054225b9075C87058F366c", nonce: 10, gas: 50000, gasPrice: 10, data: "0x000021", chainId: 1 }; var tx = (new utils_1.Utils()).buildTransaction(txn); assert.notEqual(tx, null, "failed to build transaction object"); assert.equal(tx.to.toString('hex').toLowerCase(), ('54C57ae841886D815e054225b9075C87058F366c').toLowerCase(), "invalid to"); assert.equal(tx.nonce.toString('hex'), '0a', "invalid nonce"); assert.equal(tx.gas.toString('hex'), 'c350', "invalid gas"); assert.equal(tx.gasPrice.toString('hex'), '0a', "invalid gasPrice"); assert.equal(tx.data.toString('hex'), '000021', "invalid data"); assert.equal(tx.getChainId(), '0x01', "invalid chainId"); }); it('test buildTransaction method case 2', function () { var txn = { to: "0x54C57ae841886D815e054225b9075C87058F366c", nonce: 10, gasLimit: 50000, data: "0x00", value: 10, chainId: 1 }; var tx = (new utils_1.Utils()).buildTransaction(txn); assert.notEqual(tx, null, "failed to build transaction object"); assert.equal(tx.to.toString('hex').toLowerCase(), ('54C57ae841886D815e054225b9075C87058F366c').toLowerCase(), "invalid to"); assert.equal(tx.nonce.toString('hex'), '0a', "invalid nonce"); assert.equal(tx.gas.toString('hex'), 'c350', "invalid gas"); assert.equal(tx.gasPrice.toString('hex'), '', "invalid gasPrice"); assert.equal(tx.data.toString('hex'), '00', "invalid data"); assert.equal(tx.value.toString('hex'), '0a', "invalid value"); assert.equal(tx.getChainId(), '0x01', "invalid chainId"); }); it('test objToMap method', function () { var object = { field1: "f1", field2: "f2" }; var map = (new utils_1.Utils()).objToMap(object); assert.notEqual(map, null, "failed to convert obj to map"); assert.equal(map.size, 2, "invalid no of items in map"); assert.equal(map.get("field1"), "f1", "invalid value in map"); assert.equal(map.get("field2"), "f2", "invalid value in map"); }); it('test checkIfKeyMapHasAllKeys method case 1', function () { var keyMap = new Array(); var otKey1 = new models_1.OnetimeKey({ address: "0x54C57ae841886D815e054225b9075C87058F366c" }); keyMap.push({ partyName: "P1", onetimeKey: otKey1 }); keyMap.push({ partyName: "P2", onetimeKey: null }); var result = (new utils_1.Utils()).checkIfKeyMapHasAllKeys(keyMap); assert.equal(result, false, "invalid result on checking keymap"); }); it('test checkIfKeyMapHasAllKeys method case 2', function () { var keyMap = new Array(); var otKey1 = new models_1.OnetimeKey({ address: "0x54C57ae841886D815e054225b9075C87058F366c" }); var otKey2 = new models_1.OnetimeKey({ address: "0x54C57ae841886D815e054225b9075C87058F366c" }); keyMap.push({ partyName: "P1", onetimeKey: otKey1 }); keyMap.push({ partyName: "P2", onetimeKey: otKey2 }); var result = (new utils_1.Utils()).checkIfKeyMapHasAllKeys(keyMap); assert.equal(result, true, "invalid result on checking keymap"); }); it('test sleep method', function () { return __awaiter(this, void 0, void 0, function () { var startTime, endTime; return __generator(this, function (_a) { switch (_a.label) { case 0: startTime = (new Date()).getTime(); return [4 /*yield*/, (new utils_1.Utils()).sleep(200)]; case 1: _a.sent(); endTime = (new Date()).getTime(); //console.log("sleep time", (endTime-startTime)); assert.equal((endTime - startTime) > 200, true, "sleep time is less than the requested time, " + (endTime - startTime)); return [2 /*return*/]; } }); }); }); }); //# sourceMappingURL=utils.test.js.map