Ejemplo n.º 1
0
def get_aleph_account():
    if settings.ethereum_pkey:
        pri_key = HexBytes(settings.ethereum_pkey)
        account = ETHAccount(pri_key)
        return account
    else:
        return None
Ejemplo n.º 2
0
def main(filename, pkey=None, storage_engine="IPFS", channel="TEST"):
    """Uploads or store FILENAME.
    
    If FILENAME is an IPFS multihash and IPFS is selected as an engine (default), don't try to upload, just pin it to the network.
    Else, uploads the file to the network before pining it.
    """
    if pkey is None:
        pkey = get_fallback_private_key()

    account = ETHAccount(private_key=pkey)

    upload_filename = None
    upload_hash = None

    if 46 <= len(filename) <= 48 and filename.startswith(
            "Q") and storage_engine == "IPFS":
        upload_hash = filename
    else:
        upload_filename = filename

    asyncio.run(
        do_upload(account,
                  storage_engine,
                  channel,
                  filename=upload_filename,
                  file_hash=upload_hash))
Ejemplo n.º 3
0
async def test_create_aggregate():
    _get_fallback_session.cache_clear()

    if os.path.exists(PRIVATE_KEY_FILE):
        os.remove(PRIVATE_KEY_FILE)

    private_key = get_fallback_private_key()
    account: ETHAccount = ETHAccount(private_key=private_key)

    content = {"Hello": "World"}

    mock_session = MagicMock()

    await create_aggregate(
        account=account,
        key='hello',
        content=content,
        channel="TEST",
        session=mock_session,
    )

    await create_aggregate(
        account=account,
        key='hello',
        content='world',
        channel="TEST",
        session=mock_session,
        api_server="https://example.org",
    )

    assert mock_session.post.called
Ejemplo n.º 4
0
async def test_create_store():
    _get_fallback_session.cache_clear()

    if os.path.exists(PRIVATE_KEY_FILE):
        os.remove(PRIVATE_KEY_FILE)

    private_key = get_fallback_private_key()
    account: ETHAccount = ETHAccount(private_key=private_key)

    content = {"Hello": "World"}

    mock_session = MagicMock()

    mock_ipfs_push_file = AsyncMock()
    mock_ipfs_push_file.return_value = "FAKE-HASH"

    with patch('aleph_client.asynchronous.ipfs_push_file',
               mock_ipfs_push_file):

        await create_store(
            account=account,
            file_content=b"HELLO",
            # file_hash="abcde",
            channel="TEST",
            storage_engine=StorageEnum.ipfs,
            session=mock_session,
            api_server="https://example.org",
        )

        await create_store(
            account=account,
            # file_content=b"HELLO",
            file_hash="FAKE-HASH",
            channel="TEST",
            storage_engine=StorageEnum.ipfs,
            session=mock_session,
            api_server="https://example.org",
        )

    mock_storage_push_file = AsyncMock()
    mock_storage_push_file.return_value = "FAKE-HASH"

    with patch('aleph_client.asynchronous.storage_push_file',
               mock_storage_push_file):

        await create_store(
            account=account,
            file_content=b"HELLO",
            channel="TEST",
            storage_engine=StorageEnum.storage,
            session=mock_session,
            api_server="https://example.org",
        )

    assert mock_session.post.called
Ejemplo n.º 5
0
def main(host, port, channel, pkey=None, secret=None):
    app.add_routes(routes)
    loop = asyncio.get_event_loop()

    app['secret'] = secret
    app['channel'] = channel

    if pkey is None:
        pkey = get_fallback_private_key()

    account = ETHAccount(private_key=pkey)
    app['account'] = account

    web.run_app(app, host=host, port=port)
