Skip to content

Commit

Permalink
transfer to fisco
Browse files Browse the repository at this point in the history
  • Loading branch information
youwenbusi committed Jul 24, 2020
1 parent b65b3ee commit 09f187f
Show file tree
Hide file tree
Showing 24 changed files with 613 additions and 229 deletions.
11 changes: 6 additions & 5 deletions commands/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
Constants and defaults specific to the CLI interface.
"""

ETH_RPC_ENDPOINT_DEFAULT = "http://localhost:8545"
PROVER_SERVER_ENDPOINT_DEFAULT = "localhost:50051"
FISCO_RPC_ENDPOINT_DEFAULT = "http://119.23.46.126:8545"
PROVER_SERVER_ENDPOINT_DEFAULT = "119.23.46.126:50051"

FISCO_ADDRESS_FILE = "keystore"
ADDRESS_FILE_DEFAULT = "zeth-address.json"
INSTANCE_FILE_DEFAULT = "zeth-instance.json"
ETH_ADDRESS_DEFAULT = "eth-address"
#INSTANCE_FILE_DEFAULT = "zeth-instance.json"
#ETH_ADDRESS_DEFAULT = "eth-address"

WALLET_DIR_DEFAULT = "./wallet"
WALLET_USERNAME = "zeth"
WALLET_USERNAME = "zbac"
76 changes: 47 additions & 29 deletions commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
InstanceDescription, get_block_number, get_mix_results, compile_files
from zeth.mixer_client import MixerClient
from zeth.utils import \
open_web3, short_commitment, EtherValue, get_zeth_dir, from_zeth_units
short_commitment, EtherValue, get_zeth_dir, from_zeth_units
from zeth.wallet import ZethNoteDescription, Wallet
from click import ClickException
import json
from os.path import exists, join
from typing import Dict, Tuple, Optional, Callable, Any
from web3 import Web3 # type: ignore
#from web3 import Web3 # type: ignore
from Groth16Mixer import Groth16Mixer
from bac import bac
from python_web3.client.bcoskeypair import BcosKeyPair #todo


class ClientConfig:
Expand All @@ -26,20 +29,20 @@ def __init__(
self,
eth_rpc_endpoint: str,
prover_server_endpoint: str,
instance_file: str,
#instance_file: str,
address_file: str,
wallet_dir: str):
self.eth_rpc_endpoint = eth_rpc_endpoint
self.prover_server_endpoint = prover_server_endpoint
self.instance_file = instance_file
#self.instance_file = instance_file
self.address_file = address_file
self.wallet_dir = wallet_dir


'''
def open_web3_from_ctx(ctx: ClientConfig) -> Any:
return open_web3(ctx.eth_rpc_endpoint)


'''
'''
class MixerDescription:
"""
Holds an InstanceDescription for the mixer contract, and optionally an
Expand Down Expand Up @@ -105,7 +108,7 @@ def load_mixer_description(mixer_description_file: str) -> MixerDescription:
def load_mixer_description_from_ctx(ctx: ClientConfig) -> MixerDescription:
return load_mixer_description(ctx.instance_file)

