|
| 1 | +import { |
| 2 | + isoTimeToRippleTime, |
| 3 | + rippleTimeToISOTime, |
| 4 | + isValidClassicAddress, |
| 5 | +} from "xrpl"; |
| 6 | +import { stringToHex, hexToString } from "@xrplf/isomorphic/dist/utils/index.js"; |
| 7 | + |
| 8 | +import { ValueError } from "./errors.js"; |
| 9 | + |
| 10 | +// Regex constants |
| 11 | +const CREDENTIAL_REGEX = /^[A-Za-z0-9_.-]{1,128}$/; |
| 12 | +const URI_REGEX = /^[A-Za-z0-9\-._~:/?#\[\]@!$&'()*+,;=%]{1,256}$/; |
| 13 | + |
| 14 | +/** |
| 15 | + * Validate credential request. |
| 16 | + * This function performs parameter validation. Validated fields: |
| 17 | + * - subject (required): the subject of the credential, as a classic address |
| 18 | + * - credential (required): the credential type, in human-readable (ASCII) chars |
| 19 | + * - uri (optional): URI of the credential in human-readable (ASCII) chars |
| 20 | + * - expiration (optional): time when the credential expires (displayed as an ISO 8601 format string in JSON) |
| 21 | + */ |
| 22 | +export function validateCredentialRequest({ subject, credential, uri, expiration }) { |
| 23 | + // Validate subject |
| 24 | + if (typeof subject !== "string") { |
| 25 | + throw new ValueError("Must provide a string 'subject' field"); |
| 26 | + } |
| 27 | + if (!isValidClassicAddress(subject)) { |
| 28 | + throw new ValueError(`subject not valid address: '${subject}'`); |
| 29 | + } |
| 30 | + |
| 31 | + // Validate credential |
| 32 | + if (typeof credential !== "string") { |
| 33 | + throw new ValueError("Must provide a string 'credential' field"); |
| 34 | + } |
| 35 | + if (!CREDENTIAL_REGEX.test(credential)) { |
| 36 | + /** |
| 37 | + * Checks if the specified credential type is one that this service issues. |
| 38 | + * XRPL credential types can be any binary data; this service issues |
| 39 | + * any credential that can be encoded from the following ASCII chars: |
| 40 | + * alphanumeric characters, underscore, period, and dash. (min length 1, max 128) |
| 41 | + * |
| 42 | + * You might want to further limit the credential types, depending on your |
| 43 | + * use case; for example, you might only issue one specific credential type. |
| 44 | + */ |
| 45 | + throw new ValueError(`credential not allowed: '${credential}'.`); |
| 46 | + } |
| 47 | + |
| 48 | + /* |
| 49 | + (Optional) Checks if the specified URI is acceptable for this service. |
| 50 | + |
| 51 | + XRPL Credentials' URI values can be any binary data; this service |
| 52 | + adds any user-requested URI to a Credential as long as the URI |
| 53 | + can be encoded from the characters usually allowed in URIs, namely |
| 54 | + the following ASCII chars: |
| 55 | +
|
| 56 | + alphanumeric characters (upper and lower case) |
| 57 | + the following symbols: -._~:/?#[]@!$&'()*+,;=% |
| 58 | + (minimum length 1 and max length 256 chars) |
| 59 | +
|
| 60 | + You might want to instead define your own URI and attach it to the |
| 61 | + Credential regardless of user input, or you might want to verify that the |
| 62 | + URI points to a valid Verifiable Credential document that matches the user. |
| 63 | + */ |
| 64 | + if (uri !== undefined) { |
| 65 | + if (typeof uri !== "string" || !URI_REGEX.test(uri)) { |
| 66 | + throw new ValueError(`URI isn't valid: ${uri}`); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Validate and parse expiration |
| 71 | + let parsedExpiration; |
| 72 | + if (expiration !== undefined) { |
| 73 | + if (typeof expiration !== "string") { |
| 74 | + throw new ValueError(`Unsupported expiration format: ${typeof expiration}`); |
| 75 | + } |
| 76 | + parsedExpiration = new Date(expiration); |
| 77 | + if (isNaN(parsedExpiration.getTime())) { |
| 78 | + throw new ValueError(`Invalid expiration date: ${expiration}`); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + subject, |
| 84 | + credential, |
| 85 | + uri, |
| 86 | + expiration: parsedExpiration, |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +// Convert an XRPL ledger entry into a usable credential object |
| 91 | +export function credentialFromXrpl(entry) { |
| 92 | + const { Subject, CredentialType, URI, Expiration, Flags } = entry; |
| 93 | + return { |
| 94 | + subject: Subject, |
| 95 | + credential: hexToString(CredentialType), |
| 96 | + uri: URI ? hexToString(URI) : undefined, |
| 97 | + expiration: Expiration ? rippleTimeToISOTime(Expiration) : undefined, |
| 98 | + accepted: Boolean(Flags & 0x00010000), // lsfAccepted |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +// Convert to an object in a format closer to the XRP Ledger representation |
| 103 | +export function credentialToXrpl(cred) { |
| 104 | + // Credential type and URI are hexadecimal; |
| 105 | + // Expiration, if present, is in seconds since the Ripple Epoch. |
| 106 | + return { |
| 107 | + subject: cred.subject, |
| 108 | + credential: stringToHex(cred.credential), |
| 109 | + uri: cred.uri ? stringToHex(cred.uri) : undefined, |
| 110 | + expiration: cred.expiration |
| 111 | + ? isoTimeToRippleTime(cred.expiration) |
| 112 | + : undefined, |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +export function verifyDocuments({ documents }) { |
| 118 | + /** |
| 119 | + * This is where you would check the user's documents to see if you |
| 120 | + * should issue the requested Credential to them. |
| 121 | + * Depending on the type of credentials your service needs, you might |
| 122 | + * need to implement different types of checks here. |
| 123 | + */ |
| 124 | + if (typeof documents !== "object" || Object.keys(documents).length === 0) { |
| 125 | + throw new ValueError("you must provide a non-empty 'documents' field"); |
| 126 | + } |
| 127 | + |
| 128 | + // As a placeholder, this example checks that the documents field |
| 129 | + // contains a string field named "reason" containing the word "please". |
| 130 | + const reason = documents.reason; |
| 131 | + if (typeof reason !== "string") { |
| 132 | + throw new ValueError("documents must contain a 'reason' string"); |
| 133 | + } |
| 134 | + |
| 135 | + if (!reason.toLowerCase().includes("please")) { |
| 136 | + throw new ValueError("reason must include 'please'"); |
| 137 | + } |
| 138 | +} |
0 commit comments