2015-06-12 04:23:27 +02:00
|
|
|
#ifndef LIGHTNING_BITCOIN_SIGNATURE_H
|
|
|
|
#define LIGHTNING_BITCOIN_SIGNATURE_H
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "config.h"
|
2015-05-28 23:38:27 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2020-12-05 03:23:54 +01:00
|
|
|
#include <ccan/tal/tal.h>
|
2016-07-01 03:49:28 +02:00
|
|
|
#include <secp256k1.h>
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2017-01-25 00:34:23 +01:00
|
|
|
struct sha256_double;
|
2020-12-05 03:23:54 +01:00
|
|
|
struct sha256_ctx;
|
2017-01-25 00:34:23 +01:00
|
|
|
struct bitcoin_tx;
|
|
|
|
struct pubkey;
|
|
|
|
struct privkey;
|
|
|
|
struct bitcoin_tx_output;
|
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
enum sighash_type {
|
|
|
|
SIGHASH_ALL = 1,
|
|
|
|
SIGHASH_NONE = 2,
|
|
|
|
SIGHASH_SINGLE = 3,
|
|
|
|
SIGHASH_ANYONECANPAY = 0x80
|
|
|
|
};
|
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
#define SIGHASH_MASK 0x7F
|
|
|
|
|
|
|
|
static inline bool sighash_single(enum sighash_type sighash_type)
|
|
|
|
{
|
|
|
|
return (sighash_type & SIGHASH_MASK) == SIGHASH_SINGLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool sighash_anyonecanpay(enum sighash_type sighash_type)
|
|
|
|
{
|
|
|
|
return (sighash_type & SIGHASH_ANYONECANPAY) == SIGHASH_ANYONECANPAY;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
/* We only support a limited range of sighash_type */
|
|
|
|
static inline bool sighash_type_valid(const enum sighash_type sighash_type)
|
|
|
|
{
|
2018-12-03 00:15:06 +01:00
|
|
|
return sighash_type == SIGHASH_ALL
|
|
|
|
|| sighash_type == (SIGHASH_SINGLE|SIGHASH_ANYONECANPAY);
|
2018-12-03 00:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitcoin_signature - signature with a sighash type.
|
|
|
|
*
|
|
|
|
* sighash_type is SIGHASH_ALL unless you're being tricky. */
|
|
|
|
struct bitcoin_signature {
|
|
|
|
secp256k1_ecdsa_signature s;
|
|
|
|
enum sighash_type sighash_type;
|
|
|
|
};
|
|
|
|
|
2019-11-20 00:05:16 +01:00
|
|
|
/**
|
|
|
|
* bitcoin_tx_hash_for_sig - produce hash for a transaction
|
|
|
|
*
|
|
|
|
* @tx - tx to hash
|
|
|
|
* @in - index that this 'hash' is for
|
|
|
|
* @script - script for the index that's being 'hashed for'
|
|
|
|
* @sighash_type - sighash_type to hash for
|
|
|
|
* @dest - hash result
|
|
|
|
*/
|
|
|
|
void bitcoin_tx_hash_for_sig(const struct bitcoin_tx *tx, unsigned int in,
|
|
|
|
const u8 *script,
|
|
|
|
enum sighash_type sighash_type,
|
|
|
|
struct sha256_double *dest);
|
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
/**
|
2019-10-28 01:03:19 +01:00
|
|
|
* sign_hash - produce a raw secp256k1 signature (with low R value).
|
2018-12-03 00:15:06 +01:00
|
|
|
* @p: secret key
|
|
|
|
* @h: hash to sign.
|
|
|
|
* @sig: signature to fill in and return.
|
|
|
|
*/
|
2016-12-02 08:42:58 +01:00
|
|
|
void sign_hash(const struct privkey *p,
|
2015-06-04 08:16:49 +02:00
|
|
|
const struct sha256_double *h,
|
2018-12-03 00:15:06 +01:00
|
|
|
secp256k1_ecdsa_signature *sig);
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
/**
|
|
|
|
* check_signed_hash - check a raw secp256k1 signature.
|
|
|
|
* @h: hash which was signed.
|
|
|
|
* @signature: signature.
|
|
|
|
* @key: public key corresponding to private key used to sign.
|
|
|
|
*
|
|
|
|
* Returns true if the key, hash and signature are correct. Changing any
|
|
|
|
* one of these will make it fail.
|
|
|
|
*/
|
2016-12-02 08:42:58 +01:00
|
|
|
bool check_signed_hash(const struct sha256_double *hash,
|
2017-01-25 00:33:42 +01:00
|
|
|
const secp256k1_ecdsa_signature *signature,
|
2016-01-21 21:11:45 +01:00
|
|
|
const struct pubkey *key);
|
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
/**
|
|
|
|
* sign_tx_input - produce a bitcoin signature for a transaction input
|
|
|
|
* @tx: the bitcoin transaction we're signing.
|
|
|
|
* @in: the input number to sign.
|
|
|
|
* @subscript: NULL (pure segwit) or a tal_arr of the signing subscript
|
|
|
|
* @witness: NULL (non-segwit) or the witness script.
|
|
|
|
* @privkey: the secret key to use for signing.
|
|
|
|
* @pubkey: the public key corresonding to @privkey.
|
|
|
|
* @sighash_type: a valid sighash type.
|
|
|
|
* @sig: (in) sighash_type indicates what type of signature make in (out) s.
|
|
|
|
*/
|
2018-12-03 00:07:06 +01:00
|
|
|
void sign_tx_input(const struct bitcoin_tx *tx,
|
2015-06-04 08:16:49 +02:00
|
|
|
unsigned int in,
|
2017-01-25 00:35:43 +01:00
|
|
|
const u8 *subscript,
|
2016-04-12 05:35:51 +02:00
|
|
|
const u8 *witness,
|
2015-06-26 04:24:07 +02:00
|
|
|
const struct privkey *privkey, const struct pubkey *pubkey,
|
2018-12-03 00:15:06 +01:00
|
|
|
enum sighash_type sighash_type,
|
|
|
|
struct bitcoin_signature *sig);
|
2015-05-30 12:41:10 +02:00
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
/**
|
|
|
|
* check_tx_sig - produce a bitcoin signature for a transaction input
|
|
|
|
* @tx: the bitcoin transaction which has been signed.
|
|
|
|
* @in: the input number to which @sig should apply.
|
|
|
|
* @subscript: NULL (pure segwit) or a tal_arr of the signing subscript
|
|
|
|
* @witness: NULL (non-segwit) or the witness script.
|
|
|
|
* @pubkey: the public key corresonding to @privkey used for signing.
|
|
|
|
* @sig: the signature to check.
|
|
|
|
*
|
|
|
|
* Returns true if this signature was created by @privkey and this tx
|
|
|
|
* and sighash_type, otherwise false.
|
|
|
|
*/
|
2018-12-03 00:07:06 +01:00
|
|
|
bool check_tx_sig(const struct bitcoin_tx *tx, size_t input_num,
|
2018-12-03 00:15:06 +01:00
|
|
|
const u8 *subscript,
|
2016-04-12 05:35:51 +02:00
|
|
|
const u8 *witness,
|
2015-06-09 03:26:51 +02:00
|
|
|
const struct pubkey *key,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *sig);
|
2015-06-09 03:26:51 +02:00
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
/* Give DER encoding of signature: returns length used (<= 73). */
|
|
|
|
size_t signature_to_der(u8 der[73], const struct bitcoin_signature *sig);
|
2015-06-26 03:51:08 +02:00
|
|
|
|
2016-09-07 23:34:36 +02:00
|
|
|
/* Parse DER encoding into signature sig */
|
2018-12-03 00:15:06 +01:00
|
|
|
bool signature_from_der(const u8 *der, size_t len, struct bitcoin_signature *sig);
|
2016-09-07 23:34:36 +02:00
|
|
|
|
2020-05-15 12:30:43 +02:00
|
|
|
/* Wire marshalling and unmarshalling */
|
|
|
|
void towire_bitcoin_signature(u8 **pptr, const struct bitcoin_signature *sig);
|
|
|
|
void fromwire_bitcoin_signature(const u8 **cursor, size_t *max,
|
|
|
|
struct bitcoin_signature *sig);
|
|
|
|
|
2020-12-05 03:17:54 +01:00
|
|
|
/* Schnorr */
|
|
|
|
struct bip340sig {
|
|
|
|
u8 u8[64];
|
|
|
|
};
|
|
|
|
void towire_bip340sig(u8 **pptr, const struct bip340sig *bip340sig);
|
|
|
|
void fromwire_bip340sig(const u8 **cursor, size_t *max,
|
|
|
|
struct bip340sig *bip340sig);
|
2020-12-05 03:23:54 +01:00
|
|
|
|
|
|
|
/* Get a hex string sig */
|
2021-01-07 19:47:47 +01:00
|
|
|
char *fmt_signature(const tal_t *ctx, const secp256k1_ecdsa_signature *sig);
|
2020-12-05 03:23:54 +01:00
|
|
|
char *fmt_bip340sig(const tal_t *ctx, const struct bip340sig *bip340sig);
|
|
|
|
|
|
|
|
/* For caller convenience, we hand in tag in parts (any can be "") */
|
|
|
|
void bip340_sighash_init(struct sha256_ctx *sctx,
|
|
|
|
const char *tag1,
|
|
|
|
const char *tag2,
|
|
|
|
const char *tag3);
|
|
|
|
|
2015-06-12 04:23:27 +02:00
|
|
|
#endif /* LIGHTNING_BITCOIN_SIGNATURE_H */
|