'''

def get_zeth_address_file(ctx: ClientConfig) -> str:
return ctx.address_file
Expand Down Expand Up @@ -170,25 +173,26 @@ def open_wallet(


def do_sync(
web3: Any,
#web3: Any,
wallet: Wallet,
wait_tx: Optional[str],
receipt: Any,
#wait_tx: Optional[str],
callback: Optional[Callable[[ZethNoteDescription], None]] = None) -> int:
"""
Implementation of sync, reused by several commands. Returns the
block_number synced to. Also updates and saves the MerkleTree.
"""
def _do_sync() -> int:
wallet_next_block = wallet.get_next_block()
chain_block_number: int = get_block_number(web3)
#wallet_next_block = wallet.get_next_block()
#chain_block_number: int = get_block_number(web3)

if chain_block_number >= wallet_next_block:
new_merkle_root: Optional[bytes] = None
#if chain_block_number >= wallet_next_block:
#new_merkle_root: Optional[bytes] = None

print(f"SYNCHING blocks ({wallet_next_block} - {chain_block_number})")
#print(f"SYNCHING blocks ({wallet_next_block} - {chain_block_number})")
mixer_instance = wallet.mixer_instance
for mix_result in get_mix_results(
web3, mixer_instance, wallet_next_block, chain_block_number):
mixer_instance, receipt):
new_merkle_root = mix_result.new_merkle_root
for note_desc in wallet.receive_notes(mix_result.output_events):
if callback:
Expand All @@ -198,26 +202,26 @@ def _do_sync() -> int:
for commit in spent_commits:
print(f" SPENT: {commit}")

wallet.update_and_save_state(next_block=chain_block_number + 1)
wallet.update_and_save_state()

# Check merkle root and save the updated tree
if new_merkle_root:
our_merkle_root = wallet.merkle_tree.get_root()
assert new_merkle_root == our_merkle_root

return chain_block_number
return new_merkle_root

# Do a sync upfront (it would be a waste of time to wait for a tx before
# syncing, as it can take time to traverse all blocks). Then wait for a tx
# if requested, and sync again.

'''
if wait_tx:
_do_sync()
tx_receipt = web3.eth.waitForTransactionReceipt(wait_tx, 10000)
gas_used = tx_receipt.gasUsed
status = tx_receipt.status
print(f"{wait_tx[0:8]}: gasUsed={gas_used}, status={status}")

