Skip to content

Commit 7f80874

Browse files
committed
add reviewer suggestions
1 parent cd53966 commit 7f80874

File tree

6 files changed

+187
-406
lines changed

6 files changed

+187
-406
lines changed

_code-samples/modular-tutorials/credential-manager.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// *******************************************************
2-
// *********** Create Credential *************************
3-
// *******************************************************
4-
1+
/// Create credential function
52
async function createCredential() {
63

74
let net = getNet()
@@ -12,6 +9,7 @@ async function createCredential() {
129
results = `\n\nConnected.`
1310
updateResults()
1411

12+
// Gather transaction info
1513
try {
1614

1715
// Get account wallet from seed

_code-samples/modular-tutorials/permissioned-domain-manager.js

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// *******************************************************
2-
// *********** Create Permissioned Domain ****************
3-
// *******************************************************
4-
1+
/// Create permissioned domain
52
async function createDomain() {
63

74
let net = getNet()
@@ -12,76 +9,76 @@ async function createDomain() {
129
results = `\n\nConnected.`
1310
updateResults()
1411

12+
// Gather transaction info
1513
try {
16-
17-
// Get account wallet from seed
18-
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
19-
20-
// Get Domain ID
21-
const domainID = domainIDField.value
22-
23-
// Get credential type - convert string to hex if needed
24-
let credentialType = credentialTypeField.value;
25-
if (!/^[0-9A-F]+$/i.test(credentialType)) {
26-
let hex = '';
27-
for (let i = 0; i < credentialType.length; i++) {
28-
const charCode = credentialType.charCodeAt(i);
29-
const hexCharCode = charCode.toString(16).padStart(2, '0');
30-
hex += hexCharCode;
14+
15+
// Get account wallet from seed
16+
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
17+
18+
// Get Domain ID
19+
const domainID = domainIDField.value
20+
21+
// Get credential type - convert string to hex if needed
22+
let credentialType = credentialTypeField.value;
23+
if (!/^[0-9A-F]+$/i.test(credentialType)) {
24+
let hex = '';
25+
for (let i = 0; i < credentialType.length; i++) {
26+
const charCode = credentialType.charCodeAt(i);
27+
const hexCharCode = charCode.toString(16).padStart(2, '0');
28+
hex += hexCharCode;
29+
}
30+
credentialType = hex.toUpperCase();
3131
}
32-
credentialType = hex.toUpperCase();
33-
}
34-
35-
// Prepare transaction
36-
const transaction = {
37-
"TransactionType": "PermissionedDomainSet",
38-
"Account": wallet.address,
39-
"AcceptedCredentials": [
40-
{
41-
"Credential": {
42-
"Issuer": wallet.address,
43-
"CredentialType": credentialType
32+
33+
// Prepare transaction
34+
const transaction = {
35+
"TransactionType": "PermissionedDomainSet",
36+
"Account": wallet.address,
37+
"AcceptedCredentials": [
38+
{
39+
"Credential": {
40+
"Issuer": wallet.address,
41+
"CredentialType": credentialType
42+
}
4443
}
45-
}
46-
]
47-
}
44+
]
45+
}
4846

49-
if (domainID) {
50-
transaction.DomainID = domainID
51-
}
52-
53-
results = `\n\n===Preparing and Sending Transaction===\n\n${JSON.stringify(transaction, null, 2)}`
54-
updateResults()
55-
56-
// Submit transaction
57-
const tx = await client.submitAndWait(transaction, {autofill: true, wallet: wallet})
58-
59-
if (tx.result.meta.TransactionResult == "tesSUCCESS") {
60-
// Parse for domain info
6147
if (domainID) {
62-
results = `\n\n===Create Permissioned Domain Result===\n\n${JSON.stringify(tx.result.tx_json, null, 2)}`
48+
transaction.DomainID = domainID
49+
}
50+
51+
results = `\n\n===Preparing and Sending Transaction===\n\n${JSON.stringify(transaction, null, 2)}`
52+
updateResults()
53+
54+
// Submit transaction
55+
const tx = await client.submitAndWait(transaction, {autofill: true, wallet: wallet})
56+
57+
if (tx.result.meta.TransactionResult == "tesSUCCESS") {
58+
// Parse for domain info
59+
if (domainID) {
60+
results = `\n\n===Create Permissioned Domain Result===\n\n${JSON.stringify(tx.result.tx_json, null, 2)}`
61+
} else {
62+
const parsedResponse = JSON.parse(JSON.stringify(tx.result.meta.AffectedNodes, null, 2))
63+
const domainInfo = parsedResponse.find( node => node.CreatedNode && node.CreatedNode.LedgerEntryType === "PermissionedDomain" )
64+
results = `\n\n===Create Permissioned Domain Result===\n\n${JSON.stringify(domainInfo.CreatedNode, null, 2)}`
65+
}
6366
} else {
64-
const parsedResponse = JSON.parse(JSON.stringify(tx.result.meta.AffectedNodes, null, 2))
65-
const domainInfo = parsedResponse.find( node => node.CreatedNode && node.CreatedNode.LedgerEntryType === "PermissionedDomain" )
66-
results = `\n\n===Create Permissioned Domain Result===\n\n${JSON.stringify(domainInfo.CreatedNode, null, 2)}`
67+
results = `\n\n===Error===\n\n${JSON.stringify(tx.result.meta.TransactionResult, null, 2)}: Check codes at https://xrpl.org/docs/references/protocol/transactions/types/permissioneddomainset#error-cases`
6768
}
68-
} else {
69-
results = `\n\n===Error===\n\n${JSON.stringify(tx.result.meta.TransactionResult, null, 2)}: Check codes at https://xrpl.org/docs/references/protocol/transactions/types/permissioneddomainset#error-cases`
70-
}
71-
updateResults()
69+
updateResults()
7270

73-
} catch (error) {
74-
results = `\n\n===Error===\n\n${error}`
75-
updateResults()
76-
}
71+
} catch (error) {
72+
results = `\n\n===Error===\n\n${error}`
73+
updateResults()
74+
}
7775

7876
client.disconnect()
7977
}
78+
// End create permissioned domain
8079

81-
// *******************************************************
82-
// *********** Delete Permissioned Domain ****************
83-
// *******************************************************
8480

81+
// Delete permissioned domain
8582
async function deleteDomain() {
8683

8784
let net = getNet()
@@ -92,6 +89,7 @@ async function deleteDomain() {
9289
results = `\n\nConnected.`
9390
updateResults()
9491

92+
// Get delete domain transaction info
9593
try {
9694

9795
// Get account wallet from seed
@@ -110,7 +108,7 @@ async function deleteDomain() {
110108
results = `\n\n===Preparing and Sending Transaction===\n\n${JSON.stringify(transaction, null, 2)}`
111109
updateResults()
112110

113-
// Submit transaction
111+
// Submit delete domain transaction
114112
const tx = await client.submitAndWait(transaction, {autofill: true, wallet: wallet})
115113

116114
if (tx.result.meta.TransactionResult == "tesSUCCESS") {
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
seo:
3+
description: Create a permissioned domain to restrict access to financial services that meet compliance requirements.
4+
labels:
5+
- Decentralized Finance
6+
- Permissioned Domains
7+
---
8+
# Create Permissioned Domains
9+
10+
Permissioned domains are controlled environments within the broader ecosystem of the XRP Ledger blockchain. Domains restrict access to other features such as Permissioned DEXes and Lending Protocols, only allowing access to them for accounts with specific credentials.
11+
12+
This example shows how to:
13+
14+
1. Issue a credential to an account.
15+
2. Create a permissioned domain with the issued credential.
16+
3. Delete the permissioned domain.
17+
18+
[![Create Permissioned Domain Test Harness](/docs/img/create-permissioned-domain-1.png)](/docs/img/create-permissioned-domain-1.png)
19+
20+
Download the [Modular Tutorials](https://github.com/XRPLF/xrpl-dev-portal/tree/master/_code-samples/modular-tutorials/) folder.
21+
22+
{% admonition type="info" name="Note" %}
23+
Without the Modular Tutorial Samples, you will not be able to try the examples that follow.
24+
{% /admonition %}
25+
26+
## Get Accounts
27+
28+
To get test accounts:
29+
30+
1. Open `create-permissioned-domains.html` in a browser.
31+
2. Get test accounts.
32+
- If you copied the gathered information from another tutorial:
33+
1. Paste the gathered information to the **Result** field.
34+
2. Click **Distribute Account Info**.
35+
- If you have an existing account seed:
36+
1. Paste the account seed to the **Account 1 Seed** or **Account 2 Seed** field.
37+
2. Click **Get Account 1 from Seed** or **Get Account 2 from Seed**.
38+
- If you do not have existing accounts:
39+
1. Click **Get New Account 1**.
40+
2. Click **Get New Account 2**.
41+
42+
[![Created Accounts](/docs/img/create-permissioned-domain-2.png)](/docs/img/create-permissioned-domain-2.png)
43+
44+
45+
## Issue a Credential
46+
47+
1. Click the **Account 1** radial button. This account will the credential issuer.
48+
2. Copy the account 2 address into **Subject**.
49+
3. Enter a **Credential Type**. For example, _KYC_.
50+
4. Click **Create Credential**.
51+
52+
[![Created Credential](/docs/img/create-permissioned-domain-3.png)](/docs/img/create-permissioned-domain-3.png)
53+
54+
55+
## Create a Permissioned Domain
56+
57+
1. Click **Create Permissioned Domain**.
58+
2. Copy the _LedgerIndex_ value from the metadata response.
59+
3. (Optional) Update the permissioned domain with a different credential.
60+
1. Change the **Credential Type**.
61+
2. Click **Create Credential**.
62+
3. Copy the _LedgerIndex_ value into **DomainID**.
63+
4. Click **Create Permissioned Domain**.
64+
65+
[![Created Domain](/docs/img/create-permissioned-domain-4.png)](/docs/img/create-permissioned-domain-4.png)
66+
67+
68+
## Delete a Permissioned Domain
69+
70+
1. Copy the _LedgerIndex_ value into **DomainID**.
71+
2. Click **Delete Permissioned Domain**.
72+
73+
[![Deleted Domain](/docs/img/create-permissioned-domain-5.png)](/docs/img/create-permissioned-domain-5.png)
74+
75+
76+
77+
# Code Walkthrough
78+
79+
## credential-manager.js
80+
81+
### Create Credential
82+
83+
Define a function that issues a credential to a subject and connects to the XRP Ledger.
84+
85+
{% code-snippet file="/_code-samples/modular-tutorials/credential-manager.js" language="js" from="// Create credential function" before="// Gather transaction info" /%}
86+
87+
Gather the issuer information, subject, and credential type. Convert the credential type value to a hex string if not already in hex. Wrap the code in a `try-catch` block to handle errors.
88+
89+
{% code-snippet file="/_code-samples/modular-tutorials/credential-manager.js" language="js" from="// Gather transaction info" before="// Submit transaction" /%}
90+
91+
Submit the `CredentialCreate` transaction and report the results. Parse the metadata response to return only relevant credential info.
92+
93+
{% code-snippet file="/_code-samples/modular-tutorials/credential-manager.js" language="js" from="// Submit transaction" /%}
94+
95+
96+
## permissioned-domain-manager.js
97+
98+
### Create Permissioned Domain
99+
100+
Define a function that creates a permissioned domain and connects to the XRP Ledger.
101+
102+
{% code-snippet file="/_code-samples/modular-tutorials/permissioned-domain-manager.js" language="js" from="/// Create permissioned domain" before="// Gather transaction info" /%}
103+
104+
Gather issuer information, credential type, and domain ID. Format the transaction depending on if the optional domain ID field is included. Wrap the code in a `try-catch` block to handle errors.
105+
106+
{% code-snippet file="/_code-samples/modular-tutorials/permissioned-domain-manager.js" language="js" from="// Gather transaction info" before="// Submit transaction" /%}
107+
108+
Submit the `PermissionedDomainSet` transaction and report the results. The metadata is formed differently if a domain ID was included; parse the response accordingly.
109+
110+
{% code-snippet file="/_code-samples/modular-tutorials/permissioned-domain-manager.js" language="js" from="// Submit transaction" before="// End create permissioned domain" /%}
111+
112+
### Delete Permissioned Domain
113+
114+
Define a function to delete a permissioned domain and connect to the XRP Ledger.
115+
116+
{% code-snippet file="/_code-samples/modular-tutorials/permissioned-domain-manager.js" language="js" from="// Delete permissioned domain" before="// Get delete domain transaction info" /%}
117+
118+
Gather account information and domain ID values. Wrap the code in a `try-catch` block to handle errors.
119+
120+
{% code-snippet file="/_code-samples/modular-tutorials/permissioned-domain-manager.js" language="js" from="// Get delete domain transaction info" before="// Submit delete domain transaction" /%}
121+
122+
Submit the `PermissionedDomainDelete` transaction and report the results.
123+
124+
{% code-snippet file="/_code-samples/modular-tutorials/permissioned-domain-manager.js" language="js" from="// Submit delete domain transaction" /%}

0 commit comments

Comments
 (0)