UNPKG

keybase-proofs

Version:

Publicly-verifiable proofs of identity

161 lines (147 loc) 4.17 kB
// Generated by IcedCoffeeScript 108.0.12 (function() { var ExpansionError, check_expansion_kv, check_expansions, createHmac, hmac_obj, json_at_path, json_stringify_sorted, katch, parse3, pgp_utils, prng, triplesec, _ref, _ref1; parse3 = require('./parse3'); pgp_utils = require('pgp-utils'); _ref = pgp_utils.util, katch = _ref.katch, json_stringify_sorted = _ref.json_stringify_sorted; triplesec = require('triplesec'); ExpansionError = require('./errors').errors.ExpansionError; _ref1 = require('crypto'), prng = _ref1.prng, createHmac = _ref1.createHmac; check_expansions = function(_arg) { var expansions, k, v, _results; expansions = _arg.expansions; _results = []; for (k in expansions) { v = expansions[k]; _results.push(check_expansion_kv({ k: k, v: v })); } return _results; }; exports.hmac_obj = hmac_obj = function(_arg) { var hmac, key, obj, s; obj = _arg.obj, key = _arg.key; hmac = createHmac("sha256", key); s = json_stringify_sorted(obj); return hmac.update(Buffer.from(s, 'ascii')).digest(); }; check_expansion_kv = function(_arg) { var hmac_computed, k, s, v; k = _arg.k, v = _arg.v; if (!parse3.is_hex(k, 32)) { throw new ExpansionError("bad hash: " + k); } if (!parse3.is_hex(v.key, 16)) { throw new ExpansionError("bad hmac key: " + v[1]); } s = JSON.stringify(v.obj); if (!/^[\x20-\x7e]+$/.test(s)) { throw new ExpansionError("JSON stub has non-ASCII"); } hmac_computed = hmac_obj({ obj: v.obj, key: Buffer.from(v.key, 'hex') }).toString('hex'); if (hmac_computed !== k) { throw new ExpansionError("hashcheck failure in stub import"); } }; exports.expand_json = function(_arg) { var expansions, found, json, k, xform; json = _arg.json, expansions = _arg.expansions; expansions || (expansions = {}); if (Object.keys(expansions).length === 0) { return json; } check_expansions({ expansions: expansions }); found = {}; xform = function(o) { var expansion, k, ret, v, _i, _len; if (o == null) { return o; } if (typeof o === 'string' && ((expansion = expansions[o]) != null)) { found[o] = true; return expansion.obj; } if (typeof o !== 'object') { return o; } if (Array.isArray(o)) { ret = []; for (_i = 0, _len = o.length; _i < _len; _i++) { v = o[_i]; ret.push(xform(v)); } return ret; } ret = {}; for (k in o) { v = o[k]; ret[k] = xform(v); } return ret; }; json = xform(json); for (k in expansions) { if (!found[k]) { throw new ExpansionError("Did not find expansion for " + k); } } return json; }; json_at_path = function(_arg) { var c, components, json, last_component, path, prev, repl, _i, _len; json = _arg.json, path = _arg.path, repl = _arg.repl; components = path.split(/\./); prev = null; last_component = null; for (_i = 0, _len = components.length; _i < _len; _i++) { c = components[_i]; prev = json; last_component = c; json = json[c]; if (json == null) { return null; } } if (repl != null) { if ((prev == null) || (last_component == null)) { throw new ExpansionError("failed to replace at " + path); } prev[last_component] = repl; } return json; }; exports.stub_json = function(_arg) { var expansions, h, json, key, obj, path; json = _arg.json, expansions = _arg.expansions, path = _arg.path; obj = json_at_path({ json: json, path: path }); if (obj == null) { return null; } obj = JSON.parse(JSON.stringify(obj)); key = prng(16); h = hmac_obj({ obj: obj, key: key }).toString('hex'); expansions[h] = { obj: obj, key: key.toString('hex') }; json_at_path({ json: json, path: path, repl: h }); return null; }; }).call(this);