You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial demonstrates how to build and use a microservice that issues [Credentials](../../../concepts/decentralized-storage/credentials.md) on the XRP Ledger, in the form of a RESTlike API, using the [Express](https://expressjs.com/) framework for Node.js.
11
11
12
12
13
+
## Prerequisites
14
+
15
+
To complete this tutorial, you should meet the following guidelines:
16
+
17
+
- You have [Node.js](https://nodejs.org/en/download/) v18 or higher installed.
18
+
- You are somewhat familiar with modern JavaScript programming and have completed the [Get Started Using JavaScript tutorial](./get-started.md).
19
+
- You have some understanding of the XRP Ledger, its capabilities, and of cryptocurrency in general. Ideally you have completed the [Basic XRPL guide](https://learn.xrpl.org/).
20
+
21
+
13
22
## Setup
14
23
15
24
First, download the complete sample code for this tutorial from GitHub:
@@ -78,7 +87,7 @@ To request a credential, make a request such as the following:
78
87
79
88
{% tab label="Summary" %}
80
89
* HTTP method: `POST`
81
-
* URL: `http://localhost:3000/credential`
90
+
* URL: `http://localhost:3005/credential`
82
91
* Headers:
83
92
*`Content-Type: application/json`
84
93
* Request Body:
@@ -95,7 +104,7 @@ To request a credential, make a request such as the following:
@@ -111,7 +120,7 @@ The parameters of the JSON request body should be as follows:
111
120
|`expiration`| String - ISO8601 Datetime | No | The time after which the credential expires, such as `2025-12-31T00:00:00Z`. |
112
121
|`uri`| String | No | Optional URI data to store with the credential. This data will become public on the XRP Ledger. If provided, this must be a string with minimum length 1 and max length 256, consisting of only characters that are valid in URIs, which are numbers, letters, and the following special characters: `-._~:/?#[]@!$&'()*+,;=%`. Conventionally, it should link to a Verifiable Credential document as defined by the W3C. |
113
122
114
-
This microservice immediately issues any credential that the user requests. A successful response from the API uses the HTTP status code `201 Created` and has a response body with the result of submitting the transaction to the XRP Ledger. You can use the `hash`or `ctid`value from the response to look up the transaction using an explorer such as [https://devnet.xrpl.org/](https://devnet.xrpl.org/).
123
+
This microservice immediately issues any credential that the user requests. A successful response from the API uses the HTTP status code `201 Created` and has a response body with the result of submitting the transaction to the XRP Ledger. You can use the `hash` value from the response to look up the transaction using an explorer such as [https://devnet.xrpl.org/](https://devnet.xrpl.org/).
115
124
116
125
{% admonition type="warning" name="Differences from Production" %}For a real credential issuer, you would probably check the credential type and only issue specific types of credentials, or maybe even just one type. <br><br> If checking the user's documents requires human intervention or takes longer than the amount of time an API request should wait to respond, you would need to store credential requests to some kind of storage, like a SQL database. You might also want to add a separate method for admins (or automated processes) to reject or issue the credential after checking the documents.{% /admonition %}
117
126
@@ -123,13 +132,13 @@ To show a list of credentials issued by the issuing account, make the following
123
132
124
133
{% tab label="Summary" %}
125
134
* HTTP method: `GET`
126
-
* URL: `http://localhost:3000/admin/credential`
135
+
* URL: `http://localhost:3005/admin/credential`
127
136
* Query parameters (optional): Use `?accepted=yes` to filter results to only credentials that the subject has accepted, or `?accepted=no` for credentials the user has not accepted.
128
137
{% /tab %}
129
138
130
139
{% tab label="cURL" %}
131
140
```sh
132
-
curl http://localhost:3000/admin/credential
141
+
curl http://localhost:3005/admin/credential
133
142
```
134
143
{% /tab %}
135
144
@@ -183,7 +192,7 @@ To revoke an issued credential, make a request such as the following:
183
192
184
193
{% tab label="Summary" %}
185
194
* HTTP method: `DELETE`
186
-
* URL: `http://localhost:3000/admin/credential`
195
+
* URL: `http://localhost:3005/admin/credential`
187
196
* Headers:
188
197
*`Content-Type: application/json`
189
198
* Request Body:
@@ -197,7 +206,7 @@ To revoke an issued credential, make a request such as the following:
@@ -210,7 +219,7 @@ The parameters of the JSON request body should be as follows:
210
219
|`subject`| String - Address | Yes | The XRPL classic address of the subject of the credential to revoke. |
211
220
|`credential`| String | Yes | The type of credential to revoke. This must match a credential type previously issued. |
212
221
213
-
A successful response from the API uses the HTTP status code `200 OK` and has a response body with the result of submitting the transaction to the XRP Ledger. You can use the `hash`or `ctid`value from the response to look up the transaction using an explorer.
222
+
A successful response from the API uses the HTTP status code `200 OK` and has a response body with the result of submitting the transaction to the XRP Ledger. You can use the `hash` value from the response to look up the transaction using an explorer.
214
223
215
224
## Code Walkthrough
216
225
@@ -223,7 +232,6 @@ The code for this tutorial is divided among the following files:
223
232
|`errors.js`| Custom error classes that standardize how the server reports validation errors and XRPL transaction failures. |
224
233
|`issuer_service.js`| Defines the microservice as an Express app, including API methods and error handling. |
225
234
|`look_up_credentials.js`| A helper function for looking up Credentials tied to an account, including pagination and filtering, used by both the credential issuer and holder. |
226
-
|`decode_hex.js`| A helper function for decoding hexadecimal into human-readable strings, used by both the credential issuer and holder. |
227
235
228
236
### accept_credential.js
229
237
@@ -305,12 +313,6 @@ This code performs [pagination using markers](../../../references/http-websocket
This file implements conversion of hex strings to human-readable text using ASCII, where possible. If the hex can't be decoded, it returns the original text prefaced with `(BIN) ` as a graceful fallback instead of throwing an error. This is important when reading data from the XRP Ledger because other users and tools can create Credentials with arbitrary binary data which might not decode to actual text at all. Even though the microservice from this tutorial only creates Credentials that use a restricted subset of ASCII characters, it might need to read ledger data that was created with different tools and different rules. You might even want to put more restrictions or checks in place depending on how you use the data; for example, if you output the results to a webpage you should make sure to escape or strip HTML tags to avoid visual glitches or cross-site-scripting attacks.
This file defines a set of helper functions that validate credential related input, verify request data, and convert between the issuer microservice's simplified Credential format and the XRP Ledger object representation. It throws typed errors on invalid input.
@@ -329,7 +331,7 @@ The function `validateCredentialRequest(...)` checks that the user input meets v
329
331
330
332
The `credentialFromXrpl(...)` function converts an XRPL ledger entry into a usable credential object (for example, converting the credential field from hexadecimal to a native string). The API methods that read data from the XRP Ledger use this function so that their output is formatted the same way as user input in the other API methods.
331
333
332
-
{% code-snippet file="/_code-samples/issue-credentials/js/credential.js" from="// Convert an XRPL ledger" before="Convert to an object" language="js" /%}
334
+
{% code-snippet file="/_code-samples/issue-credentials/js/credential.js" from="// Convert an XRPL ledger" before="// Convert to an object" language="js" /%}
333
335
334
336
The `credentialToXrpl(...)` function returns an object which is formatted for submitting to the XRP Ledger:
0 commit comments