Exemplo n.º 1
0
    def create_lt(self, addr_from: bytes, kyber_pk: bytes, tesla_pk: bytes,
                  xmss_pk: bytes, xmss_ots_index: int) -> LatticePublicKey:

        return LatticePublicKey.create(addr_from=addr_from,
                                       kyber_pk=kyber_pk,
                                       tesla_pk=tesla_pk,
                                       xmss_pk=xmss_pk,
                                       xmss_ots_index=xmss_ots_index)
Exemplo n.º 2
0
 def create_lattice_public_key_txn(self, addr_from: bytes, kyber_pk: bytes,
                                   dilithium_pk: bytes, fee: int,
                                   xmss_pk: bytes) -> SlaveTransaction:
     return LatticePublicKey.create(addr_from=addr_from,
                                    kyber_pk=kyber_pk,
                                    dilithium_pk=dilithium_pk,
                                    fee=fee,
                                    xmss_pk=xmss_pk)
Exemplo n.º 3
0
 def create_lattice_public_key_txn(self, kyber_pk: bytes,
                                   dilithium_pk: bytes, fee: int,
                                   xmss_pk: bytes,
                                   master_addr: bytes) -> SlaveTransaction:
     return LatticePublicKey.create(kyber_pk=kyber_pk,
                                    dilithium_pk=dilithium_pk,
                                    fee=fee,
                                    xmss_pk=xmss_pk,
                                    master_addr=master_addr)
Exemplo n.º 4
0
    def create_unsigned_tx(self, transaction):
        if transaction.type == qrl_pb2.Transaction.TRANSFER:
            return TransferTransaction.create(transaction.addr_from,
                                              transaction.Transfer.addr_to,
                                              transaction.Transfer.amount,
                                              transaction.Transfer.fee,
                                              transaction.xmss_pk,
                                              transaction.xmss_ots_index)

        elif transaction.type == qrl_pb2.Transaction.LATTICE:
            return LatticePublicKey.create(transaction.addr_from,
                                           transaction.LatticePublicKey.kyber_pk,
                                           transaction.LatticePublicKey.tesla_pk,
                                           transaction.xmss_pk,
                                           transaction.xmss_ots_index)

        elif transaction.type == qrl_pb2.Transaction.MESSAGE:
            return MessageTransaction.create(transaction.addr_from,
                                             transaction.Message.message_hash,
                                             transaction.Message.fee,
                                             transaction.xmss_pk,
                                             transaction.xmss_ots_index)

        elif transaction.type == qrl_pb2.Transaction.TOKEN:
            return TokenTransaction.create(transaction.addr_from,
                                           transaction.Token.symbol,
                                           transaction.Token.name,
                                           transaction.Token.owner,
                                           transaction.Token.decimals,
                                           transaction.Token.initial_balances,
                                           transaction.Token.fee,
                                           transaction.xmss_pk,
                                           transaction.xmss_ots_index)

        elif transaction.type == qrl_pb2.Transaction.TRANSFERTOKEN:
            return TransferTokenTransaction.create(transaction.addr_from,
                                                   transaction.TransferToken.token_txhash,
                                                   transaction.TransferToken.addr_to,
                                                   transaction.TransferToken.amount,
                                                   transaction.TransferToken.fee,
                                                   transaction.xmss_pk,
                                                   transaction.xmss_ots_index)

        else:
            return None
