コード例 #1
0
def test_enc_wrapper(alice_bob_boxes, ab_message, ba_message, num_iterations):
    alice_box, bob_box = alice_bob_boxes

    for i in range(num_iterations):
        ab_message = ''.join(
            random.choice(string.ascii_letters)
            for x in range(100)) if ab_message == 'rand' else ab_message
        ba_message = ''.join(
            random.choice(string.ascii_letters)
            for x in range(100)) if ba_message == 'rand' else ba_message
        otw_amsg = alice_box.encrypt(ab_message)
        bob_ptext = bob_box.decrypt(otw_amsg)

        assert bob_ptext == ab_message, "Encryption test: FAILED. Alice sent: %s, Bob received: " % (
            ab_message, bob_ptext)

        otw_bmsg = bob_box.encrypt(ba_message)
        alice_ptext = alice_box.decrypt(otw_bmsg)
        assert alice_ptext == ba_message, "Encryption test: FAILED. Bob sent: %s, Alice received: " % (
            ba_message, alice_ptext)
        assert decode_decrypt(encrypt_encode(ab_message, bob_box), bob_box) == ab_message
コード例 #2
0
    def on_verified_privmsg(self, nick, message):
        #Marks the nick as active on this channel; note *only* if verified.
        #Otherwise squatter/attacker can persuade us to send privmsgs to him.
        if self.on_privmsg_trigger:
            self.on_privmsg_trigger(nick, self)
        #strip sig from message for processing, having verified
        message = " ".join(message[1:].split(" ")[:-2])
        for command in message.split(COMMAND_PREFIX):
            _chunks = command.split(" ")

            #Decrypt if necessary
            if _chunks[0] in encrypted_commands:
                box, encrypt = self.daemon.mcc.get_encryption_box(
                    _chunks[0], nick)
                if encrypt:
                    if not box:
                        log.debug(
                            'error, dont have encryption box object for ' +
                            nick + ', dropping message')
                        return
                    # need to decrypt everything after the command string
                    to_decrypt = ''.join(_chunks[1:])
                    try:
                        decrypted = decode_decrypt(to_decrypt,
                                                   box).decode('ascii')
                    except Exception as e:
                        log.debug('Error when decrypting, skipping: ' +
                                  repr(e))
                        return
                    #rebuild the chunks array as if it had been plaintext
                    _chunks = [_chunks[0]] + decrypted.split(" ")

            # looks like a very similar pattern for all of these
            # check for a command name, parse arguments, call a function
            # maybe we need some eval() trickery to do it better

            try:
                # orderbook watch commands
                if self.check_for_orders(nick, _chunks):
                    pass
                elif self.check_for_fidelity_bond(nick, _chunks):
                    pass
                # taker commands
                elif _chunks[0] == 'error':
                    error = " ".join(_chunks[1:])
                    if self.on_error:
                        self.on_error(error)
                elif _chunks[0] == 'pubkey':
                    maker_pk = _chunks[1]
                    if self.on_pubkey:
                        self.on_pubkey(nick, maker_pk)
                elif _chunks[0] == 'ioauth':
                    utxo_list = _chunks[1].split(',')
                    auth_pub = _chunks[2]
                    cj_addr = _chunks[3]
                    change_addr = _chunks[4]
                    btc_sig = _chunks[5]
                    if self.on_ioauth:
                        self.on_ioauth(nick, utxo_list, auth_pub, cj_addr,
                                       change_addr, btc_sig)
                elif _chunks[0] == 'sig':
                    sig = _chunks[1]
                    if self.on_sig:
                        self.on_sig(nick, sig)

                # maker commands
                if self.check_for_commitments(nick, _chunks, private=True):
                    pass
                if _chunks[0] == 'fill':
                    try:
                        oid = int(_chunks[1])
                        amount = int(_chunks[2])
                        taker_pk = _chunks[3]
                        if len(_chunks) > 4:
                            commit = _chunks[4]
                        else:
                            commit = None
                    except (ValueError, IndexError) as e:
                        self.send_error(nick, str(e))
                        return
                    if self.on_order_fill:
                        self.on_order_fill(nick, oid, amount, taker_pk, commit)
                elif _chunks[0] == 'auth':
                    #Note index error logically impossible, would have thrown
                    #in sig check (zero message after cmd not allowed)
                    cr = _chunks[1]
                    if self.on_seen_auth:
                        self.on_seen_auth(nick, cr)
                elif _chunks[0] == 'tx':
                    b64tx = _chunks[1]
                    try:
                        txhex = binascii.hexlify(
                            base64.b64decode(b64tx)).decode('ascii')
                    except TypeError as e:
                        self.send_error(nick, 'bad base64 tx. ' + repr(e))
                        return
                    if self.on_seen_tx:
                        self.on_seen_tx(nick, txhex)
                elif _chunks[0] == 'push':
                    b64tx = _chunks[1]
                    try:
                        txhex = binascii.hexlify(
                            base64.b64decode(b64tx)).decode('ascii')
                    except TypeError as e:
                        self.send_error(nick, 'bad base64 tx. ' + repr(e))
                        return
                    if self.on_push_tx:
                        self.on_push_tx(nick, txhex)
            except (IndexError, ValueError):
                # TODO proper error handling
                log.debug('cj peer error TODO handle')
                continue