예제 #1
0
def hostport_to_privkeyaddr(host, port):
    """ Return `(private key, address)` deterministically generated. """
    myip_port = '{}:{}'.format(host, port)
    privkey = sha3(myip_port)
    addr = privtoaddr(privkey)

    return privkey, addr
예제 #2
0
def setup_tps(rpc_server, config_path, channelmanager_address, asset_address, deposit):
    """ Creates the required contract and the fully connected Raiden network
    prior to running the test.

    Args:
        rpc_server (str): A string in the format '{host}:{port}' used to define
            the JSON-RPC end-point.
        config_path (str): A full/relative path to the yaml configuration file.
        channelmanager_address (str): The address of the channel manager contract.
        asset_address (str): The address of the asset used for testing.
        deposit (int): The default deposit that will be made for all test nodes.
    """
    # TODO:
    #  - create/register the channel manager

    rpc_connection = rpc_server.split(':')
    rpc_connection = (rpc_connection[0], int(rpc_connection[1]))

    blockchain_service = BlockChainService(rpc_connection, channelmanager_address)
    blockchain_service.new_channel_manager_contract(asset_address=asset_address)

    with codecs.open(config_path, encoding='utf8') as handler:
        config = yaml.load(handler)

    node_addresses = []
    for node in config['nodes']:
        privkey = sha3('{}:{}'.format(node['host'], node['port']))
        node_addresses.append(privtoaddr(privkey))

    random_raiden_network(asset_address, blockchain_service, node_addresses, deposit)
예제 #3
0
 def __init__(self, chain, privkey, transport, discovery):
     self.chain = chain
     self.privkey = privkey
     self.address = privtoaddr(privkey)
     self.protocol = RaidenProtocol(transport, discovery, self)
     transport.protocol = self.protocol
     self.assetmanagers = dict()
     self.api = RaidenAPI(self)
예제 #4
0
 def __init__(self, chain, privkey, transport, discovery, config):  # pylint: disable=too-many-arguments
     self.chain = chain
     self.config = config
     self.privkey = privkey
     self.address = privtoaddr(privkey)
     self.protocol = RaidenProtocol(transport, discovery, self)
     transport.protocol = self.protocol
     self.assetmanagers = dict()
     self.api = RaidenAPI(self)
예제 #5
0
    def __init__(self, mapping, default_start_port=40001):
        super(PredictiveDiscovery, self).__init__()

        # [('127.0.0.1', 36), ('127.0.0.2', 15), ...]
        for entry in mapping:
            if len(entry) == 3:
                start_port = entry[-1]
            else:
                start_port = default_start_port

            (host, num_nodes) = entry[:2]

            for i in range(num_nodes):
                host_port = (host, start_port + i)
                nodeid = privtoaddr(sha3('{}:{}'.format(*host_port)))
                self.nodeid_hostport[nodeid] = host_port
예제 #6
0
    def __init__(self, chain, privkey, transport, discovery, config):  # pylint: disable=too-many-arguments
        self.registries = list()
        self.managers_by_asset_address = dict()
        self.managers_by_address = dict()
        self.event_listeners = list()

        self.chain = chain
        self.config = config
        self.privkey = privkey
        self.address = privtoaddr(privkey)
        self.protocol = RaidenProtocol(transport, discovery, self)
        transport.protocol = self.protocol

        message_handler = RaidenMessageHandler(self)
        event_handler = RaidenEventHandler(self)

        self.api = RaidenAPI(self)
        self.event_handler = event_handler
        self.message_handler = message_handler

        self.on_message = message_handler.on_message
        self.on_event = event_handler.on_event
예제 #7
0
# -*- coding: utf8 -*-
from raiden.messages import Ping, Ack, decode, Lock, MediatedTransfer
from raiden.utils import privtoaddr, sha3

PRIVKEY = 'x' * 32
ADDRESS = privtoaddr(PRIVKEY)


def test_signature():
    ping = Ping(nonce=0)
    ping.sign(PRIVKEY)
    assert ping.sender == ADDRESS


