Skip to content

Commit 78c3307

Browse files
authored
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
1 parent 8d0ff29 commit 78c3307

File tree

12 files changed

+345
-0
lines changed

12 files changed

+345
-0
lines changed

_code-samples/did/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Create, Update, and Delete Decentralized Identifiers (DIDs)
2+
3+
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.

_code-samples/did/py/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How to DID
2+
3+
Modify and run the did_set.py file to create or update a DID object for an XRPL account.
4+
5+
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

_code-samples/did/py/account_did.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from xrpl.models import LedgerEntry
2+
from xrpl.clients import JsonRpcClient
3+
4+
5+
# connect to the xrpl via a client
6+
print("Connecting to client")
7+
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
8+
print("connected!!!")
9+
10+
11+
# address of an account that has an existing DID
12+
account_did_creator = "rQB1cBMMyFXshFQd6cj3eg7vSJZtYb6d8e"
13+
14+
# build the request for the account's DID
15+
req = LedgerEntry(ledger_index="validated", did=account_did_creator)
16+
17+
# submit request and awaiting result
18+
print("submitting request")
19+
response = client.request(req)
20+
result = response.result
21+
22+
23+
# parse result
24+
if "index" in result and "Account" in result["node"]:
25+
print(f'DID index: {result["node"]["index"]}')
26+
print(f'DID Document: {result["node"]["DIDDocument"]}')
27+
print(f'Data: {result["node"]["Data"]}')
28+
print(f'URI: {result["node"]["URI"]}')
29+
else:
30+
print("No DID found for this account")

_code-samples/did/py/did_delete.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# neccasary imports
2+
from xrpl.models import DIDDelete
3+
from xrpl.clients import JsonRpcClient
4+
from xrpl.wallet import Wallet
5+
from xrpl.transaction import submit_and_wait
6+
7+
8+
# connect to the xrpl via a client
9+
print("Connecting to client")
10+
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
11+
print("connected!!!")
12+
13+
14+
# input the seed that was generated from running the did_set.py
15+
seed = input("now, enter the seed of the account that has a DID object to delete: ")
16+
17+
# restore an account that has an existing DID
18+
account_did_creator = Wallet.from_seed(seed=seed)
19+
20+
# define the account DIDDelete transaction
21+
did_delete_txn = DIDDelete(account=account_did_creator.address)
22+
23+
# sign, submit the did delete transaction and wait for result
24+
print("signed and submitting did delete transaction. awaiting response...")
25+
did_delete_response = submit_and_wait(
26+
transaction=did_delete_txn,
27+
wallet=account_did_creator,
28+
client=client,
29+
)
30+
31+
# Parse response for result
32+
did_delete_result = did_delete_response.result
33+
34+
# Print result and transaction hash
35+
print(did_delete_result["meta"]["TransactionResult"])
36+
print(did_delete_result["hash"])

_code-samples/did/py/did_set.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This allows you to create or update a DID
2+
from xrpl.models import DIDSet
3+
from xrpl.clients import JsonRpcClient
4+
from xrpl.wallet import generate_faucet_wallet
5+
from xrpl.transaction import submit_and_wait
6+
from xrpl.utils import str_to_hex
7+
8+
9+
# connect to the xrpl via a client
10+
print("Connecting to client")
11+
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
12+
print("connected!!!")
13+
14+
15+
# define/generate a wallet / account
16+
account_did_creator = generate_faucet_wallet(client)
17+
18+
print("⭐successfully generated wallet")
19+
print("here's your seed. You'll need it to modify and delete the DID set by this account/wallet")
20+
# it is not good practise to expose your seed.
21+
print(f"seed: {account_did_creator.seed}")
22+
23+
24+
# define the document associated with the DID
25+
document = "did:example:123#public-key-0"
26+
27+
# define the data associated with the DID
28+
# The public attestations of identity credentials associated with the DID.
29+
data = "did:example:123#key-1"
30+
31+
# define the uri associated with the DID
32+
# The Universal Resource Identifier associated with the DID.
33+
uri = "https://example.did.com/123"
34+
35+
36+
# build DID SET transaction
37+
# str_to_hex() converts the inputted string to blockchain understandable hexadecimal
38+
did_set_txn = DIDSet(
39+
account=account_did_creator.address,
40+
did_document=str_to_hex(document),
41+
data=str_to_hex(data),
42+
uri=str_to_hex(uri),
43+
)
44+
45+
46+
# sign, submit the transaction and wait for the response
47+
print("siging and submitting the transaction, awaiting a response")
48+
did_set_txn_response = submit_and_wait(
49+
transaction=did_set_txn, client=client, wallet=account_did_creator
50+
)
51+
52+
# Parse response for result
53+
did_set_txn_result = did_set_txn_response.result
54+
55+
# Print result and transaction hash
56+
print(did_set_txn_result["meta"]["TransactionResult"])
57+
print(did_set_txn_result["hash"])

_code-samples/did/py/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xrpl-py==4.0.0

