diff --git a/bitcoin/script.c b/bitcoin/script.c index f540f3440..8295cfe0c 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -22,6 +22,7 @@ #define OP_NOTIF 0x64 #define OP_ELSE 0x67 #define OP_ENDIF 0x68 +#define OP_RETURN 0x6a #define OP_2DROP 0x6d #define OP_DEPTH 0x74 #define OP_DROP 0x75 @@ -214,6 +215,14 @@ u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr) return script; } +u8 *scriptpubkey_opreturn(const tal_t *ctx) +{ + u8 *script = tal_arr(ctx, u8, 0); + + add_op(&script, OP_RETURN); + return script; +} + /* Create an input script which spends p2pkh */ u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey, const secp256k1_ecdsa_signature *sig) diff --git a/bitcoin/script.h b/bitcoin/script.h index fd0e89a5e..53a4425e7 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -29,6 +29,9 @@ u8 *scriptpubkey_p2sh_hash(const tal_t *ctx, const struct ripemd160 *redeemhash) /* Create an output script using p2pkh */ 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 an input script which spends p2pkh */ u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey, const secp256k1_ecdsa_signature *sig); diff --git a/onchaind/onchain.c b/onchaind/onchain.c index 74db36ae1..c3f6d6ca0 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -243,10 +243,11 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, + 1 + 3 + 73 + 0 + tal_len(wscript)) / 1000; - /* Result is trivial? Just eliminate output. */ - if (tx->output[0].amount < dust_limit_satoshis + fee) - tal_resize(&tx->output, 0); - else + /* Result is trivial? Spent to OP_RETURN to avoid leaving dust. */ + if (tx->output[0].amount < dust_limit_satoshis + fee) { + tx->output[0].amount = 0; + tx->output[0].script = scriptpubkey_opreturn(tx->output); + } else tx->output[0].amount -= fee; sign_tx_input(tx, 0, NULL, wscript, privkey, pubkey, &sig);