# Python SDK

# Install

# Build from source code

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

# Usage

# 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)

Note:replace your own url parameter

# Use case

# Create private key

alias = "test"
password = "123456"

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

# Create an account

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

# Create a receiver

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

# Create assets

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=""))

# Put assets on-chain

# Build transaction

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)

# Sign transaction

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

# Submit transaction

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

# More use case

More use case:More usage (opens new window)

Test case: API_test (opens new window)

Last Updated: 9/2/2021, 5:15:30 PM