Wrapper for blocksec2go allowing easy hardware-based signing of Ethereum transactions
Project description
Blocksec2Go-Ethereum
This repository contains the source code of blocksec2go-ethereum
Python package which wraps the blocksec2go
package to allow easier interaction with Ethereum blockchain.
If you're unsure what Blockchain Security 2 Go is, you can find more info here.
Installation
pip install blocksec2go-ethereum
Usage
After creating an instance of Blocksec2Go
you can use it to generate signatures for transaction dicts. When passed raw tx, sign_transaction()
will return a hex string of RLP-encoded signed transaction that can be directly consumed by web3.eth.sendRawTransaction()
.
Transfer Ether
Below you will find an example of signing a simple Ether transfer:
from blocksec2go_ethereum import Blocksec2GoSigner
from web3 import Web3
WEB3_ENDPOINT = 'YOUR_ENDPOINT_HERE'
web3 = Web3(Web3.HTTPProvider(WEB3_ENDPOINT))
chain_id = web3.eth.chainId
signer = Blocksec2GoSigner(chain_id=chain_id, key_id=1)
address = signer.get_address()
nonce = web3.eth.getTransactionCount(address)
raw_tx = {
'to': '0xBaBC446aee039E99d624058b0875E519190C6758',
'nonce': nonce,
'value': Web3.toWei(0.00005, 'ether'),
'gas': 21000,
'gasPrice': Web3.toWei(5, 'gwei'),
}
signed_tx = signer.sign_transaction(raw_tx)
tx_hash = web3.eth.sendRawTransaction(signed_tx)
print(f'Sent transaction with hash: {tx_hash.hex()}')
Call a contract function
You can also sign any contract calls/creation transactions by leveraging buildTransaction()
.
Please note that for some contracts buildTransaction()
may require explicitly setting from
field to properly estimate gas.
import json
from blocksec2go_ethereum import Blocksec2GoSigner
from web3 import Web3
WEB3_ENDPOINT = 'YOUR_ENDPOINT_HERE'
web3 = Web3(Web3.HTTPProvider(WEB3_ENDPOINT))
chain_id = web3.eth.chainId
signer = Blocksec2GoSigner(chain_id=chain_id, key_id=1)
address = signer.get_address()
with open('artifact.json', 'r') as artifact_file:
artifact = json.loads(artifact_file.read())
contract = web3.eth.contract(address=artifact['address'], abi=artifact['abi'])
nonce = web3.eth.getTransactionCount(address)
raw_tx = contract.functions.setValue(42).buildTransaction({'nonce': nonce, 'from': address})
signed_tx = signer.sign_transaction(raw_tx)
tx_hash = web3.eth.sendRawTransaction(signed_tx)
print(f'Sent transaction with hash: {tx_hash.hex()}')
License
ISC © 2020 rexs.io
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for blocksec2go-ethereum-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28ba2c2ec2907c7de5c2325648a5cad400085249afbd04d3008e040bae2c357e |
|
MD5 | cba69849b125043d3acb2962d92e1bae |
|
BLAKE2b-256 | 0ee26336601b4142dceb90540f8629b26c62b99ab328c92240bef08c1227937d |
Hashes for blocksec2go_ethereum-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0019addf72ed0d0a505c784c44aa62536eb90e65d3f00120a9628dd92f57ec1 |
|
MD5 | de5b3aa2034e5322ab07cdd2a33801a9 |
|
BLAKE2b-256 | 071b6d2af97ea68adcb469fa1a285a031391a5cc1960f0f546bef7a3eb0f8a2a |