diff --git a/bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c b/bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c new file mode 100644 index 000000000..af5483956 --- /dev/null +++ b/bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c @@ -0,0 +1,157 @@ +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* AUTOGENERATED MOCKS START */ +/* Generated stub for amount_asset_is_main */ +bool amount_asset_is_main(struct amount_asset *asset UNNEEDED) +{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); } +/* Generated stub for amount_asset_to_sat */ +struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) +{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); } +/* Generated stub for amount_sat_add */ + bool amount_sat_add(struct amount_sat *val UNNEEDED, + struct amount_sat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_add called!\n"); abort(); } +/* Generated stub for amount_sat_eq */ +bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); } +/* Generated stub for amount_sat_greater_eq */ +bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); } +/* Generated stub for amount_sat_sub */ + bool amount_sat_sub(struct amount_sat *val UNNEEDED, + struct amount_sat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); } +/* Generated stub for amount_sat_to_asset */ +struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED) +{ fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); } +/* Generated stub for amount_tx_fee */ +struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) +{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); } +/* Generated stub for fromwire */ +const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) +{ fprintf(stderr, "fromwire called!\n"); abort(); } +/* Generated stub for fromwire_fail */ +void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_fail called!\n"); abort(); } +/* Generated stub for fromwire_secp256k1_ecdsa_signature */ +void fromwire_secp256k1_ecdsa_signature(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + secp256k1_ecdsa_signature *signature UNNEEDED) +{ fprintf(stderr, "fromwire_secp256k1_ecdsa_signature called!\n"); abort(); } +/* Generated stub for fromwire_sha256 */ +void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED) +{ fprintf(stderr, "fromwire_sha256 called!\n"); abort(); } +/* Generated stub for fromwire_u32 */ +u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); } +/* Generated stub for fromwire_u8 */ +u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); } +/* Generated stub for fromwire_u8_array */ +void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED) +{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); } +/* Generated stub for fromwire_wally_psbt */ +struct wally_psbt *fromwire_wally_psbt(const tal_t *ctx UNNEEDED, + const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_wally_psbt called!\n"); abort(); } +/* Generated stub for new_psbt */ +struct wally_psbt *new_psbt(const tal_t *ctx UNNEEDED, + const struct wally_tx *wtx UNNEEDED) +{ fprintf(stderr, "new_psbt called!\n"); abort(); } +/* Generated stub for psbt_add_output */ +struct wally_psbt_output *psbt_add_output(struct wally_psbt *psbt UNNEEDED, + struct wally_tx_output *output UNNEEDED, + size_t insert_at UNNEEDED) +{ fprintf(stderr, "psbt_add_output called!\n"); abort(); } +/* Generated stub for psbt_append_input */ +struct wally_psbt_input *psbt_append_input(struct wally_psbt *psbt UNNEEDED, + const struct bitcoin_outpoint *outpoint UNNEEDED, + u32 sequence UNNEEDED, + const u8 *scriptSig UNNEEDED, + const u8 *input_wscript UNNEEDED, + const u8 *redeemscript UNNEEDED) +{ fprintf(stderr, "psbt_append_input called!\n"); abort(); } +/* Generated stub for psbt_elements_input_set_asset */ +void psbt_elements_input_set_asset(struct wally_psbt *psbt UNNEEDED, size_t in UNNEEDED, + struct amount_asset *asset UNNEEDED) +{ fprintf(stderr, "psbt_elements_input_set_asset called!\n"); abort(); } +/* Generated stub for psbt_final_tx */ +struct wally_tx *psbt_final_tx(const tal_t *ctx UNNEEDED, const struct wally_psbt *psbt UNNEEDED) +{ fprintf(stderr, "psbt_final_tx called!\n"); abort(); } +/* Generated stub for psbt_finalize */ +bool psbt_finalize(struct wally_psbt *psbt UNNEEDED) +{ fprintf(stderr, "psbt_finalize called!\n"); abort(); } +/* Generated stub for psbt_input_get_amount */ +struct amount_sat psbt_input_get_amount(const struct wally_psbt *psbt UNNEEDED, + size_t in UNNEEDED) +{ fprintf(stderr, "psbt_input_get_amount called!\n"); abort(); } +/* Generated stub for psbt_input_set_wit_utxo */ +void psbt_input_set_wit_utxo(struct wally_psbt *psbt UNNEEDED, size_t in UNNEEDED, + const u8 *scriptPubkey UNNEEDED, struct amount_sat amt UNNEEDED) +{ fprintf(stderr, "psbt_input_set_wit_utxo called!\n"); abort(); } +/* Generated stub for towire */ +void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) +{ fprintf(stderr, "towire called!\n"); abort(); } +/* Generated stub for towire_secp256k1_ecdsa_signature */ +void towire_secp256k1_ecdsa_signature(u8 **pptr UNNEEDED, + const secp256k1_ecdsa_signature *signature UNNEEDED) +{ fprintf(stderr, "towire_secp256k1_ecdsa_signature called!\n"); abort(); } +/* Generated stub for towire_sha256 */ +void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED) +{ fprintf(stderr, "towire_sha256 called!\n"); abort(); } +/* Generated stub for towire_u32 */ +void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) +{ fprintf(stderr, "towire_u32 called!\n"); abort(); } +/* Generated stub for towire_u8 */ +void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) +{ fprintf(stderr, "towire_u8 called!\n"); abort(); } +/* Generated stub for towire_u8_array */ +void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED) +{ fprintf(stderr, "towire_u8_array called!\n"); abort(); } +/* Generated stub for towire_wally_psbt */ +void towire_wally_psbt(u8 **pptr UNNEEDED, const struct wally_psbt *psbt UNNEEDED) +{ fprintf(stderr, "towire_wally_psbt called!\n"); abort(); } +/* AUTOGENERATED MOCKS END */ + +int main(int argc, const char *argv[]) +{ + struct privkey p; + struct pubkey pub; + struct bitcoin_signature sig; + struct sha256_double h; + u8 **wit; + size_t weight; + + common_setup(argv[0]); + chainparams = chainparams_for_network("bitcoin"); + + memset(&h, 0, sizeof(h)); + memset(&p, 1, sizeof(p)); + sign_hash(&p, &h, &sig.s); + sig.sighash_type = SIGHASH_ALL; + pubkey_from_privkey(&p, &pub); + + wit = bitcoin_witness_2of2(tmpctx, &sig, &sig, &pub, &pub); + + /* 1 byte for num witnesses, one per witness element */ + weight = 1; + for (size_t i = 0; i < tal_count(wit); i++) + weight += 1 + tal_bytelen(wit[i]); + assert(bitcoin_tx_2of2_input_witness_weight() == weight); + + common_shutdown(); + return 0; +} diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 76d47b173..215d2e25f 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -884,6 +884,16 @@ size_t bitcoin_tx_simple_input_weight(bool p2sh) bitcoin_tx_simple_input_witness_weight()); } +size_t bitcoin_tx_2of2_input_witness_weight(void) +{ + /* witness[0] = "" + * witness[1] = sig + * witness[2] = sig + * witness[3] = 2 key key 2 CHECKMULTISIG + */ + return 1 + (1 + 0) + (1 + 72) + (1 + 72) + (1 + 1 + 33 + 33 + 1 + 1); +} + struct amount_sat change_amount(struct amount_sat excess, u32 feerate_perkw, size_t total_weight) { diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 5bccaf7b6..694cc4a75 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -271,6 +271,9 @@ size_t bitcoin_tx_simple_input_witness_weight(void); /* We only do segwit inputs, and we assume witness is sig + key */ size_t bitcoin_tx_simple_input_weight(bool p2sh); +/* The witness for our 2of2 input (closing or commitment tx). */ +size_t bitcoin_tx_2of2_input_witness_weight(void); + /** * change_amount - Is it worth making a P2WPKH change output at this feerate? * @excess: input amount we have above the tx fee and other outputs.