Exemplo n.º 1
0
    def test_zcash_compact_enforcement(self):
        riemann.select_network('zcash_overwinter_main')

        with self.assertRaises(ValueError) as context:
            tx.VarInt.from_bytes(b'\xfd\x00\x00')

        self.assertIn('VarInt must be compact. Got:', str(context.exception))

        with self.assertRaises(ValueError) as context:
            tx.VarInt.from_bytes(b'\xfe\x00\x00\x00\x00')

        self.assertIn('VarInt must be compact. Got:', str(context.exception))
Exemplo n.º 2
0
    def test_sighash_forkid_all_anyone_can_pay(self):
        riemann.select_network('bitcoin_cash_main')
        t = tx.Tx(self.version, self.none_flag, self.tx_ins, self.tx_outs,
                  self.none_witnesses, self.lock_time)

        sighash = t.sighash_all(
            index=0,
            script=helpers.P2PKH1['ser']['ins'][0]['pk_script'],
            prevout_value=helpers.P2PKH1['ser']['ins'][0]['value'],
            anyone_can_pay=True)

        self.assertEqual(sighash, helpers.SIGHASH_FORKID['all_anyone_can_pay'])
Exemplo n.º 3
0
 def test_overwrites(self):
     riemann.select_network('decred_main')
     self.assertEqual(
         'OP_BLAKE256',
         ser.hex_deserialize('a8'))
     self.assertEqual(
         'a8',
         ser.hex_serialize('OP_BLAKE256'))
     self.assertEqual(
         'OP_SHA256',
         ser.hex_deserialize('c0'))
     self.assertEqual(
         'c0',
         ser.hex_serialize('OP_SHA256'))
Exemplo n.º 4
0
    def test_cashaddrs(self):
        riemann.select_network('bitcoin_cash_main')

        self.assertEqual(addr.make_legacy_p2sh_address('OP_IF'),
                         helpers.OP_IF_P2SH)

        self.assertEqual(addr.make_sh_address('OP_IF'), helpers.OP_IF_CASHADDR)

        self.assertEqual(
            addr.make_legacy_p2pkh_address(helpers.CASHADDR_PUBKEY),
            helpers.LEGACY_P2PKH_ADDRESS)

        self.assertEqual(addr.make_pkh_address(helpers.CASHADDR_PUBKEY),
                         helpers.CASHADDR_P2PKH_ADDRESS)
Exemplo n.º 5
0
 def test_make_decred_input_and_witness(self):
     riemann.select_network('decred_main')
     outpoint = tb.make_outpoint(
         tx_id_le=helpers.DCR['ser']['ins'][0]['hash'], index=0, tree=0)
     tx_in, witness = tb.make_witness_input_and_witness(
         outpoint=outpoint,
         sequence=helpers.DCR['human']['ins'][0]['sequence'],
         value=helpers.DCR['ser']['witnesses'][0]['value'],
         height=helpers.DCR['ser']['witnesses'][0]['height'],
         index=helpers.DCR['ser']['witnesses'][0]['index'],
         stack_script=helpers.DCR['ser']['witnesses'][0]['stack_script'],
         redeem_script=helpers.DCR['ser']['witnesses'][0]['redeem_script'])
     self.assertEqual(tx_in, helpers.DCR['ser']['tx']['in_unsigned'])
     self.assertEqual(witness,
                      helpers.DCR['ser']['witnesses'][0]['witness'])
Exemplo n.º 6
0
    def test_make_sh_output_script(self):
        self.assertEqual(tb.make_sh_output_script('OP_IF'),
                         helpers.OP_IF['output_script'])
        self.assertEqual(
            tb.make_sh_output_script(
                helpers.P2WSH['human']['witnesses'][0]['wit_script'],
                witness=True), helpers.P2WSH['ser']['ins'][0]['pk_script'])

        riemann.select_network('bitcoin_cash_main')
        with self.assertRaises(ValueError) as context:
            tb.make_sh_output_script(
                helpers.P2WSH['human']['witnesses'][0]['wit_script'],
                witness=True)

        self.assertIn(
            'Network bitcoin_cash_main does not support witness scripts.',
            str(context.exception))
Exemplo n.º 7
0
    def test_cashaddrs(self):
        riemann.select_network('bitcoin_cash_main')

        self.assertEqual(
            addr.make_legacy_p2sh_address('OP_IF'),
            helpers.OP_IF['p2sh'])

        self.assertEqual(
            addr.make_sh_address('OP_IF'),
            helpers.OP_IF['cashaddr'])

        self.assertEqual(
            addr.make_legacy_p2pkh_address(helpers.CASHADDR['pubkey']),
            helpers.CASHADDR['legacy_p2pkh'])

        self.assertEqual(
            addr.make_pkh_address(helpers.CASHADDR['pubkey']),
            helpers.CASHADDR['p2pkh'])
