コード例 #1
0
def test_go_sig():
    """
    go client started with:

    ethereum -port="40404" -loglevel=5  -nodekeyhex="9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658" -bootnodes="enode://2da47499d52d9161a778e4c711e22e8651cb90350ec066452f9516d1d11eb465d1ec42bb27ec6cd4488b8b6a1a411cb5ef83c16cbb8bee194624bb65fef0f7fd@127.0.0.1:30303"
    """

    r_pubkey = decode_hex(
        "ab16b8c7fc1febb74ceedf1349944ffd4a04d11802451d02e808f08cb3b0c1c1a9c4e1efb7d309a762baa4c9c8da08890b3b712d1666b5b630d6c6a09cbba171"
    )
    d = {
        'signed_data':
        'a061e5b799b5bb3a3a68a7eab6ee11207d90672e796510ac455e985bd206e240',
        'cmd':
        'find_node',
        'body':
        '03f847b840ab16b8c7fc1febb74ceedf1349944ffd4a04d11802451d02e808f08cb3b0c1c1a9c4e1efb7d309a762baa4c9c8da08890b3b712d1666b5b630d6c6a09cbba1718454e869b1',
        'signature':
        '0de032c62e30f4a9f9f07f25ac5377c5a531116147617a6c08f946c97991f351577e53ae138210bdb7447bab53f3398d746d42c64a9ce67a6248e59353f1bc6e01'
    }

    priv_seed = b'test'
    priv_key = mk_privkey(priv_seed)
    assert priv_key == decode_hex(
        "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658")
    my_pubkey = privtopub(priv_key)
    assert my_pubkey == r_pubkey, (my_pubkey, r_pubkey)
    go_body = decode_hex(d['body'])  # cmd_id, rlp.encoded
    import rlp
    target_node_id, expiry = rlp.decode(go_body[1:])
    assert target_node_id == r_pubkey  # lookup for itself
    go_signed_data = decode_hex(d['signed_data'])
    go_signature = decode_hex(d['signature'])

    my_signature = ecdsa_sign(go_signed_data, priv_key)
    assert my_signature == ecdsa_sign(go_signed_data,
                                      priv_key)  # deterministic k

    assert len(go_signed_data) == 32  # sha3()
    assert len(go_signature) == 65
    assert len(my_signature) == 65  # length is okay

    try:
        assert my_signature == go_signature
        failed = False
    except:
        "expected fail, go signatures are not generated with deterministic k"
        failed = True
        pass
    assert failed

    # decoding works when we signed it
    assert my_pubkey == ecdsa_recover(go_signed_data, my_signature)

    # problem we can not decode the pubkey from the go signature
    # and go can not decode ours
    ecdsa_recover(go_signed_data, go_signature)
コード例 #2
0
def test_get_ecdh_key():
    privkey = decode_hex("332143e9629eedff7d142d741f896258f5a1bfab54dab2121d3ec5000093d74b")
    remote_pubkey = decode_hex("f0d2b97981bd0d415a843b5dfe8ab77a30300daab3658c578f2340308a2da1a07f0821367332598b6aa4e180a41e92f4ebbae3518da847f0b1c0bbfe20bcf4e1")

    agree_expected = decode_hex("ee1418607c2fcfb57fda40380e885a707f49000a5dda056d828b7d9bd1f29a08")

    e = crypto.ECCx(raw_privkey=privkey)
    agree = e.get_ecdh_key(remote_pubkey)
    assert agree == agree_expected
コード例 #3
0
def normalize_key(key):
    if is_numeric(key):
        o = encode_int32(key)
    elif len(key) == 32:
        o = key
    elif len(key) == 64:
        o = decode_hex(key)
    elif len(key) == 66 and key[:2] == '0x':
        o = decode_hex(key[2:])
    else:
        raise Exception("Invalid key format: %r" % key)
    if o == b'\x00' * 32:
        raise Exception("Zero privkey invalid")
    return o
コード例 #4
0
def get_connected_apps():
    a_config = dict(p2p=dict(listen_host='127.0.0.1', listen_port=3005),
                    node=dict(privkey_hex=encode_hex(crypto.sha3(b'a'))))
    b_config = copy.deepcopy(a_config)
    b_config['p2p']['listen_port'] = 3006
    b_config['node']['privkey_hex'] = encode_hex(crypto.sha3(b'b'))

    a_app = BaseApp(a_config)
    peermanager.PeerManager.register_with_app(a_app)
    a_app.start()

    b_app = BaseApp(b_config)
    peermanager.PeerManager.register_with_app(b_app)
    b_app.start()

    a_peermgr = a_app.services.peermanager
    b_peermgr = b_app.services.peermanager

    # connect
    host = b_config['p2p']['listen_host']
    port = b_config['p2p']['listen_port']
    pubkey = crypto.privtopub(decode_hex(b_config['node']['privkey_hex']))
    a_peermgr.connect((host, port), remote_pubkey=pubkey)

    return a_app, b_app
