2020-12-01 13:08:14 +01:00
""" Deploys accounts index, registering arbitrary number of writers
. . moduleauthor : : Louis Holbrook < dev @holbrook.no >
. . pgp : : 0826 EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
2020-12-01 23:51:17 +01:00
import os
2020-12-01 13:08:14 +01:00
import json
import argparse
import logging
# third-party imports
import web3
2021-01-10 20:40:29 +01:00
from crypto_dev_signer . eth . signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer . keystore import DictKeystore
from crypto_dev_signer . eth . helper import EthTxExecutor
2020-12-01 13:08:14 +01:00
logging . basicConfig ( level = logging . WARNING )
logg = logging . getLogger ( )
logging . getLogger ( ' web3 ' ) . setLevel ( logging . WARNING )
logging . getLogger ( ' urllib3 ' ) . setLevel ( logging . WARNING )
2020-12-11 16:48:38 +01:00
script_dir = os . path . dirname ( __file__ )
data_dir = os . path . join ( script_dir , ' .. ' , ' data ' )
2020-12-01 13:08:14 +01:00
argparser = argparse . ArgumentParser ( )
argparser . add_argument ( ' -p ' , ' --provider ' , dest = ' p ' , default = ' http://localhost:8545 ' , type = str , help = ' Web3 provider url (http only) ' )
2021-01-10 20:40:29 +01:00
argparser . add_argument ( ' -w ' , action = ' store_true ' , help = ' Wait for the last transaction to be confirmed ' )
argparser . add_argument ( ' -ww ' , action = ' store_true ' , help = ' Wait for every transaction to be confirmed ' )
argparser . add_argument ( ' -i ' , ' --chain-spec ' , dest = ' i ' , type = str , default = ' Ethereum:1 ' , help = ' Chain specification string ' )
argparser . add_argument ( ' --writer ' , dest = ' writer ' , action = ' append ' , type = str , help = ' Writer to add ' )
argparser . add_argument ( ' -a ' , ' --signer-address ' , dest = ' a ' , type = str , help = ' Signing address ' )
argparser . add_argument ( ' -y ' , ' --key-file ' , dest = ' y ' , type = str , help = ' Ethereum keystore file to use for signing ' )
argparser . add_argument ( ' --account ' , action = ' append ' , type = str , help = ' Account to add ' )
argparser . add_argument ( ' --keep-sender ' , dest = ' keep_sender ' , action = ' store_true ' , help = ' If set, sender will be kept as writer ' )
2020-12-11 16:48:38 +01:00
argparser . add_argument ( ' --abi-dir ' , dest = ' abi_dir ' , type = str , default = data_dir , help = ' Directory containing bytecode and abi (default: {} ) ' . format ( data_dir ) )
2020-12-01 13:08:14 +01:00
argparser . add_argument ( ' -v ' , action = ' store_true ' , help = ' Be verbose ' )
2021-01-10 20:40:29 +01:00
argparser . add_argument ( ' -vv ' , action = ' store_true ' , help = ' Be more verbose ' )
2020-12-01 13:08:14 +01:00
args = argparser . parse_args ( )
2021-01-10 20:40:29 +01:00
if args . vv :
2020-12-01 13:08:14 +01:00
logg . setLevel ( logging . DEBUG )
2021-01-10 20:40:29 +01:00
elif args . v :
logg . setLevel ( logging . INFO )
block_last = args . w
block_all = args . ww
w3 = web3 . Web3 ( web3 . Web3 . HTTPProvider ( args . p ) )
signer_address = None
keystore = DictKeystore ( )
if args . y != None :
logg . debug ( ' loading keystore file {} ' . format ( args . y ) )
signer_address = keystore . import_keystore_file ( args . y )
logg . debug ( ' now have key for signer address {} ' . format ( signer_address ) )
signer = EIP155Signer ( keystore )
chain_pair = args . i . split ( ' : ' )
chain_id = int ( chain_pair [ 1 ] )
helper = EthTxExecutor (
w3 ,
signer_address ,
signer ,
chain_id ,
block = args . ww ,
)
2020-12-01 13:08:14 +01:00
2021-01-10 20:40:29 +01:00
def main ( ) :
2020-12-22 14:55:04 +01:00
f = open ( os . path . join ( args . abi_dir , ' AccountsIndex.json ' ) , ' r ' )
2020-12-01 23:51:17 +01:00
abi = json . load ( f )
2020-12-01 13:08:14 +01:00
f . close ( )
2020-12-11 16:48:38 +01:00
f = open ( os . path . join ( args . abi_dir , ' AccountsIndex.bin ' ) , ' r ' )
2020-12-01 23:51:17 +01:00
bytecode = f . read ( )
2020-12-01 13:08:14 +01:00
f . close ( )
c = w3 . eth . contract ( abi = abi , bytecode = bytecode )
2021-01-10 20:40:29 +01:00
( tx_hash , rcpt ) = helper . sign_and_send (
[
c . constructor ( ) . buildTransaction
] ,
force_wait = True ,
)
2020-12-01 13:08:14 +01:00
address = rcpt . contractAddress
2021-01-10 20:40:29 +01:00
2020-12-01 13:08:14 +01:00
c = w3 . eth . contract ( abi = abi , address = address )
logg . debug ( ' adding sender to write list ' )
2021-01-10 20:40:29 +01:00
( tx_hash , rcpt ) = helper . sign_and_send (
[
c . functions . addWriter ( signer_address ) . buildTransaction
] ,
force_wait = True ,
)
2020-12-01 13:08:14 +01:00
2021-01-10 20:40:29 +01:00
if args . writer != None :
for w in args . writer :
2020-12-01 13:08:14 +01:00
logg . info ( ' adding {} to write list' . format ( w ) )
2021-01-10 20:40:29 +01:00
( tx_hash , rcpt ) = helper . sign_and_send (
[
c . functions . addWriter ( w ) . buildTransaction
] ,
)
if args . account != None :
for a in args . account :
2020-12-01 13:08:14 +01:00
logg . info ( ' adding {} to accounts index' . format ( a ) )
2021-01-10 20:40:29 +01:00
( tx_hash , rcpt ) = helper . sign_and_send (
[
c . functions . add ( a ) . buildTransaction
] ,
)
2020-12-01 13:08:14 +01:00
2021-01-10 20:40:29 +01:00
if not args . keep_sender :
2020-12-01 13:08:14 +01:00
logg . debug ( ' deleting sender for write list ' )
2021-01-10 20:40:29 +01:00
( tx_hash , rcpt ) = helper . sign_and_send (
[
c . functions . deleteWriter ( signer_address ) . buildTransaction
] ,
)
2020-12-01 13:08:14 +01:00
print ( address )
2020-12-01 23:51:17 +01:00
if __name__ == ' __main__ ' :
main ( )