Jacky Gu

Chialisp开发笔记

06 Jun 2021 Share to

https://github.com/Quexington/chialisp_dev_utility

按文档运行helloworld.py后,将standard_transaction.py复制到My-Chia-Project中。

运行:sudo python3 standard_transaction.py,即可运行转账操作。

注意,这是运行在自建的节点上。

chia用py写 跟踪交易执行代码,是调用rpc

wallet_rpc_client.py => send_transaction {
    // 用self.fetch方式调用RPC
    wallet_rpc_api.py => send_transaction {
        wallet.py => tx: TransactionRecord = wallet.generate_signed_transaction
        wallet.py => wallet.push_transaction(tx) {
            wallet_state_manager.py => add_pending_transaction() {
                wallet_transaction_store.py => add_transaction_record()
                wallet_state_manager.py => tx_pending_changed()
                wallet_state_manager.py => state_changed()
            }
        }
        return tx
    }
}

如何通过RPC执行clvm合约???


Chia研习路径与计划:

第一阶段:

  • 任务:
    • 组织人员
    • 学习Chia开发
  • 时间:6月底前

1- 建立testnet节点并跑通tests程序

  • 目的:学习chia的基本调用方法,并了解框架

如:wallet/cc_wallet/test_cc_wallet.py, 了解如何发币,如何交易。

2- 跑通用postman调用rpc,包括读取和写入链数据

  • 目的:改成网页端可以访问链的js组件
  • 难点:ssl
  • 参考:
    • https://github.com/Chia-Network/chia-blockchain/wiki 最重要
    • https://www.chia.net/faq/
    • https://github.com/Chia-Network/chia-blockchain/wiki/RPCExamples
    • https://github.com/Chia-Network/chia-blockchain/wiki/RPC-Interfaces
    • https://github.com/freddiecoleman/chia-client JavaScript,已经有了

3- 在钱包中跑通官方几个clialisp智能交易应用

  • 目的:了解chia合约应用开发与clvm调用

第二阶段

  • 应用接口:
    • 完成开发教程;
    • 完成js接口chia.js,通过chia.js方式实现与链的交互(不知官方有没有这个计划);
    • 启动网页版钱包,支持主网币、彩色币等转账;
    • 启动Layer2(EVM),与Chia主链完成资产交换,在Layer2上实现方便快捷的DEFI生态;
  • Chia生态基金
    • 支持创新应用。
    • 第一期规模1000XCH,矿业资本占60%,项目官方40%
    • 用途:
      • js接口:100XCH
      • 网页版钱包:350XCH
      • Layer2:550XCH

研习社计划

  • 参与者:
    • 熟悉Python
    • 看好Chia未来的价值和生态应用
  • 人数:2-3人

RPC学习笔记

chia节点和服务带有一个支持JSON的RPC API服务,允许访问Chia链。可以通过HTTP、WebSockets或python客户端访问。

这些端口可以在~/.chia/mainnet/config/config.yaml中配置。RPC端口不会被暴露在互联网上,TLS证书被用来保证通信安全。

全球开放的Chia节点查询:

North Asia introducer-apne.chia.net:8444
South Asia introducer-apse.chia.net:8444
Western North America: introducer-or.chia.net:8444
Eastern North America introducer-va.chia.net:8444
Europe: introducer-eu.chia.net:8444

https://chia.keva.app 动态更新

所有开放节点端口8444,测试网58444

默认端口:

Default Ports:
Daemon: 55400
Full Node: 8555
Farmer: 8559
Harvester: 8560
Wallet: 9256

HTTP/JSON

从命令行调用RPC时必须使用证书。所有的节点都是用JSON格式的POST进行的。响应是一个带有successTrue/False字段的JSON字典。

JavaScript

参考js客户端: https://github.com/freddiecoleman/chia-client

WebSocket

调用格式:

{
    "command": "get_blockchain_state",
    "ack": false,
    "data": {},
    "request_id": "123456",
    "destination": "wallet",
    "origin": "ui",
}

