Exemple #1
0
def ls_notes(username: str):
    """
    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)
    #mixer_instance = Groth16Mixer(mixer_addr)
    js_secret = load_zeth_address_secret(username)
    wallet = open_wallet(None, js_secret, username,None, None)

    total = EtherValue(0)
    commits = []
    values = []
    for addr, short_commit, value in wallet.note_summaries():
        #print(f"{short_commit}: value={value.ether()}, addr={addr}")
        values.append(value.ether())
        total = total + value
        commits.append(short_commit)

    #print(f"TOTAL BALANCE: {total.ether()}")

    #print("SPENT NOTES:")
    spend_commits = []
    for addr, short_commit, value in wallet.spent_note_summaries():
        #print(f"{short_commit}: value={value.ether()}, addr={addr}")
        spend_commits.append(short_commit)
    return total, commits, values, spend_commits
Exemple #2
0
def ls_notes(ctx: Context, 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)
    js_secret = load_zeth_address_secret(client_ctx)
    wallet = open_wallet(mixer_instance, js_secret, client_ctx)

    total = EtherValue(0)
    for addr, short_commit, value in wallet.note_summaries():
        print(f"{short_commit}: value={value.ether()}, addr={addr}")
        total = total + value

    if balance:
        print(f"TOTAL BALANCE: {total.ether()}")

    if not spent:
        return

    print("SPENT NOTES:")
    for addr, short_commit, value in wallet.spent_note_summaries():
        print(f"{short_commit}: value={value.ether()}, addr={addr}")
Exemple #3
0
def ls_commits(ctx: Context) -> 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_address = load_zeth_address(client_ctx)
    wallet = open_wallet(zeth_client.mixer_instance, zeth_address.addr_sk,
                         client_ctx)
    print("COMMITMENTS:")
    for commit in wallet.merkle_tree.get_leaves():
        print(f"  {short_commitment(commit)}")
def ls_commits(username: str) -> None:
    """
    List all commitments in the joinsplit contract
    """
    #zeth_client = create_zeth_client_and_mixer_desc(PROVER_SERVER_ENDPOINT_DEFAULT, mixer_addr, username, password)
    zeth_address = load_zeth_address(username)
    wallet = open_wallet(None, zeth_address.addr_sk, username)
    #print("COMMITMENTS:")
    commits = []
    for commit in wallet.merkle_tree.get_leaves():
        #print(f"  {short_commitment(commit)}")
        commits.append(commit)
    return commits
Exemple #5
0
def mix(
        ctx: Context,
        vin: str,
        vout: str,
        input_notes: List[str],
        output_specs: List[str],
        eth_addr: Optional[str],
        wait: bool) -> None:
    """
    Generic mix function
    """
    # Some sanity checks
    if len(input_notes) > JS_INPUTS:
        raise ClickException(f"too many inputs (max {JS_INPUTS})")
    if len(output_specs) > JS_OUTPUTS:
        raise ClickException(f"too many outputs (max {JS_OUTPUTS})")

    print(f"vin = {vin}")
    print(f"vout = {vout}")

    vin_pub = EtherValue(vin)
    vout_pub = EtherValue(vout)
    client_ctx = ctx.obj
    zeth_client, mixer_desc = create_zeth_client_and_mixer_desc(client_ctx)
    zeth_address = load_zeth_address(client_ctx)
    wallet = open_wallet(
        zeth_client.mixer_instance, zeth_address.addr_sk, client_ctx)

    inputs: List[Tuple[int, ZethNote]] = [
        wallet.find_note(note_id).as_input() for note_id in input_notes]
    outputs: List[Tuple[ZethAddressPub, EtherValue]] = [
        parse_output(out_spec) for out_spec in output_specs]

    # Compute input and output value total and check that they match
    input_note_sum = from_zeth_units(
        sum([int(note.value, 16) for _, note in inputs]))
    output_note_sum = sum([value for _, value in outputs], EtherValue(0))
    if vin_pub + input_note_sum != vout_pub + output_note_sum:
        raise ClickException("input and output value mismatch")

    eth_address = load_eth_address(eth_addr)

    # If instance uses an ERC20 token, tx_value can be 0 not default vin_pub.
    tx_value: Optional[EtherValue] = None
    if mixer_desc.token:
        tx_value = EtherValue(0)

    tx_hash = zeth_client.joinsplit(
        wallet.merkle_tree,
        zeth_address.ownership_keypair(),
        eth_address,
        inputs,
        outputs,
        vin_pub,
        vout_pub,
        tx_value)

    if wait:
        do_sync(zeth_client.web3, wallet, tx_hash)
    else:
        print(tx_hash)
Exemple #6
0
def mixBac(request) -> None:
    result = {}
    req = json.loads(request.body)
    keystore_file = "{}/{}/{}".format(USER_DIR, req['username'],
                                      FISCO_ADDRESS_FILE)
    addr_file = "{}/{}/{}".format(USER_DIR, req['username'],
                                  ADDRESS_FILE_DEFAULT)
    if exists(keystore_file) and exists(addr_file):
        js_secret = load_zeth_address_secret(req['username'])
        wallet = open_wallet(None, js_secret, req['username'], None, None)
        inputs: List[Tuple[int, ZethNote]] = [
            wallet.find_note(note_id).as_input()
            for note_id in req['input_notes']
        ]
        outputs: List[Tuple[ZethAddressPub, EtherValue]] = [
            parse_output(out_spec) for out_spec in req['output_specs']
        ]
        mids = []
        for note_id in req['input_notes']:
            mids.append(wallet.find_note(note_id).mid)
        '''
		todo: check the reciever zeth_address whether record in server, if not, return /
		by searching the address in mysql, so for every zeth account, we need to save their zeth_address in mysql when generating /
		or specify the reciever by name instead zeth_address, so we can search the user dir
		'''
        input_note_sum = from_zeth_units(
            sum([int(note.value, 16) for _, note in inputs]))
        output_note_sum = sum([value for _, value in outputs], EtherValue(0))
        vin_pub = EtherValue(req['vin'])
        vout_pub = EtherValue(req['vout'])
        if vin_pub + input_note_sum != vout_pub + output_note_sum:
            result['status'] = 1
            result['text'] = 'input and output value mismatch'
            return JsonResponse(result)
        '''
		while (merkletree.objects.all().count() and not merkletree.objects.all().last().is_new):
			time.sleep(1)
		sqlResult = merkletree.objects.all().last()
		if sqlResult:
			sqlResult.is_new = False
			sqlResult.save()
		'''
        if req['vin']:
            outputapprove = token_approve(req['vin'], req['mixer_address'],
                                          req['token_address'],
                                          req['username'], req['password'])
            if not outputapprove:
                result['status'] = 1
                result['text'] = 'token approve failed'
                return JsonResponse(result)
        outputmix = mix(req['mixer_address'], req['username'], req['password'],
                        vin_pub, vout_pub, inputs, outputs, mids)
        if outputmix:
            '''
			event_sync(req['mixer_address'],sqlResult.blockNumber)
			total = EtherValue(0)
			commits = []
			for addr, short_commit, value in wallet.note_summaries():
				total = total + value
				commits.append(short_commit)
			result['status'] = 0
			result['commits'] = commits
			result['total_value'] = total.ether()
			'''
            traType = "mix"
            inputstr = ""
            for note_id in req['input_notes']:
                inputstr = inputstr + note_id + ';'
            outputstr = ""
            for out_spec in req['output_specs']:
                outputstr = outputstr + out_spec + ';'
            sqlInsert = "insert into transactions (traType, username, vin, vout, input_notes, output_specs) values (%s, %s, %s, %s, %s, %s);"
            db.ping(reconnect=True)
            cursor.execute(sqlInsert, [
                traType, req['username'], req['vin'], req['vout'], inputstr,
                outputstr
            ])
            db.commit()
            result['status'] = 0
            result['text'] = 'mix success'
            return JsonResponse(result)
        else:
            '''
			if merkletree.objects.all().count():
				sqlResult.is_new = True
				sqlResult.save()
			'''
            result['status'] = 1
            result['text'] = 'mix failed'
            return JsonResponse(result)
    result['status'] = 1
    result[
        'text'] = 'your account is not recorded in server, please import it firstly or create a new one'
    return JsonResponse(result)