@tgrospic/rnode-grpc-js
Version:
RNode gRPC helpers
121 lines • 4.4 kB
JavaScript
;
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rhoParToJson = void 0;
/**
* Converts `Par` object to JSON returned from the RNode gRPC API.
*
* Example of Rholang return values.
* ```scala
* new return(`rho:rchain:deployId`) in {
* return!("One argument") | // monadic
* return!((true, "A", "B")) | // monadic as tuple
* return!(true, "A", "B") | // polyadic
* return!(true) | return!("A") // multiple values
* }
* ```
* And the corresponding converted JSON objects.
*
* ```js
* [ 'One argument' ] // return!("One argument")
* [ [ true, 'A', 'B' ] ] // return!((true, "A", "B"))
* [ true, 'A', 'B' ] // return!(true, "A", "B")
* [ true, 'A' ] // return!(true) | return!("A")
* ```
*/
// TODO: make it stack safe
var rhoParToJson = function (input) {
var _a, _b;
if (input === void 666 || input === null)
return input;
if (!!((_a = input.exprsList) === null || _a === void 0 ? void 0 : _a.length)) {
var vals = input.exprsList.map(rhoExprToJson);
return vals.length === 1 ? vals[0] : vals;
}
else if (!!((_b = input.unforgeablesList) === null || _b === void 0 ? void 0 : _b.length)) {
var vals = input.unforgeablesList.map(rhoUnforgeableToJson);
return vals.length === 1 ? vals[0] : vals;
}
else {
// For all other types of fields return undefined
// (not obvious representation in JSON)
return void 666;
}
};
exports.rhoParToJson = rhoParToJson;
var rhoExprToJson = function (x) {
var _a, _b, _c, _d;
// First check if complex fields can be deserialized
if (x.eListBody !== void 666) {
return (_a = x.eListBody.psList) === null || _a === void 0 ? void 0 : _a.map(exports.rhoParToJson);
}
else if (x.eSetBody !== void 666) {
return (_b = x.eSetBody.psList) === null || _b === void 0 ? void 0 : _b.map(exports.rhoParToJson);
}
else if (x.eTupleBody !== void 666) {
return (_c = x.eTupleBody.psList) === null || _c === void 0 ? void 0 : _c.map(exports.rhoParToJson);
}
else if (x.eMapBody !== void 666) {
return (_d = x.eMapBody.kvsList) === null || _d === void 0 ? void 0 : _d.reduce(function (acc, _a) {
var _b;
var key = _a.key, value = _a.value;
// Get key from Par
var kRaw = (0, exports.rhoParToJson)(key);
// Convert binary key to hex string
var k = kRaw instanceof Uint8Array ? uint8ArrayToHex(kRaw) : kRaw;
// Get value from Par
var v = (0, exports.rhoParToJson)(value);
// Skip key if undefined (not obvious representation in JSON)
return k === void 666 || k === null ? acc : __assign(__assign({}, acc), (_b = {}, _b[k] = v, _b));
}, {});
// Only non-default primitives can be deserialized
}
else if (x.gBool !== void 666) {
return x.gBool;
}
else if (x.gInt !== void 666) {
return x.gInt;
}
else if (x.gString !== void 666) {
return x.gString;
}
else if (x.gUri !== void 666) {
return x.gUri;
}
else if (x.gByteArray !== void 666) {
return base64ToUint8Array(x.gByteArray);
}
else {
// For all other types of fields return undefined
// (not obvious representation in JSON)
return void 666;
}
};
var rhoUnforgeableToJson = function (x) {
if (x.gDeployIdBody !== void 666) {
return base64ToUint8Array(x.gDeployIdBody.sig);
}
else if (x.gDeployerIdBody !== void 666) {
return base64ToUint8Array(x.gDeployerIdBody.publickey);
}
else if (x.gPrivateBody !== void 666) {
return base64ToUint8Array(x.gPrivateBody.id);
}
};
var uint8ArrayToHex = function (x) {
return Buffer.from(x).toString('hex');
};
var base64ToUint8Array = function (x) {
return Uint8Array.from(Buffer.from(x, 'base64'));
};
//# sourceMappingURL=rho-json.js.map