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
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) ' )
argparser . add_argument ( ' -w ' , ' --writer ' , dest = ' w ' , action = ' append ' , type = str , help = ' Writer to add ' )
2020-12-08 20:04:33 +01:00
argparser . add_argument ( ' -o ' , ' --owner ' , dest = ' o ' , type = str , help = ' Accounts index owner ' )
2020-12-01 13:08:14 +01:00
argparser . add_argument ( ' -a ' , ' --account ' , dest = ' a ' , action = ' append ' , type = str , help = ' Account to add ' )
argparser . add_argument ( ' -k ' , ' --keep-sender ' , dest = ' k ' , 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 ' )
args = argparser . parse_args ( )
if args . v :
logg . setLevel ( logging . DEBUG )
2020-12-01 23:51:17 +01:00
def main ( ) :
2020-12-01 13:08:14 +01:00
w3 = web3 . Web3 ( web3 . Web3 . HTTPProvider ( args . p ) )
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 ( )
w3 . eth . defaultAccount = w3 . eth . accounts [ 0 ]
2020-12-08 20:04:33 +01:00
if args . o != None :
2020-12-08 20:54:21 +01:00
w3 . eth . defaultAccount = args . o
logg . debug ( ' owner address {} ' . format ( w3 . eth . defaultAccount ) )
2020-12-01 13:08:14 +01:00
c = w3 . eth . contract ( abi = abi , bytecode = bytecode )
2020-12-08 20:54:21 +01:00
tx_hash = c . constructor ( ) . transact ( )
2020-12-01 13:08:14 +01:00
rcpt = w3 . eth . getTransactionReceipt ( tx_hash )
address = rcpt . contractAddress
c = w3 . eth . contract ( abi = abi , address = address )
logg . debug ( ' adding sender to write list ' )
c . functions . addWriter ( w3 . eth . accounts [ 0 ] ) . transact ( )
if args . w != None :
for w in args . w :
logg . info ( ' adding {} to write list' . format ( w ) )
c . functions . addWriter ( w ) . transact ( )
if args . a != None :
for a in args . a :
logg . info ( ' adding {} to accounts index' . format ( a ) )
c . functions . add ( a ) . transact ( )
if not args . k :
logg . debug ( ' deleting sender for write list ' )
2020-12-08 20:54:21 +01:00
c . functions . deleteWriter ( w3 . eth . defaultAccount ) . transact ( )
2020-12-01 13:08:14 +01:00
print ( address )
# fail = False
# try:
# c.functions.add(w3.eth.accounts[2]).transact({'from': w3.eth.accounts[1]})
# except:
# fail = True
# assert fail
#
# c.functions.addWriter(w3.eth.accounts[1]).transact({'from': w3.eth.accounts[0]})
# c.functions.add(w3.eth.accounts[2]).transact({'from': w3.eth.accounts[1]})
# c.functions.add(w3.eth.accounts[3]).transact({'from': w3.eth.accounts[1]})
#
# assert c.functions.count().call() == 3
# assert c.functions.accountsIndex(w3.eth.accounts[3]).call() == 2
# assert c.functions.accounts(2).call() == w3.eth.accounts[3]
#
# fail = False
# try:
# c.functions.add(w3.eth.accounts[3]).transact({'from': w3.eth.accounts[1]})
# except:
# fail = True
# assert fail
2020-12-01 23:51:17 +01:00
if __name__ == ' __main__ ' :
main ( )