Python

参考客户端源码,目录在:chia/rpc

或者查看命令行源码,目录在: chia/cmds

RPC文档

1- FullNode

1.1- get_blockchain_state

  • 参数:无
  • 实例
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/full_node/private_full_node.crt \
    --key ~/.chia/testnet_7/config/ssl/full_node/private_full_node.key \
    -d '{}' -H "Content-Type: application/json" -X POST https://localhost:8555/get_blockchain_state | python -m json.tool
    

1.2- get_block

  • 参数:
    • header_hash
  • 实例
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/full_node/private_full_node.crt \
    --key ~/.chia/testnet_7/config/ssl/full_node/private_full_node.key \
    -d '{"header_hash": "0x278eb26be9d36be51748ca459bd285cf3d8538f20ab880c2bc660126aab66969"}' \
    -H "Content-Type: application/json" -X POST https://localhost:8555/get_block | python -m json.tool
    

1.3- get_blocks

1.4- get_block_record_by_height

1.5- get_block_record

1.6- get_block_records

1.7- get_unfinished_block_headers

1.8- get_network_space

1.9- get_additions_and_removals

1.10- get_initial_freeze_period

1.11- get_network_info

1.12- get_coin_records_by_puzzle_hash

1.13- get_coin_records_by_puzzle_hashes

1.14- get_coin_record_by_name

1.15- push_tx

  • 参数:
    • spend_bundle: spend bundle to submit, in JSON
  • 返回:
    {"status": "SUCCESS"}
    
  • spend_bundle数据格式
    "spend_bundle": {
      "aggregated_signature": "0xa5e5ea1f5ae2335a72fe0a7ed7ca39e8f142e2e1f6e37a348482290e88eb9cea2d973acf6145e34d0afeee7ba22f99850641e21a549b2c092bb49aa393acd938825bccca9413c1a268ba44367bc8433cd0fc0eb82e87bebe23817aa695bdb566",
      "coin_solutions": [
          {
              "coin": {
                  "amount": 1750000000000,
                  "parent_coin_info": "0xccd5bb71183532bff220ba46c268991a00000000000000000000000000004082",
                  "puzzle_hash": "0x94c6db00186900418ef7c1f05e127ee1a647cbe6e514ec3bc57acb7bbe6dfb10"
              },
              "puzzle_reveal": "0xff02ffff01ff02ffff01ff02ffff03ff0bffff01ff02ffff03ffff09ff05ffff1dff0bffff1effff0bff0bffff02ff06ffff04ff02ffff04ff17ff8080808080808080ffff01ff02ff17ff2f80ffff01ff088080ff0180ffff01ff04ffff04ff04ffff04ff05ffff04ffff02ff06ffff04ff02ffff04ff17ff80808080ff80808080ffff02ff17ff2f808080ff0180ffff04ffff01ff32ff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff06ffff04ff02ffff04ff09ff80808080ffff02ff06ffff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff018080ffff04ffff01b0aec9c2e5984fe928406abca942d55ec6b56340af8315bfefa55889dbaade669b9fd3f330af2af44c2a0626d383e64757ff018080",
              "solution": "0xff80ffff01ffff33ffa03fa549a708302b401c45cf387f8f03b4f76b7c9eabf567bea974f61dedf721e0ff840098968080ffff33ffa055b9fe4c9ce0cef8ad574bf5a9158dc0db7848b96be1a98ab2806d8f0a376a08ff860197738845808080ff8080"
          }
      ]
    },
    

    通过create_signed_transaction的返回值中获得

1.16- get_all_mempool_tx_ids

  • 参数:无
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/full_node/private_full_node.crt \
    --key ~/.chia/testnet_7/config/ssl/full_node/private_full_node.key \
    -d '{}' \
    -H "Content-Type: application/json" -X POST https://localhost:8555/get_all_mempool_tx_ids | python -m json.tool
    