_code-samples/price_oracles/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Create, Update, and Delete Price Oracles
2+
3+
Create, Update, and Delete Price Oracles. A price oracle is a mechanism that feeds external data, such as asset prices, and exchange rates, onto the XRPLedger.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Price Oracle
2+
3+
After you run the create_price_oracle.py file it returns a seed which you will input upon request from the delete_price_oracle.py file to delete the account's DID
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# neccesary imports
2+
import datetime
3+
from xrpl.clients import JsonRpcClient
4+
from xrpl.models import AccountObjects, AccountObjectType
5+
6+
7+
print("connecting to the test network")
8+
# Connect to XRPL test network
9+
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
10+
print("connected!!!")
11+
12+
13+
# define the account we are going to query
14+
oracle_creator = "rBoSibkbwaAUEpkehYixQrXp4AqZez9WqA"
15+
16+
# define array for holding oracles an account has created
17+
oracles_ = []
18+
19+
# build the request object
20+
req = AccountObjects(
21+
account=oracle_creator,
22+
ledger_index="validated",
23+
type=AccountObjectType.ORACLE,
24+
)
25+
26+
# mak the request object
27+
response = client.request(req)
28+
29+
# return the result
30+
result = response.result
31+
32+
# parse the result and print
33+
if "account_objects" in result:
34+
oracles = result["account_objects"]
35+
for oracle in oracles:
36+
oracle_data = {}
37+
price_data_ = []
38+
oracle_data["oracle_id"] = oracle["index"]
39+
oracle_data["owner"] = oracle["Owner"]
40+
oracle_data["provider"] = oracle["Provider"]
41+
oracle_data["asset_class"] = oracle["AssetClass"]
42+
oracle_data["uri"] = oracle["URI"] if "URI" in oracle else ""
43+
oracle_data["last_update_time"] = (
44+
str(datetime.datetime.fromtimestamp(oracle["LastUpdateTime"]))
45+
if "LastUpdateTime" in oracle
46+
else ""
47+
)
48+
oracle_data["price_data_series"] = (
49+
oracle["PriceDataSeries"] if "PriceDataSeries" in oracle else []
50+
)
51+
52+
# sort price data series if any
53+
if "PriceDataSeries" in oracle and len(oracle["PriceDataSeries"]) > 0:
54+
price_data_series = oracle["PriceDataSeries"]
55+
for price_data_serie in price_data_series:
56+
price_data = {}
57+
price_data["base_asset"] = price_data_serie["PriceData"]["BaseAsset"]
58+
59+
price_data["quote_asset"] = price_data_serie["PriceData"]["QuoteAsset"]
60+
61+
price_data["scale"] = (
62+
price_data_serie["PriceData"]["Scale"]
63+
if "Scale" in price_data_serie["PriceData"]
64+
else ""
65+
)
66+
price_data["asset_price"] = (
67+
price_data_serie["PriceData"]["AssetPrice"]
68+
if "AssetPrice" in price_data_serie["PriceData"]
69+
else ""
70+
)
71+
72+
price_data_.append(price_data)
73+
oracle_data["price_data_series"] = price_data_
74+
oracles_.append(oracle_data)
75+
76+
77+
print(oracles_)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# neccesary imports
2+
import datetime
3+
from xrpl.wallet import generate_faucet_wallet
4+
from xrpl.clients import JsonRpcClient
5+
from xrpl.models import (
6+
OracleSet,
7+
)
8+
from xrpl.transaction import submit_and_wait, sign_and_submit
9+
from xrpl.models.transactions.oracle_set import PriceData
10+
11+
from xrpl.utils import str_to_hex
12+
13+
14+
print("connecting to the test network")
15+
# Connect to XRPL test network
16+
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
17+
print("connected!!!")
18+
19+
# create demo wallet
20+
oracle_creator = generate_faucet_wallet(client=client)
21+
print("⭐successfully generated wallet")
22+
print("here's your seed. You'll need it to modify and delete the DID set by this account/wallet")
23+
# it is not good practise to expose your seed.
24+
print(f"seed: {oracle_creator.seed}")
25+
26+
# define the oracle document id
27+
# this should be stored offline as the blockchain doesn't retrieve it in requests
28+
oracle_document_id = 1
29+
30+
# define the provider's name and convert to hexadecimal e.g: band, chainlink etc
31+
provider = str_to_hex("provider")
32+
33+
# define the uri of the provider and convert to hexadecimal
34+
uri = str_to_hex("sampleprovider.com")
35+
36+
37+
# define the last update time of the price data being passed to the oracle as a timestamp
38+
# max time into the future is 5 minutes and max time into the past is 4 minutes from the current time
39+
# we'll use the current date time for this
40+
last_update_time = int(datetime.datetime.now().timestamp())
41+
42+
43+
# define the asset class and convert to hexadecimal
44+
# Describes the type of asset, such as "currency", "commodity", or "index".
45+
asset_class = str_to_hex("currency")
46+
47+
48+
# create a price data object, that will be tracked by the oracle
49+
pd = PriceData(
50+
base_asset="BTC",
51+
quote_asset="USD",
52+
asset_price=1000,
53+
scale=4,
54+
)
55+
56+
# create an array of up to 10 Price data objects
57+
price_data_array = [pd]
58+
59+
60+
print("building transaction")
61+
# create price oracle transaction
62+
oracle_set = OracleSet(
63+
account=oracle_creator.address,
64+
oracle_document_id=oracle_document_id,
65+
provider=provider,
66+
uri=uri,
67+
last_update_time=last_update_time,
68+
asset_class=asset_class,
69+
price_data_series=price_data_array,
70+
)
71+
72+
73+
print("signing and submitting transaction, awaiting response")
74+
# sign, submit, and wait forthe transaction result
75+
oracle_set_txn_response = submit_and_wait(
76+
transaction=oracle_set, client=client, wallet=oracle_creator
77+
)
78+
79+
80+
# print the result and transaction hash
81+
print(oracle_set_txn_response.result["meta"]["TransactionResult"])
82+
print(oracle_set_txn_response.result["hash"])

0 commit comments

Comments
 (0)