UNPKG

apla-blockchain-tools

Version:

Module contains a number of tools to work with Apla Blockchain

199 lines (177 loc) 4.78 kB
"use strict"; // MIT License // // Copyright (c) 2016-2018 AplaProject // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. /* * Package: apla-blockchain-tools * Author: Anton Zuev * mail: a.zuev@apla.io * Company: apla.io */ let {toMoney, arrayToArrayBuffer} = require("../lib/convert"); let {UnsupportedParamTypeError, BadConvertResultError} = require("./errors"); let {Int64BE} = require("int64-buffer"); /****************** Fields description ******************/ /** * Abstract field. Shouldn't be used */ class Field{ constructor(type){ this.type = type; } get(){ throw new Error("NotImplementedError"); } } class FieldInt extends Field{ constructor(value){ super("int"); this.value = Number.parseInt(value.toString()); } get(){ return this.value; } } class FieldFloat extends Field{ constructor(value){ super("float"); this.value = Number.parseFloat(value.toString()); } get(){ return this.value; } } class FieldString extends Field{ constructor(value){ super("string"); this.value = value.toString(); } get(){ return this.value; } } class FieldArray extends Field{ /** * @param {Array | *} value - if value is not Array then it will be passed to Array constructor (new Array(value)) */ constructor(value){ super("array"); if(value instanceof Array){ this.value = value; }else{ this.value = new Array(value); } } get(){ return this.value; } } class FieldBool extends Field{ constructor(value){ super("bool"); this.value = Boolean(value); } get(){ return this.value; } } class FieldBytes extends Field{ /** * @param {ArrayBuffer | Array} value */ constructor(value){ super("bytes"); if (value instanceof ArrayBuffer) { this.value = value; }else if(value instanceof Array) { this.value = arrayToArrayBuffer(value); }else{ throw new Error(`FieldBytes expects to get Array or ArrayBuffer but received ${typeof value}`) } } get(){ return this.value; } } class FieldMoney extends Field{ /** * * @param {string | number} value */ constructor(value){ super('money'); this.value = toMoney(value); if(!this.value){ throw new BadConvertResultError(value, "money") } } get(){ return this.value; } } class FieldFile extends Field{ /** * * @param data.filename {String} * @param data.mimeType {String} * @param data.body {Uint8Array | ArrayBuffer | Array} */ constructor(data){ super("file"); this.value = { Name: data.filename, MimeType: data.mimeType, Body: (new FieldBytes(data.body)).get() } } get(){ return this.value; } } /****************** End ******************/ /** * * @param type * @return {Field} */ let getFieldInstanceByType = (type) => { switch (type){ case "int": return FieldInt; case "float": return FieldFloat; case "string": return FieldString; case "array": return FieldArray; case "bool": return FieldBool; case "bytes": return FieldBytes; case "money": return FieldMoney; case "file": return FieldFile; default: throw new UnsupportedParamTypeError(type); } }; module.exports.getFieldInstanceByType = getFieldInstanceByType;