1.17- get_all_mempool_items

1.18- get_mempool_item_by_tx_id

2- Standard Wallet

2.1- log_in

登录到钥匙包中的某个指定key

  • 参数:
    • fingerprint:key对应的指纹
    • type: start restore_backup
    • host: 使用 “https://backup.chia.net”
    • file_path:如果typerestore_backup,需要指定钥匙备份目录
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"fingerprint": 1345224564, "type": "start", "host": "https://backup.chia.net"}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/log_in | python -m json.tool
    

2.2- get_public_keys

  • 参数:无
  • 对应cli:chia keys show
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/get_public_keys | python -m json.tool
    

2.3- get_private_key

  • 参数:
    • fingerprint: 指纹数字
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"fingerprint": 1345224564}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/get_private_key | python -m json.tool
    

2.4- generate_mnemonic

生成新的助记词(私钥),注意尚未添加到钥匙包中。

  • 参数:无
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/generate_mnemonic | python -m json.tool
    

2.5- add_key

将助记词私钥添加到钥匙包中

  • 参数:
    • mnemonic
    • type: new_wallet
  • 对应cli:chia keys generate
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"mnemonic": [
          "slide",
          "jungle",
          "canal",
          ...
      ], "type": "new_wallet"}' -H "Content-Type: application/json" -X POST https://localhost:9256/add_key
    

2.6- delete_key

从钥匙包中删除key

  • 参数:
    • fingerprint
  • 对应cli:chia keys delete
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"fingerprint": 789546845}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/delete_key | python -m json.tool
    

2.7- delete_all_keys

删除所有keys,慎用!

  • 参数:无
  • 对应cli:chia keys delete_all
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/delete_all_keys | python -m json.tool
    

2.8- get_sync_status

获取钱包同步状态

  • 参数:无
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key 
    -d '{}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/get_sync_status | python -m json.tool
    

2.9- get_height_info

获取当前区块高度

  • 参数:无

2.10- get_initial_freeze_period

获取初始冻结期

  • 参数:无

2.11- get_network_info

获取网络信息

  • 参数:无
  • 返回:
    {
      "network_name": "testnet7",
      "network_prefix": "txch",
      "success": true
    }
    

2.12- get_wallets

获取钱包管理器信息

  • 参数:无
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/get_wallets | python -m json.tool
    
  • 返回:
    {
      "success": true,
      "wallets": [
          {
              "data": "",
              "id": 1,
              "name": "Chia Wallet",
              "type": 0
          },
          {
              "data": "01ff02ffff01ff02ffff03ffff09ff5bff8080ffff01ff0101ffff01ff02ffff03ffff09ff13ff0280ffff01ff0101ff8080ff018080ff0180ffff04ffff01a09b3a8d052feec7f28d71910ed42c24aab8e8b396485f86a799014bd6f0bed5a6ff01808000000000",
              "id": 2,
              "name": "CC Wallet",
              "type": 6
          }
      ]
    }
    

2.13- create_new_wallet

在当前key下创建不同类型的新钱包。

可以通过ChiaLisp语言自定义特殊功能的钱包。

  • 参数:
    • host: IP或节点名称,如https://backup.chia.net
    • wallet_type:cc_wallet rl_wallet did_wallet
  • 1- 创建彩色币钱包:

    wallet_typecc_wallet,则还要添加mode属性,mode属性为new或者existing

    以及amount属性,即初始化数量(精度为1000)

curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
--key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
-d '{"host": "https://backup.chia.net", "wallet_type": "cc_wallet", "mode": "new", "amount": 1000000}' \
-H "Content-Type: application/json" -X POST https://localhost:9256/create_new_wallet | python -m json.tool

返回:

{
"colour": "ff02ffff01ff02ffff03ffff09ff5bff8080ffff01ff0101ffff01ff02ffff03ffff09ff13ff0280ffff01ff0101ff8080ff018080ff0180ffff04ffff01a09b3a8d052feec7f28d71910ed42c24aab8e8b396485f86a799014bd6f0bed5a6ff018080",
"success": true,
"type": 6,
"wallet_id": 2
}