Exemplo n.º 8
0
def createMultiSigAddress(pubkey0, pubkey1, verbose=None):
    msig_script = examples.msig_two_two
    msig_scriptpubkey = msig_script.format(pk0=pubkey0, pk1=pubkey1)

    if verbose: print("Multi sig script: ", msig_scriptpubkey)

    riemann.select_network(NETWORK)
    if ZCASH in NETWORK:
        msig_address = addresses.make_p2sh_address(msig_scriptpubkey)
    elif BITCOIN in NETWORK:
        msig_address = addresses.make_p2wsh_address(msig_scriptpubkey)
    else:
        sys.exit("%s not supported" % NETWORK)
    # TODO: add BOLT opcode here for channel opening?
    redeem_script = msig_scriptpubkey
    script = serialization.serialize(redeem_script)

    return msig_address, script.hex()
Exemplo n.º 9
0
async def zeta(header_q: Optional[asyncio.Queue] = None,
               prevout_q: Optional[asyncio.Queue] = None,
               network: str = 'bitcoin_main') -> Tuple[Any, Any]:
    '''
    Main function.
    Starts the various tasks.
    Pass in queues to access task outputs (new headers/prevout events)
    Returns references to the tasks
    '''
    # switch zeta and riemann over to whatever network we're using
    chain_name = os.environ.get('ZETA_NETWORK', network)
    riemann.select_network(chain_name)
    connection.init_conn(chain_name=chain_name)
    await electrum.electrum._make_client(chain_name)

    chain_task = asyncio.ensure_future(chain.sync(header_q, chain_name))
    coin_task = asyncio.ensure_future(coins.sync(prevout_q))

    return chain_task, coin_task
Exemplo n.º 10
0
    def test_make_pkh_output_script(self):
        self.assertEqual(tb.make_pkh_output_script(helpers.PK['ser'][0]['pk']),
                         helpers.PK['ser'][0]['pkh_output'])
        self.assertEqual(
            tb.make_pkh_output_script(helpers.PK['ser'][0]['pk'],
                                      witness=True),
            helpers.PK['ser'][0]['pkh_p2wpkh_output'])

        riemann.select_network('bitcoin_cash_main')
        with self.assertRaises(ValueError) as context:
            tb.make_pkh_output_script(helpers.PK['ser'][0]['pk'], witness=True)

        self.assertIn(
            'Network bitcoin_cash_main does not support witness scripts.',
            str(context.exception))

        with self.assertRaises(ValueError) as context:
            tb.make_pkh_output_script('hello world')
        self.assertIn('Unknown pubkey format. Expected bytes. Got: ',
                      str(context.exception))
Exemplo n.º 11
0
    def test_unsigned_input(self):
        outpoint = simple.outpoint(
            tx_id=helpers.P2PKH['human']['ins'][0]['hash'],
            index=helpers.P2PKH['human']['ins'][0]['index'])

        self.assertEqual(
            simple.unsigned_input(
                outpoint=outpoint),
            outpoint.to_bytes() + b'\x00' + b'\xFE\xFF\xFF\xFF')

        self.assertEqual(
            simple.unsigned_input(
                outpoint=outpoint,
                sequence=0x1234abcd),
            outpoint.to_bytes() + b'\x00' + b'\xcd\xab\x34\x12')

        self.assertEqual(
            simple.unsigned_input(
                outpoint=outpoint,
                redeem_script='11AA OP_CHECKSEQUENCEVERIFY'),
            outpoint.to_bytes() + b'\x00' + b'\xaa\x11\x00\x00')

        self.assertEqual(
            simple.unsigned_input(
                outpoint=outpoint,
                redeem_script='11AA OP_CHECKSEQUENCEVERIFY',
                sequence=0x1234abcd),
            outpoint.to_bytes() + b'\x00' + b'\xcd\xab\x34\x12')

        riemann.select_network('decred_main')

        outpoint = simple.outpoint(
            tx_id=helpers.DCR['human']['ins'][0]['hash'],
            index=helpers.DCR['human']['ins'][0]['index'],
            tree=helpers.DCR['human']['ins'][0]['tree'])

        self.assertEqual(
            simple.unsigned_input(
                outpoint=outpoint,
                sequence=helpers.DCR['human']['ins'][0]['sequence']),
            helpers.DCR['ser']['tx']['in_unsigned'])
