@blockcerts/hashlink-verifier
Version:
Handling hashlinks in Blockcerts
136 lines (135 loc) • 6.11 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HashlinkVerifier = void 0;
const Hashlink_1 = require("./hashlink/Hashlink");
const codecs = __importStar(require("./hashlink/codecs"));
class HashlinkVerifier {
constructor() {
this.hashlinkTable = {}; // keep decoded hashlinks in memory
this.hl = new Hashlink_1.Hashlink();
this.hl.use(new codecs.MultihashSha2256());
this.hl.use(new codecs.MultihashBlake2b64());
this.hl.use(new codecs.MultibaseBase58btc());
}
/**
* decode method, abstract wrapper over Hashlink class from digital bazaar hashlink package
*
* @param {string} hashlink: the hashlink to be decoded. In this instance it expects a url to be specified.
* @param {function} onHashlinkUrlDecoded: a callback function called when the source url has been discovered to enable
* early manipulation (ie: update image in DOM).
*/
decode(hashlink, onHashlinkUrlDecoded) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const decodedHashlink = yield this.hl.decode({ hashlink });
// console.log('hashlink decoded', decodedHashlink);
if (!decodedHashlink.meta && !((_a = decodedHashlink.meta.url) === null || _a === void 0 ? void 0 : _a.length)) {
throw new Error('unparseable document, no url provided as meta data');
}
this.hashlinkTable[hashlink] = decodedHashlink;
const sourceUrl = decodedHashlink.meta.url[0];
onHashlinkUrlDecoded === null || onHashlinkUrlDecoded === void 0 ? void 0 : onHashlinkUrlDecoded(sourceUrl);
return decodedHashlink;
});
}
verifyHashlinkTable() {
return __awaiter(this, void 0, void 0, function* () {
const hashlinks = Object.keys(this.hashlinkTable);
for (let i = 0; i < hashlinks.length; i++) {
yield this.verify(hashlinks[i])
.catch(error => console.log('caught an hashlink exception', error));
}
return true;
});
}
/**
* verify method, abstract wrapper over Hashlink class from digital bazaar hashlink package
*
* @param {string} hashlink: the hashlink to be decoded. It will lookup in the previously decoded hashlinks table.
* if not found it will decode the hashlink before verification.
*/
verify(hashlink) {
return __awaiter(this, void 0, void 0, function* () {
const sourceUrl = yield this.getSourceUrlFromHashlink(hashlink);
let imageData;
yield fetch(sourceUrl)
.then((response) => __awaiter(this, void 0, void 0, function* () { return yield response.text(); }))
.then(data => {
imageData = data;
});
const textEncoder = new TextEncoder();
const verified = yield this.hl.verify({
data: textEncoder.encode(imageData), // needs an ArrayBuffer
hashlink
});
if (verified) {
console.log(`hashlink ${hashlink} bound to ${sourceUrl} was successfully verified`);
}
else {
throw new Error(`Hashlink ${hashlink} does not match data from url ${sourceUrl}`);
}
return verified;
});
}
hasHashlinksToVerify() {
return Object.keys(this.hashlinkTable).length > 0;
}
getSourceUrlFromHashlink(hashlink) {
return __awaiter(this, void 0, void 0, function* () {
let decodedHashlink;
if (this.hashlinkTable[hashlink]) {
decodedHashlink = this.hashlinkTable[hashlink];
}
else {
decodedHashlink = yield this.decode(hashlink);
}
return this.getMetaUrl(decodedHashlink);
});
}
getMetaUrl(decodedHashlink) {
return decodedHashlink.meta.url[0];
}
}
exports.HashlinkVerifier = HashlinkVerifier;