Implement external signer

This commit is contained in:
nolash 2021-01-10 22:03:26 +01:00
parent d1f2b2cbdc
commit 8f9834105d
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
17 changed files with 222 additions and 138 deletions

View File

@ -1,3 +1,6 @@
- 0.1.0-unreleased
* Implement external signer
* Standardize cli arg flags
- 0.0.3
* Add viewer cli
- 0.0.2

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,9 @@ import logging
# third-party imports
import web3
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
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@ -26,33 +28,62 @@ data_dir = os.path.join(script_dir, '..', 'data')
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', 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('-r', '--contract-address', dest='r', type=str, help='Address declaration contract address')
argparser.add_argument('-o', '--declarator-address', dest='o', type=str, help='Signing address for the declaration')
argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Accounts declarator owner')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_dir))
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('address', type=str, help='Ethereum address to add declaration to')
argparser.add_argument('declaration', type=str, help='SHA256 sum of endorsement data to add')
args = argparser.parse_args()
if args.v:
if args.vv:
logg.setLevel(logging.DEBUG)
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,
)
def main():
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.json'), 'r')
abi = json.load(f)
f.close()
w3.eth.defaultAccount = w3.eth.accounts[0]
if args.o != None:
w3.eth.defaultAccount = args.o
logg.debug('endorser address {}'.format(w3.eth.defaultAccount))
c = w3.eth.contract(abi=abi, address=args.r)
tx_hash = c.functions.addDeclaration(args.address, args.declaration).transact()
print(tx_hash.hex())
(tx_hash, rcpt) = helper.sign_and_send(
[
c.functions.addDeclaration(args.address, args.declaration).buildTransaction,
],
)
print(tx_hash)
if __name__ == '__main__':

View File

@ -13,7 +13,9 @@ import logging
# third-party imports
import web3
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
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@ -26,18 +28,48 @@ data_dir = os.path.join(script_dir, '..', 'data')
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('-o', '--owner', dest='o', type=str, help='Accounts declarator owner')
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('-a', '--signer-address', dest='a', type=str, help='Accounts declarator owner')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_dir))
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('owner_description_digest', type=str, help='SHA256 of description metadata of contract deployer')
args = argparser.parse_args()
if args.v:
if args.vv:
logg.setLevel(logging.DEBUG)
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,
)
def main():
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.json'), 'r')
abi = json.load(f)
f.close()
@ -46,15 +78,15 @@ def main():
bytecode = f.read()
f.close()
w3.eth.defaultAccount = w3.eth.accounts[0]
if args.o != None:
w3.eth.defaultAccount = args.o
logg.debug('owner address {}'.format(w3.eth.defaultAccount))
c = w3.eth.contract(abi=abi, bytecode=bytecode)
logg.info('digest {}'.format(args.owner_description_digest))
tx_hash = c.constructor(args.owner_description_digest).transact()
(tx_hash, rcpt) = helper.sign_and_send(
[
c.constructor(args.owner_description_digest).buildTransaction,
],
force_wait=True,
)
rcpt = w3.eth.getTransactionReceipt(tx_hash)
print(rcpt.contractAddress)

View File

@ -14,7 +14,7 @@ import logging
# third-party imports
import web3
from crypto_dev_signer.keystore import DictKeystore
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@ -28,17 +28,23 @@ data_dir = os.path.join(script_dir, '..', 'data')
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('-r', '--contract-address', dest='r', type=str, help='Address declaration contract address')
argparser.add_argument('-o', '--declarator-address', dest='o', type=str, help='Signing address for the declaration')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
argparser.add_argument('-a', '-declarator-address', dest='a', type=str, help='Signing address for the declaration')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('--resolve', action='store_true', help='Attempt to resolve the hashes to actual content')
argparser.add_argument('--resolve-http', dest='resolve_http', type=str, help='Base url to look up content hashes')
argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_dir))
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('address', type=str, help='Ethereum declaration address to look up')
args = argparser.parse_args()
if args.v:
if args.vv:
logg.setLevel(logging.DEBUG)
elif args.v:
logg.setLevel(logging.INFO)
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
def try_sha256(s):
r = urllib.request.urlopen(os.path.join(args.resolve_http, s.hex()))
@ -47,22 +53,32 @@ def try_sha256(s):
def try_utf8(s):
return s.decode('utf-8')
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))
def main():
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.json'), 'r')
abi = json.load(f)
f.close()
w3.eth.defaultAccount = w3.eth.accounts[0]
if args.o != None:
w3.eth.defaultAccount = args.o
logg.debug('endorser address {}'.format(w3.eth.defaultAccount))
declarator_address = signer_address
if declarator_address == None:
declarator_address = args.a
if declarator_address == None:
sys.stderr.write('missing declarator address, specify -y or -a\n')
sys.exit(1)
logg.debug('declarator address {}'.format(declarator_address))
c = w3.eth.contract(abi=abi, address=args.r)
declarations = c.functions.declaration(w3.eth.defaultAccount, args.address).call()
declarations = c.functions.declaration(declarator_address, args.address).call()
for d in declarations:
if not args.resolve:

