2015-05-28 23:38:27 +02:00
|
|
|
#ifndef LIGHTNING_BITCOIN_SCRIPT_H
|
|
|
|
#define LIGHTNING_BITCOIN_SCRIPT_H
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "signature.h"
|
2016-04-24 12:24:35 +02:00
|
|
|
#include "tx.h"
|
2015-05-28 23:38:27 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
|
|
|
#include <ccan/tal/tal.h>
|
|
|
|
|
|
|
|
struct bitcoin_address;
|
2016-04-12 05:37:04 +02:00
|
|
|
struct bitcoin_tx_input;
|
2017-02-07 02:44:21 +01:00
|
|
|
struct preimage;
|
2015-06-02 06:03:21 +02:00
|
|
|
struct pubkey;
|
2015-06-02 06:08:38 +02:00
|
|
|
struct sha256;
|
2016-01-21 21:08:08 +01:00
|
|
|
struct rel_locktime;
|
|
|
|
struct abs_locktime;
|
2015-05-28 23:38:27 +02:00
|
|
|
|
|
|
|
/* tal_count() gives the length of the script. */
|
|
|
|
u8 *bitcoin_redeem_2of2(const tal_t *ctx,
|
2015-06-02 06:03:21 +02:00
|
|
|
const struct pubkey *key1,
|
|
|
|
const struct pubkey *key2);
|
2015-05-28 23:38:27 +02:00
|
|
|
|
|
|
|
/* tal_count() gives the length of the script. */
|
2016-07-01 03:49:28 +02:00
|
|
|
u8 *bitcoin_redeem_single(const tal_t *ctx,
|
|
|
|
const struct pubkey *key);
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2015-07-24 08:30:10 +02:00
|
|
|
/* A common script pattern: A can have it with secret, or B can have
|
|
|
|
* it after delay. */
|
|
|
|
u8 *bitcoin_redeem_secret_or_delay(const tal_t *ctx,
|
|
|
|
const struct pubkey *delayed_key,
|
2016-01-21 21:08:08 +01:00
|
|
|
const struct rel_locktime *locktime,
|
2015-07-24 08:30:10 +02:00
|
|
|
const struct pubkey *key_if_secret_known,
|
|
|
|
const struct sha256 *hash_of_secret);
|
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
/* Create an output script using p2sh for this redeem script. */
|
|
|
|
u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript);
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
/* Create an output script using p2pkh */
|
|
|
|
u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct pubkey *pubkey);
|
|
|
|
|
|
|
|
/* Create an input script which spends p2pkh */
|
|
|
|
u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey,
|
|
|
|
const secp256k1_ecdsa_signature *sig);
|
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
/* Create the redeemscript for a P2SH + P2WPKH. */
|
2016-07-01 03:49:28 +02:00
|
|
|
u8 *bitcoin_redeem_p2wpkh(const tal_t *ctx,
|
|
|
|
const struct pubkey *key);
|
2016-04-12 05:37:04 +02:00
|
|
|
|
|
|
|
/* Create a witness which spends the 2of2. */
|
|
|
|
void bitcoin_witness_p2sh_p2wpkh(const tal_t *ctx,
|
|
|
|
struct bitcoin_tx_input *input,
|
2017-01-25 00:34:23 +01:00
|
|
|
const secp256k1_ecdsa_signature *sig,
|
2016-04-12 05:37:04 +02:00
|
|
|
const struct pubkey *key);
|
|
|
|
|
2016-04-12 05:36:51 +02:00
|
|
|
/* Create scriptcode (fake witness, basically) for P2WPKH */
|
2016-12-02 08:42:58 +01:00
|
|
|
u8 *p2wpkh_scriptcode(const tal_t *ctx, const struct pubkey *key);
|
2016-04-12 05:36:51 +02:00
|
|
|
|
2016-05-03 03:38:20 +02:00
|
|
|
/* Create a script for our HTLC output: sending. */
|
|
|
|
u8 *bitcoin_redeem_htlc_send(const tal_t *ctx,
|
|
|
|
const struct pubkey *ourkey,
|
|
|
|
const struct pubkey *theirkey,
|
|
|
|
const struct abs_locktime *htlc_abstimeout,
|
|
|
|
const struct rel_locktime *locktime,
|
|
|
|
const struct sha256 *commit_revoke,
|
|
|
|
const struct sha256 *rhash);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
/* Create a script for our HTLC output: receiving. */
|
2016-05-03 03:38:20 +02:00
|
|
|
u8 *bitcoin_redeem_htlc_recv(const tal_t *ctx,
|
|
|
|
const struct pubkey *ourkey,
|
|
|
|
const struct pubkey *theirkey,
|
|
|
|
const struct abs_locktime *htlc_abstimeout,
|
|
|
|
const struct rel_locktime *locktime,
|
|
|
|
const struct sha256 *commit_revoke,
|
|
|
|
const struct sha256 *rhash);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-04-24 12:27:35 +02:00
|
|
|
/* Create an output script for a 32-byte witness program. */
|
2016-04-24 12:24:35 +02:00
|
|
|
u8 *scriptpubkey_p2wsh(const tal_t *ctx, const u8 *witnessscript);
|
|
|
|
|
2016-04-24 12:27:35 +02:00
|
|
|
/* Create an output script for a 20-byte witness program. */
|
2016-12-02 08:42:58 +01:00
|
|
|
u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key);
|
2016-04-24 12:27:35 +02:00
|
|
|
|
2016-04-24 12:24:35 +02:00
|
|
|
/* Create a witness which spends the 2of2. */
|
|
|
|
u8 **bitcoin_witness_2of2(const tal_t *ctx,
|
2017-01-25 00:34:23 +01:00
|
|
|
const secp256k1_ecdsa_signature *sig1,
|
|
|
|
const secp256k1_ecdsa_signature *sig2,
|
2016-04-24 12:24:35 +02:00
|
|
|
const struct pubkey *key1,
|
|
|
|
const struct pubkey *key2);
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* Create a witness which spends a p2wpkh. */
|
|
|
|
u8 **bitcoin_witness_p2wpkh(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *sig,
|
|
|
|
const struct pubkey *key);
|
|
|
|
|
2016-04-24 12:27:35 +02:00
|
|
|
/* Create a witness which spends a "secret_or_delay" scriptpubkey */
|
|
|
|
u8 **bitcoin_witness_secret(const tal_t *ctx,
|
|
|
|
const void *secret, size_t secret_len,
|
2017-01-25 00:34:23 +01:00
|
|
|
const secp256k1_ecdsa_signature *sig,
|
2016-04-24 12:27:35 +02:00
|
|
|
const u8 *witnessscript);
|
|
|
|
|
2016-05-03 03:54:56 +02:00
|
|
|
/* Create a witness which spends bitcoin_redeeem_htlc_recv/send */
|
|
|
|
u8 **bitcoin_witness_htlc(const tal_t *ctx,
|
2016-07-01 03:46:58 +02:00
|
|
|
const void *htlc_or_revocation_preimage,
|
2017-01-25 00:34:23 +01:00
|
|
|
const secp256k1_ecdsa_signature *sig,
|
2016-05-03 03:54:56 +02:00
|
|
|
const u8 *witnessscript);
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
/* BOLT #3 to-local output */
|
|
|
|
u8 *bitcoin_wscript_to_local(const tal_t *ctx,
|
|
|
|
u16 to_self_delay,
|
|
|
|
const struct pubkey *revocation_pubkey,
|
|
|
|
const struct pubkey *local_delayedkey);
|
|
|
|
u8 **bitcoin_to_local_spend_delayedkey(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *local_delayedsig,
|
|
|
|
const u8 *wscript);
|
|
|
|
u8 **bitcoin_to_local_spend_revocation(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *revocation_sig,
|
|
|
|
const u8 *wscript);
|
|
|
|
|
|
|
|
/* BOLT #3 offered/accepted HTLC outputs */
|
|
|
|
u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
|
|
|
|
const struct pubkey *localkey,
|
|
|
|
const struct pubkey *remotekey,
|
|
|
|
const struct sha256 *payment_hash);
|
|
|
|
u8 **bitcoin_htlc_offer_spend_timeout(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *localsig,
|
|
|
|
const secp256k1_ecdsa_signature *remotesig,
|
|
|
|
const u8 *wscript);
|
|
|
|
u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx,
|
|
|
|
const struct abs_locktime *htlc_abstimeout,
|
|
|
|
const struct pubkey *localkey,
|
|
|
|
const struct pubkey *remotekey,
|
|
|
|
const struct sha256 *payment_hash);
|
|
|
|
u8 **bitcoin_htlc_receive_spend_preimage(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *localsig,
|
|
|
|
const secp256k1_ecdsa_signature *remotesig,
|
|
|
|
const struct preimage *preimage,
|
|
|
|
const u8 *wscript);
|
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
/* BOLT #3 HTLC-success/HTLC-timeout output */
|
|
|
|
u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx,
|
|
|
|
u16 to_self_delay,
|
|
|
|
const struct pubkey *revocation_pubkey,
|
|
|
|
const struct pubkey *local_delayedkey);
|
|
|
|
|
|
|
|
/* Is this a pay to pubkey hash? */
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2pkh(const u8 *script);
|
2016-08-31 08:36:32 +02:00
|
|
|
|
2015-06-02 06:10:00 +02:00
|
|
|
/* Is this a pay to script hash? */
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2sh(const u8 *script);
|
2015-06-02 06:10:00 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* Is this (version 0) pay to witness script hash? */
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2wsh(const u8 *script);
|
2016-08-18 06:53:45 +02:00
|
|
|
|
2016-08-31 08:36:32 +02:00
|
|
|
/* Is this (version 0) pay to witness pubkey hash? */
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2wpkh(const u8 *script);
|
2016-08-31 08:36:32 +02:00
|
|
|
|
2016-05-02 08:29:56 +02:00
|
|
|
/* Are these two scripts equal? */
|
2017-01-25 00:35:43 +01:00
|
|
|
bool scripteq(const tal_t *s1, const tal_t *s2);
|
2016-05-02 08:29:56 +02:00
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
#endif /* LIGHTNING_BITCOIN_SCRIPT_H */
|