| 19 | } |
| 20 | |
| 21 | void run(const uint8_t *data, size_t size) |
| 22 | { |
| 23 | const uint8_t *wire_ptr; |
| 24 | size_t wire_max, min_size, script_size; |
| 25 | struct bitcoin_outpoint outpoint; |
| 26 | struct amount_sat funding, to_us, to_them, dust_limit, max; |
| 27 | const uint8_t *our_script, *their_script, *funding_script; |
| 28 | struct pubkey *pk1, *pk2; |
| 29 | |
| 30 | /* create_close_tx wants: |
| 31 | * - 3 scripts: 3 * N bytes |
| 32 | * - 1 txid: 32 bytes |
| 33 | * - 1 u32: 4 bytes |
| 34 | * - 4 amount_sat: 4 * 8 bytes |
| 35 | * |
| 36 | * Since both output scripts size are not restricted, we also try |
| 37 | * to vary their length. |
| 38 | * Therefore, we allocate the entire remaining bytes to scripts. |
| 39 | */ |
| 40 | min_size = 8 * 3 + 4 + 32; |
| 41 | if (size < min_size + 2) |
| 42 | return; |
| 43 | |
| 44 | script_size = (size - min_size) / 2; |
| 45 | wire_ptr = data; |
| 46 | |
| 47 | wire_max = 8; |
| 48 | to_us = fromwire_amount_sat(&wire_ptr, &wire_max); |
| 49 | assert(wire_ptr); |
| 50 | wire_max = 8; |
| 51 | to_them = fromwire_amount_sat(&wire_ptr, &wire_max); |
| 52 | assert(wire_ptr); |
| 53 | wire_max = 8; |
| 54 | dust_limit = fromwire_amount_sat(&wire_ptr, &wire_max); |
| 55 | /* The funding must be > to_us + to_them (TODO: we could simulate some fees) .. */ |
| 56 | if (!(amount_sat_add(&funding, to_us, to_them))) |
| 57 | return; |
| 58 | /* .. And < max_btc as we assert it's not nonsensical! */ |
| 59 | max = AMOUNT_SAT((u64)WALLY_SATOSHI_PER_BTC * WALLY_BTC_MAX); |
| 60 | if (amount_sat_greater(funding, max)) { |
| 61 | funding = max; |
| 62 | to_us = amount_sat_div(max, 2); |
| 63 | to_them = amount_sat_div(max, 2); |
| 64 | } |
| 65 | |
| 66 | wire_max = 36; |
| 67 | fromwire_bitcoin_outpoint(&wire_ptr, &wire_max, &outpoint); |
| 68 | |
| 69 | our_script = tal_dup_arr(tmpctx, const uint8_t, wire_ptr, script_size, 0); |
| 70 | their_script = tal_dup_arr(tmpctx, const uint8_t, wire_ptr + script_size, |
| 71 | script_size, 0); |
| 72 | |
| 73 | /* We assert it's valid, so we can't throw garbage at the funding script.. */ |
| 74 | pk1 = tal(tmpctx, struct pubkey); |
| 75 | pk2 = tal(tmpctx, struct pubkey); |
| 76 | assert(pubkey_from_hexstr("034fede2c619f647fe7c01d40ae22e4c285291ca2ffb47937bbfb7d6e8285a081f", |
| 77 | 2 * PUBKEY_CMPR_LEN, pk1)); |
| 78 | assert(pubkey_from_hexstr("028dfe31019dd61fa04c76ad065410e5d063ac2949c04c14b214c1b363e517452f", |
nothing calls this directly
no test coverage detected