File diff suppressed because one or more lines are too long

View File

@ -14,7 +14,9 @@ import hashlib
# third-party imports
import web3
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
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@ -27,19 +29,49 @@ data_dir = os.path.join(script_dir, '..', 'data')
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', 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('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
argparser.add_argument('-r', '--contract-address', dest='r', type=str, help='Token endorsement contract address')
argparser.add_argument('-o', '--owner-address', dest='o', type=str, help='Address to use to sign endorsement transaction')
argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Accounts declarator owner')
argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_dir))
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('address', type=str, help='Ethereum address to add declaration to')
args = argparser.parse_args()
if args.v:
if args.vv:
logg.setLevel(logging.DEBUG)
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,
)
def main():
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
f = open(os.path.join(args.abi_dir, 'TokenUniqueSymbolIndex.json'), 'r')
abi = json.load(f)
f.close()
@ -51,11 +83,6 @@ def main():
t = w3.eth.contract(abi=erc20_abi, address=args.address)
token_symbol = t.functions.symbol().call()
w3.eth.defaultAccount = w3.eth.accounts[0]
if args.o != None:
w3.eth.defaultAccount = args.o
logg.debug('owner address {}'.format(w3.eth.defaultAccount))
c = w3.eth.contract(abi=abi, address=args.r)
h = hashlib.new('sha256')
@ -63,8 +90,13 @@ def main():
z = h.digest()
logg.info('token symbol {} => {}'.format(token_symbol, z.hex()))
tx_hash = c.functions.register('0x' + z.hex(), args.address).transact()
print(tx_hash.hex())
(tx_hash, rcpt) = helper.sign_and_send(
[
c.functions.register('0x' + z.hex(), args.address).buildTransaction,
],
)
print(tx_hash)
if __name__ == '__main__':

View File

@ -13,7 +13,9 @@ import logging
# third-party imports
import web3
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
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@ -26,17 +28,47 @@ data_dir = os.path.join(script_dir, '..', 'data')
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('-o', '--owner', dest='o', type=str, help='Accounts declarator owner')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
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('-a', '--signer-address', dest='a', type=str, help='Accounts declarator owner')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_dir))
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
args = argparser.parse_args()
if args.v:
if args.vv:
logg.setLevel(logging.DEBUG)
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,
)
def main():
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
f = open(os.path.join(args.abi_dir, 'TokenUniqueSymbolIndex.json'), 'r')
abi = json.load(f)
f.close()
@ -45,16 +77,14 @@ def main():
bytecode = f.read()
f.close()
w3.eth.defaultAccount = w3.eth.accounts[0]
if args.o != None:
w3.eth.defaultAccount = args.o
logg.debug('owner address {}'.format(w3.eth.defaultAccount))
c = w3.eth.contract(abi=abi, bytecode=bytecode)
tx_hash = c.constructor().transact()
rcpt = w3.eth.getTransactionReceipt(tx_hash)
(tx_hash, rcpt) = helper.sign_and_send(
[
c.constructor().buildTransaction,
],
force_wait=True,
)
print(rcpt.contractAddress)

View File

