Skip to content

Commit

Permalink
bitcoin/script.c: Add scriptpubkey_opreturn_padded, which creates an …
Browse files Browse the repository at this point in the history
…OP_RETURN with a pointless random 20-byte padding.

In the case of `donateutxo`, this is needed since a simple spend of a P2WPKH to an `OP_RETURN` would be below the minimum transaction size.
Sizes below 20 are not plausible as commitments.
  • Loading branch information
ZmnSCPxj authored and rustyrussell committed Sep 8, 2020
1 parent 6a9c43e commit 61cc22e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bitcoin/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ccan/endian/endian.h>
#include <ccan/mem/mem.h>
#include <common/utils.h>
#include <sodium/randombytes.h>

/* Some standard ops */
#define OP_0 0x00
Expand Down Expand Up @@ -221,6 +222,16 @@ u8 *scriptpubkey_opreturn(const tal_t *ctx)
add_op(&script, OP_RETURN);
return script;
}
u8 *scriptpubkey_opreturn_padded(const tal_t *ctx)
{
u8 *script = tal_arr(ctx, u8, 0);
u8 random[20];
randombytes_buf(random, sizeof(random));

add_op(&script, OP_RETURN);
script_push_bytes(&script, random, sizeof(random));
return script;
}

/* Create an input script which spends p2pkh */
u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey,
Expand Down
5 changes: 5 additions & 0 deletions bitcoin/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr);

/* Create a prunable output script */
u8 *scriptpubkey_opreturn(const tal_t *ctx);
/* Create a prunable output script with 20 random bytes.
* This is needed since a spend from a p2wpkh to an `OP_RETURN` without
* any other outputs would result in a transaction smaller than the
* minimum size. */
u8 *scriptpubkey_opreturn_padded(const tal_t *ctx);

/* Create an input script which spends p2pkh */
u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey,
Expand Down

0 comments on commit 61cc22e

Please sign in to comment.