@gnolang/tm2-js-client
Version:
Tendermint2 JS / TS Client
81 lines (80 loc) • 2.61 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.uint8ArrayToBase64 = exports.base64ToUint8Array = exports.stringToBase64 = exports.parseProto = exports.parseABCI = exports.newResponse = exports.newRequest = void 0;
var uuid_1 = require("uuid");
// The version of the supported JSON-RPC protocol
var standardVersion = '2.0';
/**
* Creates a new JSON-RPC 2.0 request
* @param {string} method the requested method
* @param {string[]} [params] the requested params, if any
*/
var newRequest = function (method, params) {
return {
// the ID of the request is not that relevant for this helper method;
// for finer ID control, instantiate the request object directly
id: (0, uuid_1.v4)(),
jsonrpc: standardVersion,
method: method,
params: params,
};
};
exports.newRequest = newRequest;
/**
* Creates a new JSON-RPC 2.0 response
* @param {Result} result the response result, if any
* @param {RPCError} error the response error, if any
*/
var newResponse = function (result, error) {
return {
id: (0, uuid_1.v4)(),
jsonrpc: standardVersion,
result: result,
error: error,
};
};
exports.newResponse = newResponse;
/**
* Parses the base64 encoded ABCI JSON into a concrete type
* @param {string} data the base64-encoded JSON
*/
var parseABCI = function (data) {
var jsonData = Buffer.from(data, 'base64').toString();
var parsedData = JSON.parse(jsonData);
if (!parsedData) {
throw new Error('unable to parse JSON response');
}
return parsedData;
};
exports.parseABCI = parseABCI;
var parseProto = function (data, decodeFn) {
var protoData = decodeFn(Buffer.from(data, 'base64'));
return protoData;
};
exports.parseProto = parseProto;
/**
* Converts a string into base64 representation
* @param {string} str the raw string
*/
var stringToBase64 = function (str) {
var buffer = Buffer.from(str, 'utf-8');
return buffer.toString('base64');
};
exports.stringToBase64 = stringToBase64;
/**
* Converts a base64 string into a Uint8Array representation
* @param {string} str the base64-encoded string
*/
var base64ToUint8Array = function (str) {
var buffer = Buffer.from(str, 'base64');
return new Uint8Array(buffer);
};
exports.base64ToUint8Array = base64ToUint8Array;
/**
* Converts a Uint8Array into base64 representation
* @param {Uint8Array} data the Uint8Array to be encoded
*/
var uint8ArrayToBase64 = function (data) {
return Buffer.from(data).toString('base64');
};
exports.uint8ArrayToBase64 = uint8ArrayToBase64;
;