@ -1,6 +1,6 @@
[metadata]
name = eth-address-index
version = 0.0.3
version = 0.1.0a1
description = Signed metadata declarations for ethereum addresses
author = Louis Holbrook
author_email = dev@holbrook.no
@ -30,6 +30,7 @@ packages =
eth_token_index.runnable
install_requires =
web3==5.12.2
crypto-dev-signer~=0.4.13b2
tests_require =
eth-tester==0.5.0b2
py-evm==0.3.0a20

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
pragma solidity >=0.6.12;
pragma solidity >0.6.11;
// SPDX-License-Identifier: GPL-3.0-or-later
@ -13,7 +13,7 @@ contract AddressDeclarator {
mapping( address => address[] ) declaratorReverse;
bytes32[][] public contents;
constructor(bytes32 _initialDescription) {
constructor(bytes32 _initialDescription) public {
bytes32[] memory foundation;
owner = msg.sender;

View File

@ -1,19 +1,19 @@
SOLC = /usr/bin/solc
all:
solc AddressDeclarator.sol --abi | awk 'NR>3' > AddressDeclarator.json
solc AddressDeclarator.sol --bin | awk 'NR>3' > AddressDeclarator.bin
$(SOLC) AddressDeclarator.sol --abi --evm-version byzantium | awk 'NR>3' > AddressDeclarator.json
$(SOLC) AddressDeclarator.sol --bin --evm-version byzantium | awk 'NR>3' > AddressDeclarator.bin
truncate -s -1 AddressDeclarator.bin
solc TokenUniqueSymbolIndex.sol --abi | awk 'NR>3' > TokenUniqueSymbolIndex.json
solc TokenUniqueSymbolIndex.sol --bin | awk 'NR>3' > TokenUniqueSymbolIndex.bin
$(SOLC) TokenUniqueSymbolIndex.sol --abi --evm-version byzantium | awk 'NR>3' > TokenUniqueSymbolIndex.json
$(SOLC) TokenUniqueSymbolIndex.sol --bin --evm-version byzantium | awk 'NR>3' > TokenUniqueSymbolIndex.bin
truncate -s -1 TokenUniqueSymbolIndex.bin
old:
solc TokenEndorser.sol --abi | awk 'NR>3' > TokenEndorser.json
solc TokenEndorser.sol --bin | awk 'NR>3' > TokenEndorser.bin
truncate -s -1 TokenEndorser.bin
test: all
#python test.py
python test_tokenindex.py
install: all
cp -v AddressDeclarator.{json,bin} ../python/eth_address_declarator/data/
cp -v TokenUniqueSymbolIndex.{json,bin} ../python/eth_token_index/data/
.PHONY: test
.PHONY: test install

File diff suppressed because one or more lines are too long

View File

@ -1 +0,0 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_token","type":"address"},{"indexed":true,"internalType":"address","name":"_adder","type":"address"},{"indexed":true,"internalType":"uint256","name":"_index","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"EndorsementAdded","type":"event"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"endorsement","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"endorser","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"endorserTokenCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"tokenIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"tokenSymbolIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]

View File

@ -1,59 +0,0 @@
pragma solidity >=0.6.12;
// SPDX-License-Identifier: GPL-3.0-or-later
contract TokenEndorsement {
uint256 count;
mapping ( bytes32 => bytes32 ) public endorsement;
mapping ( address => uint256 ) public tokenIndex;
mapping ( address => uint256[] ) public endorser;
mapping ( address => uint256 ) public endorserTokenCount;
mapping ( string => address ) public tokenSymbolIndex;
address[] public tokens;
event EndorsementAdded(address indexed _token, address indexed _adder, uint256 indexed _index, bytes32 _data);
constructor() {
count = 1;
tokens.push(address(0x0));
}
function register(address _token) private returns (bool) {
if (tokenIndex[_token] > 0) {
return false;
}
(bool _ok, bytes memory _r) = _token.call(abi.encodeWithSignature('symbol()'));
require(_ok);
string memory token_symbol = abi.decode(_r, (string));
require(tokenSymbolIndex[token_symbol] == address(0));
tokens.push(_token);
tokenIndex[_token] = count;
tokenSymbolIndex[token_symbol] = _token;
count++;
return true;
}
function add(address _token, bytes32 _data) public returns (bool) {
register(_token);
bytes32 k;
bytes memory signMaterial = new bytes(40);
bytes memory addrBytes = abi.encodePacked(_token);
for (uint256 i = 0; i < 20; i++) {
signMaterial[i] = addrBytes[i];
}
addrBytes = abi.encodePacked(msg.sender);
for (uint256 i = 0; i < 20; i++) {
signMaterial[i+20] = addrBytes[i];
}
k = sha256(signMaterial);
require(endorsement[k] == bytes32(0x00));
endorsement[k] = _data;
endorser[msg.sender].push(tokenIndex[_token]);
endorserTokenCount[msg.sender]++;
emit EndorsementAdded(_token, msg.sender, count, _data);
return true;
}
}

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
pragma solidity >=0.6.12;
pragma solidity >0.6.11;
// SPDX-License-Identifier: GPL-3.0-or-later
@ -10,7 +10,7 @@ contract TokenUniqueSymbolIndex {
mapping ( bytes32 => uint256 ) public registry;
address[] tokens;
constructor() {
constructor() public {
owner = msg.sender;
tokens.push(address(0));
}