def on_htlc_accepted(onion, htlc, plugin, **kwargs): payload = OnionPayload.from_hex(onion['payload']) if not isinstance(payload, TlvPayload): plugin.log("Payload is not a TLV payload") return {'result': 'continue'} body_field = payload.get(34349334) signature_field = payload.get(34349335) if body_field is None or signature_field is None: plugin.log("Missing message body or signature, ignoring HTLC") return {'result': 'continue'} msg = Message(id=len(plugin.messages), sender=None, body=body_field.value, signature=signature_field.value, payment=None) # Filter out the signature so we can check it against the rest of the payload sigpayload = TlvPayload() sigpayload.fields = filter(lambda x: x.typenum != TLV_NOISE_SIGNATURE, payload.fields) sigmsg = hexlify(sigpayload.to_bytes()).decode('ASCII') zsig = zbase32.encode(msg.signature).decode('ASCII') sigcheck = plugin.rpc.checkmessage(sigmsg, zsig) msg.sender = sigcheck['pubkey'] msg.verified = sigcheck['verified'] preimage = payload.get(TLV_KEYSEND_PREIMAGE) if preimage is not None: msg.payment = Payment(preimage.value, htlc['amount']) res = { 'result': 'resolve', 'payment_key': hexlify(preimage.value).decode('ASCII') } else: res = {'result': 'continue'} plugin.messages.append(msg) print("Delivering message to {c} waiters".format( c=len(plugin.receive_waiters))) for r in plugin.receive_waiters: m = msg.to_dict() m['total_messages'] = len(plugin.messages) r.set_result(m) plugin.receive_waiters = [] return res
def test_zbase32(): zb32 = b'd75qtmgijm79rpooshmgzjwji9gj7dsdat8remuskyjp9oq1ugkaoj6orbxzhuo4njtyh96e3aq84p1tiuz77nchgxa1s4ka4carnbiy' b = zbase32.decode(zb32) enc = zbase32.encode(b) assert (enc == zb32)