Differentiate between deactivate and remove in smart contract, reinstate indexed list

This commit is contained in:
lash 2023-02-12 05:47:47 +00:00
parent 69fe24e593
commit 2579abaf3f
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 202 additions and 177 deletions

View File

@ -1,5 +1,6 @@
* 0.3.3
- Rename cli script from deploy to publish
- Refactor cli scripts to use current chainlib setup
* 0.3.2
- Add remove option
* 0.3.0

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"addedAccount","type":"address"},{"indexed":true,"internalType":"uint256","name":"accountIndex","type":"uint256"}],"name":"AddressAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"addWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"deleteWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"entryCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"entryIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"have","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"remove","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressActive","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":true,"internalType":"uint256","name":"_accountIndex","type":"uint256"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressInactive","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_executor","type":"address"},{"indexed":false,"internalType":"address","name":"_account","type":"address"}],"name":"AddressRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"activate","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"addWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"deactivate","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_writer","type":"address"}],"name":"deleteWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_idx","type":"uint256"}],"name":"entry","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"entryCount","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"entryIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"have","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"isActive","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"remove","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

View File

@ -18,6 +18,18 @@ from chainlib.chain import ChainSpec
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from chainlib.eth.address import to_checksum_address
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
from chainlib.eth.cli.log import process_log
from chainlib.eth.settings import process_settings
from chainlib.settings import ChainSettings
# local imports
from eth_accounts_index.registry import AccountRegistry
@ -25,46 +37,52 @@ from eth_accounts_index.registry import AccountRegistry
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_positional('address', type=str, help='Address to add to registry')
def process_config_local(config, arg, args, flags):
address = to_checksum_address(config.get('_POSARG'))
config.add(address, '_ADDRESS')
return config
arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_WRITE | arg_flags.EXEC
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
argparser.add_argument('address', type=str, help='Address to add to registry')
args = argparser.parse_args()
extra_args = {
'address': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AccountRegistry.gas())
logg = process_log(args, logg)
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
config = Config()
config = process_config(config, arg, args, flags, positional_name='address')
config = process_config_local(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
def main():
signer = rpc.get_signer()
signer_address = rpc.get_sender_address()
conn = settings.get('CONN')
c = AccountRegistry(
settings.get('CHAIN_SPEC'),
signer=settings.get('SIGNER'),
gas_oracle=settings.get('FEE_ORACLE'),
nonce_oracle=settings.get('NONCE_ORACLE'),
)
gas_oracle = rpc.get_gas_oracle()
nonce_oracle = rpc.get_nonce_oracle()
(tx_hash_hex, o) = c.add(
settings.get('EXEC'),
settings.get('SENDER_ADDRESS'),
config.get('_ADDRESS'),
)
subject_address = to_checksum_address(config.get('_ADDRESS'))
if not config.true('_UNSAFE') and subject_address != add_0x(config.get('_ADDRESS')):
raise ValueError('invalid checksum address for subject_address')
contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
raise ValueError('invalid checksum address for contract')
c = AccountRegistry(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.add(contract_address, signer_address, subject_address)
if config.get('_RPC_SEND'):
if settings.get('RPC_SEND'):
conn.do(o)
if config.get('_WAIT'):
if config.true('_WAIT'):
r = conn.wait(tx_hash_hex)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')

View File

@ -19,6 +19,19 @@ from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from chainlib.eth.constant import ZERO_CONTENT
from chainlib.error import JSONRPCException
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
from chainlib.eth.cli.log import process_log
from chainlib.eth.settings import process_settings
from chainlib.settings import ChainSettings
from chainlib.eth.address import to_checksum_address
# local imports
from eth_accounts_index import AccountsIndex
@ -27,23 +40,34 @@ from eth_accounts_index.registry import AccountRegistry
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_positional('address', required=False, type=str, help='Check only whether given address is in registry')
def process_config_local(config, arg, args, flags):
address = config.get('_POSARG')
if address != None:
address = to_checksum_address(address)
config.add(address, '_ADDRESS')
return config
arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_READ | arg_flags.EXEC
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
argparser.add_argument('address', type=str, help='Address to add to registry')
args = argparser.parse_args()
extra_args = {
'address': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AccountRegistry.gas())
logg = process_log(args, logg)
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
config = Config()
config = process_config(config, arg, args, flags, positional_name='address')
config = process_config_local(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
def out_element(e, w=sys.stdout):
@ -71,13 +95,26 @@ def ls(ifc, conn, contract_address, w=sys.stdout):
def main():
conn = settings.get('CONN')
address = config.get('_ADDRESS')
contract_address = config.get('_EXEC_ADDRESS')
c = AccountsIndex(chain_spec)
c = AccountsIndex(
settings.get('CHAIN_SPEC')
)
if address != None:
element(c, conn, contract_address, address, w=sys.stdout)
element(
c,
conn,
settings.get('EXEC'),
address,
w=sys.stdout,
)
else:
ls(c, conn, contract_address, w=sys.stdout)
ls(
c,
conn,
settings.get('EXEC'),
w=sys.stdout,
)
if __name__ == '__main__':

View File

@ -8,8 +8,6 @@
# standard imports
import sys
import os
import json
import argparse
import logging
# external imports
@ -17,6 +15,18 @@ import chainlib.eth.cli
from chainlib.chain import ChainSpec
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
from chainlib.eth.cli.log import process_log
from chainlib.eth.settings import process_settings
from chainlib.settings import ChainSettings
# local imports
from eth_accounts_index.registry import AccountRegistry
@ -24,35 +34,42 @@ from eth_accounts_index.registry import AccountRegistry
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
arg_flags = chainlib.eth.cli.argflag_std_write
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_WRITE
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
args = argparser.parse_args()
config = chainlib.eth.cli.Config.from_args(args, arg_flags, default_fee_limit=AccountRegistry.gas())
logg = process_log(args, logg)
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
config = Config()
config = process_config(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
def main():
signer = rpc.get_signer()
signer_address = rpc.get_sender_address()
conn = settings.get('CONN')
gas_oracle = rpc.get_gas_oracle()
nonce_oracle = rpc.get_nonce_oracle()
c = AccountRegistry(
settings.get('CHAIN_SPEC'),
signer=settings.get('SIGNER'),
gas_oracle=settings.get('FEE_ORACLE'),
nonce_oracle=settings.get('NONCE_ORACLE'),
)
c = AccountRegistry(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.constructor(
settings.get('SENDER_ADDRESS'),
)
(tx_hash_hex, o) = c.constructor(signer_address)
if config.get('_RPC_SEND'):
if settings.get('RPC_SEND'):
conn.do(o)
if config.get('_WAIT'):
if config.true('_WAIT'):
r = conn.wait(tx_hash_hex)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')

View File

@ -1,86 +0,0 @@
"""Adds/removes writers to accounts index
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
import os
import json
import argparse
import logging
import sys
# external imports
import chainlib.eth.cli
from chainlib.chain import ChainSpec
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from chainlib.eth.address import to_checksum_address
from hexathon import add_0x
# local imports
from eth_accounts_index.registry import AccountRegistry
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_argument('--rm', action='store_true', help='Delete entry')
argparser.add_positional('address', type=str, help='Update writer array with this address')
args = argparser.parse_args()
extra_args = {
'rm': None,
'address': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AccountRegistry.gas())
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def main():
signer = rpc.get_signer()
signer_address = rpc.get_sender_address()
gas_oracle = rpc.get_gas_oracle()
nonce_oracle = rpc.get_nonce_oracle()
c = AccountRegistry(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
subject_address = to_checksum_address(config.get('_ADDRESS'))
if not config.true('_UNSAFE') and subject_address != add_0x(config.get('_ADDRESS')):
raise ValueError('invalid checksum address for subject_address')
contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
raise ValueError('invalid checksum address for contract')
if config.get('_RM'):
(tx_hash_hex, o) = c.delete_writer(contract_address, signer_address, subject_address)
else:
(tx_hash_hex, o) = c.add_writer(contract_address, signer_address, subject_address)
if config.get('_RPC_SEND'):
conn.do(o)
if config.get('_WAIT'):
r = conn.wait(tx_hash_hex)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1)
print(tx_hash_hex)
else:
print(o)
if __name__ == '__main__':
main()

View File

@ -42,5 +42,4 @@ testing =
console_scripts =
eth-accounts-index-publish = eth_accounts_index.runnable.publish:main
eth-accounts-index-add = eth_accounts_index.runnable.add:main
eth-accounts-index-writer = eth_accounts_index.runnable.writer:main
eth-accounts-index-list = eth_accounts_index.runnable.list:main

View File

@ -1,21 +1,27 @@
pragma solidity >0.6.11;
pragma solidity >0.6.12;
// SPDX-License-Identifier: GPL-3.0-or-later
// File-Version: 1
// File-Version: 2
contract CustodialAccountIndex {
uint256 public entryCount;
uint256 constant blockedField = 1 << 64;
uint64 public entryCount;
address[] entryList;
mapping(address => uint256) public entryIndex;
mapping(address => bool) writers;
address public owner;
address newOwner;
event AddressAdded(address indexed addedAccount, uint256 indexed accountIndex); // AccountsIndex
event AddressAdded(address indexed _executor, uint256 indexed _accountIndex, address _account); // AccountsIndex
event AddressActive(address indexed _executor, address _account);
event AddressInactive(address indexed _executor, address _account);
event AddressRemoved(address indexed _executor, address _account);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173
constructor() public {
owner = msg.sender;
entryList.push(address(0));
}
function addWriter(address _writer) public returns (bool) {
@ -32,20 +38,46 @@ contract CustodialAccountIndex {
// Implements AccountsIndex
function add(address _account) external returns (bool) {
uint256 i;
require(writers[msg.sender]);
require(entryIndex[_account] == 0);
entryIndex[_account] = block.timestamp;
entryCount = entryCount + 1;
emit AddressAdded(_account, block.timestamp);
require(entryList.length < (1 << 128));
i = entryList.length;
entryList.push(_account);
entryIndex[_account] = i;
entryCount += 1;
emit AddressAdded(msg.sender, i, _account);
return true;
}
// Implements AccountsIndex
function remove(address _account) external returns (bool) {
// Deactivate account, without removing the entry. The entry will still be part of the entry count.
function deactivate(address _account) external returns (bool) {
require(writers[msg.sender]);
require(entryIndex[_account] > 0 && entryIndex[_account] & blockedField == 0);
entryIndex[_account] <<= 65;
entryIndex[_account] |= blockedField;
emit AddressInactive(msg.sender, _account);
}
// Implements AccountsIndex
// Activate previously deactivated account. Will not affect the entry count.
function activate(address _account) external returns (bool) {
require(writers[msg.sender]);
require(entryIndex[_account] > 0 && entryIndex[_account] & blockedField == blockedField);
entryIndex[_account] >>= 65;
emit AddressActive(msg.sender, _account);
}
// Implements AccountsIndex
// (An account can remove itself).
function remove(address _account) external returns (bool) {
require(writers[msg.sender] || msg.sender == _account);
require(entryIndex[_account] > 0);
entryIndex[_account] = 0;
entryCount = entryCount - 1;
emit AddressRemoved(msg.sender, _account);
return true;
}
@ -54,23 +86,33 @@ contract CustodialAccountIndex {
return entryIndex[_account] > 0;
}
// Implements EIP173
function transferOwnership(address _newOwner) public returns (bool) {
require(msg.sender == owner);
newOwner = _newOwner;
// Implements AccountsIndex
function isActive(address _account) external view returns (bool) {
return this.have(_account) && entryIndex[_account] != blockedField;
}
// Implements OwnedAccepter
function acceptOwnership() public returns (bool) {
// Implements EIP173
function transferOwnership(address _newOwner) public returns (bool) {
address oldOwner;
require(msg.sender == owner);
require(msg.sender == newOwner);
oldOwner = owner;
owner = newOwner;
newOwner = address(0);
owner = _newOwner;
emit OwnershipTransferred(oldOwner, owner);
}
// Implements AccountsIndex
function entry(uint256 _idx) public returns(address) {
address r;
r = entryList[_idx + 1];
if (entryIndex[r] > 0) {
return r;
}
return address(0);
}
// Implements EIP165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0xcbdb05c7) { // AccountsIndex
@ -82,9 +124,6 @@ contract CustodialAccountIndex {
if (_sum == 0x9493f8b2) { // EIP173
return true;
}
if (_sum == 0x37a47be4) { // OwnedAccepter
return true;
}
if (_sum == 0x80c84bd6) { // Writer
return true;
}