More Permission Delegation edits per review

This commit is contained in:
mDuo13
2025-08-05 11:31:51 -07:00
parent 4103095b39
commit b7fe46fe19
4 changed files with 49 additions and 43 deletions

View File

@@ -10,15 +10,7 @@ These code samples demonstrate how to delegate permissions to another account an
npm i
```
2. Go to the [XRP Faucet](https://xrpl.org/resources/dev-tools/xrp-faucets) and generate a **Devnet** account to be your delegate account.
3. Edit `delegate-permisions.js` and change the following line to use the address you got from the faucet:
```js
const delegate_address = "r9GAKojMTyexqvy8DXFWYq63Mod5k5wnkT"
```
4. Run `delegate-permissions.js`.
2. Run `delegate-permisions.js`.
```sh
node delegate-permissions.js
@@ -26,9 +18,9 @@ These code samples demonstrate how to delegate permissions to another account an
If it runs successfully, it should output several things including "Delegate successfully set." followed by an [account_objects API method](https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_objects) response showing the delegate permissions.
Take note of the `account` address in this output. That's the address of the delegator.
Take note of the **Delegator address** and **Delegate seed** from the output.
5. Run `use-delegate-permissions.js` and provide both the delegator's address (from the previous step's output) and the delegate's secret key (from the devnet faucet earlier).
3. Run `use-delegate-permissions.js` and provide both the delegator's address and the delegate's secret key that were output in the previous step.
If it runs successfully, it should output various things ending in the following:

View File

@@ -4,15 +4,21 @@ async function main() {
const client = new xrpl.Client("wss://s.devnet.rippletest.net:51233")
await client.connect()
console.log("Funding new wallet from faucet...")
const { wallet } = await client.fundWallet()
console.log("Funding new wallets from faucet...")
const delegator_wallet = (await client.fundWallet()).wallet
console.log("Delegator account:")
console.log(" Address:", delegator_wallet.address)
const delegate_wallet = (await client.fundWallet()).wallet
console.log("Delegate account:")
console.log(" Address:", delegate_wallet.address)
console.log(" Seed:", delegate_wallet.seed)
console.log("Please note these values for later.")
// Define the transaction
const delegate_address = "r9GAKojMTyexqvy8DXFWYq63Mod5k5wnkT"
const delegateset = {
"TransactionType": "DelegateSet",
"Account": wallet.address, // Delegator address
"Authorize": delegate_address,
"Account": delegator_wallet.address,
"Authorize": delegate_wallet.address,
"Permissions": [
{
"Permission": {
@@ -23,9 +29,10 @@ async function main() {
}
// Prepare, sign, and submit the transaction
const prepared = await client.autofill(delegateset)
const signed = wallet.sign(prepared)
const result = await client.submitAndWait(signed.tx_blob)
const result = await client.submitAndWait(delegateset, {
wallet: delegator_wallet,
autofill: true
})
// Check transaction results
console.log(result)
@@ -36,7 +43,7 @@ async function main() {
// Confirm presence of Delegate ledger entry using account_objects
response = await client.request({
"command": "account_objects",
"account": wallet.address,
"account": delegator_wallet.address,
"type": "delegate",
"ledger_index": "validated"
})

View File

@@ -5,9 +5,9 @@ const readline = require('readline').createInterface({
})
const { stringToHex, hexToString } = require('@xrplf/isomorphic/utils')
// Set delegating account and delegate from user input
readline.question(`Address of delegating account? `, async function(from_address) {
readline.question(`Delegate's secret key? `, async function(secret) {
// Get delegator and delegate accounts from user input
readline.question(`Delegator's address? `, async function(delegator_address) {
readline.question(`Delegate's seed? `, async function(secret) {
const client = new xrpl.Client("wss://s.devnet.rippletest.net:51233")
await client.connect()
@@ -17,13 +17,15 @@ readline.question(`Address of delegating account? `, async function(from_address
// Check which permissions the delegate has been granted, if any
response = await client.request({
"command": "account_objects",
"account": from_address,
"account": delegator_address,
"type": "delegate",
"ledger_index": "validated"
})
let found_match = false
for (delegate_entry of response.result.account_objects) {
if (delegate_entry.Account == from_address && delegate_entry.Authorize == delegate_wallet.address) {
if (delegate_entry.Account == delegator_address &&
delegate_entry.Authorize == delegate_wallet.address) {
found_match = true
console.log("Delegate has the following permissions:")
for (perm of delegate_entry.Permissions) {
@@ -33,26 +35,29 @@ readline.question(`Address of delegating account? `, async function(from_address
}
}
if (!found_match) {
console.warn("Delegate appears not to have any permissions granted by delegating account.")
console.warn("Did you run delegate-permissions.js first with your delegate address?")
console.warn("Delegate appears not to have any permissions granted"+
" by the delegator.")
console.warn("Make sure you ran delegate-permissions.js and input the"+
" correct delegate/delegator values to this script.")
return
}
// Use the AccountDomainSet granular permission to set the "Domain" field
// of the delegating account
// of the delegator
const set_domain_example = {
"TransactionType": "AccountSet",
"Account": from_address,
"Account": delegator_address,
"Delegate": delegate_wallet.address,
"Domain": stringToHex("example.com")
}
// Prepare, sign, and submit the transaction
const prepared = await client.autofill(set_domain_example)
const signed = delegate_wallet.sign(prepared)
console.log("Submitting transaction:")
console.log(JSON.stringify(signed, null, 2))
const result = await client.submitAndWait(signed.tx_blob)
console.log(set_domain_example)
const result = await client.submitAndWait(set_domain_example, {
wallet: delegate_wallet,
autofill: true
})
// Check transaction results and disconnect
console.log(result)
@@ -61,14 +66,15 @@ readline.question(`Address of delegating account? `, async function(from_address
}
// Confirm that the account's Domain field has been set as expected
const account_info_resp = await client.request({
const acct_info_resp = await client.request({
"command": "account_info",
"account": from_address,
"account": delegator_address,
"ledger_index": "validated"
})
const domain_str = hexToString(account_info_resp.result.account_data.Domain)
const domain_str = hexToString(acct_info_resp.result.account_data.Domain)
console.log(`Domain is ${domain_str}`)
client.disconnect()
readline.close()
})
})