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.
Read more about rationale behind this package on our blog post
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().
The replay attack protection introduced with EIP-155 is used by default. Set chain_id=None to force the legacy behaviour for backward compatibility.
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
Release files for blocksec2go-ethereum 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| blocksec2go-ethereum-0.2.0.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| blocksec2go_ethereum-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.6 kB
Release files / blocksec2go-ethereum-0.2.0.tar.gz
| Download URL | blocksec2go-ethereum-0.2.0.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c383af182a53771192428414f85ee8877adb8a80b2adcad5eadf0eb8619c32a1
|
|
BLAKE2b-256 checksum How to use checksums |
497f4d5c1c940f5bc77c81d585f78ca78a41841e7e2412404e435290ae44ac39
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1
|
Release files / blocksec2go_ethereum-0.2.0-py3-none-any.whl
| Download URL | blocksec2go_ethereum-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3a7bb388d4c4dda7a920e5f9f45aa11e64e857948f75f5c8acc499c146c01296
|
|
BLAKE2b-256 checksum How to use checksums |
981f62e9f34f75d5f04a8d84a6b49d62a8994023b6626deccaa2de243d670745
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1
|