1 #ifndef LIGHTNING_COMMON_PSBT_OPEN_H 2 #define LIGHTNING_COMMON_PSBT_OPEN_H 3 #include "config.h" 4 #include <ccan/short_types/short_types.h> 5 #include <ccan/tal/tal.h> 6 #include <common/tx_roles.h> 7 #include <wally_psbt.h> 8 9 struct channel_id; 10 struct wally_tx_input; 11 struct wally_tx_output; 12 struct wally_psbt; 13 struct wally_psbt_input; 14 struct wally_psbt_output; 15 struct wally_map; 16 17 struct input_set { 18 struct wally_tx_input tx_input; 19 struct wally_psbt_input input; 20 /* index on PSBT of this input */ 21 size_t idx; 22 }; 23 24 struct output_set { 25 struct wally_tx_output tx_output; 26 struct wally_psbt_output output; 27 /* index on PSBT of this output */ 28 size_t idx; 29 }; 30 31 struct psbt_changeset { 32 struct input_set *added_ins; 33 struct input_set *rm_ins; 34 struct output_set *added_outs; 35 struct output_set *rm_outs; 36 }; 37 38 #define PSBT_TYPE_SERIAL_ID 0x01 39 #define PSBT_TYPE_INPUT_MARKER 0x02 40 41 /* psbt_get_serial_id - Returns the serial_id from an unknowns map 42 * 43 * @map - the map to find the serial id entry within 44 * @serial_id - found serial_id 45 * 46 * Returns false if serial_id is not present 47 */ 48 WARN_UNUSED_RESULT bool psbt_get_serial_id(const struct wally_map *map, 49 u64 *serial_id); 50 51 /* psbt_sort_by_serial_id - Sort PSBT by serial_ids 52 * 53 * MUST have a serial_id on every input/output. 54 * 55 * @psbt - psbt to sort 56 */ 57 void psbt_sort_by_serial_id(struct wally_psbt *psbt); 58 59 /* psbt_get_changeset - Returns set of diffs btw orig + new psbt 60 * 61 * All inputs+outputs MUST have a serial_id field present before 62 * calling this. 63 * 64 * @ctx - allocation context for returned changeset 65 * @orig - original psbt 66 * @new - updated psbt 67 * 68 * Note that the input + output data returned in the changeset 69 * contains references to the originating PSBT; they are not copies. 70 */ 71 struct psbt_changeset *psbt_get_changeset(const tal_t *ctx, 72 struct wally_psbt *orig, 73 struct wally_psbt *new); 74 75 /* psbt_input_set_serial_id - Sets a serial id on given input 76 * 77 * @ctx - tal context for allocations 78 * @input - to set serial_id on 79 * @serial_id - to set 80 */ 81 void psbt_input_set_serial_id(const tal_t *ctx, 82 struct wally_psbt_input *input, 83 u64 serial_id); 84 /* psbt_output_set_serial_id - Sets a serial id on given output 85 * 86 * @ctx - tal context for allocations 87 * @output - to set serial_id on 88 * @serial_id - to set 89 */ 90 void psbt_output_set_serial_id(const tal_t *ctx, 91 struct wally_psbt_output *output, 92 u64 serial_id); 93 94 /* psbt_sort_by_serial_id - Sorts the inputs + outputs by serial_id 95 * 96 * Requires every input/output to have a serial_id entry. 97 * 98 * @psbt - psbt to sort inputs/outputs 99 */ 100 void psbt_sort_by_serial_id(struct wally_psbt *psbt); 101 102 /* psbt_find_serial_input - Checks inputs for provided serial_id 103 * 104 * @psbt - psbt's inputs to check 105 * @serial_id - id to look for 106 * 107 * Returns index of input with matching serial if found or -1 108 */ 109 int psbt_find_serial_input(struct wally_psbt *psbt, u64 serial_id); 110 111 /* psbt_find_serial_output - Checks outputs for provided serial_id 112 * 113 * @psbt - psbt's outputs to check 114 * @serial_id - id to look for 115 * 116 * Returns index of output with matching serial if found or -1 117 */ 118 int psbt_find_serial_output(struct wally_psbt *psbt, u64 serial_id); 119 120 /* psbt_new_input_serial - Generate a new serial for an input for {role} 121 * 122 * @psbt - psbt to get a new serial for 123 * @role - which tx role to generate the serial for 124 * 125 * Returns a new, unique serial of the correct parity for the specified {role} 126 */ 127 u64 psbt_new_input_serial(struct wally_psbt *psbt, enum tx_role role); 128 129 /* psbt_new_output_serial - Generate a new serial for an output for {role} 130 * 131 * @psbt - psbt to get a new serial for 132 * @role - which tx role to generate the serial for 133 * 134 * Returns a new, unique serial of the correct parity for the specified {role} 135 */ 136 u64 psbt_new_output_serial(struct wally_psbt *psbt, enum tx_role role); 137 138 /* psbt_has_required_fields - Validates psbt field completion 139 * 140 * Required fields are: 141 * - a serial_id; input+output 142 * - a prev_tx; input,non_witness_utxo 143 * - redeemscript; input,iff is P2SH-P2W* 144 * @psbt - psbt to validate 145 * 146 * Returns true if all required fields are present 147 */ 148 bool psbt_has_required_fields(struct wally_psbt *psbt); 149 150 /* psbt_side_finalized - True if designated role has all signature data */ 151 bool psbt_side_finalized(const struct wally_psbt *psbt, 152 enum tx_role role); 153 154 /* psbt_add_serials - Add serials to inputs/outputs that are missing them 155 * 156 * Adds a serial of the correct parity for the designated {role} to all 157 * inputs and outputs of this PSBT that do not currently have a serial_id 158 * set. 159 * 160 * @psbt - the psbt to add serials to 161 * @role - the role we should use to select serial parity 162 */ 163 void psbt_add_serials(struct wally_psbt *psbt, enum tx_role role); 164 165 /* psbt_input_mark_ours - Sets the PSBT_TYPE_INPUT_MARKER on this input 166 */ 167 void psbt_input_mark_ours(const tal_t *ctx, 168 struct wally_psbt_input *input); 169 170 /* psbt_input_is_ours - Returns true if this psbt input has 171 * the PSBT_TYPE_INPUT_MARKER set on it. 172 */ 173 bool psbt_input_is_ours(const struct wally_psbt_input *input); 174 175 /* psbt_has_our_input - Returns true if this psbt contains 176 * any input that is ours 177 */ 178 bool psbt_has_our_input(const struct wally_psbt *psbt); 179 180 /* psbt_contribs_changed - Returns true if the psbt's inputs/outputs 181 * have changed. 182 * 183 * @orig - originating psbt 184 * @new - 'updated' psbt, to verify is unchanged 185 */ 186 bool psbt_contribs_changed(struct wally_psbt *orig, 187 struct wally_psbt *new); 188 #endif /* LIGHTNING_COMMON_PSBT_OPEN_H */ 189