def run(method, address, game_id): # Web3 INFURA_PROJECT_ID = os.environ['INFURA_PROJECT_ID'] INFURA_PROJECT_SECRET = os.environ['INFURA_PROJECT_SECRET'] headers = {"auth": ("", INFURA_PROJECT_SECRET)} uri = 'https://ropsten.infura.io/v3/%s' % (INFURA_PROJECT_ID) web3 = Web3(HTTPProvider(uri, headers)) # Owner owner = Account.from_key(os.environ['PRIVATE_KEY_0x2048']) # ABI filepath = 'bin/combined.json' with open(filepath) as f: compiled_artifacts = json.load(f) data = compiled_artifacts["contracts"] contract_data = data["solidity/ArcadeProtocol.sol:ArcadeProtocol"] abi = contract_data["abi"] bytecode = contract_data["bin"] # Run interactor = ContractInteractor(web3) if method == 'deploy': interactor.deploy(abi, bytecode, from_addr=owner) elif method == 'add_game': price = 100000000000000 # 0.0001 ETH fee = 1 interactor.set_contract(abi, address) interactor.add_game(game_id, price, fee, from_addr=owner)
async def asyncSetUp(self) -> None: self.account = Account.from_key(self.private_key) ethereum_signer = EthereumSignerWeb3(account=self.account) self.library = ZkSyncLibrary() w3 = Web3( HTTPProvider( endpoint_uri= "https://rinkeby.infura.io/v3/bcf42e619a704151a1b0d95a35cb2e62" )) provider = ZkSyncProviderV01(provider=HttpJsonRPCTransport( network=rinkeby)) address = await provider.get_contract_address() self.zksync = ZkSync(account=self.account, web3=w3, zksync_contract_address=address.main_contract) ethereum_provider = EthereumProvider(w3, self.zksync) signer = ZkSyncSigner.from_account(self.account, self.library, rinkeby.chain_id) self.wallet = Wallet(ethereum_provider=ethereum_provider, zk_signer=signer, eth_signer=ethereum_signer, provider=provider)
def test_is_valid_order_deserialized(self): account = Account.from_key(PRIVATE_KEY) zksync_signer = ZkSyncSigner.from_account(account, self.library, ChainId.MAINNET) ethereum_signer = EthereumSignerWeb3(account=account) token1 = Token(id=1, symbol='', address='', decimals=0) # only id matters token2 = Token(id=2, symbol='', address='', decimals=0) # only id matters tokens_pool = Tokens(tokens=[token1, token2]) order = Order(account_id=7, nonce=18, token_sell=token1, token_buy=token2, ratio=Fraction(1, 4), amount=1000000, recipient='0x823b6a996cea19e0c41e250b20e2e804ea72ccdf', valid_from=0, valid_until=4294967295) order.signature = zksync_signer.sign_tx(order) order.eth_signature = ethereum_signer.sign_tx(order) zksync_validator = EncodedTxValidator(self.library) serialized_order = json.dumps(order.dict(), indent=4) deserialized_order = Order.from_json(json.loads(serialized_order), tokens_pool) ret = zksync_validator.is_valid_signature(deserialized_order) self.assertTrue(ret) ret = deserialized_order.is_valid_eth_signature( ethereum_signer.address()) self.assertTrue(ret)
def get_nonce(*, web3: Web3, nonce: int, private_key: bytes): """get the nonce to be used as specified via command line options """ if nonce is not None: return nonce elif private_key is not None: return web3.eth.getTransactionCount( Account.from_key(private_key).address, block_identifier="pending") else: return web3.eth.getTransactionCount(web3.eth.accounts[0], block_identifier="pending")
def setUp(self) -> None: self.account = Account.from_key(self.private_key) w3 = Web3( HTTPProvider( endpoint_uri= "https://rinkeby.infura.io/v3/bcf42e619a704151a1b0d95a35cb2e62" )) self.zksync = ZkSync( account=self.account, web3=w3, zksync_contract_address="0x82F67958A5474e40E1485742d648C0b0686b6e5D" )
def create_tx(coin, account, recipient, amount): gasEstimate = w3.eth.estimateGas( {"from": account.address, "to": recipient, "value": amount} ) return { "from": account.address, "to": recipient, "account_one": Account.from_key(private_key), "value": amount, "gasPrice": w3.eth.gasPrice, "gas": gasEstimate, "nonce": w3.eth.getTransactionCount(w3.toChecksumAddress(account.address)) }
def test_signature(self): account = Account.from_key(PRIVATE_KEY) signer = ZkSyncSigner.from_account(account, self.library, ChainId.MAINNET) tr = Transfer( from_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a", to_address="0x19aa2ed8712072e918632259780e587698ef58df", token=Token.eth(), amount=1000000000000, fee=1000000, nonce=12, valid_from=0, valid_until=4294967295, account_id=44) res = signer.sign_tx(tr) assert res.signature == '849281ea1b3a97b3fe30fbd25184db3e7860db96e3be9d53cf643bd5cf7805a30dbf685c1e63fd75968a61bd83d3a1fb3a0b1c68c71fe87d96f1c1cb7de45b05'
def test_signature(self): account = Account.from_key(PRIVATE_KEY) signer = ZkSyncSigner.from_account(account, self.library, ChainId.MAINNET) tr = Transfer( from_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a", to_address="0x19aa2ed8712072e918632259780e587698ef58df", token=Token.eth(), amount=1000000000000, fee=1000000, nonce=12, valid_from=0, valid_until=4294967295, account_id=44) res = signer.sign_tx(tr) assert res.signature == 'b3211c7e15d31d64619e0c7f65fce8c6e45637b5cfc8711478c5a151e6568d875ec7f48e040225fe3cc7f1e7294625cad6d98b4595d007d36ef62122de16ae01'
async def get_wallet(): # Load crypto library library = ZkSyncLibrary() # Create Zksync Provider zk_provider = ZkSyncProviderV01(provider=HttpJsonRPCTransport( network=network.rinkeby)) # Load contract addresses from server contracts = await zk_provider.get_contract_address() pprint(contracts) contracts.main_contract print(contracts.main_contract) # Setup web3 account account = Account.from_key("") pprint(account) addr = account.address print(addr) # Create EthereumSigner eth_signer = EthereumSignerWeb3(account=account) # Setup web3 w3 = Web3(HTTPProvider(endpoint_uri="")) # Setup zksync contract interactor zksync = ZkSync(account=account, web3=w3, zksync_contract_address=contracts.main_contract) # Create ethereum provider for interacting with ethereum node eth_provider = EthereumProvider(w3, zksync) # Initialize zksync signer, all creating options were described earlier zk_signer = ZkSyncSigner.from_account(account, library, network.rinkeby.chain_id)
def generate_keystore(keystore_path: str, private_key: str): if path.exists(keystore_path): raise click.BadOptionUsage( # type: ignore "--keystore-file", f"The file {keystore_path} does already exist!" ) if private_key: account = Account.from_key(private_key) else: account = Account.create() password = click.prompt( "Please enter the password to encrypt the keystore", type=str, hide_input=True, confirmation_prompt=True, ) keystore = account.encrypt(password=password) with open(keystore_path, "w") as file: file.write(json.dumps(keystore)) file.close() click.echo(f"Stored keystore for {account.address} at {keystore_path}")
def test_derive_pub_key(self): account = Account.from_key(PRIVATE_KEY) signer = ZkSyncSigner.from_account(account, self.library, ChainId.MAINNET) assert signer.public_key.hex( ) == "40771354dc314593e071eaf4d0f42ccb1fad6c7006c57464feeb7ab5872b7490"
import pytest from web3 import Account from eth_utils import ( int_to_big_endian, ) num_accounts = 3 accounts = [] for i in range(1, num_accounts + 1): pk_bytes = int_to_big_endian(i).rjust(32, b'\x00') account = Account.from_key(pk_bytes) accounts.append(account) @pytest.fixture(scope="session") def owner(): account = accounts[0] return account @pytest.fixture(scope="session") def user(): account = accounts[1] return account @pytest.fixture(scope="session") def user2(): account = accounts[2] return account
def priv_key_to_account(coin, priv_key): private_key = os.getenv('PRIVATE_KEY') account_one = Account.from_key(private_key)
def int_to_test_account(i): pk_bytes = int_to_big_endian(i).rjust(32, b'\x00') account = Account.from_key(pk_bytes) return account