UNPKG

@blockcerts/hashlink-verifier

Version:
169 lines (168 loc) 6.6 kB
/*! * Copyright (c) 2019 Digital Bazaar, Inc. All rights reserved. */ 'use strict'; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MultibaseBase58btc = exports.MultihashBlake2b64 = exports.MultihashSha2256 = exports.Codec = void 0; // @ts-expect-error not a TS package const base58 = __importStar(require("base58-universal")); const blakejs_1 = require("blakejs"); const crypto_1 = __importDefault(require("./crypto")); const util_1 = require("./util"); class Codec { } exports.Codec = Codec; class MultihashSha2256 extends Codec { /** * Creates a new MultihashSha2256 data codec. * * @returns {MultihashSha2256} A MultihashSha2256 used to encode and decode * Multihash SHA-2 256-bit values. */ constructor() { super(); this.identifier = new Uint8Array([0x12, 0x20]); this.algorithm = 'mh-sha2-256'; this.name = 'sha2-256'; } /** * Encoder that takes a Uint8Array as input and performs a SHA-2 * cryptographic hash on the data and outputs a multihash-encoded value. * * @param {Uint8Array} input - The input for the encode function. * * @returns {Uint8Array} The output of the encode function. */ encode(input) { return __awaiter(this, void 0, void 0, function* () { const sha2256 = new Uint8Array(yield crypto_1.default.subtle.digest({ name: 'SHA-256' }, input)); const mhsha2256 = new Uint8Array(sha2256.byteLength + this.identifier.byteLength); mhsha2256.set(this.identifier, 0); mhsha2256.set(sha2256, this.identifier.byteLength); return mhsha2256; }); } decode(input) { return input.slice(this.identifier.length); } } exports.MultihashSha2256 = MultihashSha2256; class MultihashBlake2b64 extends Codec { /** * Creates a new MultihashBlake2b64 data codec. * * @returns {MultihashBlake2b64} A MultihashBlake2b64 used to encode and * decode Multihash Blake2b 64-bit values. */ constructor() { super(); this.identifier = new Uint8Array([0xb2, 0x08, 0x08]); this.algorithm = 'mh-blake2b-64'; this.name = 'blake2b-64'; } /** * Encoder function that takes a Uint8Array as input and performs a blake2b * cryptographic hash on the data and outputs a multihash-encoded value. * * @param {Uint8Array} input - The input for the encode function. * * @returns {Uint8Array} The output of the encode function. */ encode(input) { return __awaiter(this, void 0, void 0, function* () { const blake2b64 = (0, blakejs_1.blake2b)(input, null, 8); const mhblake2b64 = new Uint8Array(blake2b64.byteLength + this.identifier.byteLength); mhblake2b64.set(this.identifier, 0); mhblake2b64.set(blake2b64, this.identifier.byteLength); return mhblake2b64; }); } decode(input) { return input.slice(this.identifier.length); } } exports.MultihashBlake2b64 = MultihashBlake2b64; class MultibaseBase58btc extends Codec { /** * Creates a new MultibaseBase58btc data codec. * * @returns {MultibaseBase58btc} A MultibaseBase58btc used to encode and * decode Multibase base58btc values. */ constructor() { super(); this.identifier = new Uint8Array([0x7a]); this.algorithm = 'mb-base58-btc'; this.name = 'base58-btc'; } /** * Encoder function that takes a Uint8Array as input and performs a multibase * base58btc encoding on the data. * * @param {Uint8Array} input - The input for the encode function. * * @returns {Uint8Array} The output of the encode function. */ encode(input) { return __awaiter(this, void 0, void 0, function* () { return new Uint8Array((0, util_1.stringToUint8Array)('z' + base58.encode(input))); }); } /** * Decoder function that takes a Uint8Array as input and performs a multibase * base58btc decode on the data. * * @param {Uint8Array} input - The input for the decode function. * * @returns {Uint8Array} The output of the decode function. */ decode(input) { return base58.decode(new TextDecoder('utf-8').decode(input.slice(1))); } } exports.MultibaseBase58btc = MultibaseBase58btc;