|
| 1 | +import base32Decode from "base32-decode"; |
| 2 | +import { decode as cborDecode } from "cbor-x"; |
| 3 | +import pako from "pako"; |
| 4 | +import * as base64 from "@stablelib/base64"; |
| 5 | + |
| 6 | +const urlOrCompact = process.argv[2]; |
| 7 | +const index = process.argv[3]; |
| 8 | + |
| 9 | +if (!urlOrCompact) { |
| 10 | + console.error("No URL or compact credential provided."); |
| 11 | + process.exit(); |
| 12 | +} |
| 13 | + |
| 14 | +if ( |
| 15 | + !urlOrCompact.startsWith("http") && |
| 16 | + !urlOrCompact.startsWith("CSC") && |
| 17 | + !urlOrCompact.startsWith("CSS") |
| 18 | +) { |
| 19 | + console.error("Invalid argument provided. Neither URL, nor encoded compact credential."); |
| 20 | + process.exit(); |
| 21 | +} |
| 22 | + |
| 23 | +if (urlOrCompact.startsWith("http") && !index) { |
| 24 | + console.error("No index provided for revocation list URL."); |
| 25 | + process.exit(); |
| 26 | +} |
| 27 | + |
| 28 | +function toBitArray(input) { |
| 29 | + const bitArray = []; |
| 30 | + |
| 31 | + for (let byte of input) { |
| 32 | + let bits = byte.toString(2); |
| 33 | + for (let bit of bits) { |
| 34 | + bitArray.push(Number(bit)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return bitArray; |
| 39 | +} |
| 40 | + |
| 41 | +// CWT credentials are base32 encoded CBOR web tokens |
| 42 | +// Revocable CWT credentials contain a reference to the |
| 43 | +// public revocation list and their index in that list. |
| 44 | +function retrieveRevocationUrlFromCompact(compact) { |
| 45 | + const decoded = base32Decode(compact.slice(7), "RFC4648"); |
| 46 | + const cwt = cborDecode(Buffer.from(decoded)); |
| 47 | + const payload = cborDecode(cwt.value[2]); |
| 48 | + |
| 49 | + const status = payload["-65537"]; |
| 50 | + const index = status["2"]; |
| 51 | + const listUrl = status["3"]; |
| 52 | + |
| 53 | + return { listUrl, index }; |
| 54 | +} |
| 55 | + |
| 56 | +// The public revocation list endpoint for CWT credentials |
| 57 | +// returns a CBOR web token that includes the gzip compressed |
| 58 | +// revocation list as a bitstring. |
| 59 | +async function isCompactRevoked(url, index) { |
| 60 | + let response; |
| 61 | + try { |
| 62 | + response = await fetch(url); |
| 63 | + if (!response.ok) { |
| 64 | + console.error("Can not fetch revocation list from provided URL.") |
| 65 | + process.exit() |
| 66 | + } |
| 67 | + } catch (_) { |
| 68 | + console.error("Fetching revocation list from URL failed.") |
| 69 | + process.exit() |
| 70 | + } |
| 71 | + const bytes = await response.arrayBuffer(); |
| 72 | + |
| 73 | + const cwt = cborDecode(Buffer.from(bytes)); |
| 74 | + const payload = cborDecode(cwt.value[2]); |
| 75 | + |
| 76 | + const compressedList = payload["-65538"]; |
| 77 | + const list = pako.ungzip(compressedList); |
| 78 | + const revocationList = toBitArray(list); |
| 79 | + |
| 80 | + return revocationList[index] === 1; |
| 81 | +} |
| 82 | + |
| 83 | +// The public revocation list endpoint for JSON credentials |
| 84 | +// returns a revocation credential that includes the encoded revocation |
| 85 | +// list. The list is a base64url encoded and gzip compressed bitstring. |
| 86 | +async function isWebSemanticRevoked(url, index) { |
| 87 | + let response; |
| 88 | + try { |
| 89 | + response = await fetch(url); |
| 90 | + if (!response.ok) { |
| 91 | + console.error("Can not fetch revocation list from provided URL.") |
| 92 | + process.exit() |
| 93 | + } |
| 94 | + } catch (_) { |
| 95 | + console.error("Fetching revocation list from URL failed.") |
| 96 | + process.exit() |
| 97 | + } |
| 98 | + |
| 99 | + const revocationCredential = await response.json(); |
| 100 | + const { encodedList } = revocationCredential.credentialSubject; |
| 101 | + |
| 102 | + const list = pako.ungzip(base64.decodeURLSafe(encodedList)); |
| 103 | + const listBitArray = toBitArray(list); |
| 104 | + |
| 105 | + return listBitArray[index] === 1; |
| 106 | +} |
| 107 | + |
| 108 | +let isRevoked; |
| 109 | +if (urlOrCompact.startsWith("CSC") || urlOrCompact.startsWith("CSS")) { |
| 110 | + console.log(`\n[Credential Type] ${urlOrCompact.startsWith("CSS") ? "Semantic " : ""}CWT (encoded)`); |
| 111 | + const { listUrl, index } = retrieveRevocationUrlFromCompact(urlOrCompact); |
| 112 | + isRevoked = await isCompactRevoked(listUrl, index); |
| 113 | +} else if (urlOrCompact.includes("compact")) { |
| 114 | + console.log(`\n[Credential Type] ${urlOrCompact.includes("semantic") ? "Semantic " : ""}CWT`); |
| 115 | + isRevoked = await isCompactRevoked(urlOrCompact, index); |
| 116 | +} else if (urlOrCompact.includes("web-semantic")) { |
| 117 | + console.log("\n[Credential Type] JSON"); |
| 118 | + isRevoked = await isWebSemanticRevoked(urlOrCompact, index); |
| 119 | +} else { |
| 120 | + console.error("Can not identify credential type."); |
| 121 | + process.exit() |
| 122 | +} |
| 123 | + |
| 124 | +console.log(`[Revocation status] ${isRevoked ? "Revoked" : "Not revoked"}`); |
0 commit comments