Exemplo n.º 12
0
    def test_guess_version(self):
        self.assertEqual(simple.guess_version('OP_IF'), 1)
        self.assertEqual(simple.guess_version('OP_CHECKSEQUENCEVERIFY'), 2)

        riemann.select_network('zcash_sprout_main')
        self.assertEqual(simple.guess_version('OP_IF'), 1)

        riemann.select_network('zcash_overwinter_main')
        self.assertEqual(simple.guess_version('OP_IF'), 3)

        riemann.select_network('zcash_sapling_main')
        self.assertEqual(simple.guess_version('OP_IF'), 4)
Exemplo n.º 13
0
 def setUp(self):
     riemann.select_network('zcash_overwinter_main')
Exemplo n.º 14
0
 def setUp(self):
     riemann.select_network('zcash_sprout_main')
Exemplo n.º 15
0
 def test_not_overwinter(self):
     riemann.select_network('zcash_sprout_main')
     with self.assertRaises(ValueError) as context:
         tx.OverwinterTx(None, None, None, None, None, None, None)
     self.assertIn('OverwinterTx not supported by network ',
                   str(context.exception))
Exemplo n.º 16
0
 def test_overwinter_snowflake(self, mock_tx):
     # TODO: Improve
     riemann.select_network('zcash_overwinter_main')
     mock_tx.OverwinterTx.return_value = 0
     self.assertEqual(tb.make_tx(0, 0, 0, 0, expiry=0), 0)
Exemplo n.º 17
0
 def test_sprout_snowflake(self, mock_tx):
     # TODO: Improve
     riemann.select_network('zcash_sprout_main')
     mock_tx.SproutTx.return_value = 0
     self.assertEqual(tb.make_tx(0, 0, 0, 0, tx_joinsplits=[]), 0)
Exemplo n.º 18
0
    chain_name = os.environ.get('ZETA_NETWORK', network)
    riemann.select_network(chain_name)
    connection.init_conn(chain_name=chain_name)
    await electrum.electrum._make_client(chain_name)

    chain_task = asyncio.ensure_future(chain.sync(header_q, chain_name))
    coin_task = asyncio.ensure_future(coins.sync(prevout_q))

    return chain_task, coin_task


if __name__ == '__main__':
    # start tracking
    header_q: asyncio.Queue = asyncio.Queue()
    prevout_q: asyncio.Queue = asyncio.Queue()

    # store the sample address
    chain_name = os.environ.get('ZETA_NETWORK', 'bitcoin_main')
    riemann.select_network(chain_name)
    connection.init_conn(chain_name=chain_name)
    # addresses.store_address('tb1qk0mul90y844ekgqpan8mg9lljasd59ny99ata4')

    asyncio.ensure_future(zeta(header_q, prevout_q))

    # wait a few seconds then start the status updater
    asyncio.ensure_future(_status_updater())
    asyncio.ensure_future(_report_new_headers(header_q))
    asyncio.ensure_future(_report_new_prevouts(prevout_q))

    asyncio.get_event_loop().run_forever()
Exemplo n.º 19
0
 def test_get_current_network(self):
     for n in networks.SUPPORTED:
         riemann.select_network(n)
         self.assertIs(riemann.get_current_network(), networks.SUPPORTED[n])
Exemplo n.º 20
0
 def setUp(self):
     riemann.select_network('zcash_overwinter_main')
     self.tx = tx.OverwinterTx.from_bytes(helpers.RAW_TX)
