UNPKG

@airgap/serializer

Version:

The @airgap/serializer provides serializers used in AirGap applications.

170 lines (168 loc) 6.92 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 (_) 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateSyncScheme = void 0; var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber")); var validate_1 = require("@airgap/coinlib-core/dependencies/src/validate.js-0.13.1/validate"); validate_1.validators.type = function (value, options, key, attributes) { // allow empty values by default (needs to be checked by "presence" check) if (value === null || typeof value === 'undefined') { return null; } // allow defining object of any type using their constructor. --> options = {clazz: ClassName} /* if (typeof options === 'object' && options.clazz) { return value instanceof options.clazz ? null : 'is not of type "' + options.clazz.name + '"' } */ if (!validate_1.validators.type.checks[options]) { throw new Error("Could not find validator for type ".concat(options)); } return validate_1.validators.type.checks[options](value) ? null : "is not of type \"".concat(options, "\""); }; validate_1.validators.type.checks = { Object: function (value) { return (0, validate_1.isObject)(value) && !(0, validate_1.isArray)(value); }, Array: validate_1.isArray, Integer: validate_1.isInteger, Number: validate_1.isNumber, String: validate_1.isString, Date: validate_1.isDate, Boolean: function (value) { return typeof value === 'boolean'; }, BigNumber: function (value) { return bignumber_1.default.isBigNumber(value); } }; validate_1.validators.isHexStringWithPrefix = function (value) { if (typeof value !== 'string') { return 'is not hex string'; } if (!value.startsWith('0x')) { return 'is not hex string'; } var hexWithoutPrefix = value.substr(2); if (hexWithoutPrefix.length === 0) { // For ethereum, "0x" is valid return null; } return /[0-9A-F]/gi.test(hexWithoutPrefix) ? null : 'is not hex string'; }; validate_1.validators.isPublicKey = function (value) { if (typeof value !== 'string') { return 'is not a valid public key: should be of type string'; } if (value.length !== 64) { return 'is not a valid public key: wrong length'; } return /[0-9A-F]/gi.test(value) ? null : 'is not a valid public key: invalid characters'; }; function validateSyncScheme(syncScheme) { return __awaiter(this, void 0, void 0, function () { var constraints; return __generator(this, function (_a) { constraints = { version: { presence: { allowEmpty: false }, numericality: { noStrings: true, onlyInteger: true, greaterThanOrEqualTo: 0 } }, type: { presence: { allowEmpty: false }, numericality: { noStrings: true, onlyInteger: true, greaterThanOrEqualTo: 0 } }, protocol: { presence: { allowEmpty: false }, type: 'String', length: { minimum: 1 } }, payload: { presence: { allowEmpty: false }, type: 'Array' } }; return [2 /*return*/, (0, validate_1.validate)(syncScheme, constraints)]; }); }); } exports.validateSyncScheme = validateSyncScheme; // tslint:disable-next-line /* function validateSerializationInput(from: string, fee: BigNumber, amount: BigNumber, publicKey: string, transaction: any) { const constraints = { from: { presence: { allowEmpty: false }, type: 'String' }, fee: { presence: { allowEmpty: false }, type: 'BigNumber' }, amount: { presence: { allowEmpty: false }, type: 'BigNumber' }, publicKey: { presence: { allowEmpty: false }, type: 'String' }, transaction: { presence: { allowEmpty: false }, type: 'Array' } } let test = validate( { from, fee, amount, publicKey, transaction }, constraints ) return test } */ //# sourceMappingURL=validators.js.map