def test_encoding():
    ping = Ping(nonce=0)
    ping.sign(PRIVKEY)
    decoded_ping = decode(ping.encode())
    assert isinstance(decoded_ping, Ping)
    assert decoded_ping.sender == ADDRESS == ping.sender
    assert ping.nonce == decoded_ping.nonce
    assert ping.signature == decoded_ping.signature
    assert ping.cmdid == decoded_ping.cmdid
    assert ping.hash == decoded_ping.hash


def test_hash():
    ping = Ping(nonce=0)
    ping.sign(PRIVKEY)
    data = ping.encode()
    msghash = sha3(data)
예제 #8
0
from raiden.encoding import SignatureMissingError
from raiden.messages import Ping, Ack, decode, Lock, MediatedTransfer
from raiden.utils import privtoaddr, isaddress, pex, sha3
import pytest
import rlp

privkey = 'x' * 32
address = privtoaddr(privkey)


def test_signature():
    p = Ping(nonce=0)
    with pytest.raises(SignatureMissingError):
        p.sender
    p.sign(privkey)
    assert p.sender == address


def test_encoding():
    p = Ping(nonce=0)
    with pytest.raises(SignatureMissingError):
        p.encode()
    p.sign(privkey)
    d = p.encode()
    p2 = decode(d)
    assert isinstance(p2, Ping)
    assert p2.sender == address == p.sender
    assert p.nonce == p2.nonce
    assert p.signature == p2.signature
    assert p.cmdid == p.cmdid
    assert p.hash == p2.hash
예제 #9
0
def test_ncc():

    token_library_path = get_contract_path('StandardToken.sol')
    token_path = get_contract_path('HumanStandardToken.sol')

    s = tester.state()
    assert s.block.number < 1150000
    s.block.number = 1158001
    assert s.block.number > 1150000
    # Token creation
    lib_token = s.abi_contract(None, path=token_library_path, language="solidity")
    token = s.abi_contract(None, path=token_path, language="solidity", libraries={'StandardToken': lib_token.address.encode('hex')}, constructor_parameters=[10000, "raiden", 0, "rd"])
    # Getter creation
    lib_getter = s.abi_contract(None, path=decode_lib, language="solidity")
    getter = s.abi_contract(None, path=getter_path, language="solidity", libraries={'Decoder': lib_getter.address.encode('hex')})

    INITIATOR_PRIVKEY = tester.k0
    INITIATOR_ADDRESS = privtoaddr(INITIATOR_PRIVKEY)

    RECIPIENT_PRIVKEY = tester.k1
    RECIPIENT_ADDRESS = privtoaddr(RECIPIENT_PRIVKEY)

    ASSET_ADDRESS = token.address

    HASHLOCK = sha3(INITIATOR_PRIVKEY)
    LOCK_AMOUNT = 29
    LOCK_EXPIRATION = 31
    LOCK = Lock(LOCK_AMOUNT, LOCK_EXPIRATION, HASHLOCK)
    LOCKSROOT = merkleroot([
        sha3(LOCK.as_bytes), ])   # print direct_transfer.encode('hex')

    nonce = 1
    asset = ASSET_ADDRESS
    balance = 1
    recipient = RECIPIENT_ADDRESS
    locksroot = LOCKSROOT

    msg = DirectTransfer(
        nonce,
        asset,
        balance,
        recipient,
        locksroot,
    ).sign(INITIATOR_PRIVKEY)
    packed = msg.packed()
    direct_transfer = str(packed.data)

    # pure python recover
    sen = recover_publickey(direct_transfer[:148], str(packed.signature))
    assert address_from_key(sen) == tester.a0

    # addr = getter.ecTest(direct_transfer[:148], sig)
    # assert addr == INITIATOR_ADDRESS.encode('hex')
    sender = getter.getSender(direct_transfer)
    assert sender == tester.a0.encode('hex')

    # with sigSplit directly in Getters.sol
    r, s, v = getter.sigSplit(str(packed.signature))
    assert r == str(packed.signature[:32])
    assert s == str(packed.signature[32:64])
    assert v == packed.signature[64] + 27

    sender = getter.getSender(direct_transfer)
    assert sender == tester.a0.encode('hex')