Exemplo n.º 5
0
def tx_latticepk(ctx, src, master, kyber_pk, dilithium_pk, fee, ots_key_index):
    """
    Create Lattice Public Keys Transaction
    """
    if not ctx.obj.remote:
        click.echo('This command is unsupported for local wallets')
        return

    try:
        _, src_xmss = _select_wallet(ctx, src)
        if not src_xmss:
            click.echo("A local wallet is required to sign the transaction")
            quit(1)

        address_src_pk = src_xmss.pk
        src_xmss.set_ots_index(ots_key_index)
        kyber_pk = kyber_pk.encode()
        dilithium_pk = dilithium_pk.encode()
        master_addr = _parse_qaddress(master)
        # FIXME: This could be problematic. Check
        fee_shor = _shorize(fee)
    except Exception as e:
        click.echo("Error validating arguments: {}".format(e))
        quit(1)

    try:
        tx = LatticePublicKey.create(fee=fee_shor,
                                     kyber_pk=kyber_pk,
                                     dilithium_pk=dilithium_pk,
                                     xmss_pk=address_src_pk,
                                     master_addr=master_addr)
        tx.sign(src_xmss)

        stub = ctx.obj.get_stub_public_api()
        pushTransactionReq = qrl_pb2.PushTransactionReq(
            transaction_signed=tx.pbdata)
        pushTransactionResp = stub.PushTransaction(pushTransactionReq,
                                                   timeout=5)

        print(pushTransactionResp.error_code)
    except Exception as e:
        print("Error {}".format(str(e)))
Exemplo n.º 6
0
def tx_latticepk(ctx, src, kyber_pk, dilithium_pk, fee, ots_key_index):
    """
    Create Lattice Public Keys Transaction
    """
    if not ctx.obj.remote:
        click.echo('This command is unsupported for local wallets')
        return

    stub = qrl_pb2_grpc.PublicAPIStub(ctx.obj.channel_public)

    try:
        address_src, src_xmss = _select_wallet(ctx, src)
        if not src_xmss:
            click.echo("A local wallet is required to sign the transaction")
            quit(1)

        address_src_pk = src_xmss.pk
        src_xmss.set_ots_index(ots_key_index)
        kyber_pk = kyber_pk.encode()
        dilithium_pk = dilithium_pk.encode()
        # FIXME: This could be problematic. Check
        fee_shor = int(fee * 1.e9)
    except Exception:
        click.echo("Error validating arguments")
        quit(1)

    try:
        tx = LatticePublicKey.create(addr_from=address_src,
                                     fee=fee_shor,
                                     kyber_pk=kyber_pk,
                                     dilithium_pk=dilithium_pk,
                                     xmss_pk=address_src_pk)
        tx.sign(src_xmss)

        pushTransactionReq = qrl_pb2.PushTransactionReq(
            transaction_signed=tx.pbdata)
        pushTransactionResp = stub.PushTransaction(pushTransactionReq,
                                                   timeout=5)

        print(pushTransactionResp.error_code)
    except Exception as e:
        print("Error {}".format(str(e)))