Exemplo n.º 21
0
def make_several_auctions(
        tx_id: str,
        index: int,
        control_addr: str,
        control_addr_keypair: Tuple[str, str],
        prevout_value: int,
        start_nonce: int,
        contract_address: str,
        reqDiff: int,
        eth_value: int,
        eth_privkey: str,
        num_auctions: int,
        recipient: str,
        form: List[Tuple[int, int]],
        network_id: int = 1) -> Tuple[tx.Tx, List[str], List[str]]:
    '''
    Args:
        tx_id                            (str): A txid containing an output
                                                held by control_addr
        index                            (int): The index controlled
        control_addr                     (str): the addr controlling the
                                                outpoint described above
        control_addr_keypair (Tuple(str, str)): the keypair to the control addr
        prevout_value                    (int): The value of the prevout
        start_nonce                      (int): the ethereum account nonce to
                                                use in the first tx
        contract_address                 (str): the ether auction contract
        eth_value                        (int): the amount of ethereum in wei
                                                to sell in each auc
        eth_privkey                      (str): the privkey to an eth account
        num_auctions                     (int): the number of auctions to make
        recipient                        (str): the bitcoin address to send
                                                proceeds to
        form                       list(tuple): the price/timelock tuples for
                                                the auctions
        network_id                       (int): ether network id, 1 for main,
                                                3 for ropsten
    Returns:
        tuple(riemann.tx.Tx, List(str), List(str)):
            the Bitcoin tx,
            the ether data blobs,
            and the signed Ethereum txns
    '''
    if network_id != 1:
        riemann.select_network('bitcoin_test')
    split_tx = make_and_sign_split_tx(
        tx_id=tx_id,
        index=index,
        prevout_value=prevout_value,
        control_addr=control_addr,
        control_addr_keypair=control_addr_keypair,
        num_auctions=num_auctions,
        change_addr=recipient)

    split_tx_id = split_tx.tx_id.hex()
    prevout_tuples = [(split_tx_id, i, 550) for i in range(num_auctions)]

    partial_txns = pt.multidutch_as_hex(prevouts=prevout_tuples,
                                        recipient_addr=recipient,
                                        format_tuples=form,
                                        keypair=control_addr_keypair)

    ether_blobs = [
        iw.create_open_data(partial_tx=p[0],
                            reservePrice=1000000,
                            reqDiff=reqDiff,
                            asset=ETH_ZERO_ADDRESS,
                            value=eth_value).hex()
        for p in zip(partial_txns, range(len(partial_txns)))
    ]

    unsigned_ether = [
        iw.create_open_tx(partial_tx=p[0],
                          reservePrice=1000000,
                          reqDiff=reqDiff,
                          asset=ETH_ZERO_ADDRESS,
                          value=eth_value,
                          nonce=start_nonce + p[1],
                          gas_price=15 * GWEI,
                          start_gas=500000,
                          contract_address=contract_address,
                          network_id=network_id)
        for p in zip(partial_txns, range(len(partial_txns)))
    ]

    secret_key = bytes.fromhex(eth_privkey)
    signed_ether_txns = [
        tx.sign(secret_key).serialize().hex() for tx in unsigned_ether
    ]

    return split_tx, ether_blobs, signed_ether_txns
Exemplo n.º 22
0
import riemann
from riemann import simple, utils
from riemann.encoding import addresses as addr


riemann.select_network('zcash_sapling_main')

# Needs a 32 byte hash, alice's pubkey, a timeout, and bob's pubkey
htlc_redeem_script = (
    'OP_IF '
        'OP_SHA256 {secret_hash} OP_EQUALVERIFY '   # noqa: E131
        'OP_DUP OP_HASH160 {pkh0} '
    'OP_ELSE '
        '{timeout} OP_CHECKLOCKTIMEVERIFY OP_DROP '
        'OP_DUP OP_HASH160 {pkh1} '
    'OP_ENDIF '
    'OP_EQUALVERIFY '
    'OP_CHECKSIG')

# this spends via the HTLC secret revelation path
# Needs (signature, pubkey, secret, TRUE)
# IN THAT ORDER!
htlc_stack_script_execute = '{sig} {pk} {secret} OP_1'

# this spends via the timelocked refund path
# Needs (signature, pubkey, FALSE serialized_redeem_script)
# IN THAT ORDER!
htlc_stack_script_refund = '{sig} {pk} OP_0'

# Worst secret in the world :D
secret = '32' * 32
Exemplo n.º 23
0
 def test_cashaddr_from_output_script(self):
     riemann.select_network('bitcoin_cash_main')
     self.assertEqual(addr.from_output_script(helpers.PKH_0_OUTPUT_SCRIPT),
                      helpers.P2PKH_0_CASHADDR)
     self.assertEqual(addr.from_output_script(helpers.OP_IF_OUTPUT_SCRIPT),
                      helpers.OP_IF_CASHADDR)
Exemplo n.º 24
0
 def test_get_current_network_name(self):
     for n in networks.SUPPORTED:
         riemann.select_network(n)
         self.assertEqual(riemann.get_current_network_name(), n)
Exemplo n.º 25
0
 def tearDown(self):
     riemann.select_network('bitcoin_main')
Exemplo n.º 26
0
 def setUp(self):
     riemann.select_network('decred_main')
Exemplo n.º 27
0
 def test_select_network(self):
     for n in networks.SUPPORTED:
         riemann.select_network(n)
         self.assertIs(riemann.network, networks.SUPPORTED[n])
Exemplo n.º 28
0
 def test_make_decred_outpoint(self):
     riemann.select_network('decred_main')
     self.assertEqual(
         tb.make_outpoint(tx_id_le=helpers.DCR['ser']['ins'][0]['hash'],
                          index=0,
                          tree=0), helpers.DCR['ser']['ins'][0]['outpoint'])