添加成功后,再用get_walletsRPC命令查询,看到钱包列表中多了一个类型为CC_WALLET的钱包。

如果要为现有的彩色币创建钱包,则以上mode应为existing,对应的colour参数使用以上返回的colour值。如:

curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
--key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
-d '{"host": "https://backup.chia.net", "wallet_type": "cc_wallet", "mode": "existing", "colour": "ff02ffff01ff02ffff03ffff09ff5bff8080ffff01ff0101ffff01ff02ffff03ffff09ff13ff0280ffff01ff0101ff8080ff018080ff0180ffff04ffff01a09b3a8d052feec7f28d71910ed42c24aab8e8b396485f86a799014bd6f0bed5a6ff018080"}' \
-H "Content-Type: application/json" -X POST https://localhost:9256/create_new_wallet | python -m json.tool

执行完后,用get_wallets查看,则会发现又多了一个CC_WALLET类型的钱包。

  • 2- 创建rate-limited钱包:

    wallet_typerl_wallet

  • 3- 创建distributed identity wallets钱包:

    wallet_typedid_wallet

2.14- get_next_address

获取钱包新的地址

注意:首先需要选择当前钱包chia wallet show或者用log_in

  • 参数:
    • wallet_id:默认1
    • new_address: 默认true
      curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
      --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
      -d '{"wallet_id": 1, "new_address":true}' \
      -H "Content-Type: application/json" -X POST https://localhost:9256/get_next_address | python -m json.tool
      

2.15- get_wallet_balance

获取当前钱包余额

注意:首先需要选择当前钱包chia wallet show或者用log_in

  • 参数:
    • wallet_id:默认1
      curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
      --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
      -d '{"wallet_id": 1}' \
      -H "Content-Type: application/json" -X POST https://localhost:9256/get_wallet_balance | python -m json.tool
      

2.16- create_signed_transaction

创建签名交易

  • 参数:
    • additions:数组
      • amount:
      • puzzle_hash:
    • fee
    • coins
  • 返回:signed_tx
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"additions": [{"amount": 10000000, "puzzle_hash": "3fa549a708302b401c45cf387f8f03b4f76b7c9eabf567bea974f61dedf721e0"}]}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/create_signed_transaction | python -m json.tool
    

    获取返回值中的spend_bundle,作为push_tx的参数,广播交易。如下:

    curl --insecure --cert ~/.chia/testnet_7/config/ssl/full_node/private_full_node.crt \
    --key ~/.chia/testnet_7/config/ssl/full_node/private_full_node.key \
    -d '{        
          "spend_bundle": {
              "aggregated_signature": "0xb4b9b8a09feaf43dfb7d9c2650b09df273515d89607b07398370249abd8d97ac3c5b6417c909c3370b934576601662970f34e114025f23ace55d26bad8832a3e0a5f27def88c135190edaf429266a0d26bb7c5ec749df739b44e0ba05fc610bc",
              "coin_solutions": [
                  {
                      "coin": {
                          "amount": 88000000000000,
                          "parent_coin_info": "0x5fe426d0461b5cec6f92d1a6585e34029563ae0c17e4e2a211684c28ba38c8fe",
                          "puzzle_hash": "0xf446f12f59d8bd5c7f9ac7189e8ce17853e476c8e292aebc9a6be9bb544c771e"
                      },
                      "puzzle_reveal": "0xff02ffff01ff02ffff01ff02ffff03ff0bffff01ff02ffff03ffff09ff05ffff1dff0bffff1effff0bff0bffff02ff06ffff04ff02ffff04ff17ff8080808080808080ffff01ff02ff17ff2f80ffff01ff088080ff0180ffff01ff04ffff04ff04ffff04ff05ffff04ffff02ff06ffff04ff02ffff04ff17ff80808080ff80808080ffff02ff17ff2f808080ff0180ffff04ffff01ff32ff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff06ffff04ff02ffff04ff09ff80808080ffff02ff06ffff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff018080ffff04ffff01b0843313d267b34fd00b6f98a07b5e8b8539ae2c21e55b88d147bb164d96228eb81ac40661bee7a84b4d6e030c97168062ff018080",
                      "solution": "0xff80ffff01ffff33ffa03fa549a708302b401c45cf387f8f03b4f76b7c9eabf567bea974f61dedf721e0ff834c4b4080ffff33ffa0fd905a7451fa0d205de192b883c0f5cd47dbe4f92385e37cd7c8a05d8588c0beff865009187134c080ffff3cffa043c672e9cc574b48e23a0c21aa6164114d7e2cd0d31b6a06c5d0b251703ca05a8080ff8080"
                  }
              ]
          }}' \
          -H "Content-Type: application/json" -X POST https://localhost:8555/push_tx | python -m json.tool
    

