1079 lines
40 KiB
JavaScript
1079 lines
40 KiB
JavaScript
"use strict";
|
|
/**
|
|
* @module Serialize
|
|
*/
|
|
// copyright defined in eosjs/LICENSE.txt
|
|
var __assign = (this && this.__assign) || function () {
|
|
__assign = Object.assign || function(t) {
|
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
s = arguments[i];
|
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
t[p] = s[p];
|
|
}
|
|
return t;
|
|
};
|
|
return __assign.apply(this, arguments);
|
|
};
|
|
var __read = (this && this.__read) || function (o, n) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
if (!m) return o;
|
|
var i = m.call(o), r, ar = [], e;
|
|
try {
|
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
}
|
|
catch (error) { e = { error: error }; }
|
|
finally {
|
|
try {
|
|
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
}
|
|
finally { if (e) throw e.error; }
|
|
}
|
|
return ar;
|
|
};
|
|
var __spread = (this && this.__spread) || function () {
|
|
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
|
return ar;
|
|
};
|
|
var __values = (this && this.__values) || function (o) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
|
|
if (m) return m.call(o);
|
|
return {
|
|
next: function () {
|
|
if (o && i >= o.length) o = void 0;
|
|
return { value: o && o[i++], done: !o };
|
|
}
|
|
};
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var numeric = require("./eosjs-numeric");
|
|
/** State for serialize() and deserialize() */
|
|
var SerializerState = /** @class */ (function () {
|
|
function SerializerState(options) {
|
|
if (options === void 0) { options = {}; }
|
|
/** Have any binary extensions been skipped? */
|
|
this.skippedBinaryExtension = false;
|
|
this.options = options;
|
|
}
|
|
return SerializerState;
|
|
}());
|
|
exports.SerializerState = SerializerState;
|
|
/** Serialize and deserialize data */
|
|
var SerialBuffer = /** @class */ (function () {
|
|
/**
|
|
* @param __namedParameters
|
|
* * `array`: `null` if serializing, or binary data to deserialize
|
|
* * `textEncoder`: `TextEncoder` instance to use. Pass in `null` if running in a browser
|
|
* * `textDecoder`: `TextDecider` instance to use. Pass in `null` if running in a browser
|
|
*/
|
|
function SerialBuffer(_a) {
|
|
var _b = _a === void 0 ? {} : _a, textEncoder = _b.textEncoder, textDecoder = _b.textDecoder, array = _b.array;
|
|
/** Current position while reading (deserializing) */
|
|
this.readPos = 0;
|
|
this.array = array || new Uint8Array(1024);
|
|
this.length = array ? array.length : 0;
|
|
this.textEncoder = textEncoder || new TextEncoder();
|
|
this.textDecoder = textDecoder || new TextDecoder('utf-8', { fatal: true });
|
|
}
|
|
/** Resize `array` if needed to have at least `size` bytes free */
|
|
SerialBuffer.prototype.reserve = function (size) {
|
|
if (this.length + size <= this.array.length) {
|
|
return;
|
|
}
|
|
var l = this.array.length;
|
|
while (this.length + size > l) {
|
|
l = Math.ceil(l * 1.5);
|
|
}
|
|
var newArray = new Uint8Array(l);
|
|
newArray.set(this.array);
|
|
this.array = newArray;
|
|
};
|
|
/** Is there data available to read? */
|
|
SerialBuffer.prototype.haveReadData = function () {
|
|
return this.readPos < this.length;
|
|
};
|
|
/** Restart reading from the beginning */
|
|
SerialBuffer.prototype.restartRead = function () {
|
|
this.readPos = 0;
|
|
};
|
|
/** Return data with excess storage trimmed away */
|
|
SerialBuffer.prototype.asUint8Array = function () {
|
|
return new Uint8Array(this.array.buffer, this.array.byteOffset, this.length);
|
|
};
|
|
/** Append bytes */
|
|
SerialBuffer.prototype.pushArray = function (v) {
|
|
this.reserve(v.length);
|
|
this.array.set(v, this.length);
|
|
this.length += v.length;
|
|
};
|
|
/** Append bytes */
|
|
SerialBuffer.prototype.push = function () {
|
|
var v = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
v[_i] = arguments[_i];
|
|
}
|
|
this.pushArray(v);
|
|
};
|
|
/** Get a single byte */
|
|
SerialBuffer.prototype.get = function () {
|
|
if (this.readPos < this.length) {
|
|
return this.array[this.readPos++];
|
|
}
|
|
throw new Error('Read past end of buffer');
|
|
};
|
|
/** Append bytes in `v`. Throws if `len` doesn't match `v.length` */
|
|
SerialBuffer.prototype.pushUint8ArrayChecked = function (v, len) {
|
|
if (v.length !== len) {
|
|
throw new Error('Binary data has incorrect size');
|
|
}
|
|
this.pushArray(v);
|
|
};
|
|
/** Get `len` bytes */
|
|
SerialBuffer.prototype.getUint8Array = function (len) {
|
|
if (this.readPos + len > this.length) {
|
|
throw new Error('Read past end of buffer');
|
|
}
|
|
var result = new Uint8Array(this.array.buffer, this.array.byteOffset + this.readPos, len);
|
|
this.readPos += len;
|
|
return result;
|
|
};
|
|
/** Append a `uint16` */
|
|
SerialBuffer.prototype.pushUint16 = function (v) {
|
|
this.push((v >> 0) & 0xff, (v >> 8) & 0xff);
|
|
};
|
|
/** Get a `uint16` */
|
|
SerialBuffer.prototype.getUint16 = function () {
|
|
var v = 0;
|
|
v |= this.get() << 0;
|
|
v |= this.get() << 8;
|
|
return v;
|
|
};
|
|
/** Append a `uint32` */
|
|
SerialBuffer.prototype.pushUint32 = function (v) {
|
|
this.push((v >> 0) & 0xff, (v >> 8) & 0xff, (v >> 16) & 0xff, (v >> 24) & 0xff);
|
|
};
|
|
/** Get a `uint32` */
|
|
SerialBuffer.prototype.getUint32 = function () {
|
|
var v = 0;
|
|
v |= this.get() << 0;
|
|
v |= this.get() << 8;
|
|
v |= this.get() << 16;
|
|
v |= this.get() << 24;
|
|
return v >>> 0;
|
|
};
|
|
/** Append a `uint64`. *Caution*: `number` only has 53 bits of precision */
|
|
SerialBuffer.prototype.pushNumberAsUint64 = function (v) {
|
|
this.pushUint32(v >>> 0);
|
|
this.pushUint32(Math.floor(v / 4294967296) >>> 0);
|
|
};
|
|
/**
|
|
* Get a `uint64` as a `number`. *Caution*: `number` only has 53 bits of precision; some values will change.
|
|
* `numeric.binaryToDecimal(serialBuffer.getUint8Array(8))` recommended instead
|
|
*/
|
|
SerialBuffer.prototype.getUint64AsNumber = function () {
|
|
var low = this.getUint32();
|
|
var high = this.getUint32();
|
|
return (high >>> 0) * 4294967296 + (low >>> 0);
|
|
};
|
|
/** Append a `varuint32` */
|
|
SerialBuffer.prototype.pushVaruint32 = function (v) {
|
|
while (true) {
|
|
if (v >>> 7) {
|
|
this.push(0x80 | (v & 0x7f));
|
|
v = v >>> 7;
|
|
}
|
|
else {
|
|
this.push(v);
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
/** Get a `varuint32` */
|
|
SerialBuffer.prototype.getVaruint32 = function () {
|
|
var v = 0;
|
|
var bit = 0;
|
|
while (true) {
|
|
var b = this.get();
|
|
v |= (b & 0x7f) << bit;
|
|
bit += 7;
|
|
if (!(b & 0x80)) {
|
|
break;
|
|
}
|
|
}
|
|
return v >>> 0;
|
|
};
|
|
/** Append a `varint32` */
|
|
SerialBuffer.prototype.pushVarint32 = function (v) {
|
|
this.pushVaruint32((v << 1) ^ (v >> 31));
|
|
};
|
|
/** Get a `varint32` */
|
|
SerialBuffer.prototype.getVarint32 = function () {
|
|
var v = this.getVaruint32();
|
|
if (v & 1) {
|
|
return ((~v) >> 1) | 2147483648;
|
|
}
|
|
else {
|
|
return v >>> 1;
|
|
}
|
|
};
|
|
/** Append a `float32` */
|
|
SerialBuffer.prototype.pushFloat32 = function (v) {
|
|
this.pushArray(new Uint8Array((new Float32Array([v])).buffer));
|
|
};
|
|
/** Get a `float32` */
|
|
SerialBuffer.prototype.getFloat32 = function () {
|
|
return new Float32Array(this.getUint8Array(4).slice().buffer)[0];
|
|
};
|
|
/** Append a `float64` */
|
|
SerialBuffer.prototype.pushFloat64 = function (v) {
|
|
this.pushArray(new Uint8Array((new Float64Array([v])).buffer));
|
|
};
|
|
/** Get a `float64` */
|
|
SerialBuffer.prototype.getFloat64 = function () {
|
|
return new Float64Array(this.getUint8Array(8).slice().buffer)[0];
|
|
};
|
|
/** Append a `name` */
|
|
SerialBuffer.prototype.pushName = function (s) {
|
|
if (typeof s !== 'string') {
|
|
throw new Error('Expected string containing name');
|
|
}
|
|
function charToSymbol(c) {
|
|
if (c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) {
|
|
return (c - 'a'.charCodeAt(0)) + 6;
|
|
}
|
|
if (c >= '1'.charCodeAt(0) && c <= '5'.charCodeAt(0)) {
|
|
return (c - '1'.charCodeAt(0)) + 1;
|
|
}
|
|
return 0;
|
|
}
|
|
var a = new Uint8Array(8);
|
|
var bit = 63;
|
|
for (var i = 0; i < s.length; ++i) {
|
|
var c = charToSymbol(s.charCodeAt(i));
|
|
if (bit < 5) {
|
|
c = c << 1;
|
|
}
|
|
for (var j = 4; j >= 0; --j) {
|
|
if (bit >= 0) {
|
|
a[Math.floor(bit / 8)] |= ((c >> j) & 1) << (bit % 8);
|
|
--bit;
|
|
}
|
|
}
|
|
}
|
|
this.pushArray(a);
|
|
};
|
|
/** Get a `name` */
|
|
SerialBuffer.prototype.getName = function () {
|
|
var a = this.getUint8Array(8);
|
|
var result = '';
|
|
for (var bit = 63; bit >= 0;) {
|
|
var c = 0;
|
|
for (var i = 0; i < 5; ++i) {
|
|
if (bit >= 0) {
|
|
c = (c << 1) | ((a[Math.floor(bit / 8)] >> (bit % 8)) & 1);
|
|
--bit;
|
|
}
|
|
}
|
|
if (c >= 6) {
|
|
result += String.fromCharCode(c + 'a'.charCodeAt(0) - 6);
|
|
}
|
|
else if (c >= 1) {
|
|
result += String.fromCharCode(c + '1'.charCodeAt(0) - 1);
|
|
}
|
|
else {
|
|
result += '.';
|
|
}
|
|
}
|
|
while (result.endsWith('.')) {
|
|
result = result.substr(0, result.length - 1);
|
|
}
|
|
return result;
|
|
};
|
|
/** Append length-prefixed binary data */
|
|
SerialBuffer.prototype.pushBytes = function (v) {
|
|
this.pushVaruint32(v.length);
|
|
this.pushArray(v);
|
|
};
|
|
/** Get length-prefixed binary data */
|
|
SerialBuffer.prototype.getBytes = function () {
|
|
return this.getUint8Array(this.getVaruint32());
|
|
};
|
|
/** Append a string */
|
|
SerialBuffer.prototype.pushString = function (v) {
|
|
this.pushBytes(this.textEncoder.encode(v));
|
|
};
|
|
/** Get a string */
|
|
SerialBuffer.prototype.getString = function () {
|
|
return this.textDecoder.decode(this.getBytes());
|
|
};
|
|
/** Append a `symbol_code`. Unlike `symbol`, `symbol_code` doesn't include a precision. */
|
|
SerialBuffer.prototype.pushSymbolCode = function (name) {
|
|
if (typeof name !== 'string') {
|
|
throw new Error('Expected string containing symbol_code');
|
|
}
|
|
var a = [];
|
|
a.push.apply(a, __spread(this.textEncoder.encode(name)));
|
|
while (a.length < 8) {
|
|
a.push(0);
|
|
}
|
|
this.pushArray(a.slice(0, 8));
|
|
};
|
|
/** Get a `symbol_code`. Unlike `symbol`, `symbol_code` doesn't include a precision. */
|
|
SerialBuffer.prototype.getSymbolCode = function () {
|
|
var a = this.getUint8Array(8);
|
|
var len;
|
|
for (len = 0; len < a.length; ++len) {
|
|
if (!a[len]) {
|
|
break;
|
|
}
|
|
}
|
|
var name = this.textDecoder.decode(new Uint8Array(a.buffer, a.byteOffset, len));
|
|
return name;
|
|
};
|
|
/** Append a `symbol` */
|
|
SerialBuffer.prototype.pushSymbol = function (_a) {
|
|
var name = _a.name, precision = _a.precision;
|
|
var a = [precision & 0xff];
|
|
a.push.apply(a, __spread(this.textEncoder.encode(name)));
|
|
while (a.length < 8) {
|
|
a.push(0);
|
|
}
|
|
this.pushArray(a.slice(0, 8));
|
|
};
|
|
/** Get a `symbol` */
|
|
SerialBuffer.prototype.getSymbol = function () {
|
|
var precision = this.get();
|
|
var a = this.getUint8Array(7);
|
|
var len;
|
|
for (len = 0; len < a.length; ++len) {
|
|
if (!a[len]) {
|
|
break;
|
|
}
|
|
}
|
|
var name = this.textDecoder.decode(new Uint8Array(a.buffer, a.byteOffset, len));
|
|
return { name: name, precision: precision };
|
|
};
|
|
/** Append an asset */
|
|
SerialBuffer.prototype.pushAsset = function (s) {
|
|
if (typeof s !== 'string') {
|
|
throw new Error('Expected string containing asset');
|
|
}
|
|
s = s.trim();
|
|
var pos = 0;
|
|
var amount = '';
|
|
var precision = 0;
|
|
if (s[pos] === '-') {
|
|
amount += '-';
|
|
++pos;
|
|
}
|
|
var foundDigit = false;
|
|
while (pos < s.length && s.charCodeAt(pos) >= '0'.charCodeAt(0) && s.charCodeAt(pos) <= '9'.charCodeAt(0)) {
|
|
foundDigit = true;
|
|
amount += s[pos];
|
|
++pos;
|
|
}
|
|
if (!foundDigit) {
|
|
throw new Error('Asset must begin with a number');
|
|
}
|
|
if (s[pos] === '.') {
|
|
++pos;
|
|
while (pos < s.length && s.charCodeAt(pos) >= '0'.charCodeAt(0) && s.charCodeAt(pos) <= '9'.charCodeAt(0)) {
|
|
amount += s[pos];
|
|
++precision;
|
|
++pos;
|
|
}
|
|
}
|
|
var name = s.substr(pos).trim();
|
|
this.pushArray(numeric.signedDecimalToBinary(8, amount));
|
|
this.pushSymbol({ name: name, precision: precision });
|
|
};
|
|
/** Get an asset */
|
|
SerialBuffer.prototype.getAsset = function () {
|
|
var amount = this.getUint8Array(8);
|
|
var _a = this.getSymbol(), name = _a.name, precision = _a.precision;
|
|
var s = numeric.signedBinaryToDecimal(amount, precision + 1);
|
|
if (precision) {
|
|
s = s.substr(0, s.length - precision) + '.' + s.substr(s.length - precision);
|
|
}
|
|
return s + ' ' + name;
|
|
};
|
|
/** Append a public key */
|
|
SerialBuffer.prototype.pushPublicKey = function (s) {
|
|
var key = numeric.stringToPublicKey(s);
|
|
this.push(key.type);
|
|
this.pushArray(key.data);
|
|
};
|
|
/** Get a public key */
|
|
SerialBuffer.prototype.getPublicKey = function () {
|
|
var type = this.get();
|
|
var data = this.getUint8Array(numeric.publicKeyDataSize);
|
|
return numeric.publicKeyToString({ type: type, data: data });
|
|
};
|
|
/** Append a private key */
|
|
SerialBuffer.prototype.pushPrivateKey = function (s) {
|
|
var key = numeric.stringToPrivateKey(s);
|
|
this.push(key.type);
|
|
this.pushArray(key.data);
|
|
};
|
|
/** Get a private key */
|
|
SerialBuffer.prototype.getPrivateKey = function () {
|
|
var type = this.get();
|
|
var data = this.getUint8Array(numeric.privateKeyDataSize);
|
|
return numeric.privateKeyToString({ type: type, data: data });
|
|
};
|
|
/** Append a signature */
|
|
SerialBuffer.prototype.pushSignature = function (s) {
|
|
var key = numeric.stringToSignature(s);
|
|
this.push(key.type);
|
|
this.pushArray(key.data);
|
|
};
|
|
/** Get a signature */
|
|
SerialBuffer.prototype.getSignature = function () {
|
|
var type = this.get();
|
|
var data = this.getUint8Array(numeric.signatureDataSize);
|
|
return numeric.signatureToString({ type: type, data: data });
|
|
};
|
|
return SerialBuffer;
|
|
}()); // SerialBuffer
|
|
exports.SerialBuffer = SerialBuffer;
|
|
/** Is this a supported ABI version? */
|
|
function supportedAbiVersion(version) {
|
|
return version.startsWith('eosio::abi/1.');
|
|
}
|
|
exports.supportedAbiVersion = supportedAbiVersion;
|
|
function checkDateParse(date) {
|
|
var result = Date.parse(date);
|
|
if (Number.isNaN(result)) {
|
|
throw new Error('Invalid time format');
|
|
}
|
|
return result;
|
|
}
|
|
/** Convert date in ISO format to `time_point` (miliseconds since epoch) */
|
|
function dateToTimePoint(date) {
|
|
return Math.round(checkDateParse(date + 'Z') * 1000);
|
|
}
|
|
exports.dateToTimePoint = dateToTimePoint;
|
|
/** Convert `time_point` (miliseconds since epoch) to date in ISO format */
|
|
function timePointToDate(us) {
|
|
var s = (new Date(us / 1000)).toISOString();
|
|
return s.substr(0, s.length - 1);
|
|
}
|
|
exports.timePointToDate = timePointToDate;
|
|
/** Convert date in ISO format to `time_point_sec` (seconds since epoch) */
|
|
function dateToTimePointSec(date) {
|
|
return Math.round(checkDateParse(date + 'Z') / 1000);
|
|
}
|
|
exports.dateToTimePointSec = dateToTimePointSec;
|
|
/** Convert `time_point_sec` (seconds since epoch) to to date in ISO format */
|
|
function timePointSecToDate(sec) {
|
|
var s = (new Date(sec * 1000)).toISOString();
|
|
return s.substr(0, s.length - 1);
|
|
}
|
|
exports.timePointSecToDate = timePointSecToDate;
|
|
/** Convert date in ISO format to `block_timestamp_type` (half-seconds since a different epoch) */
|
|
function dateToBlockTimestamp(date) {
|
|
return Math.round((checkDateParse(date + 'Z') - 946684800000) / 500);
|
|
}
|
|
exports.dateToBlockTimestamp = dateToBlockTimestamp;
|
|
/** Convert `block_timestamp_type` (half-seconds since a different epoch) to to date in ISO format */
|
|
function blockTimestampToDate(slot) {
|
|
var s = (new Date(slot * 500 + 946684800000)).toISOString();
|
|
return s.substr(0, s.length - 1);
|
|
}
|
|
exports.blockTimestampToDate = blockTimestampToDate;
|
|
/** Convert `string` to `Symbol`. format: `precision,NAME`. */
|
|
function stringToSymbol(s) {
|
|
if (typeof s !== 'string') {
|
|
throw new Error('Expected string containing symbol');
|
|
}
|
|
var m = s.match(/^([0-9]+),([A-Z]+)$/);
|
|
if (!m) {
|
|
throw new Error('Invalid symbol');
|
|
}
|
|
return { name: m[2], precision: +m[1] };
|
|
}
|
|
exports.stringToSymbol = stringToSymbol;
|
|
/** Convert `Symbol` to `string`. format: `precision,NAME`. */
|
|
function symbolToString(_a) {
|
|
var name = _a.name, precision = _a.precision;
|
|
return precision + ',' + name;
|
|
}
|
|
exports.symbolToString = symbolToString;
|
|
/** Convert binary data to hex */
|
|
function arrayToHex(data) {
|
|
var e_1, _a;
|
|
var result = '';
|
|
try {
|
|
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
|
|
var x = data_1_1.value;
|
|
result += ('00' + x.toString(16)).slice(-2);
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
return result.toUpperCase();
|
|
}
|
|
exports.arrayToHex = arrayToHex;
|
|
/** Convert hex to binary data */
|
|
function hexToUint8Array(hex) {
|
|
if (typeof hex !== 'string') {
|
|
throw new Error('Expected string containing hex digits');
|
|
}
|
|
if (hex.length % 2) {
|
|
throw new Error('Odd number of hex digits');
|
|
}
|
|
var l = hex.length / 2;
|
|
var result = new Uint8Array(l);
|
|
for (var i = 0; i < l; ++i) {
|
|
var x = parseInt(hex.substr(i * 2, 2), 16);
|
|
if (Number.isNaN(x)) {
|
|
throw new Error('Expected hex string');
|
|
}
|
|
result[i] = x;
|
|
}
|
|
return result;
|
|
}
|
|
exports.hexToUint8Array = hexToUint8Array;
|
|
function serializeUnknown(buffer, data) {
|
|
throw new Error('Don\'t know how to serialize ' + this.name);
|
|
}
|
|
function deserializeUnknown(buffer) {
|
|
throw new Error('Don\'t know how to deserialize ' + this.name);
|
|
}
|
|
function serializeStruct(buffer, data, state, allowExtensions) {
|
|
if (state === void 0) { state = new SerializerState(); }
|
|
if (allowExtensions === void 0) { allowExtensions = true; }
|
|
var e_2, _a;
|
|
if (typeof data !== 'object') {
|
|
throw new Error('expected object containing data: ' + JSON.stringify(data));
|
|
}
|
|
if (this.base) {
|
|
this.base.serialize(buffer, data, state, allowExtensions);
|
|
}
|
|
try {
|
|
for (var _b = __values(this.fields), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var field = _c.value;
|
|
if (field.name in data) {
|
|
if (state.skippedBinaryExtension) {
|
|
throw new Error('unexpected ' + this.name + '.' + field.name);
|
|
}
|
|
field.type.serialize(buffer, data[field.name], state, allowExtensions && field === this.fields[this.fields.length - 1]);
|
|
}
|
|
else {
|
|
if (allowExtensions && field.type.extensionOf) {
|
|
state.skippedBinaryExtension = true;
|
|
}
|
|
else {
|
|
throw new Error('missing ' + this.name + '.' + field.name + ' (type=' + field.type.name + ')');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
}
|
|
}
|
|
function deserializeStruct(buffer, state, allowExtensions) {
|
|
if (state === void 0) { state = new SerializerState(); }
|
|
if (allowExtensions === void 0) { allowExtensions = true; }
|
|
var e_3, _a;
|
|
var result;
|
|
if (this.base) {
|
|
result = this.base.deserialize(buffer, state, allowExtensions);
|
|
}
|
|
else {
|
|
result = {};
|
|
}
|
|
try {
|
|
for (var _b = __values(this.fields), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var field = _c.value;
|
|
if (allowExtensions && field.type.extensionOf && !buffer.haveReadData()) {
|
|
state.skippedBinaryExtension = true;
|
|
}
|
|
else {
|
|
result[field.name] = field.type.deserialize(buffer, state, allowExtensions);
|
|
}
|
|
}
|
|
}
|
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_3) throw e_3.error; }
|
|
}
|
|
return result;
|
|
}
|
|
function serializeVariant(buffer, data, state, allowExtensions) {
|
|
if (!Array.isArray(data) || data.length !== 2 || typeof data[0] !== 'string') {
|
|
throw new Error('expected variant: ["type", value]');
|
|
}
|
|
var i = this.fields.findIndex(function (field) { return field.name === data[0]; });
|
|
if (i < 0) {
|
|
throw new Error("type \"" + data[0] + "\" is not valid for variant");
|
|
}
|
|
buffer.pushVaruint32(i);
|
|
this.fields[i].type.serialize(buffer, data[1], state, allowExtensions);
|
|
}
|
|
function deserializeVariant(buffer, state, allowExtensions) {
|
|
var i = buffer.getVaruint32();
|
|
if (i >= this.fields.length) {
|
|
throw new Error("type index " + i + " is not valid for variant");
|
|
}
|
|
var field = this.fields[i];
|
|
return [field.name, field.type.deserialize(buffer, state, allowExtensions)];
|
|
}
|
|
function serializeArray(buffer, data, state, allowExtensions) {
|
|
var e_4, _a;
|
|
buffer.pushVaruint32(data.length);
|
|
try {
|
|
for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
|
|
var item = data_2_1.value;
|
|
this.arrayOf.serialize(buffer, item, state, false);
|
|
}
|
|
}
|
|
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
finally {
|
|
try {
|
|
if (data_2_1 && !data_2_1.done && (_a = data_2.return)) _a.call(data_2);
|
|
}
|
|
finally { if (e_4) throw e_4.error; }
|
|
}
|
|
}
|
|
function deserializeArray(buffer, state, allowExtensions) {
|
|
var len = buffer.getVaruint32();
|
|
var result = [];
|
|
for (var i = 0; i < len; ++i) {
|
|
result.push(this.arrayOf.deserialize(buffer, state, false));
|
|
}
|
|
return result;
|
|
}
|
|
function serializeOptional(buffer, data, state, allowExtensions) {
|
|
if (data === null || data === undefined) {
|
|
buffer.push(0);
|
|
}
|
|
else {
|
|
buffer.push(1);
|
|
this.optionalOf.serialize(buffer, data, state, allowExtensions);
|
|
}
|
|
}
|
|
function deserializeOptional(buffer, state, allowExtensions) {
|
|
if (buffer.get()) {
|
|
return this.optionalOf.deserialize(buffer, state, allowExtensions);
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
function serializeExtension(buffer, data, state, allowExtensions) {
|
|
this.extensionOf.serialize(buffer, data, state, allowExtensions);
|
|
}
|
|
function deserializeExtension(buffer, state, allowExtensions) {
|
|
return this.extensionOf.deserialize(buffer, state, allowExtensions);
|
|
}
|
|
function createType(attrs) {
|
|
return __assign({ name: '<missing name>', aliasOfName: '', arrayOf: null, optionalOf: null, extensionOf: null, baseName: '', base: null, fields: [], serialize: serializeUnknown, deserialize: deserializeUnknown }, attrs);
|
|
}
|
|
function checkRange(orig, converted) {
|
|
if (Number.isNaN(+orig) || Number.isNaN(+converted) || (typeof orig !== 'number' && typeof orig !== 'string')) {
|
|
throw new Error('Expected number');
|
|
}
|
|
if (+orig !== +converted) {
|
|
throw new Error('Number is out of range');
|
|
}
|
|
return +orig;
|
|
}
|
|
/** Create the set of types built-in to the abi format */
|
|
function createInitialTypes() {
|
|
var result = new Map(Object.entries({
|
|
bool: createType({
|
|
name: 'bool',
|
|
serialize: function (buffer, data) {
|
|
if (typeof data !== 'boolean') {
|
|
throw new Error('Expected true or false');
|
|
}
|
|
buffer.push(data ? 1 : 0);
|
|
},
|
|
deserialize: function (buffer) { return !!buffer.get(); },
|
|
}),
|
|
uint8: createType({
|
|
name: 'uint8',
|
|
serialize: function (buffer, data) { buffer.push(checkRange(data, data & 0xff)); },
|
|
deserialize: function (buffer) { return buffer.get(); },
|
|
}),
|
|
int8: createType({
|
|
name: 'int8',
|
|
serialize: function (buffer, data) { buffer.push(checkRange(data, data << 24 >> 24)); },
|
|
deserialize: function (buffer) { return buffer.get() << 24 >> 24; },
|
|
}),
|
|
uint16: createType({
|
|
name: 'uint16',
|
|
serialize: function (buffer, data) { buffer.pushUint16(checkRange(data, data & 0xffff)); },
|
|
deserialize: function (buffer) { return buffer.getUint16(); },
|
|
}),
|
|
int16: createType({
|
|
name: 'int16',
|
|
serialize: function (buffer, data) { buffer.pushUint16(checkRange(data, data << 16 >> 16)); },
|
|
deserialize: function (buffer) { return buffer.getUint16() << 16 >> 16; },
|
|
}),
|
|
uint32: createType({
|
|
name: 'uint32',
|
|
serialize: function (buffer, data) { buffer.pushUint32(checkRange(data, data >>> 0)); },
|
|
deserialize: function (buffer) { return buffer.getUint32(); },
|
|
}),
|
|
uint64: createType({
|
|
name: 'uint64',
|
|
serialize: function (buffer, data) {
|
|
buffer.pushArray(numeric.decimalToBinary(8, '' + data));
|
|
},
|
|
deserialize: function (buffer) { return numeric.binaryToDecimal(buffer.getUint8Array(8)); },
|
|
}),
|
|
int64: createType({
|
|
name: 'int64',
|
|
serialize: function (buffer, data) {
|
|
buffer.pushArray(numeric.signedDecimalToBinary(8, '' + data));
|
|
},
|
|
deserialize: function (buffer) { return numeric.signedBinaryToDecimal(buffer.getUint8Array(8)); },
|
|
}),
|
|
int32: createType({
|
|
name: 'int32',
|
|
serialize: function (buffer, data) { buffer.pushUint32(checkRange(data, data | 0)); },
|
|
deserialize: function (buffer) { return buffer.getUint32() | 0; },
|
|
}),
|
|
varuint32: createType({
|
|
name: 'varuint32',
|
|
serialize: function (buffer, data) { buffer.pushVaruint32(checkRange(data, data >>> 0)); },
|
|
deserialize: function (buffer) { return buffer.getVaruint32(); },
|
|
}),
|
|
varint32: createType({
|
|
name: 'varint32',
|
|
serialize: function (buffer, data) { buffer.pushVarint32(checkRange(data, data | 0)); },
|
|
deserialize: function (buffer) { return buffer.getVarint32(); },
|
|
}),
|
|
uint128: createType({
|
|
name: 'uint128',
|
|
serialize: function (buffer, data) { buffer.pushArray(numeric.decimalToBinary(16, '' + data)); },
|
|
deserialize: function (buffer) { return numeric.binaryToDecimal(buffer.getUint8Array(16)); },
|
|
}),
|
|
int128: createType({
|
|
name: 'int128',
|
|
serialize: function (buffer, data) {
|
|
buffer.pushArray(numeric.signedDecimalToBinary(16, '' + data));
|
|
},
|
|
deserialize: function (buffer) { return numeric.signedBinaryToDecimal(buffer.getUint8Array(16)); },
|
|
}),
|
|
float32: createType({
|
|
name: 'float32',
|
|
serialize: function (buffer, data) { buffer.pushFloat32(data); },
|
|
deserialize: function (buffer) { return buffer.getFloat32(); },
|
|
}),
|
|
float64: createType({
|
|
name: 'float64',
|
|
serialize: function (buffer, data) { buffer.pushFloat64(data); },
|
|
deserialize: function (buffer) { return buffer.getFloat64(); },
|
|
}),
|
|
float128: createType({
|
|
name: 'float128',
|
|
serialize: function (buffer, data) { buffer.pushUint8ArrayChecked(hexToUint8Array(data), 16); },
|
|
deserialize: function (buffer) { return arrayToHex(buffer.getUint8Array(16)); },
|
|
}),
|
|
bytes: createType({
|
|
name: 'bytes',
|
|
serialize: function (buffer, data) {
|
|
if (data instanceof Uint8Array || Array.isArray(data)) {
|
|
buffer.pushBytes(data);
|
|
}
|
|
else {
|
|
buffer.pushBytes(hexToUint8Array(data));
|
|
}
|
|
},
|
|
deserialize: function (buffer, state) {
|
|
if (state && state.options.bytesAsUint8Array) {
|
|
return buffer.getBytes();
|
|
}
|
|
else {
|
|
return arrayToHex(buffer.getBytes());
|
|
}
|
|
},
|
|
}),
|
|
string: createType({
|
|
name: 'string',
|
|
serialize: function (buffer, data) { buffer.pushString(data); },
|
|
deserialize: function (buffer) { return buffer.getString(); },
|
|
}),
|
|
name: createType({
|
|
name: 'name',
|
|
serialize: function (buffer, data) { buffer.pushName(data); },
|
|
deserialize: function (buffer) { return buffer.getName(); },
|
|
}),
|
|
time_point: createType({
|
|
name: 'time_point',
|
|
serialize: function (buffer, data) { buffer.pushNumberAsUint64(dateToTimePoint(data)); },
|
|
deserialize: function (buffer) { return timePointToDate(buffer.getUint64AsNumber()); },
|
|
}),
|
|
time_point_sec: createType({
|
|
name: 'time_point_sec',
|
|
serialize: function (buffer, data) { buffer.pushUint32(dateToTimePointSec(data)); },
|
|
deserialize: function (buffer) { return timePointSecToDate(buffer.getUint32()); },
|
|
}),
|
|
block_timestamp_type: createType({
|
|
name: 'block_timestamp_type',
|
|
serialize: function (buffer, data) { buffer.pushUint32(dateToBlockTimestamp(data)); },
|
|
deserialize: function (buffer) { return blockTimestampToDate(buffer.getUint32()); },
|
|
}),
|
|
symbol_code: createType({
|
|
name: 'symbol_code',
|
|
serialize: function (buffer, data) { buffer.pushSymbolCode(data); },
|
|
deserialize: function (buffer) { return buffer.getSymbolCode(); },
|
|
}),
|
|
symbol: createType({
|
|
name: 'symbol',
|
|
serialize: function (buffer, data) { buffer.pushSymbol(stringToSymbol(data)); },
|
|
deserialize: function (buffer) { return symbolToString(buffer.getSymbol()); },
|
|
}),
|
|
asset: createType({
|
|
name: 'asset',
|
|
serialize: function (buffer, data) { buffer.pushAsset(data); },
|
|
deserialize: function (buffer) { return buffer.getAsset(); },
|
|
}),
|
|
checksum160: createType({
|
|
name: 'checksum160',
|
|
serialize: function (buffer, data) { buffer.pushUint8ArrayChecked(hexToUint8Array(data), 20); },
|
|
deserialize: function (buffer) { return arrayToHex(buffer.getUint8Array(20)); },
|
|
}),
|
|
checksum256: createType({
|
|
name: 'checksum256',
|
|
serialize: function (buffer, data) { buffer.pushUint8ArrayChecked(hexToUint8Array(data), 32); },
|
|
deserialize: function (buffer) { return arrayToHex(buffer.getUint8Array(32)); },
|
|
}),
|
|
checksum512: createType({
|
|
name: 'checksum512',
|
|
serialize: function (buffer, data) { buffer.pushUint8ArrayChecked(hexToUint8Array(data), 64); },
|
|
deserialize: function (buffer) { return arrayToHex(buffer.getUint8Array(64)); },
|
|
}),
|
|
public_key: createType({
|
|
name: 'public_key',
|
|
serialize: function (buffer, data) { buffer.pushPublicKey(data); },
|
|
deserialize: function (buffer) { return buffer.getPublicKey(); },
|
|
}),
|
|
private_key: createType({
|
|
name: 'private_key',
|
|
serialize: function (buffer, data) { buffer.pushPrivateKey(data); },
|
|
deserialize: function (buffer) { return buffer.getPrivateKey(); },
|
|
}),
|
|
signature: createType({
|
|
name: 'signature',
|
|
serialize: function (buffer, data) { buffer.pushSignature(data); },
|
|
deserialize: function (buffer) { return buffer.getSignature(); },
|
|
}),
|
|
}));
|
|
result.set('extended_asset', createType({
|
|
name: 'extended_asset',
|
|
baseName: '',
|
|
fields: [
|
|
{ name: 'quantity', typeName: 'asset', type: result.get('asset') },
|
|
{ name: 'contract', typeName: 'name', type: result.get('name') },
|
|
],
|
|
serialize: serializeStruct,
|
|
deserialize: deserializeStruct,
|
|
}));
|
|
return result;
|
|
} // createInitialTypes()
|
|
exports.createInitialTypes = createInitialTypes;
|
|
/** Get type from `types` */
|
|
function getType(types, name) {
|
|
var type = types.get(name);
|
|
if (type && type.aliasOfName) {
|
|
return getType(types, type.aliasOfName);
|
|
}
|
|
if (type) {
|
|
return type;
|
|
}
|
|
if (name.endsWith('[]')) {
|
|
return createType({
|
|
name: name,
|
|
arrayOf: getType(types, name.substr(0, name.length - 2)),
|
|
serialize: serializeArray,
|
|
deserialize: deserializeArray,
|
|
});
|
|
}
|
|
if (name.endsWith('?')) {
|
|
return createType({
|
|
name: name,
|
|
optionalOf: getType(types, name.substr(0, name.length - 1)),
|
|
serialize: serializeOptional,
|
|
deserialize: deserializeOptional,
|
|
});
|
|
}
|
|
if (name.endsWith('$')) {
|
|
return createType({
|
|
name: name,
|
|
extensionOf: getType(types, name.substr(0, name.length - 1)),
|
|
serialize: serializeExtension,
|
|
deserialize: deserializeExtension,
|
|
});
|
|
}
|
|
throw new Error('Unknown type: ' + name);
|
|
}
|
|
exports.getType = getType;
|
|
/**
|
|
* Get types from abi
|
|
* @param initialTypes Set of types to build on.
|
|
* In most cases, it's best to fill this from a fresh call to `getTypesFromAbi()`.
|
|
*/
|
|
function getTypesFromAbi(initialTypes, abi) {
|
|
var e_5, _a, e_6, _b, e_7, _c, e_8, _d, e_9, _e;
|
|
var types = new Map(initialTypes);
|
|
if (abi.types) {
|
|
try {
|
|
for (var _f = __values(abi.types), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
var _h = _g.value, new_type_name = _h.new_type_name, type = _h.type;
|
|
types.set(new_type_name, createType({ name: new_type_name, aliasOfName: type }));
|
|
}
|
|
}
|
|
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
finally {
|
|
try {
|
|
if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
|
|
}
|
|
finally { if (e_5) throw e_5.error; }
|
|
}
|
|
}
|
|
if (abi.structs) {
|
|
try {
|
|
for (var _j = __values(abi.structs), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
var _l = _k.value, name_1 = _l.name, base = _l.base, fields = _l.fields;
|
|
types.set(name_1, createType({
|
|
name: name_1,
|
|
baseName: base,
|
|
fields: fields.map(function (_a) {
|
|
var n = _a.name, type = _a.type;
|
|
return ({ name: n, typeName: type, type: null });
|
|
}),
|
|
serialize: serializeStruct,
|
|
deserialize: deserializeStruct,
|
|
}));
|
|
}
|
|
}
|
|
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
finally {
|
|
try {
|
|
if (_k && !_k.done && (_b = _j.return)) _b.call(_j);
|
|
}
|
|
finally { if (e_6) throw e_6.error; }
|
|
}
|
|
}
|
|
if (abi.variants) {
|
|
try {
|
|
for (var _m = __values(abi.variants), _o = _m.next(); !_o.done; _o = _m.next()) {
|
|
var _p = _o.value, name_2 = _p.name, t = _p.types;
|
|
types.set(name_2, createType({
|
|
name: name_2,
|
|
fields: t.map(function (s) { return ({ name: s, typeName: s, type: null }); }),
|
|
serialize: serializeVariant,
|
|
deserialize: deserializeVariant,
|
|
}));
|
|
}
|
|
}
|
|
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
finally {
|
|
try {
|
|
if (_o && !_o.done && (_c = _m.return)) _c.call(_m);
|
|
}
|
|
finally { if (e_7) throw e_7.error; }
|
|
}
|
|
}
|
|
try {
|
|
for (var types_1 = __values(types), types_1_1 = types_1.next(); !types_1_1.done; types_1_1 = types_1.next()) {
|
|
var _q = __read(types_1_1.value, 2), name_3 = _q[0], type = _q[1];
|
|
if (type.baseName) {
|
|
type.base = getType(types, type.baseName);
|
|
}
|
|
try {
|
|
for (var _r = __values(type.fields), _s = _r.next(); !_s.done; _s = _r.next()) {
|
|
var field = _s.value;
|
|
field.type = getType(types, field.typeName);
|
|
}
|
|
}
|
|
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
finally {
|
|
try {
|
|
if (_s && !_s.done && (_e = _r.return)) _e.call(_r);
|
|
}
|
|
finally { if (e_9) throw e_9.error; }
|
|
}
|
|
}
|
|
}
|
|
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
finally {
|
|
try {
|
|
if (types_1_1 && !types_1_1.done && (_d = types_1.return)) _d.call(types_1);
|
|
}
|
|
finally { if (e_8) throw e_8.error; }
|
|
}
|
|
return types;
|
|
} // getTypesFromAbi
|
|
exports.getTypesFromAbi = getTypesFromAbi;
|
|
/** TAPoS: Return transaction fields which reference `refBlock` and expire `expireSeconds` after `refBlock.timestamp` */
|
|
function transactionHeader(refBlock, expireSeconds) {
|
|
return {
|
|
expiration: timePointSecToDate(dateToTimePointSec(refBlock.timestamp) + expireSeconds),
|
|
ref_block_num: refBlock.block_num & 0xffff,
|
|
ref_block_prefix: refBlock.ref_block_prefix,
|
|
};
|
|
}
|
|
exports.transactionHeader = transactionHeader;
|
|
/** Convert action data to serialized form (hex) */
|
|
function serializeActionData(contract, account, name, data, textEncoder, textDecoder) {
|
|
var action = contract.actions.get(name);
|
|
if (!action) {
|
|
throw new Error("Unknown action " + name + " in contract " + account);
|
|
}
|
|
var buffer = new SerialBuffer({ textEncoder: textEncoder, textDecoder: textDecoder });
|
|
action.serialize(buffer, data);
|
|
return arrayToHex(buffer.asUint8Array());
|
|
}
|
|
exports.serializeActionData = serializeActionData;
|
|
/** Return action in serialized form */
|
|
function serializeAction(contract, account, name, authorization, data, textEncoder, textDecoder) {
|
|
return {
|
|
account: account,
|
|
name: name,
|
|
authorization: authorization,
|
|
data: serializeActionData(contract, account, name, data, textEncoder, textDecoder),
|
|
};
|
|
}
|
|
exports.serializeAction = serializeAction;
|
|
/** Deserialize action data. If `data` is a `string`, then it's assumed to be in hex. */
|
|
function deserializeActionData(contract, account, name, data, textEncoder, textDecoder) {
|
|
var action = contract.actions.get(name);
|
|
if (typeof data === 'string') {
|
|
data = hexToUint8Array(data);
|
|
}
|
|
if (!action) {
|
|
throw new Error("Unknown action " + name + " in contract " + account);
|
|
}
|
|
var buffer = new SerialBuffer({ textDecoder: textDecoder, textEncoder: textEncoder });
|
|
buffer.pushArray(data);
|
|
return action.deserialize(buffer);
|
|
}
|
|
exports.deserializeActionData = deserializeActionData;
|
|
/** Deserialize action. If `data` is a `string`, then it's assumed to be in hex. */
|
|
function deserializeAction(contract, account, name, authorization, data, textEncoder, textDecoder) {
|
|
return {
|
|
account: account,
|
|
name: name,
|
|
authorization: authorization,
|
|
data: deserializeActionData(contract, account, name, data, textEncoder, textDecoder),
|
|
};
|
|
}
|
|
exports.deserializeAction = deserializeAction;
|
|
//# sourceMappingURL=eosjs-serialize.js.map
|