Ejemplo n.º 6
0
async def test_remote_storage():
    host = "http://localhost:8888"
    private_key = (b"xRR\xd4P\xdb9\x93(U\xa7\xd5\x81\xba\xc7\x9fiT"
                   b"\xb8]\x12\x82 \xd1\x81\xc8\x94\xf0\xdav\xbb\xfb")
    local_account = ETHAccount(private_key=private_key)

    with patch("aiohttp.client.ClientSession") as mock_session:
        mock_session.get.return_value.__aenter__.return_value.json.return_value = (
            AccountProperties(
                chain="ETH",
                curve="secp256k1",
                address=local_account.get_address(),
                public_key=local_account.get_public_key(),
            ).dict())

        remote_account = await RemoteAccount.from_crypto_host(
            host=host, session=mock_session)

        assert remote_account.get_address() == local_account.get_address()
        assert remote_account.get_public_key() == local_account.get_public_key(
        )

        # --- Test remote signing ---

        expected_signature = ("0xa943de6c550ddf9cd1d3e58e77e9952b9f97e1bcb2c69"
                              "a2f4ee56446dc8a38f02fb4a4e85c2d02efa26750456090"
                              "3b983b4eef8b8030cc0d89550c18c69aef081c")
        message = {
            "chain": "ETH",
            "sender": local_account.get_address(),
            "type": "POST",
            "item_hash": "HASH",
        }
        expected_signed_message = {
            "signature": expected_signature,
        }
        expected_signed_message.update(message)
        mock_session.post.return_value.__aenter__.return_value.json.return_value = (
            expected_signed_message)

        signed_message = await remote_account.sign_message(message)

        assert set(signed_message.keys()) == set(message.keys()).union(
            ["signature"])
        assert signed_message["signature"] == expected_signature

        local_signed_message = await local_account.sign_message(message)
        assert signed_message == local_signed_message
Ejemplo n.º 7
0
async def gateway(loop,
                  host='api1.aleph.im',
                  port=1883,
                  ca_cert=None,
                  pkey=None,
                  keepalive=10,
                  transport='tcp',
                  auth=None):

    if pkey is None:
        pkey = get_fallback_private_key()

    account = ETHAccount(private_key=pkey)
    state = dict()
    userdata = {'account': account, 'state': state, 'received': False}
    client = aiomqtt.Client(loop, userdata=userdata, transport=transport)
    client.on_connect = on_connect
    client.on_disconnect = on_disconnect
    client.on_message = on_message

    if ca_cert is not None:
        client.tls_set(ca_cert)
    if auth is not None:
        client.username_pw_set(**auth)

    asyncio.ensure_future(client.loop_forever())

    await client.connect(host, port, keepalive)
    # client.loop_forever()
    while True:
        await asyncio.sleep(10)
        if not userdata['received']:
            await client.reconnect()

        for key, value in state.items():
            ret = create_aggregate(account, key, value, channel='IOT_TEST')
            print("sent", ret['item_hash'])
            userdata['received'] = False
Ejemplo n.º 8
0
async def test_forget():
    _get_fallback_session.cache_clear()

    if os.path.exists(PRIVATE_KEY_FILE):
        os.remove(PRIVATE_KEY_FILE)

    private_key = get_fallback_private_key()
    account: ETHAccount = ETHAccount(private_key=private_key)

    content = {"Hello": "World"}

    mock_session = MagicMock()

    await forget(
        account=account,
        hashes=["FAKE-HASH"],
        reason="GDPR",
        channel="TEST",
        session=mock_session,
        api_server="https://example.org",
    )

    assert mock_session.post.called
Ejemplo n.º 9
0
async def test_create_program():
    _get_fallback_session.cache_clear()

    if os.path.exists(PRIVATE_KEY_FILE):
        os.remove(PRIVATE_KEY_FILE)

    private_key = get_fallback_private_key()
    account: ETHAccount = ETHAccount(private_key=private_key)

    content = {"Hello": "World"}

    mock_session = MagicMock()

    await create_program(
        account=account,
        program_ref="FAKE-HASH",
        entrypoint="main:app",
        runtime="FAKE-HASH",
        channel="TEST",
        session=mock_session,
        api_server="https://example.org",
    )

    assert mock_session.post.called
Ejemplo n.º 10
0
def get_aleph_account():
    return ETHAccount(settings.ethereum_pkey)
Ejemplo n.º 11
0
def fixture_account():
    private_key = get_fallback_private_key()
    return ETHAccount(private_key)
Ejemplo n.º 12
0
def get_aleph_account():
    return ETHAccount(config['web3']['pkey'])