@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
742 lines (610 loc) • 26.3 kB
JavaScript
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
import _typeof from "@babel/runtime-corejs3/helpers/typeof";
import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
import _toConsumableArray from "@babel/runtime-corejs3/helpers/toConsumableArray";
import _toArray from "@babel/runtime-corejs3/helpers/toArray";
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
var _context;
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context29; _forEachInstanceProperty(_context29 = ownKeys(Object(source), true)).call(_context29, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context30; _forEachInstanceProperty(_context30 = ownKeys(Object(source))).call(_context30, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import _Object$entries3 from "@babel/runtime-corejs3/core-js-stable/object/entries";
import _indexOfInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/index-of";
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values";
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array";
import _entriesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/entries";
import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
import _JSON$stringify from "@babel/runtime-corejs3/core-js-stable/json/stringify";
import Joi from 'joi-browser';
import { isAeAddress, isHex } from '../../utils/string';
import { toBytes } from '../../utils/bytes';
import { decode } from '../../tx/builder/helpers';
import { parseBigNumber } from '../../utils/bignumber';
import { addressFromDecimal, hash } from '../../utils/crypto';
export var SOPHIA_TYPES = _reduceInstanceProperty(_context = ['int', 'string', 'tuple', 'address', 'bool', 'list', 'map', 'record', 'option', 'oracle', 'oracleQuery', 'hash', 'signature', 'bytes', 'variant']).call(_context, function (acc, type) {
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, type, type));
}, {
ChainTtl: 'Chain.ttl'
});
/**
* Transform decoded event to JS type
* @param {Object[]} events Array of events
* @param {Object} [options={}] Options
* @param {Object} [options.schema=[]] SC function ACI schema
* @return {Object}
*/
export function decodeEvents(events) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
schema: []
};
if (!events.length) return [];
return _mapInstanceProperty(events).call(events, function (l) {
var _context2;
var _l$topics = _toArray(l.topics),
eName = _l$topics[0],
eParams = _sliceInstanceProperty(_l$topics).call(_l$topics, 1);
var hexHash = toBytes(eName, true).toString('hex');
var _options$schema$reduc = _reduceInstanceProperty(_context2 = options.schema).call(_context2, function (acc, el) {
if (hash(el.name).toString('hex') === hexHash) {
l.name = el.name;
return {
schema: el.types,
name: el.name
};
}
return acc;
}, {
schema: []
}),
schema = _options$schema$reduc.schema;
var _schema$reduce = _reduceInstanceProperty(schema).call(schema, function (acc, el) {
var _context4;
if (el === SOPHIA_TYPES.string) {
var _context3;
return {
decoded: _concatInstanceProperty(_context3 = []).call(_context3, _toConsumableArray(acc.decoded), [transformEvent(l.data, el)]),
params: acc.params
};
}
var _acc$params = _toArray(acc.params),
event = _acc$params[0],
tail = _sliceInstanceProperty(_acc$params).call(_acc$params, 1);
return {
decoded: _concatInstanceProperty(_context4 = []).call(_context4, _toConsumableArray(acc.decoded), [transformEvent(event, el)]),
params: tail
};
}, {
decoded: [],
params: eParams
}),
decoded = _schema$reduce.decoded;
return _objectSpread(_objectSpread({}, l), {}, {
decoded: decoded
});
});
}
/**
* Transform Event based on type
* @param {String|Number} event Event data
* @param {String} type Event type from schema
* @return {*}
*/
export function transformEvent(event, type) {
switch (type) {
case SOPHIA_TYPES["int"]:
return parseBigNumber(event);
case SOPHIA_TYPES.bool:
return !!event;
case SOPHIA_TYPES.hash:
return toBytes(event, true).toString('hex');
case SOPHIA_TYPES.address:
return addressFromDecimal(event).split('_')[1];
case SOPHIA_TYPES.string:
return decode(event).toString('utf-8');
default:
return toBytes(event, true);
}
}
export function injectVars(t, aciType) {
var _Object$entries = _Object$entries3(aciType.typedef),
_Object$entries2 = _slicedToArray(_Object$entries, 1),
_Object$entries2$ = _slicedToArray(_Object$entries2[0], 2),
baseType = _Object$entries2$[0],
generic = _Object$entries2$[1];
var _Object$entries4 = _Object$entries3(t),
_Object$entries5 = _slicedToArray(_Object$entries4, 1),
_Object$entries5$ = _slicedToArray(_Object$entries5[0], 2),
varianValue = _Object$entries5$[1];
switch (baseType) {
case SOPHIA_TYPES.variant:
return _defineProperty({}, baseType, _mapInstanceProperty(generic).call(generic, function (el) {
var _Object$entries$ = _slicedToArray(_Object$entries3(el)[0], 2),
tag = _Object$entries$[0],
gen = _Object$entries$[1];
return _defineProperty({}, tag, _mapInstanceProperty(gen).call(gen, function (type) {
var _context5, _context6;
var index = _indexOfInstanceProperty(_context5 = _mapInstanceProperty(_context6 = aciType.vars).call(_context6, function (e) {
return e.name;
})).call(_context5, type);
return index === -1 ? type : varianValue[index];
}));
}));
}
}
/**
* Ling Type Defs
* @param t
* @param bindings
* @return {Object}
*/
export function linkTypeDefs(t, bindings) {
var _context7, _context8;
var _ref3 = _typeof(t) === 'object' ? _Object$keys(t)[0].split('.') : t.split('.'),
_ref4 = _slicedToArray(_ref3, 2),
root = _ref4[0],
typeDef = _ref4[1];
var contractTypeDefs = _findInstanceProperty(bindings).call(bindings, function (c) {
return c.name === root;
});
var aciType = _findInstanceProperty(_context7 = _concatInstanceProperty(_context8 = []).call(_context8, _toConsumableArray(contractTypeDefs.type_defs), [{
name: 'state',
typedef: contractTypeDefs.state,
vars: []
}])).call(_context7, function (_ref5) {
var name = _ref5.name;
return name === typeDef;
});
if (aciType.vars.length) {
aciType.typedef = injectVars(t, aciType);
}
return isTypedDefOrState(aciType.typedef, bindings) ? linkTypeDefs(aciType.typedef, bindings) : aciType.typedef;
}
var isTypedDefOrState = function isTypedDefOrState(t, bindings) {
var _context9, _context10;
if (!_includesInstanceProperty(_context9 = ['string', 'object']).call(_context9, _typeof(t))) return false;
t = _typeof(t) === 'object' ? _Object$keys(t)[0] : t;
var _t$split = t.split('.'),
_t$split2 = _toArray(_t$split),
root = _t$split2[0],
path = _sliceInstanceProperty(_t$split2).call(_t$split2, 1); // Remote Contract Address
if (!path.length) return false;
return _includesInstanceProperty(_context10 = _mapInstanceProperty(bindings).call(bindings, function (c) {
return c.name;
})).call(_context10, root);
};
var isRemoteAddress = function isRemoteAddress(t) {
var _context11;
if (typeof t !== 'string') return false;
var _t$split3 = t.split('.'),
_t$split4 = _toArray(_t$split3),
root = _t$split4[0],
path = _sliceInstanceProperty(_t$split4).call(_t$split4, 1);
return !path.length && !_includesInstanceProperty(_context11 = _Object$values(SOPHIA_TYPES)).call(_context11, root);
};
/**
* Parse sophia type
* @param type
* @param returnType
* @return {Object}
*/
export function readType(type) {
var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
bindings = _ref6.bindings;
var _ref7 = _Array$isArray(type) ? type : [type],
_ref8 = _slicedToArray(_ref7, 1),
t = _ref8[0]; // If remote address
if (isRemoteAddress(t)) return {
t: SOPHIA_TYPES.address
}; // Link State and typeDef
if (isTypedDefOrState(t, bindings)) t = linkTypeDefs(t, bindings); // Map, Tuple, List, Record, Bytes
if (_typeof(t) === 'object') {
var _Object$entries6 = _Object$entries3(t),
_Object$entries7 = _slicedToArray(_Object$entries6, 1),
_Object$entries7$ = _slicedToArray(_Object$entries7[0], 2),
baseType = _Object$entries7$[0],
generic = _Object$entries7$[1];
return {
t: baseType,
generic: generic
};
} // Base types
if (typeof t === 'string') return {
t: t
};
} // FUNCTION ARGUMENTS TRANSFORMATION ↓↓↓
/**
* Transform JS type to Sophia-type
* @param type
* @param value
* @param bindings
* @return {string}
*/
export function transform(type, value) {
var _ref9 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
bindings = _ref9.bindings;
var _readType = readType(type, {
bindings: bindings
}),
t = _readType.t,
generic = _readType.generic;
switch (t) {
case SOPHIA_TYPES.ChainTtl:
return "".concat(value);
case SOPHIA_TYPES.string:
return "\"".concat(value, "\"");
case SOPHIA_TYPES.list:
return "[".concat(_mapInstanceProperty(value).call(value, function (el) {
return transform(generic, el, {
bindings: bindings
});
}), "]");
case SOPHIA_TYPES.tuple:
return "(".concat(_mapInstanceProperty(value).call(value, function (el, i) {
return transform(generic[i], el, {
bindings: bindings
});
}), ")");
case SOPHIA_TYPES.option:
{
return value === undefined ? 'None' : "Some(".concat(transform(generic, value, {
bindings: bindings
}), ")");
}
case SOPHIA_TYPES.hash:
case SOPHIA_TYPES.bytes:
case SOPHIA_TYPES.signature:
if (typeof value === 'string') {
if (isHex(value)) return "#".concat(value);
if (isAeAddress(value)) return "#".concat(decode(value).toString('hex'));
}
return "#".concat(Buffer.from(value).toString('hex'));
case SOPHIA_TYPES.record:
return "{".concat(_reduceInstanceProperty(generic).call(generic, function (acc, _ref10, i) {
var _context12, _context13;
var name = _ref10.name,
type = _ref10.type;
acc += _concatInstanceProperty(_context12 = _concatInstanceProperty(_context13 = "".concat(i !== 0 ? ',' : '')).call(_context13, name, " = ")).call(_context12, transform(type, value[name], {
bindings: bindings
}));
return acc;
}, ''), "}");
case _mapInstanceProperty(SOPHIA_TYPES):
return transformMap(value, generic, {
bindings: bindings
});
case SOPHIA_TYPES.variant:
return transformVariant(value, generic, {
bindings: bindings
});
}
return "".concat(value);
}
export function transformVariant(value, generic, _ref11) {
var _context14, _context15;
var bindings = _ref11.bindings;
var _ref12 = typeof value === 'string' ? [[value, []]] : _Object$entries3(value),
_ref13 = _slicedToArray(_ref12, 1),
_ref13$ = _slicedToArray(_ref13[0], 2),
variant = _ref13$[0],
variantArgs = _ref13$[1];
var _Object$entries8 = _Object$entries3(_findInstanceProperty(generic).call(generic, function (o) {
return _Object$keys(o)[0].toLowerCase() === variant.toLowerCase();
})),
_Object$entries9 = _slicedToArray(_Object$entries8, 1),
_Object$entries9$ = _slicedToArray(_Object$entries9[0], 2),
v = _Object$entries9$[0],
type = _Object$entries9$[1];
return _concatInstanceProperty(_context14 = "".concat(v)).call(_context14, !type.length ? '' : "(".concat(_mapInstanceProperty(_context15 = _sliceInstanceProperty(variantArgs).call(variantArgs, 0, type.length)).call(_context15, function (el, i) {
return transform(type[i], el, {
bindings: bindings
});
}), ")"));
}
export function transformMap(value, generic, _ref14) {
var bindings = _ref14.bindings;
if (!_Array$isArray(value)) {
if (_entriesInstanceProperty(value)) value = _Array$from(_entriesInstanceProperty(value).call(value));else if (value instanceof Object) value = _Object$entries3(value);
}
return ['{', _mapInstanceProperty(value).call(value, function (_ref15) {
var _ref16 = _slicedToArray(_ref15, 2),
key = _ref16[0],
value = _ref16[1];
return ["[".concat(transform(generic[0], key, {
bindings: bindings
}), "]"), transform(generic[1], value, {
bindings: bindings
})].join(' = ');
}).join(), '}'].join('');
} // FUNCTION RETURN VALUE TRANSFORMATION ↓↓↓
/**
* Transform decoded data to JS type
* @param aci
* @param result
* @param transformDecodedData
* @return {*}
*/
export function transformDecodedData(aci, result) {
var _ref17 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref17$skipTransformD = _ref17.skipTransformDecoded,
skipTransformDecoded = _ref17$skipTransformD === void 0 ? false : _ref17$skipTransformD,
_ref17$addressPrefix = _ref17.addressPrefix,
addressPrefix = _ref17$addressPrefix === void 0 ? 'ak' : _ref17$addressPrefix,
bindings = _ref17.bindings;
if (skipTransformDecoded) return result;
var _readType2 = readType(aci, {
bindings: bindings
}),
t = _readType2.t,
generic = _readType2.generic;
switch (t) {
case SOPHIA_TYPES.bool:
return !!result;
case SOPHIA_TYPES.address:
return result === 0 ? 0 : result;
case SOPHIA_TYPES.hash:
case SOPHIA_TYPES.bytes:
case SOPHIA_TYPES.signature:
return result.split('#')[1];
case _mapInstanceProperty(SOPHIA_TYPES):
{
var _generic = _slicedToArray(generic, 2),
keyT = _generic[0],
valueT = _generic[1];
return _reduceInstanceProperty(result).call(result, function (acc, _ref18) {
var _ref19 = _slicedToArray(_ref18, 2),
key = _ref19[0],
val = _ref19[1];
key = transformDecodedData(keyT, key, {
bindings: bindings
});
val = transformDecodedData(valueT, val, {
bindings: bindings
});
acc.push([key, val]);
return acc;
}, []);
}
case SOPHIA_TYPES.option:
{
if (result === 'None') return undefined;
var _Object$entries10 = _Object$entries3(result),
_Object$entries11 = _slicedToArray(_Object$entries10, 1),
_Object$entries11$ = _slicedToArray(_Object$entries11[0], 2),
variantType = _Object$entries11$[0],
_Object$entries11$$ = _slicedToArray(_Object$entries11$[1], 1),
value = _Object$entries11$$[0];
return variantType === 'Some' ? transformDecodedData(generic, value, {
bindings: bindings
}) : undefined;
}
case SOPHIA_TYPES.list:
return _mapInstanceProperty(result).call(result, function (value) {
return transformDecodedData(generic, value, {
bindings: bindings
});
});
case SOPHIA_TYPES.tuple:
return _mapInstanceProperty(result).call(result, function (value, i) {
return transformDecodedData(generic[i], value, {
bindings: bindings
});
});
case SOPHIA_TYPES.record:
{
var _context16;
var genericMap = _reduceInstanceProperty(generic).call(generic, function (acc, val) {
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, val.name, {
type: val.type
}));
}, {});
return _reduceInstanceProperty(_context16 = _Object$entries3(result)).call(_context16, function (acc, _ref20) {
var _ref21 = _slicedToArray(_ref20, 2),
name = _ref21[0],
value = _ref21[1];
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, name, transformDecodedData(genericMap[name].type, value, {
bindings: bindings
})));
}, {});
}
}
return result;
} // FUNCTION ARGUMENTS VALIDATION ↓↓↓
/**
* Prepare Joi validation schema for sophia types
* @param type
* @param bindings
* @return {Object} JoiSchema
*/
export function prepareSchema(type) {
var _Joi$string, _Joi$object;
var _ref22 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
bindings = _ref22.bindings;
var _readType3 = readType(type, {
bindings: bindings
}),
t = _readType3.t,
generic = _readType3.generic;
switch (t) {
case SOPHIA_TYPES["int"]:
return Joi.number().error(getJoiErrorMsg);
case SOPHIA_TYPES.variant:
return Joi.alternatives()["try"]([(_Joi$string = Joi.string()).valid.apply(_Joi$string, _toConsumableArray(_reduceInstanceProperty(generic).call(generic, function (acc, el) {
var _Object$entries12 = _Object$entries3(el),
_Object$entries13 = _slicedToArray(_Object$entries12, 1),
_Object$entries13$ = _slicedToArray(_Object$entries13[0], 2),
t = _Object$entries13$[0],
g = _Object$entries13$[1];
if (!g || !g.length) acc.push(t);
return acc;
}, []))), (_Joi$object = Joi.object(_reduceInstanceProperty(generic).call(generic, function (acc, el) {
var variant = _Object$keys(el)[0];
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, variant, Joi.array()));
}, {}))).or.apply(_Joi$object, _toConsumableArray(_mapInstanceProperty(generic).call(generic, function (e) {
return _Object$keys(e)[0];
})))]);
case SOPHIA_TYPES.ChainTtl:
return Joi.string().error(getJoiErrorMsg);
case SOPHIA_TYPES.string:
return Joi.string().error(getJoiErrorMsg);
case SOPHIA_TYPES.address:
return Joi.string().regex(/^(ak_|ct_|ok_|oq_)/).error(getJoiErrorMsg);
case SOPHIA_TYPES.bool:
return Joi["boolean"]().error(getJoiErrorMsg);
case SOPHIA_TYPES.list:
return Joi.array().items(prepareSchema(generic, {
bindings: bindings
})).error(getJoiErrorMsg);
case SOPHIA_TYPES.tuple:
return Joi.array().ordered(_mapInstanceProperty(generic).call(generic, function (type) {
return prepareSchema(type, {
bindings: bindings
}).required();
})).label('Tuple argument').error(getJoiErrorMsg);
case SOPHIA_TYPES.record:
return Joi.object(_reduceInstanceProperty(generic).call(generic, function (acc, _ref23) {
var name = _ref23.name,
type = _ref23.type;
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, name, prepareSchema(type, {
bindings: bindings
})));
}, {})).error(getJoiErrorMsg);
case SOPHIA_TYPES.hash:
return JoiBinary.binary().bufferCheck(32).error(getJoiErrorMsg);
case SOPHIA_TYPES.bytes:
return JoiBinary.binary().bufferCheck(generic).error(getJoiErrorMsg);
case SOPHIA_TYPES.signature:
return JoiBinary.binary().bufferCheck(64).error(getJoiErrorMsg);
case SOPHIA_TYPES.option:
return prepareSchema(generic, {
bindings: bindings
}).optional().error(getJoiErrorMsg);
// @Todo Need to transform Map to Array of arrays before validating it
// case SOPHIA_TYPES.map:
// return Joi.array().items(Joi.array().ordered(generic.map(type => prepareSchema(type))))
default:
return Joi.any();
}
}
/**
* Parse Joi validation error message
* @param errors
* @return {Object} JoiError
*/
export function getJoiErrorMsg(errors) {
return _mapInstanceProperty(errors).call(errors, function (err) {
var _context17, _context18, _context19, _context20, _context21, _context22, _context23, _context24, _context25, _context26, _context27;
var path = err.path,
type = err.type,
context = err.context;
var value = Object.prototype.hasOwnProperty.call(context, 'value') ? context.value : context.label;
value = _typeof(value) === 'object' ? _sliceInstanceProperty(_context17 = _sliceInstanceProperty(_context18 = _JSON$stringify(value)).call(_context18, 1)).call(_context17, 0, -1) : value;
switch (type) {
case 'string.base':
return _objectSpread(_objectSpread({}, err), {}, {
message: _concatInstanceProperty(_context19 = "Value \"".concat(value, "\" at path: [")).call(_context19, path, "] not a string")
});
case 'number.base':
return _objectSpread(_objectSpread({}, err), {}, {
message: _concatInstanceProperty(_context20 = "Value \"".concat(value, "\" at path: [")).call(_context20, path, "] not a number")
});
case 'boolean.base':
return _objectSpread(_objectSpread({}, err), {}, {
message: _concatInstanceProperty(_context21 = "Value \"".concat(value, "\" at path: [")).call(_context21, path, "] not a boolean")
});
case 'array.base':
return _objectSpread(_objectSpread({}, err), {}, {
message: _concatInstanceProperty(_context22 = "Value \"".concat(value, "\" at path: [")).call(_context22, path, "] not a array")
});
case 'object.base':
return _objectSpread(_objectSpread({}, err), {}, {
message: _concatInstanceProperty(_context23 = "Value '".concat(value, "' at path: [")).call(_context23, path, "] not a object")
});
case 'object.type':
return _objectSpread(_objectSpread({}, err), {}, {
message: _concatInstanceProperty(_context24 = _concatInstanceProperty(_context25 = "Value '".concat(value, "' at path: [")).call(_context25, path, "] not a ")).call(_context24, context.type)
});
case 'binary.bufferCheck':
return _objectSpread(_objectSpread({}, err), {}, {
message: _concatInstanceProperty(_context26 = _concatInstanceProperty(_context27 = "Value '".concat(Buffer.from(value).toString('hex'), "' at path: [")).call(_context27, path, "] not a ")).call(_context26, context.size, " bytes")
});
default:
return err;
}
});
}
/**
* Custom Joi Validator for binary type
*/
var JoiBinary = Joi.extend(function (joi) {
return {
name: 'binary',
base: joi.any(),
pre: function pre(value, state, options) {
if (options.convert && typeof value === 'string') {
if (isAeAddress(value)) {
return decode(value);
}
try {
return Buffer.from(value, 'hex');
} catch (e) {
return undefined;
}
}
return Buffer.from(value);
},
rules: [{
name: 'bufferCheck',
params: {
size: joi.number().required()
},
validate: function validate(params, value, state, options) {
if (!Buffer.isBuffer(value)) {
return this.createError('binary.base', {
value: value
}, state, options);
}
if (value.length !== params.size) {
return this.createError('binary.bufferCheck', {
value: value,
size: params.size
}, state, options);
}
return value;
}
}]
};
});
/**
* Validation contract function arguments
* @param aci
* @param params
*/
export function validateArguments(aci, params) {
var _context28;
var validationSchema = Joi.array().ordered(_mapInstanceProperty(_context28 = aci.arguments).call(_context28, function (_ref24, i) {
var type = _ref24.type;
return prepareSchema(type, {
bindings: aci.bindings
}).label("[".concat(params[i], "]"));
})).sparse(true).label('Argument');
var _Joi$validate = Joi.validate(params, validationSchema, {
abortEarly: false
}),
error = _Joi$validate.error;
if (error) {
throw error;
}
}
//# sourceMappingURL=transformation.js.map