There are 2 ways to interact with this library.
-
With Cardano node and Cardano wallet installed (installation instruction in separate file) you can just simply install the CardanoPython SDK and start using it: Cardano_installation.md.
-
If you want to use this library but don't have installed Cardano node or Cardano wallet, then you need to configure your server/interface as a client API. We are using IOT core to open a websocket subscription to allow communication with our infrastructure. Instructions below.
- Cardano-node running
- Cardano wallet running. Some of the built-in functionalities can be covered from CLI but there are some functionalities that we want to make them through the API wallet.
This library relies on a file called config_file.json
{"node":{
"keys_path": "./priv/wallets",
"transactions": "./priv/transactions",
"CARDANO_NETWORK": "testnet",
"CARDANO_NETWORK_MAGIC": 1097911063,
"CARDANO_CLI_PATH": "cardano-cli",
"URL": "http://localhost:8090/v2/wallets/"
}
}
There are 3 classes in the library all contained in the file node_lib.py: Node, Wallet, IOT
a. Wallet class
List of methods:
- list_wallets
- generate_mnemonic
- create_wallet
- wallet_info
- get_addresses
- delete_wallet
- min_fees
- send_transaction
- confirm_transaction
- assets_balance
b. Node class
- query_protocol
- query_tip
- get_transactions
- get_balance
- minting
To instantiate the class, just call it with the working directory param. The working directory path must contain the config_file.json file.
For example:
from node_lib import Wallet
working_dir = "/home/cardanodatos/git/cnft-python/"
wallet = Wallet(working_dir)
list_wallets = wallet.list_wallets()
print(list_wallets)
mnemonic = wallet.generate_mnemonic(24)
print(mnemonic)
For a full list of methods available and the usage see: wallet_lib_use
- AWS IOT Core configure. Basic instructions are explained here.
Steps to configure basic communication (subscribe and connect)
In AWS account:
- Create a thing
- Create certificates and attach to thing
- Create policyID and attach to the certificates
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Connect",
"Resource": "arn:<your specific end_point>:client/${iot:Connection.Thing.ThingName}"
},
{
"Effect": "Allow",
"Action": "iot:Publish",
"Resource": "arn:aws:<your specific end_point>:topic/sensor/data"
}
]
}
- Upload certificates to the remote "sensor" (Private and public keys, and root CA certificates Starfield Root CA Certificate)
- (Optional) Start the app (AWS-thing.py)
If you subscribe in AWS IoT, you should see a return message similar to:
{
"deviceid": "1",
"datatime": "2021-09-24 10:48:41",
"temperature": "11",
"humidity": "90",
"windDirection": "5",
"windIntensity": "28",
"rainHeight": "23"
}
Use this file as reference:
https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/pubsub.py
In addition to the previous steps in the basic setup and to start the pubsub using the websocket option
- Configure IAM user to allow the use of websockets with the sdk according to this guideline:
Ref: https://docs.aws.amazon.com/iot/latest/developerguide/device-advisor-setting-up.html
- Create the files in ~/.aws/credentials and ~/.aws/config
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
[default]
region=us-west-2
output=json
Ref: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html
-
(Optional) In AWS CloudShell you could do:
aws configure
And follow the steps from the shell.
- Create additional folders to contain certificates and secrets
It is needed cert, key and root-ca files. (Similar to Point 4 of the AWS basic setup)
Source CARDANO_NODE_SOCKET_PATH to bashrc
echo "export CARDANO_NODE_SOCKET_PATH=/opt/cardano/cnode/sockets/node0.socket" >> ~/.bashrc
Commands are sent in json format. The fields are:
seq (type: int): Indicates the number of messages to receive to build the command. Currently 1 is used which means that all the information needed to build the command is sent in one json.
cmd_id (type: string): command identifier
message (type: string): in case any additional parameter is needed to build the command i.e. wallet address.
- Query tip
{
"seq": 1,
"cmd_id": "query_tip",
"message": ""
}
- List all known wallets
{
"seq": 1,
"cmd_id": "list_wallets",
"message": ""
}
- generate_new_mnemonic_phrase
{
"seq": 1,
"cmd_id": "generate_new_mnemonic_phrase",
"message": {
"size": 24
}
}
- generate wallet
{
"seq": 1,
"cmd_id": "generate_wallet",
"message": {
"wallet_name": "wallet_name",
"passphrase": "passphrase",
"mnemonic": "mnemonic",
}
}
- wallet info
{
"seq": 1,
"cmd_id": "wallet_info",
"message": {
"id": "id"
}
}
- min fees
{
"seq": 1,
"cmd_id": "min_fees",
"message": {
"id": "2781d44e82ad834750c8fd2654faccd2db912eaa",
"tx_info": {
"payments": [
{
"address": "addr_test1qp09lnuch5vgswuxcjlta78mlp88taudhgymyktu3qy44pk8tuvg9dmy75z2dpj0e4kzw642e3hpjt937e4t3jun3l3sftz57e",
"amount": {
"quantity": 5000000,
"unit": "lovelace"
},
"assets": null
}
],
"metadata": null
}
}
}
- Send transactions
{
"seq": 1,
"cmd_id": "send_transaction",
"passphrase": "Contraseña de gastos",
"message": {
"id": "987f6d81f4f72c484f6d34c53e7d7f2719f40705",
"tx_info": {
"payments": [
{
"address": "addr_test1qp09lnuch5vgswuxcjlta78mlp88taudhgymyktu3qy44pk8tuvg9dmy75z2dpj0e4kzw642e3hpjt937e4t3jun3l3sftz57e",
"amount": {
"quantity": 5000000,
"unit": "lovelace"
},
"assets": null
}
],
"metadata": null
}
}
}
- Confirm transaction
{
"seq": 1,
"cmd_id": "confirm_transaction",
"message": {
"id": "2781d44e82ad834750c8fd2654faccd2db912eaa"
}
}
- Confirm transaction by tx id
{
"seq": 1,
"cmd_id": "confirm_transaction_by_tx",
"message": {
"id": "2781d44e82ad834750c8fd2654faccd2db912eaa",
"tx_id": "b9f044b9c1f4f981ede4f576b0aee37a2b3b0d153c924d69763daa392822c0ae"
}
}
- Mint token
{
"seq": 1,
"cmd_id": "mint_asset",
"message": {
"tx_info":{
"id": "689f3a6a8132d58fbefe9673bcbe39f1cd8307c4",
"metadata": {
"4567":{
"título": "Graduación",
"Institución": "ACME"
}
},
"mint": [
{
"name": "PruebaTolima",
"amount": 1,
"policyID":"",
},
],
}
}
}
- Delete wallet
{
"seq": 1,
"cmd_id": "delete_wallet",
"message": {
"id": "id"
}
}
- assets info
{
"seq": 1,
"cmd_id": "assets_balance",
"message": {
"id": "id"
}
}
- Get transactions
{
"seq": 1,
"cmd_id": "get_transactions",
"message": {
"address": "address"
}
}
- Generate policyID
{
"seq": 1,
"cmd_id": "create_minting_policy",
"message": {
"id": "id"
}
}