コード例 #5
0
def coerce_addr_to_bin(x):
    if is_numeric(x):
        return encode_hex(zpad(big_endian_int.serialize(x), 20))
    elif len(x) == 40 or len(x) == 0:
        return decode_hex(x)
    else:
        return zpad(x, 20)[-20:]
コード例 #6
0
def coerce_to_int(x):
    if is_numeric(x):
        return x
    elif len(x) == 40:
        return big_endian_to_int(decode_hex(x))
    else:
        return big_endian_to_int(x)
コード例 #7
0
def coerce_to_bytes(x):
    if is_numeric(x):
        return big_endian_int.serialize(x)
    elif len(x) == 40:
        return decode_hex(x)
    else:
        return x
コード例 #8
0
    def __init__(self, peermanager, connection, remote_pubkey=None):
        super(Peer, self).__init__()
        self.is_stopped = False
        self.hello_received = False
        self.peermanager = peermanager
        self.connection = connection
        self.config = peermanager.config
        self.protocols = OrderedDict()
        log.debug('peer init', peer=self)

        # create multiplexed encrypted session
        privkey = decode_hex(self.config['node']['privkey_hex'])
        hello_packet = P2PProtocol.get_hello_packet(self)
        self.mux = MultiplexedSession(privkey, hello_packet, remote_pubkey=remote_pubkey)
        self.remote_pubkey = remote_pubkey
        self.remote_capabilities = None

        # register p2p protocol
        assert issubclass(self.peermanager.wire_protocol, P2PProtocol)
        self.connect_service(self.peermanager)

        # assure, we don't get messages while replies are not read
        self.safe_to_read = gevent.event.Event()
        self.safe_to_read.set()

        self.greenlets = dict()

        # Stop peer if hello not received in self.dumb_remote_timeout seconds
        self.greenlets['dumb_checker'] = gevent.spawn_later(self.dumb_remote_timeout, self.check_if_dumb_remote)
コード例 #9
0
 def _with_dict(d):
     "recursively search and decode hex encoded data"
     for k, v in d.items():
         if k.endswith('_hex'):
             d[k[:-len('_hex')]] = decode_hex(v)
         if isinstance(v, dict):
             _with_dict(v)
コード例 #10
0
def parse_int_or_hex(s):
    if is_numeric(s):
        return s
    elif s[:2] in (b'0x', '0x'):
        s = to_string(s)
        tail = (b'0' if len(s) % 2 else b'') + s[2:]
        return big_endian_to_int(decode_hex(tail))
    else:
        return int(s)
コード例 #11
0
def host_port_pubkey_from_uri(uri):
    b_node_uri_scheme = str_to_bytes(node_uri_scheme)
    b_uri = str_to_bytes(uri)
    assert b_uri.startswith(b_node_uri_scheme) and \
        b'@' in b_uri and b':' in b_uri, b_uri
    pubkey_hex, ip_port = b_uri[len(b_node_uri_scheme):].split(b'@')
    assert len(pubkey_hex) == 2 * 512 // 8
    ip, port = ip_port.split(b':')
    return ip, port, decode_hex(pubkey_hex)
コード例 #12
0
def test_eip8_key_derivation():
    responder = RLPxSession(ECCx(raw_privkey=eip8_values['key_b']),
                            ephemeral_privkey=eip8_values['eph_key_b'])
    responder.decode_authentication(eip8_handshakes[1]['auth'])
    ack = responder.create_auth_ack_message(nonce=eip8_values['nonce_b'])
    responder.encrypt_auth_ack_message(ack)

    responder.setup_cipher()
    want_aes_secret = decode_hex(
        b'80e8632c05fed6fc2a13b0f8d31a3cf645366239170ea067065aba8e28bac487')
    want_mac_secret = decode_hex(
        b'2ea74ec5dae199227dff1af715362700e989d889d7a493cb0639691efb8e5f98')
    assert responder.aes_secret == want_aes_secret
    assert responder.mac_secret == want_mac_secret

    responder.ingress_mac.update(b'foo')
    mac_digest = responder.ingress_mac.digest()
    want_mac_digest = decode_hex(
        b'0c7ec6340062cc46f5e9f1e3cf86f8c8c403c5a0964f5df0ebd34a75ddc86db5')
    assert mac_digest == want_mac_digest
