mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-25 14:15:50 +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
|
||||||
3
_code-samples/price_oracles/README.md
Normal file
3
_code-samples/price_oracles/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Create, Update, and Delete Price Oracles
|
||||||
|
|
||||||
|
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.
|
||||||
3
_code-samples/price_oracles/py/README.md
Normal file
3
_code-samples/price_oracles/py/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# How to Price Oracle
|
||||||
|
|
||||||
|
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
|
||||||
77
_code-samples/price_oracles/py/account_price_oracles.py
Normal file
77
_code-samples/price_oracles/py/account_price_oracles.py
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
# neccesary imports
|
||||||
|
import datetime
|
||||||
|
from xrpl.clients import JsonRpcClient
|
||||||
|
from xrpl.models import AccountObjects, AccountObjectType
|
||||||
|
|
||||||
|
|
||||||
|
print("connecting to the test network")
|
||||||
|
# Connect to XRPL test network
|
||||||
|
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||||
|
print("connected!!!")
|
||||||
|
|
||||||
|
|
||||||
|
# define the account we are going to query
|
||||||
|
oracle_creator = "rBoSibkbwaAUEpkehYixQrXp4AqZez9WqA"
|
||||||
|
|
||||||
|
# define array for holding oracles an account has created
|
||||||
|
oracles_ = []
|
||||||
|
|
||||||
|
# build the request object
|
||||||
|
req = AccountObjects(
|
||||||
|
account=oracle_creator,
|
||||||
|
ledger_index="validated",
|
||||||
|
type=AccountObjectType.ORACLE,
|
||||||
|
)
|
||||||
|
|
||||||
|
# mak the request object
|
||||||
|
response = client.request(req)
|
||||||
|
|
||||||
|
# return the result
|
||||||
|
result = response.result
|
||||||
|
|
||||||
|
# parse the result and print
|
||||||
|
if "account_objects" in result:
|
||||||
|
oracles = result["account_objects"]
|
||||||
|
for oracle in oracles:
|
||||||
|
oracle_data = {}
|
||||||
|
price_data_ = []
|
||||||
|
oracle_data["oracle_id"] = oracle["index"]
|
||||||
|
oracle_data["owner"] = oracle["Owner"]
|
||||||
|
oracle_data["provider"] = oracle["Provider"]
|
||||||
|
oracle_data["asset_class"] = oracle["AssetClass"]
|
||||||
|
oracle_data["uri"] = oracle["URI"] if "URI" in oracle else ""
|
||||||
|
oracle_data["last_update_time"] = (
|
||||||
|
str(datetime.datetime.fromtimestamp(oracle["LastUpdateTime"]))
|
||||||
|
if "LastUpdateTime" in oracle
|
||||||
|
else ""
|
||||||
|
)
|
||||||
|
oracle_data["price_data_series"] = (
|
||||||
|
oracle["PriceDataSeries"] if "PriceDataSeries" in oracle else []
|
||||||
|
)
|
||||||
|
|
||||||
|
# sort price data series if any
|
||||||
|
if "PriceDataSeries" in oracle and len(oracle["PriceDataSeries"]) > 0:
|
||||||
|
price_data_series = oracle["PriceDataSeries"]
|
||||||
|
for price_data_serie in price_data_series:
|
||||||
|
price_data = {}
|
||||||
|
price_data["base_asset"] = price_data_serie["PriceData"]["BaseAsset"]
|
||||||
|
|
||||||
|
price_data["quote_asset"] = price_data_serie["PriceData"]["QuoteAsset"]
|
||||||
|
|
||||||
|
price_data["scale"] = (
|
||||||
|
price_data_serie["PriceData"]["Scale"]
|
||||||
|
if "Scale" in price_data_serie["PriceData"]
|
||||||
|
else ""
|
||||||
|
)
|
||||||
|
price_data["asset_price"] = (
|
||||||
|
price_data_serie["PriceData"]["AssetPrice"]
|
||||||
|
if "AssetPrice" in price_data_serie["PriceData"]
|
||||||
|
else ""
|
||||||
|
)
|
||||||
|
|
||||||
|
price_data_.append(price_data)
|
||||||
|
oracle_data["price_data_series"] = price_data_
|
||||||
|
oracles_.append(oracle_data)
|
||||||
|
|
||||||
|
|
||||||
|
print(oracles_)
|
||||||
82
_code-samples/price_oracles/py/create_price_oracle.py
Normal file
82
_code-samples/price_oracles/py/create_price_oracle.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
# neccesary imports
|
||||||
|
import datetime
|
||||||
|
from xrpl.wallet import generate_faucet_wallet
|
||||||
|
from xrpl.clients import JsonRpcClient
|
||||||
|
from xrpl.models import (
|
||||||
|
OracleSet,
|
||||||
|
)
|
||||||
|
from xrpl.transaction import submit_and_wait, sign_and_submit
|
||||||
|
from xrpl.models.transactions.oracle_set import PriceData
|
||||||
|
|
||||||
|
from xrpl.utils import str_to_hex
|
||||||
|
|
||||||
|
|
||||||
|
print("connecting to the test network")
|
||||||
|
# Connect to XRPL test network
|
||||||
|
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||||
|
print("connected!!!")
|
||||||
|
|
||||||
|
# create demo wallet
|
||||||
|
oracle_creator = generate_faucet_wallet(client=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: {oracle_creator.seed}")
|
||||||
|
|
||||||
|
# define the oracle document id
|
||||||
|
# this should be stored offline as the blockchain doesn't retrieve it in requests
|
||||||
|
oracle_document_id = 1
|
||||||
|
|
||||||
|
# define the provider's name and convert to hexadecimal e.g: band, chainlink etc
|
||||||
|
provider = str_to_hex("provider")
|
||||||
|
|
||||||
|
# define the uri of the provider and convert to hexadecimal
|
||||||
|
uri = str_to_hex("sampleprovider.com")
|
||||||
|
|
||||||
|
|
||||||
|
# define the last update time of the price data being passed to the oracle as a timestamp
|
||||||
|
# max time into the future is 5 minutes and max time into the past is 4 minutes from the current time
|
||||||
|
# we'll use the current date time for this
|
||||||
|
last_update_time = int(datetime.datetime.now().timestamp())
|
||||||
|
|
||||||
|
|
||||||
|
# define the asset class and convert to hexadecimal
|
||||||
|
# Describes the type of asset, such as "currency", "commodity", or "index".
|
||||||
|
asset_class = str_to_hex("currency")
|
||||||
|
|
||||||
|
|
||||||
|
# create a price data object, that will be tracked by the oracle
|
||||||
|
pd = PriceData(
|
||||||
|
base_asset="BTC",
|
||||||
|
quote_asset="USD",
|
||||||
|
asset_price=1000,
|
||||||
|
scale=4,
|
||||||
|
)
|
||||||
|
|
||||||
|
# create an array of up to 10 Price data objects
|
||||||
|
price_data_array = [pd]
|
||||||
|
|
||||||
|
|
||||||
|
print("building transaction")
|
||||||
|
# create price oracle transaction
|
||||||
|
oracle_set = OracleSet(
|
||||||
|
account=oracle_creator.address,
|
||||||
|
oracle_document_id=oracle_document_id,
|
||||||
|
provider=provider,
|
||||||
|
uri=uri,
|
||||||
|
last_update_time=last_update_time,
|
||||||
|
asset_class=asset_class,
|
||||||
|
price_data_series=price_data_array,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
print("signing and submitting transaction, awaiting response")
|
||||||
|
# sign, submit, and wait forthe transaction result
|
||||||
|
oracle_set_txn_response = submit_and_wait(
|
||||||
|
transaction=oracle_set, client=client, wallet=oracle_creator
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# print the result and transaction hash
|
||||||
|
print(oracle_set_txn_response.result["meta"]["TransactionResult"])
|
||||||
|
print(oracle_set_txn_response.result["hash"])
|
||||||
47
_code-samples/price_oracles/py/delete_price_oracle.py
Normal file
47
_code-samples/price_oracles/py/delete_price_oracle.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# neccesary imports
|
||||||
|
from xrpl.wallet import Wallet
|
||||||
|
from xrpl.clients import JsonRpcClient
|
||||||
|
from xrpl.models import (
|
||||||
|
OracleDelete,
|
||||||
|
)
|
||||||
|
from xrpl.transaction import submit_and_wait
|
||||||
|
|
||||||
|
|
||||||
|
print("connecting to the test network")
|
||||||
|
# Connect to XRPL test network
|
||||||
|
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 owns a Price Oracle object to delete: ")
|
||||||
|
|
||||||
|
# create demo wallet or use an existing one as created in the oracle set transaction
|
||||||
|
oracle_creator = Wallet.from_seed(seed=seed)
|
||||||
|
|
||||||
|
# define the oracle document id
|
||||||
|
# this should be stored offline as the blockchain doesn't retrieve it in requests
|
||||||
|
oracle_document_id = 1
|
||||||
|
|
||||||
|
|
||||||
|
print("building transaction")
|
||||||
|
# create price oracle delete transaction
|
||||||
|
oracle_set = OracleDelete(
|
||||||
|
account=oracle_creator.address,
|
||||||
|
oracle_document_id=oracle_document_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
print("signing and submitting transaction, awaiting response")
|
||||||
|
# sign, submit and wait for transaction result
|
||||||
|
oracle_set_txn_response = submit_and_wait(
|
||||||
|
transaction=oracle_set, client=client, wallet=oracle_creator
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# print the result and transaction hash
|
||||||
|
print(oracle_set_txn_response.result["meta"]["TransactionResult"])
|
||||||
|
print(oracle_set_txn_response.result["hash"])
|
||||||
|
|
||||||
|
# check if the transaction was successful
|
||||||
|
if oracle_set_txn_response.result["meta"]["TransactionResult"] == "tesSUCCESS":
|
||||||
|
print("oracle deleted successfully")
|
||||||
1
_code-samples/price_oracles/py/requirements.txt
Normal file
1
_code-samples/price_oracles/py/requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
xrpl-py==4.0.0
|
||||||
Reference in New Issue
Block a user