Exemplo n.º 7
0
    def test_add_4(self, mock_difficulty_tracker_get):
        with set_qrl_dir('wallet_ver1'):
            with State() as state:
                with mocked_genesis() as custom_genesis:
                    chain_manager = ChainManager(state)

                    chain_manager._difficulty_tracker = Mock()
                    tmp_difficulty = StringToUInt256('2')
                    tmp_target = DifficultyTracker.get_target(tmp_difficulty)
                    mock_difficulty_tracker_get.return_value = [
                        tmp_difficulty, tmp_target
                    ]

                    alice_xmss = get_alice_xmss()
                    slave_xmss = XMSS(
                        XmssFast(alice_xmss.seed, alice_xmss.height))
                    random_xmss1 = get_random_xmss()
                    random_kyber1 = Kyber()
                    random_dilithium1 = Dilithium()
                    random_xmss2 = get_random_xmss()
                    random_kyber2 = Kyber()
                    random_dilithium2 = Dilithium()

                    message = b'Hello World How are you?'
                    prf512_seed = b'10192'

                    custom_genesis.genesis_balance.extend([
                        qrl_pb2.GenesisBalance(address=random_xmss1.address,
                                               balance=65000000000000000)
                    ])
                    custom_genesis.genesis_balance.extend([
                        qrl_pb2.GenesisBalance(address=random_xmss2.address,
                                               balance=65000000000000000)
                    ])
                    chain_manager.load(custom_genesis)

                    with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                        time_mock.return_value = 1615270948

                        lattice_public_key_txn = LatticePublicKey.create(
                            fee=1,
                            kyber_pk=random_kyber1.getPK(),
                            dilithium_pk=random_dilithium1.getPK(),
                            xmss_pk=random_xmss1.pk)
                        lattice_public_key_txn._data.nonce = 1
                        lattice_public_key_txn.sign(random_xmss1)
                        genesis_block = GenesisBlock()
                        tmp_block1 = Block.create(
                            block_number=1,
                            prev_block_headerhash=genesis_block.headerhash,
                            prev_block_timestamp=genesis_block.timestamp,
                            transactions=[lattice_public_key_txn],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block1.blockheader, False):
                            tmp_block1.set_nonces(tmp_block1.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block1)
                        self.assertTrue(res)

                        # Need to move forward the time to align with block times
                        time_mock.return_value += config.dev.minimum_minting_delay * 2

                        encrypted_eph_message = create_ephemeral_channel(
                            msg_id=lattice_public_key_txn.txhash,
                            ttl=time_mock.return_value,
                            ttr=0,
                            addr_from=random_xmss2.address,
                            kyber_pk=random_kyber2.getPK(),
                            kyber_sk=random_kyber2.getSK(),
                            receiver_kyber_pk=random_kyber1.getPK(),
                            dilithium_pk=random_dilithium2.getPK(),
                            dilithium_sk=random_dilithium2.getSK(),
                            prf512_seed=prf512_seed,
                            data=message,
                            nonce=1)

                        chain_manager.state.update_ephemeral(
                            encrypted_eph_message)
                        eph_metadata = chain_manager.state.get_ephemeral_metadata(
                            lattice_public_key_txn.txhash)

                        # Decrypting Payload

                        encrypted_eph_message = eph_metadata.encrypted_ephemeral_message_list[
                            0]
                        encrypted_payload = encrypted_eph_message.payload

                        random_kyber1.kem_decode(
                            encrypted_eph_message.channel.enc_aes256_symkey)
                        aes_key = bytes(random_kyber1.getMyKey())
                        myAES = AES(aes_key)
                        decrypted_payload = myAES.decrypt(encrypted_payload)
                        ephemeral_channel_payload = EphemeralChannelPayload.from_json(
                            decrypted_payload)

                        self.assertEqual(ephemeral_channel_payload.prf512_seed,
                                         b'10192')
                        self.assertEqual(ephemeral_channel_payload.data,
                                         b'Hello World How are you?')

                        # TODO (cyyber): Add Ephemeral Testing code using Naive RNG

                        tmp_block2 = Block.create(
                            block_number=2,
                            prev_block_headerhash=tmp_block1.headerhash,
                            prev_block_timestamp=tmp_block1.timestamp,
                            transactions=[],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block2.blockheader, False):
                            tmp_block2.set_nonces(tmp_block2.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block2)
                        self.assertTrue(res)

                        # Need to move forward the time to align with block times
                        time_mock.return_value += config.dev.minimum_minting_delay * 2

                        tmp_block3 = Block.create(
                            block_number=3,
                            prev_block_headerhash=tmp_block2.headerhash,
                            prev_block_timestamp=tmp_block1.timestamp,
                            transactions=[],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block3.blockheader, False):
                            tmp_block3.set_nonces(tmp_block3.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block3)
                        self.assertTrue(res)

                        time_mock.return_value += config.dev.minimum_minting_delay

                        tmp_block4 = Block.create(
                            block_number=4,
                            prev_block_headerhash=tmp_block3.headerhash,
                            prev_block_timestamp=tmp_block1.timestamp,
                            transactions=[],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block4.blockheader, False):
                            tmp_block4.set_nonces(tmp_block4.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block4)
                        self.assertTrue(res)

                        address_state = chain_manager.get_address(
                            random_xmss1.address)

                        self.assertEqual(
                            address_state.latticePK_list[0].kyber_pk,
                            lattice_public_key_txn.kyber_pk)
                        self.assertEqual(
                            address_state.latticePK_list[0].dilithium_pk,
                            lattice_public_key_txn.dilithium_pk)

                        self.assertEqual(address_state.address,
                                         lattice_public_key_txn.addr_from)

                        random_xmss1_state = chain_manager.get_address(
                            random_xmss1.address)

                        self.assertEqual(64999999999999999,
                                         random_xmss1_state.balance)