コード例 #13
0
def test_eip8_hello():
    peer = PeerMock()
    proto = P2PProtocol(peer, WiredService(BaseApp()))
    test_packet = Packet(cmd_id=1, payload=eip8_hello)
    proto._receive_hello(test_packet)
    assert peer.hello_received
    assert peer.remote_client_version == b"kneth/v0.91/plan9"
    assert peer.remote_hello_version == 22
    assert peer.remote_pubkey == decode_hex(
        'fda1cff674c90c9a197539fe3dfb53086ace64f83ed7c6eabec741f7f381cc803e52ab2cd55d5569bce4347107a310dfd5f88a010cd2ffd1005ca406f1842877'
    )
コード例 #14
0
 def __init__(self, app, transport):
     self.app = app
     self.transport = transport
     self.privkey = decode_hex(app.config['node']['privkey_hex'])
     self.pubkey = crypto.privtopub(self.privkey)
     self.nodes = LRUCache(2048)  # nodeid->Node,  fixme should be loaded
     self.this_node = Node(self.pubkey, self.transport.address)
     self.kademlia = KademliaProtocolAdapter(self.this_node, wire=self)
     this_enode = utils.host_port_pubkey_to_uri(
         self.app.config['discovery']['listen_host'],
         self.app.config['discovery']['listen_port'], self.pubkey)
     log.info('starting discovery proto', this_enode=this_enode)
コード例 #15
0
def normalize_address(x, allow_blank=False):
    if is_numeric(x):
        return int_to_addr(x)
    if allow_blank and x in {'', b''}:
        return b''
    if len(x) in (42, 50) and x[:2] in {'0x', b'0x'}:
        x = x[2:]
    if len(x) in (40, 48):
        x = decode_hex(x)
    if len(x) == 24:
        assert len(x) == 24 and sha3(x[:20])[:4] == x[-4:]
        x = x[:20]
    if len(x) != 20:
        raise Exception("Invalid address format: %r" % x)
    return x
コード例 #16
0
    def __init__(self, app):
        log.info('PeerManager init')
        WiredService.__init__(self, app)
        self.peers = []
        self.errors = PeerErrors(
        ) if self.config['log_disconnects'] else PeerErrorsBase()

        # setup nodeid based on privkey
        if 'id' not in self.config['p2p']:
            self.config['node']['id'] = crypto.privtopub(
                decode_hex(self.config['node']['privkey_hex']))

        self.listen_addr = (self.config['p2p']['listen_host'],
                            self.config['p2p']['listen_port'])
        self.server = StreamServer(self.listen_addr,
                                   handle=self._on_new_connection)
コード例 #17
0
def connect_go():
    a_config = dict(p2p=dict(listen_host='127.0.0.1', listen_port=3010),
                    node=dict(privkey_hex=encode_hex(crypto.sha3(b'a'))))

    a_app = BaseApp(a_config)
    peermanager.PeerManager.register_with_app(a_app)
    a_app.start()

    a_peermgr = a_app.services.peermanager

    # connect
    pubkey = decode_hex(
        "6ed2fecb28ff17dec8647f08aa4368b57790000e0e9b33a7b91f32c41b6ca9ba21600e9a8c44248ce63a71544388c6745fa291f88f8b81e109ba3da11f7b41b9"
    )
    a_peermgr.connect(('127.0.0.1', 30303), remote_pubkey=pubkey)
    gevent.sleep(50)
    a_app.stop()
コード例 #18
0
    def _discovery_loop(self):
        log.info('waiting for bootstrap')
        gevent.sleep(self.discovery_delay)
        while not self.is_stopped:
            try:
                num_peers, min_peers = self.num_peers(
                ), self.config['p2p']['min_peers']
                kademlia_proto = self.app.services.discovery.protocol.kademlia
                if num_peers < min_peers:
                    log.debug('missing peers',
                              num_peers=num_peers,
                              min_peers=min_peers,
                              known=len(kademlia_proto.routing))
                    nodeid = kademlia.random_nodeid()
                    kademlia_proto.find_node(nodeid)  # fixme, should be a task
                    gevent.sleep(self.discovery_delay)  # wait for results
                    neighbours = kademlia_proto.routing.neighbours(nodeid, 2)
                    if not neighbours:
                        gevent.sleep(self.connect_loop_delay)
                        continue
                    node = random.choice(neighbours)
                    if node.pubkey in self.remote_pubkeys():
                        gevent.sleep(self.discovery_delay)
                        continue
                    log.debug('connecting random', node=node)
                    local_pubkey = crypto.privtopub(
                        decode_hex(self.config['node']['privkey_hex']))
                    if node.pubkey == local_pubkey:
                        continue
                    if node.pubkey in [p.remote_pubkey for p in self.peers]:
                        continue
                    self.connect((node.address.ip, node.address.tcp_port),
                                 node.pubkey)
            except AttributeError:
                # TODO: Is this the correct thing to do here?
                log.error("Discovery service not available.")
                break
            except Exception as e:
                log.error("discovery failed",
                          error=e,
                          num_peers=num_peers,
                          min_peers=min_peers)
            gevent.sleep(self.connect_loop_delay)

        evt = gevent.event.Event()
        evt.wait()
