analitics

Pages

Showing posts with label web3. Show all posts
Showing posts with label web3. Show all posts

Sunday, September 11, 2022

Python : Blockchain Programming - part 001.

This is the first tutorial in the blockchain programming series using the python programming language.
To program a blockchain, we must consider two elements: in addition to the blockchain address, it can have a programmable area and the second essential element, the interaction of the blockchain with external web areas can be programmed.
In this tutorial I will use the python web3 package, an etherium address and an online web utility called infura.io.
In the infura.io account, create a web3 project and in the dashboard - manage key you will have to add the ethereum address and use the url created to mainnet.infura.io.
I created a project in python in Fedora 37 using the conda utility and installed the web3 package.
Here is the python source code I used
from web3 import Web3
node_provider = "https://mainnet.infura.io/v3/1f2fb5d1e1be4c11acdbbb07a2e06a1c"

web3_connection = Web3(Web3.HTTPProvider(node_provider))


def is_connected():
    print(web3_connection.isConnected())

def latest_block():
    print(web3_connection.eth.block_number)

def balanceETH(ETH_address):
    balance = web3_connection.eth.get_balance(ETH_address)
    balance_for_ETH = web3_connection.fromWei(balance,'ether')
    print(balance_for_ETH)
Here is the answer to running this source code using the etherium address:
(web3_001) [mythcat@fedora PythonProjects]$ vi web3_func_001.py
(web3_001) [mythcat@fedora PythonProjects]$ python
Python 3.9.13 (main, Aug 25 2022, 23:26:10)
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from web3_func_001 import *
>>> is_connected()
True
>>> latest_block()
15516253
>>> balanceETH("0x74E55f28a8A0158b466FcB481EC7e6bE45D1DB91")
0
Since it is a rather complex field, I will come back with other tutorials when I have the necessary resources.

Friday, May 3, 2019

Python 3.7.3 : Try Ethereum with web3.

Today I tested the web3 python module.
This is the Ethereum python module.
More about this python module can be found here.
Ethereum programming has a large area of development.
For transaction you can use:
from web3 import Web3, HTTPProvider, IPCProvider, WebsocketProvider
I tested with an account from infura.io.
>>> from web3 import Web3, HTTPProvider, IPCProvider, WebsocketProvider
>>> w3 = Web3(HTTPProvider('ropsten.infura.io/v3/1f2fb5d1e1be4c11acdbbb07a2e06a1
c'))
>>> dir(w3)
['EthereumTesterProvider', 'HTTPProvider', 'IPCProvider', 'Iban', 'RequestManage
r', 'TestRPCProvider', 'WebsocketProvider', '__class__', '__delattr__', '__dict_
_', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__mo
dule__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__seta
ttr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_ens', 'adm
in', 'ens', 'eth', 'fromWei', 'isAddress', 'isChecksumAddress', 'isConnected', '
manager', 'middleware_stack', 'miner', 'net', 'parity', 'personal', 'providers',
 'sha3', 'soliditySha3', 'testing', 'toBytes', 'toChecksumAddress', 'toHex', 'to
Int', 'toText', 'toWei', 'txpool', 'version']
>>> from eth_account import Account
>>> my_account = Account.create('KEYSMASH FESTILA_GEORGE_CATALIN 1530')
>>> my_account.address
'0x2b8e7E92064F718B0AC91c381968baC8D1561d7d'
>>> # check the adresss and add it to infura.io
...
>>> address=Web3.toChecksumAddress(my_account.address)
>>> print (address)
0x2b8e7E92064F718B0AC91c381968baC8D1561d7d
Let's start with the default installation and some testing with a message:
C:\Python373\Scripts>pip install virtualenv
Requirement already satisfied: virtualenv in c:\python373\lib\site-packages (16.
5.0)
C:\Python373\Scripts>cd ..
C:\Python373>virtualenv env
Using base prefix 'c:\\python373'
New python executable in C:\Python373\env\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
(env) C:\Python373\Scripts>pip install web3
(env) C:\Python373\Scripts>cd ..
(env) C:\Python373>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from web3.auto import w3
>>> dir(w3)
['EthereumTesterProvider', 'HTTPProvider', 'IPCProvider', 'Iban', 'RequestManage
r', 'TestRPCProvider', 'WebsocketProvider', '__class__', '__delattr__', '__dict_
_', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__mo
dule__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__seta
ttr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_ens', 'adm
in', 'ens', 'eth', 'fromWei', 'isAddress', 'isChecksumAddress', 'isConnected', '
manager', 'middleware_stack', 'miner', 'net', 'parity', 'personal', 'providers',
 'sha3', 'soliditySha3', 'testing', 'toBytes', 'toChecksumAddress', 'toHex', 'to
Int', 'toText', 'toWei', 'txpool', 'version']
One example with message hashing mechanism :
>>> from web3.auto import w3
>>> from eth_account.messages import defunct_hash_message
>>> msg = "Hello world!"
>>> private_key = b"46474346474346474330313246474346"
>>> b"46474346474346474330313246474346".decode("utf-8", errors="ignore")
'46474346474346474330313246474346'
>>> signed_message = w3.eth.account.signHash(message_hash, private_key=private_
ey)
>>> signed_message
AttrDict({'messageHash': HexBytes('0xaa05af77f274774b8bdc7b61d98bc40da523dc2821f
dea555f4d6aa413199bcc'), 'r': 77081816383569854304871908993806785907809199379052
64818114708355336522791320, 's': 50065392922095131225302404096180206949656260456
560091946111418092912822128476, 'v': 27, 'signature': HexBytes('0x110aad1b6fa3f8
9ef0ceff3579de2a166c6cfefee73bd8b13364d71745f979986eb00219a1d6630a09fb0e7db3b713
51bce570d5842fa39fbae390fdfa7b0f5c1b')})