mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 11:45:50 +00:00
Address SME review comments
This commit is contained in:
@@ -14,14 +14,16 @@ const logger = winston.createLogger({
|
||||
|
||||
// Define an error to throw when XRPL lookup fails unexpectedly
|
||||
class XRPLLookupError extends Error {
|
||||
constructor(xrplResponse) {
|
||||
constructor(error) {
|
||||
super("XRPL look up error");
|
||||
this.name = "XRPLLookupError";
|
||||
this.body = xrplResponse.result;
|
||||
this.body = error;
|
||||
}
|
||||
}
|
||||
|
||||
const CREDENTIAL_REGEX = /^[0-9A-F]{2,128}$/;
|
||||
// See https://xrpl.org/docs/references/protocol/ledger-data/ledger-entry-types/credential#credential-flags
|
||||
// to learn more about the lsfAccepted flag.
|
||||
const LSF_ACCEPTED = 0x00010000;
|
||||
|
||||
async function verifyCredential(client, issuer, subject, credentialType, binary=false) {
|
||||
@@ -33,18 +35,13 @@ async function verifyCredential(client, issuer, subject, credentialType, binary=
|
||||
* issuer - Address of the credential issuer, in base58.
|
||||
* subject - Address of the credential holder/subject, in base58.
|
||||
* credentialType - Credential type to check for as a string,
|
||||
* which will be encoded as UTF-8 (1-128 bytes long).
|
||||
* which will be encoded as UTF-8 (1-128 characters long).
|
||||
* binary - Specifies that the credential type is provided in hexadecimal format.
|
||||
* You must provide the credential_type as input.
|
||||
* Returns True if the account holds the specified, valid credential.
|
||||
* Returns False if the credential is missing, expired, or not accepted.
|
||||
*/
|
||||
|
||||
// Handle function inputs --------------------------------------------------
|
||||
if (!credentialType) {
|
||||
throw new Error("Provide a non-empty credential_type");
|
||||
}
|
||||
|
||||
// Encode credentialType as uppercase hex, if needed
|
||||
let credentialTypeHex = "";
|
||||
if (binary) {
|
||||
@@ -56,7 +53,7 @@ async function verifyCredential(client, issuer, subject, credentialType, binary=
|
||||
|
||||
if (credentialTypeHex.length % 2 !== 0 || !CREDENTIAL_REGEX.test(credentialTypeHex)) {
|
||||
// Hexadecimal is always 2 chars per byte, so an odd length is invalid.
|
||||
throw new Error("Credential type must be 1-128 bytes as hexadecimal.");
|
||||
throw new Error("Credential type must be 128 characters as hexadecimal.");
|
||||
}
|
||||
|
||||
// Perform XRPL lookup of Credential ledger entry --------------------------
|
||||
@@ -80,8 +77,8 @@ async function verifyCredential(client, issuer, subject, credentialType, binary=
|
||||
logger.info("Credential was not found");
|
||||
return false;
|
||||
} else {
|
||||
// Other errors, for example invalidly pecified addresses.
|
||||
throw new XRPLLookupError(xrplResponse);
|
||||
// Other errors, for example invalidly specified addresses.
|
||||
throw new XRPLLookupError(err?.data || err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +86,7 @@ async function verifyCredential(client, issuer, subject, credentialType, binary=
|
||||
logger.info("Found credential:");
|
||||
logger.info(JSON.stringify(credential, null, 2));
|
||||
|
||||
// Confirm that the credential has been accepted ---------------------------
|
||||
// Check if the credential has been accepted ---------------------------
|
||||
if (!(credential.Flags & LSF_ACCEPTED)) {
|
||||
logger.info("Credential is not accepted.");
|
||||
return false
|
||||
|
||||
@@ -17,7 +17,7 @@ This tutorial uses sample code in Javascript using the [xrpl-js library](../inde
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- You must have Node.js insalled and know how to run Javascript code from the command line. Node.js v18 is required for xrpl.js.
|
||||
- You must have Node.js installed and know how to run Javascript code from the command line. Node.js v18 is required for xrpl.js.
|
||||
- You should have a basic understanding of the XRP Ledger.
|
||||
- The credential you want to verify should exist in the ledger already, and you should know the addresses of both the issuer and the holder, as well as the official credential type you want to check.
|
||||
- For sample code showing how to create credentials, see [Build a Credential Issuing Service](../build-apps/credential-issuing-service.md).
|
||||
@@ -253,7 +253,7 @@ Then it defines a type of exception to throw if something goes wrong when connec
|
||||
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Define an error to throw" before="const CREDENTIAL" /%}
|
||||
|
||||
Finally, a regular expression to validate the credential format and the `lsfAccepted` flag are defined as constants for use further on in the code.
|
||||
Finally, a regular expression to validate the credential format and the [lsfAccepted](../../../references/protocol/ledger-data/ledger-entry-types/credential.md#credential-flags) flag are defined as constants for use further on in the code.
|
||||
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="const CREDENTIAL" before="async function verifyCredential" /%}
|
||||
|
||||
@@ -261,19 +261,19 @@ Finally, a regular expression to validate the credential format and the `lsfAcce
|
||||
|
||||
The `verifyCredential(...)` function performs the main work for this tutorial. The function definition and comments define the parameters:
|
||||
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="async function verifyCredential" before="// Handle function inputs" /%}
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="async function verifyCredential" before="// Encode credentialType as uppercase hex" /%}
|
||||
|
||||
The first thing the function does is verify that a credential type is provided. The XRP Ledger APIs require the credential type to be hexadecimal, so it converts the user input if necessary:
|
||||
The XRP Ledger APIs require the credential type to be hexadecimal, so it converts the user input if necessary:
|
||||
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Handle function inputs" before="// Perform XRPL lookup" /%}
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Encode credentialType as uppercase hex" before="// Perform XRPL lookup" /%}
|
||||
|
||||
Next, it calls the [ledger_entry method](/docs/references/http-websocket-apis/public-api-methods/ledger-methods/ledger_entry#get-credential-entry) to look up the requested Credential ledger entry:
|
||||
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Perform XRPL lookup" before="// Confirm that the credential has been accepted" /%}
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Perform XRPL lookup" before="// Check if the credential has been accepted" /%}
|
||||
|
||||
If it succeeds in finding the credential, the function continues by checking that the credential has been accepted by its holder. Since anyone can issue a credential to anyone else, a credential is only considered valid if its subject has accepted it.
|
||||
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Confirm that the credential has been accepted" before="// Confirm that the credential is not expired" /%}
|
||||
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="Check if the credential has been accepted" before="// Confirm that the credential is not expired" /%}
|
||||
|
||||
Then, if the credential has an expiration time, the function checks that the credential is not expired. If the credential has no expiration, this step can be skipped. A credential is officially considered expired if its expiration time is before the [official close time](/docs/concepts/ledgers/ledger-close-times) of the most recently validated ledger. This is more universal than comparing the expiration to your own local clock. Thus, the code uses the [ledger method][] to look up the most recently validated ledger:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user