# Python SDK

# 安装

# 从源代码构建

git clone https://github.com/Bytom/python-bytomlib ./python-bytomlib
cd python-bytomlib
sudo python3 setup.py install

# 使用方式

# coding:utf-8

import time

from pybtmsdk import BytomAPI
from pybtmsdk.transaction import decode_raw_tx, encode_raw_tx
from pybtmsdk.signature import generate_signatures_use_mnemonic

url = 'http://127.0.0.1:9888'
access_token = 'YOUR_ACCESS_TOKEN'

api = BytomAPI(url=url)

注意:你需要修改您使用的URL地址

# 使用案例

# 创建密钥

alias = "test"
password = "123456"

my_key = api.create_key(alias, password, "en")

# 创建账户

alias = "sender-account"
quorum = 1
root_xpubs = [my_key.xpub]
  
receiverAccount = api.create_account(root_xpubs=root_xpubs, alias=alias, quorum=quorum)

# 创建地址

alias = receiverAccount.alias
id = receiverAccount.id
receiver = api.create_account_receiver(account_alias=alias, account_id=id)

# 创建资产

alias = "receiver-asset"
root_xpubs = [receiverAccount.xpubs]
# issuance_program = "0014462adc0c3aa6ad7b12e110b03477acded3455aa3"
receiverAsset = api.create_asset(alias=alias, root_xpubs=root_xpubs, quorum=1, definition={}, limit_height=100, issuance_program=""))

# 资产上链

# 创建交易

actions = [
    {
        "account_id": "1SA0QDN500A02",
        "amount": 4000000,
        "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        "type": "spend_account"
    },
    {
        "amount": 2000000,
        "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        "address": "sm1qfgrgle0ljfxs0zlprf5q02zs9v3zdrcv6tmuvx",
        "type": "control_address"
    }
]

transaction = api.build_transaction(base_transaction=None, actions=actions, ttl=10, time_range=int(time.time(), return_dict=True)

# 签名交易

signed_transaction = api.sign_transaction(password="123456", transaction=transaction, return_dict=True)

# 提交交易

submit_response = api.submit_transaction(raw_transaction=signed_transaction["transaction"]["raw_transaction"])

# 更多的使用案例

更多案例:More usage (opens new window)

测试用例: API_test (opens new window)

上次更新: 2021/9/2下午5:15:30