From 0646ffa168dae7a00fccea31b806ff4f4efb6fd3 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 15 Dec 2020 19:40:24 -0600 Subject: [PATCH] df-plugin: add `signonly` to signing in df_accepter We're the accepter, so we look up which inputs we need to sign and only sign those. --- tests/plugins/df_accepter.py | 42 ++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/tests/plugins/df_accepter.py b/tests/plugins/df_accepter.py index 6368edeced93..c20d36ae47ae 100755 --- a/tests/plugins/df_accepter.py +++ b/tests/plugins/df_accepter.py @@ -5,8 +5,15 @@ from pyln.client import Plugin, Millisatoshi from pyln.proto import bech32_decode from typing import Iterable, List, Optional -from wallycore import psbt_add_output_at, psbt_from_base64, psbt_to_base64, tx_output_init - +from wallycore import ( + psbt_add_output_at, + psbt_find_input_unknown, + psbt_from_base64, + psbt_get_input_unknown, + psbt_get_num_inputs, + psbt_to_base64, + tx_output_init, +) plugin = Plugin() @@ -58,6 +65,26 @@ def find_feerate(best, their_min, their_max, our_min, our_max): return our_max +def find_inputs(b64_psbt): + serial_id_key = bytes.fromhex('fc096c696768746e696e6701') + psbt = psbt_from_base64(b64_psbt) + input_idxs = [] + + for i in range(psbt_get_num_inputs(psbt)): + idx = psbt_find_input_unknown(psbt, i, serial_id_key) + if idx == 0: + continue + # returned index is off by one, so 0 can be 'not found' + serial_bytes = psbt_get_input_unknown(psbt, i, idx - 1) + serial_id = int.from_bytes(serial_bytes, byteorder='big', signed=False) + + # We're the accepter, so our inputs have odd serials + if serial_id % 2: + input_idxs.append(i) + + return input_idxs + + @plugin.hook('openchannel2') def on_openchannel(openchannel2, plugin, **kwargs): # We mirror what the peer does, wrt to funding amount ... @@ -109,12 +136,13 @@ def on_tx_sign(openchannel2_sign, plugin, **kwargs): psbt = openchannel2_sign['psbt'] # We only sign the ones with our parity of a serial_id - # FIXME: find the inputs with an odd-serial, these are ours - # the key for a serial_id :: - # our_inputs = [1] + input_idxs = find_inputs(psbt) + if len(input_idxs) > 0: + final_psbt = plugin.rpc.signpsbt(psbt, signonly=input_idxs)['signed_psbt'] + else: + final_psbt = psbt - signed_psbt = plugin.rpc.signpsbt(psbt)['signed_psbt'] - return {'result': 'continue', 'psbt': signed_psbt} + return {'result': 'continue', 'psbt': final_psbt} plugin.run()