Skip to content

Commit 6f53d63

Browse files
committed
Address SME review comments
1 parent 3e5c391 commit 6f53d63

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

_code-samples/verify-credential/js/verify_credential.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ const logger = winston.createLogger({
1414

1515
// Define an error to throw when XRPL lookup fails unexpectedly
1616
class XRPLLookupError extends Error {
17-
constructor(xrplResponse) {
17+
constructor(error) {
1818
super("XRPL look up error");
1919
this.name = "XRPLLookupError";
20-
this.body = xrplResponse.result;
20+
this.body = error;
2121
}
2222
}
2323

2424
const CREDENTIAL_REGEX = /^[0-9A-F]{2,128}$/;
25-
const LSF_ACCEPTED = 0x00010000;
25+
// See https://xrpl.org/docs/references/protocol/ledger-data/ledger-entry-types/credential#credential-flags
26+
// to learn more about the lsfAccepted flag.
27+
const LSF_ACCEPTED = 0x00010000;
2628

2729
async function verifyCredential(client, issuer, subject, credentialType, binary=false) {
2830
/**
@@ -33,18 +35,13 @@ async function verifyCredential(client, issuer, subject, credentialType, binary=
3335
* issuer - Address of the credential issuer, in base58.
3436
* subject - Address of the credential holder/subject, in base58.
3537
* credentialType - Credential type to check for as a string,
36-
* which will be encoded as UTF-8 (1-128 bytes long).
38+
* which will be encoded as UTF-8 (1-128 characters long).
3739
* binary - Specifies that the credential type is provided in hexadecimal format.
3840
* You must provide the credential_type as input.
3941
* Returns True if the account holds the specified, valid credential.
4042
* Returns False if the credential is missing, expired, or not accepted.
4143
*/
4244

43-
// Handle function inputs --------------------------------------------------
44-
if (!credentialType) {
45-
throw new Error("Provide a non-empty credential_type");
46-
}
47-
4845
// Encode credentialType as uppercase hex, if needed
4946
let credentialTypeHex = "";
5047
if (binary) {
@@ -56,7 +53,7 @@ async function verifyCredential(client, issuer, subject, credentialType, binary=
5653

5754
if (credentialTypeHex.length % 2 !== 0 || !CREDENTIAL_REGEX.test(credentialTypeHex)) {
5855
// Hexadecimal is always 2 chars per byte, so an odd length is invalid.
59-
throw new Error("Credential type must be 1-128 bytes as hexadecimal.");
56+
throw new Error("Credential type must be 128 characters as hexadecimal.");
6057
}
6158

6259
// Perform XRPL lookup of Credential ledger entry --------------------------
@@ -80,16 +77,16 @@ async function verifyCredential(client, issuer, subject, credentialType, binary=
8077
logger.info("Credential was not found");
8178
return false;
8279
} else {
83-
// Other errors, for example invalidly pecified addresses.
84-
throw new XRPLLookupError(xrplResponse);
80+
// Other errors, for example invalidly specified addresses.
81+
throw new XRPLLookupError(err?.data || err);
8582
}
8683
}
8784

8885
const credential = xrplResponse.result.node;
8986
logger.info("Found credential:");
9087
logger.info(JSON.stringify(credential, null, 2));
9188

92-
// Confirm that the credential has been accepted ---------------------------
89+
// Check if the credential has been accepted ---------------------------
9390
if (!(credential.Flags & LSF_ACCEPTED)) {
9491
logger.info("Credential is not accepted.");
9592
return false

docs/tutorials/javascript/compliance/verify-credential.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This tutorial uses sample code in Javascript using the [xrpl-js library](../inde
1717

1818
## Prerequisites
1919

20-
- 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.
20+
- 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.
2121
- You should have a basic understanding of the XRP Ledger.
2222
- 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.
2323
- For sample code showing how to create credentials, see [Build a Credential Issuing Service](../build-apps/credential-issuing-service.md).
@@ -253,27 +253,27 @@ Then it defines a type of exception to throw if something goes wrong when connec
253253

254254
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Define an error to throw" before="const CREDENTIAL" /%}
255255

256-
Finally, a regular expression to validate the credential format and the `lsfAccepted` flag are defined as constants for use further on in the code.
256+
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.
257257

258258
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="const CREDENTIAL" before="async function verifyCredential" /%}
259259

260260
### 2. verifyCredential function
261261

262262
The `verifyCredential(...)` function performs the main work for this tutorial. The function definition and comments define the parameters:
263263

264-
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="async function verifyCredential" before="// Handle function inputs" /%}
264+
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="async function verifyCredential" before="// Encode credentialType as uppercase hex" /%}
265265

266-
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:
266+
The XRP Ledger APIs require the credential type to be hexadecimal, so it converts the user input if necessary:
267267

268-
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Handle function inputs" before="// Perform XRPL lookup" /%}
268+
{% code-snippet file="/_code-samples/verify-credential/js/verify_credential.js" language="js" from="// Encode credentialType as uppercase hex" before="// Perform XRPL lookup" /%}
269269

270270
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:
271271

272-
{% 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" /%}
272+
{% 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" /%}
273273

274274
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.
275275

276-
{% 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" /%}
276+
{% 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" /%}
277277

278278
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:
279279

0 commit comments

Comments
 (0)