incubed
Version:
Typescript-version of the incubed client
123 lines • 4.48 kB
JavaScript
;
/***********************************************************
* This file is part of the Slock.it IoT Layer. *
* The Slock.it IoT Layer contains: *
* - USN (Universal Sharing Network) *
* - INCUBED (Trustless INcentivized remote Node Network) *
************************************************************
* Copyright (C) 2016 - 2018 Slock.it GmbH *
* All Rights Reserved. *
************************************************************
* You may use, distribute and modify this code under the *
* terms of the license contract you have concluded with *
* Slock.it GmbH. *
* For information about liability, maintenance etc. also *
* refer to the contract concluded with Slock.it GmbH. *
************************************************************
* For more information, please refer to https://slock.it *
* For questions, please contact info@slock.it *
***********************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
const cbor = require("cbor");
/**
* turn
*/
function encodeRequests(requests) {
return cbor.encode(requests.map(r => [r.id, r.method, convertToBuffer(r.params), convertToBuffer(r.in3)]));
}
exports.encodeRequests = encodeRequests;
function decodeRequests(request) {
return cbor.decode(request).map(r => ({
jsonrpc: '2.0',
id: r[0],
method: r[1],
params: convertToHex(r[2]),
in3: convertToHex(r[3])
}));
}
exports.decodeRequests = decodeRequests;
function encodeResponses(responses) {
return cbor.encode(responses.map(r => [r.id, convertToBuffer(r.result), r.error, convertToBuffer(r.in3)]));
}
exports.encodeResponses = encodeResponses;
function decodeResponses(responses) {
return cbor.decode(responses).map(r => ({
jsonrpc: '2.0',
id: r[0],
result: convertToHex(r[1]),
error: r[2],
in3: convertToHex(r[3])
}));
}
exports.decodeResponses = decodeResponses;
function convertToBuffer(val) {
switch (typeof val) {
case 'string':
return val.startsWith('0x') ? Buffer.from((val.length % 2 ? '0' : '') + val.substr(2), 'hex') : val;
case 'object':
if (val === null)
return null;
return Array.isArray(val)
? val.map(convertToBuffer)
: Object.keys(val).reduce((p, c) => { p[c] = convertToBuffer(val[c]); return p; }, {});
default:
return val;
}
}
function convertToHex(val) {
if (Buffer.isBuffer(val))
return '0x' + val.toString('hex');
switch (typeof val) {
case 'object':
if (val === null)
return null;
return Array.isArray(val)
? val.map(convertToHex)
: Object.keys(val).reduce((p, c) => { p[c] = convertToHex(val[c]); return p; }, {});
default:
return val;
}
}
function createRefs(val, cache = []) {
switch (typeof val) {
case 'string':
const s = val;
if (s.startsWith('0x')) {
const i = cache.indexOf(s);
if (i >= 0)
return (':' + i);
cache.push(s);
}
return val;
case 'object':
if (val === null)
return null;
return (Array.isArray(val)
? val.map(_ => createRefs(_, cache))
: Object.keys(val).reduce((p, c) => { p[c] = createRefs(val[c], cache); return p; }, {}));
default:
return val;
}
}
exports.createRefs = createRefs;
function resolveRefs(val, cache = []) {
switch (typeof val) {
case 'string':
const s = val;
if (s.startsWith('0x'))
cache.push(s);
if (s.startsWith(':'))
return cache[parseInt(s.substr(1))];
return val;
case 'object':
if (val === null)
return null;
return (Array.isArray(val)
? val.map(_ => resolveRefs(_, cache))
: Object.keys(val).reduce((p, c) => { p[c] = resolveRefs(val[c], cache); return p; }, {}));
default:
return val;
}
}
exports.resolveRefs = resolveRefs;
//# sourceMappingURL=cbor.js.map