mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-28 23:55:49 +00:00
Add sample python code for DIDs and Price Oracles (#2932)
* Create create_price_oracle.py * Create README.md * Update README.md * Update create_price_oracle.py * Create delete_price_oracle.py * Update delete_price_oracle.py * Create account_price_oracles.py * Create did_set.py * Create did_delete.py * Create account_did.py * Create requirements.txt * Create README.md * Create requirements.txt * Update README.md * Update README.md * Create README.md * Create README.md * Update did_set.py * Update did_set.py * Update did_set.py * Update did_delete.py * Update README.md * Update create_price_oracle.py * Update did_set.py * Update README.md * Update README.md * Update delete_price_oracle.py * Update README.md
This commit is contained in:
3
_code-samples/did/README.md
Normal file
3
_code-samples/did/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Create, Update, and Delete Decentralized Identifiers (DIDs)
|
||||
|
||||
Create, Update, and Delete Decentralized Identifiers (DIDs). A Decentralized Identifier (DID) is a new type of identifier defined by the World Wide Web Consortium (W3C) that enables verifiable, digital identities.
|
||||
5
_code-samples/did/py/README.md
Normal file
5
_code-samples/did/py/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# How to DID
|
||||
|
||||
Modify and run the did_set.py file to create or update a DID object for an XRPL account.
|
||||
|
||||
After you run the did_set.py file it returns a seed which you will input upon request from the did_delete.py file to delete the account's DID
|
||||
30
_code-samples/did/py/account_did.py
Normal file
30
_code-samples/did/py/account_did.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from xrpl.models import LedgerEntry
|
||||
from xrpl.clients import JsonRpcClient
|
||||
|
||||
|
||||
# connect to the xrpl via a client
|
||||
print("Connecting to client")
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||
print("connected!!!")
|
||||
|
||||
|
||||
# address of an account that has an existing DID
|
||||
account_did_creator = "rQB1cBMMyFXshFQd6cj3eg7vSJZtYb6d8e"
|
||||
|
||||
# build the request for the account's DID
|
||||
req = LedgerEntry(ledger_index="validated", did=account_did_creator)
|
||||
|
||||
# submit request and awaiting result
|
||||
print("submitting request")
|
||||
response = client.request(req)
|
||||
result = response.result
|
||||
|
||||
|
||||
# parse result
|
||||
if "index" in result and "Account" in result["node"]:
|
||||
print(f'DID index: {result["node"]["index"]}')
|
||||
print(f'DID Document: {result["node"]["DIDDocument"]}')
|
||||
print(f'Data: {result["node"]["Data"]}')
|
||||
print(f'URI: {result["node"]["URI"]}')
|
||||
else:
|
||||
print("No DID found for this account")
|
||||
36
_code-samples/did/py/did_delete.py
Normal file
36
_code-samples/did/py/did_delete.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# neccasary imports
|
||||
from xrpl.models import DIDDelete
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.wallet import Wallet
|
||||
from xrpl.transaction import submit_and_wait
|
||||
|
||||
|
||||
# connect to the xrpl via a client
|
||||
print("Connecting to client")
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||
print("connected!!!")
|
||||
|
||||
|
||||
# input the seed that was generated from running the did_set.py
|
||||
seed = input("now, enter the seed of the account that has a DID object to delete: ")
|
||||
|
||||
# restore an account that has an existing DID
|
||||
account_did_creator = Wallet.from_seed(seed=seed)
|
||||
|
||||
# define the account DIDDelete transaction
|
||||
did_delete_txn = DIDDelete(account=account_did_creator.address)
|
||||
|
||||
# sign, submit the did delete transaction and wait for result
|
||||
print("signed and submitting did delete transaction. awaiting response...")
|
||||
did_delete_response = submit_and_wait(
|
||||
transaction=did_delete_txn,
|
||||
wallet=account_did_creator,
|
||||
client=client,
|
||||
)
|
||||
|
||||
# Parse response for result
|
||||
did_delete_result = did_delete_response.result
|
||||
|
||||
# Print result and transaction hash
|
||||
print(did_delete_result["meta"]["TransactionResult"])
|
||||
print(did_delete_result["hash"])
|
||||
57
_code-samples/did/py/did_set.py
Normal file
57
_code-samples/did/py/did_set.py
Normal file
@@ -0,0 +1,57 @@
|
||||
# This allows you to create or update a DID
|
||||
from xrpl.models import DIDSet
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
from xrpl.transaction import submit_and_wait
|
||||
from xrpl.utils import str_to_hex
|
||||
|
||||
|
||||
# connect to the xrpl via a client
|
||||
print("Connecting to client")
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||
print("connected!!!")
|
||||
|
||||
|
||||
# define/generate a wallet / account
|
||||
account_did_creator = generate_faucet_wallet(client)
|
||||
|
||||
print("⭐successfully generated wallet")
|
||||
print("here's your seed. You'll need it to modify and delete the DID set by this account/wallet")
|
||||
# it is not good practise to expose your seed.
|
||||
print(f"seed: {account_did_creator.seed}")
|
||||
|
||||
|
||||
# define the document associated with the DID
|
||||
document = "did:example:123#public-key-0"
|
||||
|
||||
# define the data associated with the DID
|
||||
# The public attestations of identity credentials associated with the DID.
|
||||
data = "did:example:123#key-1"
|
||||
|
||||
# define the uri associated with the DID
|
||||
# The Universal Resource Identifier associated with the DID.
|
||||
uri = "https://example.did.com/123"
|
||||
|
||||
|
||||
# build DID SET transaction
|
||||
# str_to_hex() converts the inputted string to blockchain understandable hexadecimal
|
||||
did_set_txn = DIDSet(
|
||||
account=account_did_creator.address,
|
||||
did_document=str_to_hex(document),
|
||||
data=str_to_hex(data),
|
||||
uri=str_to_hex(uri),
|
||||
)
|
||||
|
||||
|
||||
# sign, submit the transaction and wait for the response
|
||||
print("siging and submitting the transaction, awaiting a response")
|
||||
did_set_txn_response = submit_and_wait(
|
||||
transaction=did_set_txn, client=client, wallet=account_did_creator
|
||||
)
|
||||
|
||||
# Parse response for result
|
||||
did_set_txn_result = did_set_txn_response.result
|
||||
|
||||
# Print result and transaction hash
|
||||
print(did_set_txn_result["meta"]["TransactionResult"])
|
||||
print(did_set_txn_result["hash"])
|
||||
1
_code-samples/did/py/requirements.txt
Normal file
1
_code-samples/did/py/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
xrpl-py==4.0.0
|
||||
Reference in New Issue
Block a user