コード例 #19
0
ファイル: messages.py プロジェクト: zrthxn/pyquarkchain
 def __init__(self, address, topics, data):
     if len(address) == 40:
         address = decode_hex(address)
     assert len(address) == 20
     super(Log, self).__init__(address, topics, data)
コード例 #20
0
 def __init__(self, app):
     log.info("ExampleService init")
     self.config = app.config
     self.address = privtopub_raw(
         decode_hex(self.config["node"]["privkey_hex"]))
     super(ExampleService, self).__init__(app)
コード例 #21
0
        "auth_ciphertext": "04a0274c5951e32132e7f088c9bdfdc76c9d91f0dc6078e848f8e3361193dbdc43b94351ea3d89e4ff33ddcefbc80070498824857f499656c4f79bbd97b6c51a514251d69fd1785ef8764bd1d262a883f780964cce6a14ff206daf1206aa073a2d35ce2697ebf3514225bef186631b2fd2316a4b7bcdefec8d75a1025ba2c5404a34e7795e1dd4bc01c6113ece07b0df13b69d3ba654a36e35e69ff9d482d88d2f0228e7d96fe11dccbb465a1831c7d4ad3a026924b182fc2bdfe016a6944312021da5cc459713b13b86a686cf34d6fe6615020e4acf26bf0d5b7579ba813e7723eb95b3cef9942f01a58bd61baee7c9bdd438956b426a4ffe238e61746a8c93d5e10680617c82e48d706ac4953f5e1c4c4f7d013c87d34a06626f498f34576dc017fdd3d581e83cfd26cf125b6d2bda1f1d56",
        "authresp_ciphertext": "049934a7b2d7f9af8fd9db941d9da281ac9381b5740e1f64f7092f3588d4f87f5ce55191a6653e5e80c1c5dd538169aa123e70dc6ffc5af1827e546c0e958e42dad355bcc1fcb9cdf2cf47ff524d2ad98cbf275e661bf4cf00960e74b5956b799771334f426df007350b46049adb21a6e78ab1408d5e6ccde6fb5e69f0f4c92bb9c725c02f99fa72b9cdc8dd53cff089e0e73317f61cc5abf6152513cb7d833f09d2851603919bf0fbe44d79a09245c6e8338eb502083dc84b846f2fee1cc310d2cc8b1b9334728f97220bb799376233e113",
        "ecdhe_shared_secret": "e3f407f83fc012470c26a93fdff534100f2c6f736439ce0ca90e9914f7d1c381",
        "initiator_nonce": "cd26fecb93657d1cd9e9eaf4f8be720b56dd1d39f190c4e1c6b7ec66f077bb11",
        "receiver_nonce": "f37ec61d84cea03dcc5e8385db93248584e8af4b4d1c832d8c7453c0089687a7",
        "aes_secret": "c0458fa97a5230830e05f4f20b7c755c1d4e54b1ce5cf43260bb191eef4e418d",
        "mac_secret": "48c938884d5067a1598272fcddaa4b833cd5e7d92e8228c0ecdfabbe68aef7f1",
        "token": "3f9ec2592d1554852b1f54d228f042ed0a9310ea86d038dc2b401ba8cd7fdac4",
        "initial_egress_MAC": "09771e93b1a6109e97074cbe2d2b0cf3d3878efafe68f53c41bb60c0ec49097e",
        "initial_ingress_MAC": "75823d96e23136c89666ee025fb21a432be906512b3dd4a3049e898adb433847",
        "initiator_hello_packet": "6ef23fcf1cec7312df623f9ae701e63b550cdb8517fefd8dd398fc2acd1d935e6e0434a2b96769078477637347b7b01924fff9ff1c06df2f804df3b0402bbb9f87365b3c6856b45e1e2b6470986813c3816a71bff9d69dd297a5dbd935ab578f6e5d7e93e4506a44f307c332d95e8a4b102585fd8ef9fc9e3e055537a5cec2e9",
        "receiver_hello_packet": "6ef23fcf1cec7312df623f9ae701e63be36a1cdd1b19179146019984f3625d4a6e0434a2b96769050577657247b7b02bc6c314470eca7e3ef650b98c83e9d7dd4830b3f718ff562349aead2530a8d28a8484604f92e5fced2c6183f304344ab0e7c301a0c05559f4c25db65e36820b4b909a226171a60ac6cb7beea09376d6d8"
    }