2.17- send_transaction

发送标准钱包内的xch交易

  • 参数:
    • wallet_id
    • amount
    • address
    • fee
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"wallet_id": 1, "amount": 1000, "address": "txch1gn8zaepqmg4rk9s49xs6ms625vdk3k85sd3pk0jh2p65yvr2qfuqd07qn9"}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/send_transaction | python -m json.tool
    
  • 返回:transaction_id

2.18- get_transaction

获取交易明细

  • 参数:
    • transaction_id
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"transaction_id": "0xa507e88a32c722ec9d194b5ba91295d8e43e74ef61b44d686d5b58b497b1a48e"}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/get_transaction | python -m json.tool
    

2.19- get_transactions

获取所有交易

  • 参数:
    • wallet_id
    • start
    • end
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"wallet_id": 1, "start": 0, "end": 10}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/get_transactions | python -m json.tool
    

2.20- get_transaction_count

获取交易数量

  • 参数:
    • wallet_id
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"wallet_id": 1}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/get_transaction_count | python -m json.tool
    

2.21- get_farmed_amount

3- CC Wallet

3.1- cc_set_name

为彩色币钱包命名

  • 参数:
    • wallet_id
    • name
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"wallet_id": 2, "name": "GUGU"}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/cc_set_name | python -m json.tool
    

3.2- cc_get_name

获取彩色币钱包名称

  • 参数:
    • wallet_id
  • 实例:
    curl --insecure --cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt \
    --key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key \
    -d '{"wallet_id": 2}' \
    -H "Content-Type: application/json" -X POST https://localhost:9256/cc_get_name | python -m json.tool
    

3.3- cc_spend

支付彩色币

  • 参数:
    • wallet_id
    • inner_address
    • amount:注意金额精度位1000
    • fee (option)
  • 实例 ``` curl –insecure –cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt
    –key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key
    -d ‘{“wallet_id”: 2, “inner_address”: “txch173r0zt6emz74clu6cuvfar8p0pf7gakgu2f2a0y6d05mk4zvwu0qwqmhcf”, “amount”: 15}’
    -H “Content-Type: application/json” -X POST https://localhost:9256/cc_spend | python -m json.tool
* 注意:代码有bug,查看链接:https://github.com/Chia-Network/chia-blockchain/issues/6477

#### 3.4- cc_get_colour
获取彩色币钱包的colour
* 参数:
    * wallet_id
* 实例:

curl –insecure –cert ~/.chia/testnet_7/config/ssl/wallet/private_wallet.crt
–key ~/.chia/testnet_7/config/ssl/wallet/private_wallet.key
-d ‘{“wallet_id”: 2}’
-H “Content-Type: application/json” -X POST https://localhost:9256/cc_get_colour | python -m json.tool ```

3.5- create_offer_for_ids

3.6- respond_to_offer

3.7- get_discrepancies_for_offer

3.8- get_trade

3.9- get_all_trades

3.10- cancel_trade