In my last article about basic usage of SmartBCH with Python I've talked about how to get some general info from the block chain like current block number, block data, balance and transaction info. In this article I will explain how to create an account/address with private and public key/address so we can send and receive transactions.
Generate an Address
To generate an address, it's similar to how you do it on Ethereum. We here uses the eth_account
lib.
>>> from eth_account import Account
>>> import secrets
>>> priv = secrets.token_hex(32)
>>> private_key = "0x" + priv
>>> print ("SAVE BUT DO NOT SHARE THIS:", private_key)
SAVE BUT DO NOT SHARE THIS: 0x27bf4f3060b0f5d8350153a1fcfe88d840b7e1855b2a5904a9916d82697f26e1
>>> acct = Account.from_key(private_key)
>>> print("Address:", acct.address)
Address: 0xdc9850d0A682dA74B397c9cd2C316DF601Ac14B4
After importing the lib, we generated random private key using secrets.token_hex(32)
and added the 0x
prefix, then we extracted they public address using Account.from_key(private_key)
function.
Keep the private key safe, anyone who hold it can spend any amount in your balance, we will use it later to spend.
For more info check Generating an Ethereum address in Python .
Get some test coins
Head to the SmartBCH documentation about testnet. You should find a link for a faucet where you can get testnet coins, it could be used to preform tests on the testnet but has no value.
Let us check if the faucet have sent us coins to our newly created address, we first connect to the testnet work network:
w3 = Web3(Web3.HTTPProvider('http://35.220.203.194:8545'))
Then we ask for balance:
>>> w3.eth.get_balance('0xdc9850d0A682dA74B397c9cd2C316DF601Ac14B4')
100000000000000000
It gives results in wei, we convert it to something easier to understand:
>>> balance = w3.eth.get_balance('0xdc9850d0A682dA74B397c9cd2C316DF601Ac14B4')
>>> w3.fromWei(balance, 'ether')
0.1
Now it shows 0.1 sBCH (BCH on SmartBCH), we used ether as the library was designed to deal with Ethereum net.
Preparing a Transaction
We need an address to send too. You can duplicate the steps in the Generate address section to get a new one or you can chose another address from another source.
My new address is: 0x1E61A61C76a22172619B33547F6e1bd1E755cc13
To sign we provide the key to our account that has the fund from the faucet:
private_key = 0x27bf4f3060b0f5d8350153a1fcfe88d840b7e1855b2a5904a9916d82697f26e1
Then we create the transaction, if you are sending for the first time this should work.
transaction = {
'to': '0x1E61A61C76a22172619B33547F6e1bd1E755cc13',
'value': 10,
'gas': 26038,
'gasPrice': 1050000000,
'nonce': 0,
'chainId': 10001
}
to: the receiving address apparently .
value: the amount we want to send in wei.
gas: number of weis required by the network.
gas price: gas price required by the network in wei.
nonce: the number of transactions sent from this address.
chainId: the ID of the chain you are connected to.
For SmartBCH you can set gas to 26038
and gasPrice to 1050000000
for BCH transfer. Gas is the fee paid to the network.
Get the Nonce
Nonce will change with each transaction from the account. You can get the current nonce
w3.eth.getTransactionCount('0xdc9850d0A682dA74B397c9cd2C316DF601Ac14B4')
0
Get the ChainID
If you don't know the ChainID for smartBCH you can get it using this:
>>> w3.eth.chainId
10001
Signing a Transaction
After we got all the right details and set up our private_key
and transaction
variable we can no use them to sign a transaction.
signed = w3.eth.account.sign_transaction(transaction, private_key)
Sending the Raw Transaction
Now in singed
we have the raw transaction that we can send to the network. let us do it:
w3.eth.send_raw_transaction(signed.rawTransaction)
HexBytes('0xd13634b0390c483e99595806cdfdad2249059dcdead673aae1926baf8e8f3e18')
Our transaction have passed and we got the transaction ID. Congratulation!
Sending more transaction, nonce
Now if we want to send more transaction the previous transaction
data wouldn't work. We have to update the nonce number. So our second transaction would have the nonce field changed from 0 to 1.
transaction = {
'to': '0x1E61A61C76a22172619B33547F6e1bd1E755cc13',
'value': 10,
'gas': 26038,
'gasPrice': 1050000000,
'nonce': 1,
'chainId': 10001
}
then we can run the signing and broadcasting again:
signed = w3.eth.account.sign_transaction(transaction, private_key)
w3.eth.send_raw_transaction(signed.rawTransaction)
Notes
SmartBCH is a very interesting subject. I love the UTXO system for it's privacy, for ease of control of funds in different address belonging to one key and for the capability of using a post office to pay the fee of a token from a different wallet.
In BCH's SmartBCH we got benefits from both system. In my last article I got a great support form community who encouraged me to go further and write this article not so long after my previous one. Thank you.
Very useful article, I'll go read the first part!👍