jsonld-signatures-merkleproof2019
Version:
A jsonld signature implementation to support MerkleProof2019 verification in Verifiable Credential context
42 lines (41 loc) • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getChain;
const string_1 = require("../utils/string");
const explorer_lookup_1 = require("@blockcerts/explorer-lookup");
// merkleRoot2019: see https://w3c-dvcg.github.io/lds-merkle-proof-2019/#blockchain-keymap
function getMerkleRoot2019Chain(anchor) {
const dataArray = anchor.split(':');
let mainChain;
switch (dataArray[1]) {
case explorer_lookup_1.BLOCKCHAINS.mocknet.blinkCode:
return getChainObject(explorer_lookup_1.BLOCKCHAINS.mocknet.signatureValue);
case explorer_lookup_1.BLOCKCHAINS.bitcoin.blinkCode:
mainChain = explorer_lookup_1.BLOCKCHAINS.bitcoin.name;
break;
case explorer_lookup_1.BLOCKCHAINS.ethmain.blinkCode:
mainChain = explorer_lookup_1.BLOCKCHAINS.ethmain.name;
break;
default:
throw new Error('Could not retrieve chain.');
}
const network = dataArray[2];
const chainCodeSignatureValue = mainChain.toLowerCase() + (0, string_1.capitalize)(network);
return getChainObject(chainCodeSignatureValue);
}
function getChainObject(chainCodeProofValue) {
const chainObject = Object.keys(explorer_lookup_1.BLOCKCHAINS)
.map(key => explorer_lookup_1.BLOCKCHAINS[key])
.find((entry) => entry.signatureValue === chainCodeProofValue);
return chainObject;
}
function getChain(proof = null) {
if (proof === null || proof === void 0 ? void 0 : proof.anchors) {
const { anchors } = proof;
const anchor = anchors[0];
if (typeof anchor === 'string') {
return getMerkleRoot2019Chain(anchor);
}
}
return null;
}