| 522 | } |
| 523 | |
| 524 | void psbt_elements_normalize_fees(struct wally_psbt *psbt) |
| 525 | { |
| 526 | size_t fee_output_idx = psbt->num_outputs; |
| 527 | |
| 528 | if (!is_elements(chainparams)) |
| 529 | return; |
| 530 | |
| 531 | /* Elements requires that every input value is accounted for, |
| 532 | * including the fees */ |
| 533 | struct amount_sat total_fee = AMOUNT_SAT(0), val; |
| 534 | for (size_t i = 0; i < psbt->num_inputs; i++) { |
| 535 | val = psbt_input_get_amount(psbt, i); |
| 536 | if (!amount_sat_add(&total_fee, total_fee, val)) |
| 537 | return; |
| 538 | } |
| 539 | for (size_t i = 0; i < psbt->num_outputs; i++) { |
| 540 | struct amount_asset output_amount = wally_psbt_output_get_amount(&psbt->outputs[i]); |
| 541 | if (elements_psbt_output_is_fee(psbt, i)) { |
| 542 | if (fee_output_idx == psbt->num_outputs) { |
| 543 | fee_output_idx = i; |
| 544 | continue; |
| 545 | } |
| 546 | /* We already have at least one fee output, |
| 547 | * remove this one */ |
| 548 | psbt_rm_output(psbt, i--); |
| 549 | continue; |
| 550 | } |
| 551 | if (!amount_asset_is_main(&output_amount)) |
| 552 | continue; |
| 553 | |
| 554 | if (!amount_sat_sub(&total_fee, total_fee, |
| 555 | amount_asset_to_sat(&output_amount))) |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | if (amount_sat_eq(total_fee, AMOUNT_SAT(0))) |
| 560 | return; |
| 561 | |
| 562 | /* We need to add a fee output */ |
| 563 | if (fee_output_idx == psbt->num_outputs) { |
| 564 | psbt_append_output(psbt, NULL, total_fee); |
| 565 | } else { |
| 566 | int ret; |
| 567 | u64 sats = total_fee.satoshis; /* Raw: wally API */ |
| 568 | struct wally_psbt_output *out = &psbt->outputs[fee_output_idx]; |
| 569 | ret = wally_psbt_output_set_amount(out, sats); |
| 570 | assert(ret == WALLY_OK); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | void wally_psbt_input_get_txid(const struct wally_psbt_input *in, |
| 575 | struct bitcoin_txid *txid) |
no test coverage detected