chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit f26abe04ab360f38a52e63ca21610a963eecd6a9
parent 219c24b624377585bac39a7d8e2907debc09e689
Author: nolash <dev@holbrook.no>
Date:   Fri, 30 Apr 2021 07:23:41 +0200

Add eip173/owned interface, test contract, test stub

Diffstat:
Achainlib/eth/owned.py | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/test_eip165.py | 1-
Atests/test_owned.py | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/testdata/Owned.bin | 2++
Atests/testdata/Owned.sol | 32++++++++++++++++++++++++++++++++
5 files changed, 153 insertions(+), 1 deletion(-)

diff --git a/chainlib/eth/owned.py b/chainlib/eth/owned.py @@ -0,0 +1,67 @@ +# external imports +from hexathon import ( + add_0x, + ) + +# local imports +from .contract import ( + ABIContractEncoder, + ABIContractDecoder, + ABIContractType, + ) +from chainlib.jsonrpc import jsonrpc_template + +class EIP173(TxFactory): + + def transfer_ownership(self, contract_address, sender_address, new_owner_address): + enc = ABIContractEncoder() + enc.method('transferOwnership') + enc.typ(ABIContractType.ADDRESS) + enc.address(new_owner_address) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + def owner(self, contract_address, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('owner') + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + o['params'].append('latest') + return o + + + @classmethod + def parse_owner(self, v): + return abi_decode_single(ABIContractType.ADDRESS, v) + + +class Owned(EIP173): + + def accept_ownership(self, contract_address, sender_address): + enc = ABIContractEncoder() + enc.method('acceptOwnership') + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + def take_ownership(self, contract_address, sender_address, resource_address): + enc = ABIContractEncoder() + enc.method('takeOwnership') + enc.typ(ABIContractType.ADDRESS) + enc.address(resource_address) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx diff --git a/tests/test_eip165.py b/tests/test_eip165.py @@ -24,7 +24,6 @@ class TestSupports(EthTesterCase): def setUp(self): super(TestSupports, self).setUp() - #nonce_oracle = TestNonceOracle(self.accounts[0]) self.conn = RPCConnection.connect(self.chain_spec, 'default') nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) diff --git a/tests/test_owned.py b/tests/test_owned.py @@ -0,0 +1,52 @@ +# standard imports +import unittest +import os +import logging + +# local imports +from chainlib.eth.unittest.ethtester import EthTesterCase +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.gas import OverrideGasOracle +from chainlib.connection import RPCConnection +from chainlib.eth.tx import ( + TxFactory, + receipt, + ) +from chainlib.eth.eip165 import EIP165 + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +script_dir = os.path.realpath(os.path.dirname(__file__)) + + +class TestOwned(EthTesterCase): + + def setUp(self): + super(TestOwned, self).setUp() + self.conn = RPCConnection.connect(self.chain_spec, 'default') + nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) + + f = open(os.path.join(script_dir, 'testdata', 'Owned.bin')) + code = f.read() + f.close() + + txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + tx = txf.template(self.accounts[0], None, use_nonce=True) + tx = txf.set_code(tx, code) + (tx_hash_hex, o) = txf.build(tx) + + r = self.conn.do(o) + logg.debug('deployed with hash {}'.format(r)) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.address = r['contract_address'] + + + def test_owned(self): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/testdata/Owned.bin b/tests/testdata/Owned.bin @@ -0,0 +1 @@ +608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610624806100606000396000f3fe608060405234801561001057600080fd5b5060043610610069576000357c0100000000000000000000000000000000000000000000000000000000900480636b5783391461006e57806379ba50971461009e5780638da5cb5b146100bc578063f2fde38b146100da575b600080fd5b6100886004803603810190610083919061040e565b61010a565b60405161009591906104db565b60405180910390f35b6100a661024c565b6040516100b391906104db565b60405180910390f35b6100c4610331565b6040516100d191906104c0565b60405180910390f35b6100f460048036038101906100ef919061040e565b610355565b60405161010191906104db565b60405180910390f35b60008060608373ffffffffffffffffffffffffffffffffffffffff166040516024016040516020818303038152906040527f79ba5097000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516101b691906104a9565b6000604051808303816000865af19150503d80600081146101f3576040519150601f19603f3d011682016040523d82523d6000602084013e6101f8565b606091505b50809250819350505081610241576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610238906104f6565b60405180910390fd5b600192505050919050565b60003373ffffffffffffffffffffffffffffffffffffffff16600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146102a857600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60003373ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146103af57600080fd5b81600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060019050919050565b600081359050610408816105d7565b92915050565b60006020828403121561042057600080fd5b600061042e848285016103f9565b91505092915050565b6104408161053d565b82525050565b61044f8161054f565b82525050565b600061046082610516565b61046a8185610521565b935061047a81856020860161057b565b80840191505092915050565b6000610493600a8361052c565b915061049e826105ae565b602082019050919050565b60006104b58284610455565b915081905092915050565b60006020820190506104d56000830184610437565b92915050565b60006020820190506104f06000830184610446565b92915050565b6000602082019050818103600083015261050f81610486565b9050919050565b600081519050919050565b600081905092915050565b600082825260208201905092915050565b60006105488261055b565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60005b8381101561059957808201518184015260208101905061057e565b838111156105a8576000848401525b50505050565b7f4552525f41434345505400000000000000000000000000000000000000000000600082015250565b6105e08161053d565b81146105eb57600080fd5b5056fea26469706673582212205909b3637c67bdd4ec307d714ab75291de5f5d7ae0872a6fd6889aad21de847464736f6c63430008030033 +\ No newline at end of file diff --git a/tests/testdata/Owned.sol b/tests/testdata/Owned.sol @@ -0,0 +1,32 @@ +pragma solidity ^0.8.0; + +contract Owned { + address public owner; + address newOwner; + + constructor() public { + owner = msg.sender; + } + + function transferOwnership(address _newOwner) public returns (bool) { + require(owner == msg.sender); + newOwner = _newOwner; + return true; + } + + function acceptOwnership() public returns (bool) { + require(newOwner == msg.sender); + owner = msg.sender; + newOwner = address(0); + return true; + } + + function takeOwnership(address _ownable) public returns (bool) { + bool ok; + bytes memory result; + + (ok, result) = _ownable.call(abi.encodeWithSignature("acceptOwnership()")); + require(ok, "ERR_ACCEPT"); + return true; + } +}