for k, v in test_values.items():
    test_values[k] = decode_hex(v)


keys = ['initiator_private_key',
        'receiver_private_key',
        'initiator_ephemeral_private_key',
        'receiver_ephemeral_private_key',
        'initiator_nonce',
        'receiver_nonce',
        # auth
        'auth_plaintext',
        'auth_ciphertext',
        # auth response
        'authresp_plaintext',
        'authresp_ciphertext',
        # on ack receive
コード例 #22
0
    packet1 = Packet(p1, cmd_id=0, payload=b'\x00' * 100)
    imux.add_packet(packet1)
    msg = imux.pop_all_frames_as_bytes()
    packets = rmux.decode(msg)
    assert len(packets) == 1
    assert packet1 == packets[0]


def test_many_sessions():
    for i in range(20):
        test_session()


eip8_values = dict(
    key_a=decode_hex(
        b'49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee'),
    key_b=decode_hex(
        b'b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291'),
    pub_a=decode_hex(
        b'fda1cff674c90c9a197539fe3dfb53086ace64f83ed7c6eabec741f7f381cc803e52ab2cd55d5569bce4347107a310dfd5f88a010cd2ffd1005ca406f1842877'
    ),
    pub_b=decode_hex(
        b'ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f'
    ),
    eph_key_a=decode_hex(
        b'869d6ecf5211f1cc60418a13b9d870b22959d0c16f02bec714c960dd2298a32d'),
    eph_key_b=decode_hex(
        b'e238eb8e04fee6511ab04c6dd3c89ce097b11f25d584863ac2b6d5b35b1847e4'),
    eph_pub_a=decode_hex(
        b'654d1044b69c577a44e5f01a1209523adb4026e70c62d1c13a067acabc09d2667a49821a0ad4b634554d330a15a58fe61f8a8e0544b310c6de7b0c8da7528a8d'
    ),
コード例 #23
0
def parse_as_bin(s):
    return decode_hex(s[2:] if s[:2] == '0x' else s)
コード例 #24
0
def scan_int(v):
    if v[:2] in ('0x', b'0x'):
        return big_endian_to_int(decode_hex(v[2:]))
    else:
        return int(v)
コード例 #25
0
def scan_bin(v):
    if v[:2] in ('0x', b'0x'):
        return decode_hex(v[2:])
    else:
        return decode_hex(v)
コード例 #26
0
 def __init__(self, app):
     log.info('ExampleService init')
     self.config = app.config
     self.address = privtopub_raw(decode_hex(self.config['node']['privkey_hex']))
     super(ExampleService, self).__init__(app)
コード例 #27
0
ファイル: p2p_network.py プロジェクト: wyh136/pyquarkchain
 def __init__(self, app):
     Logger.info("Devp2pService init")
     self.config = app.config
     self.address = privtopub_raw(
         decode_hex(self.config["node"]["privkey_hex"]))
     super(Devp2pService, self).__init__(app)
コード例 #28
0
def scan_bin(v):
    if v[:2] in ("0x", b"0x"):
        return decode_hex(v[2:])
    else:
        return decode_hex(v)
コード例 #29
0
def scan_int(v):
    if v[:2] in ("0x", b"0x"):
        return big_endian_to_int(decode_hex(v[2:]))
    else:
        return int(v)
コード例 #30
0
# Encoding to printable format
printers = {
    "bin": lambda v: '0x' + encode_hex(v),
    "addr": lambda v: v,
    "int": lambda v: to_string(v),
    "trie_root": lambda v: encode_hex(v),
    "int256b": lambda x: encode_hex(zpad(encode_int256(x), 256))
}

# Decoding from printable format
scanners = {
    "bin": scan_bin,
    "addr": lambda x: x[2:] if x[:2] in (b'0x', '0x') else x,
    "int": scan_int,
    "trie_root": lambda x: scan_bin,
    "int256b": lambda x: big_endian_to_int(decode_hex(x))
}


def int_to_hex(x):
    o = encode_hex(encode_int(x))
    return '0x' + (o[1:] if (len(o) > 0 and o[0] == b'0') else o)


def remove_0x_head(s):
    return s[2:] if s[:2] in (b'0x', '0x') else s


def parse_as_bin(s):
    return decode_hex(s[2:] if s[:2] == '0x' else s)