'''
return _do_sync()


Expand All @@ -241,7 +245,7 @@ def find_pub_address_file(base_file: str) -> str:

raise ClickException(f"No public key file {pub_addr_file} or {base_file}")


'''
def create_mixer_client(ctx: ClientConfig) -> MixerClient:
"""
Create a MixerClient for an existing deployment.
Expand All @@ -250,19 +254,32 @@ def create_mixer_client(ctx: ClientConfig) -> MixerClient:
mixer_desc = load_mixer_description_from_ctx(ctx)
mixer_instance = mixer_desc.mixer.instantiate(web3)
return MixerClient.open(web3, ctx.prover_server_endpoint, mixer_instance)

'''

def create_zeth_client_and_mixer_desc(
ctx: ClientConfig) -> Tuple[MixerClient, MixerDescription]:
ctx: ClientConfig, mixer_addr: str, password: str) -> Tuple[MixerClient, MixerDescription]:
"""
Create a MixerClient and MixerDescription object, for an existing deployment.
"""
web3 = open_web3_from_ctx(ctx)
mixer_desc = load_mixer_description_from_ctx(ctx)
mixer_instance = mixer_desc.mixer.instantiate(web3)
#web3 = open_web3_from_ctx(ctx)
#mixer_desc = load_mixer_description_from_ctx(ctx)
mixer_instance = Groth16Mixer(mixer_addr)
keystore_file = "pyaccount.keystore"
mixer_instance.client.keystore_file = "pyaccount.keystore"
if os.path.exists(keystore_file) is False:
raise ClickException(f"invalid output spec: {keystore_file}")
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privkey = Account.decrypt(keytext, password)
mixer_instance.client.ecdsa_account = Account.from_key(privkey)
keypair = BcosKeyPair()
keypair.private_key = mixer_instance.client.ecdsa_account.privateKey
keypair.public_key = mixer_instance.client.ecdsa_account.publickey
keypair.address = mixer_instance.client.ecdsa_account.address
mixer_instance.client.keypair = keypair
zeth_client = MixerClient.open(
web3, ctx.prover_server_endpoint, mixer_instance)
return (zeth_client, mixer_desc)
ctx.prover_server_endpoint, mixer_instance)
return (zeth_client)


def zeth_note_short(note_desc: ZethNoteDescription) -> str:
Expand All @@ -288,7 +305,7 @@ def parse_output(output_str: str) -> Tuple[ZethAddressPub, EtherValue]:
raise ClickException(f"invalid output spec: {output_str}")
return (ZethAddressPub.parse(parts[0]), EtherValue(parts[1]))


'''
def load_eth_address(eth_addr: Optional[str]) -> str:
"""
Given an --eth-addr command line param, either parse the address, load from
Expand All @@ -301,3 +318,4 @@ def load_eth_address(eth_addr: Optional[str]) -> str:
with open(eth_addr, "r") as eth_addr_f:
return Web3.toChecksumAddress(eth_addr_f.read().rstrip())
raise ClickException(f"could find file or parse eth address: {eth_addr}")
'''
12 changes: 3 additions & 9 deletions commands/zeth
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# SPDX-License-Identifier: LGPL-3.0+

from commands.constants import \
PROVER_SERVER_ENDPOINT_DEFAULT, INSTANCE_FILE_DEFAULT, \
ADDRESS_FILE_DEFAULT, WALLET_DIR_DEFAULT, ETH_RPC_ENDPOINT_DEFAULT
PROVER_SERVER_ENDPOINT_DEFAULT, \
ADDRESS_FILE_DEFAULT, WALLET_DIR_DEFAULT, FISCO_RPC_ENDPOINT_DEFAULT
from commands.utils import ClientConfig
from commands.zeth_deploy import deploy
from commands.zeth_gen_address import gen_address
Expand Down Expand Up @@ -35,16 +35,12 @@ def help(ctx: Context) -> None:
@group(cls=DefaultGroup, default_if_no_args=True, default="help")
@option(
"--eth-rpc",
default=ETH_RPC_ENDPOINT_DEFAULT,
default=FISCO_RPC_ENDPOINT_DEFAULT,
help=f"Ethereum rpc end-point")
@option(
"--prover-server",
default=PROVER_SERVER_ENDPOINT_DEFAULT,
help=f"Prover server endpoint (default={PROVER_SERVER_ENDPOINT_DEFAULT})")
@option(
"--instance-file",
default=INSTANCE_FILE_DEFAULT,
help=f"Instance file (default={INSTANCE_FILE_DEFAULT})")
@option(
"--address-file",
default=ADDRESS_FILE_DEFAULT,
Expand All @@ -58,7 +54,6 @@ def zeth(
ctx: Context,
eth_rpc: Optional[str],
prover_server: str,
instance_file: str,
address_file: str,
wallet_dir: str) -> None:
if ctx.invoked_subcommand == "help":
Expand All @@ -67,7 +62,6 @@ def zeth(
ctx.obj = ClientConfig(
eth_rpc_endpoint=eth_rpc,
prover_server_endpoint=prover_server,
instance_file=instance_file,
address_file=address_file,
wallet_dir=wallet_dir)

Expand Down
55 changes: 23 additions & 32 deletions commands/zeth_deploy.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
# Copyright (c) 2015-2020 Clearmatics Technologies Ltd
#
# SPDX-License-Identifier: LGPL-3.0+

from commands.constants import INSTANCE_FILE_DEFAULT
'''
from commands.utils import \
open_web3_from_ctx, get_erc20_instance_description, load_eth_address, \
write_mixer_description, MixerDescription
from zeth.mixer_client import MixerClient
from zeth.utils import EtherValue
'''
from click import Context, command, option, pass_context
from typing import Optional
import sys
sys.path.append('../')
from contract.Groth16Mixer import Groth16Mixer


@command()
@option("--eth-addr", help="Sender eth address or address filename")
@option(
"--instance-out",
default=INSTANCE_FILE_DEFAULT,
help=f"File to write deployment address to (default={INSTANCE_FILE_DEFAULT})")
@option("--token-address", help="Address of token contract (if used)")
@option("--deploy-gas", help="Maximum gas, in Wei")
#@option("--eth-addr", help="Sender eth address or address filename")
#@option("--token-address", help="Address of token contract (if used)")
@pass_context
def deploy(
ctx: Context,
eth_addr: Optional[str],
instance_out: str,
token_address: str,
deploy_gas: str) -> None:
ctx: Context
#eth_addr: Optional[str],
) -> None:
"""
Deploy the zeth contracts and record the instantiation details.
"""
eth_address = load_eth_address(eth_addr)
client_ctx = ctx.obj
web3 = open_web3_from_ctx(client_ctx)
deploy_gas_value = EtherValue(deploy_gas, 'wei') if deploy_gas else None

print(f"deploy: eth_address={eth_address}")
print(f"deploy: instance_out={instance_out}")
print(f"deploy: token_address={token_address}")
#eth_address = load_eth_address(eth_addr)
#client_ctx = ctx.obj
#web3 = open_web3_from_ctx(client_ctx)
#deploy_gas_value = EtherValue(deploy_gas, 'wei') if deploy_gas else None

token_instance_desc = get_erc20_instance_description(token_address) \
if token_address else None
#print(f"deploy: eth_address={eth_address}")
#print(f"deploy: token_address={token_address}")

_zeth_client, mixer_instance_desc = MixerClient.deploy(
web3,
client_ctx.prover_server_endpoint,
eth_address,
token_address,
deploy_gas_value)
#token_instance = bac(token_address) \
# if token_address else None
si = Groth16Mixer("")
result = si.deploy("contract/Groth16Mixer.bin")
address = result['contractAddress']
print(f"deploy: mixer_address={address}")
#mixer_instance = Groth16Mixer(address)

mixer_desc = MixerDescription(mixer_instance_desc, token_instance_desc)
write_mixer_description(instance_out, mixer_desc)
8 changes: 5 additions & 3 deletions commands/zeth_ls_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
from commands.utils import \
create_zeth_client_and_mixer_desc, load_zeth_address, open_wallet
from zeth.utils import short_commitment
from click import Context, command, pass_context
from click import Context, command, pass_context, option


@command()
@option("--mixer-addr", help="The Groth16Mixer contract address you want to use")
@option("--password", help="the password of you keystore")
@pass_context
def ls_commits(ctx: Context) -> None:
def ls_commits(ctx: Context, mixer_addr:str, password: str) -> None:
"""
List all commitments in the joinsplit contract
"""
client_ctx = ctx.obj
zeth_client, _mixer_desc = create_zeth_client_and_mixer_desc(client_ctx)
zeth_client = create_zeth_client_and_mixer_desc(client_ctx, mixer_addr, password)
zeth_address = load_zeth_address(client_ctx)
wallet = open_wallet(
zeth_client.mixer_instance, zeth_address.addr_sk, client_ctx)
Expand Down
17 changes: 10 additions & 7 deletions commands/zeth_ls_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@
#
# SPDX-License-Identifier: LGPL-3.0+

from commands.utils import open_web3_from_ctx, load_zeth_address_secret, \
open_wallet, load_mixer_description_from_ctx
from commands.utils import load_zeth_address_secret, open_wallet
from zeth.utils import EtherValue
from click import Context, command, option, pass_context

import sys
sys.path.append('../')
from Groth16Mixer import Groth16Mixer

@command()
@option("--mixer-addr", help="The Groth16Mixer contract address you want to use")
@option("--balance", is_flag=True, help="Show total balance")
@option("--spent", is_flag=True, help="Show spent notes")
@pass_context
def ls_notes(ctx: Context, balance: bool, spent: bool) -> None:
def ls_notes(ctx: Context, mixer_addr: str, balance: bool, spent: bool) -> None:
"""
List the set of notes owned by this wallet
"""
client_ctx = ctx.obj
web3 = open_web3_from_ctx(client_ctx)
mixer_desc = load_mixer_description_from_ctx(client_ctx)
mixer_instance = mixer_desc.mixer.instantiate(web3)
#web3 = open_web3_from_ctx(client_ctx)
#mixer_desc = load_mixer_description_from_ctx(client_ctx)
#mixer_instance = mixer_desc.mixer.instantiate(web3)
mixer_instance = Groth16Mixer(mixer_addr)
js_secret = load_zeth_address_secret(client_ctx)
wallet = open_wallet(mixer_instance, js_secret, client_ctx)

Expand Down
Loading

0 comments on commit 09f187f

Please sign in to comment.