2017-02-07 02:44:21 +01:00
|
|
|
#include "address.h"
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "locktime.h"
|
2017-02-07 02:44:21 +01:00
|
|
|
#include "preimage.h"
|
2015-06-12 04:56:59 +02:00
|
|
|
#include "pubkey.h"
|
|
|
|
#include "script.h"
|
|
|
|
#include "signature.h"
|
2016-04-12 05:37:04 +02:00
|
|
|
#include "tx.h"
|
2016-01-21 21:08:08 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ccan/crypto/ripemd160/ripemd160.h>
|
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
|
|
|
#include <ccan/endian/endian.h>
|
2016-03-08 01:01:15 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2015-05-28 23:38:27 +02:00
|
|
|
|
|
|
|
/* Some standard ops */
|
2016-04-24 12:24:35 +02:00
|
|
|
#define OP_0 0x00
|
2015-05-28 23:38:27 +02:00
|
|
|
#define OP_PUSHBYTES(val) (val)
|
|
|
|
#define OP_PUSHDATA1 0x4C
|
|
|
|
#define OP_PUSHDATA2 0x4D
|
|
|
|
#define OP_PUSHDATA4 0x4E
|
|
|
|
#define OP_NOP 0x61
|
2015-05-30 12:41:10 +02:00
|
|
|
#define OP_IF 0x63
|
2015-08-07 05:15:30 +02:00
|
|
|
#define OP_NOTIF 0x64
|
2015-05-30 12:41:10 +02:00
|
|
|
#define OP_ELSE 0x67
|
|
|
|
#define OP_ENDIF 0x68
|
2015-08-07 05:15:30 +02:00
|
|
|
#define OP_2DROP 0x6d
|
2015-05-30 12:41:10 +02:00
|
|
|
#define OP_DEPTH 0x74
|
2015-06-08 01:56:14 +02:00
|
|
|
#define OP_DROP 0x75
|
2015-05-28 23:38:27 +02:00
|
|
|
#define OP_DUP 0x76
|
2015-08-07 05:15:30 +02:00
|
|
|
#define OP_SWAP 0x7c
|
2015-05-28 23:38:27 +02:00
|
|
|
#define OP_EQUAL 0x87
|
|
|
|
#define OP_EQUALVERIFY 0x88
|
2015-05-30 12:41:10 +02:00
|
|
|
#define OP_SIZE 0x82
|
|
|
|
#define OP_1SUB 0x8C
|
2015-08-07 05:15:30 +02:00
|
|
|
#define OP_ADD 0x93
|
2015-05-28 23:38:27 +02:00
|
|
|
#define OP_CHECKSIG 0xAC
|
|
|
|
#define OP_CHECKMULTISIG 0xAE
|
|
|
|
#define OP_HASH160 0xA9
|
2015-06-08 01:54:54 +02:00
|
|
|
#define OP_CHECKSEQUENCEVERIFY 0xB2
|
2015-08-07 05:15:30 +02:00
|
|
|
#define OP_CHECKLOCKTIMEVERIFY 0xB1
|
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
/* Bitcoin's OP_HASH160 is RIPEMD(SHA256()) */
|
|
|
|
static void hash160(struct ripemd160 *redeemhash, const void *mem, size_t len)
|
|
|
|
{
|
|
|
|
struct sha256 h;
|
|
|
|
|
|
|
|
sha256(&h, mem, len);
|
|
|
|
ripemd160(redeemhash, h.u.u8, sizeof(h));
|
|
|
|
}
|
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
static void add(u8 **scriptp, const void *mem, size_t len)
|
|
|
|
{
|
|
|
|
size_t oldlen = tal_count(*scriptp);
|
|
|
|
tal_resize(scriptp, oldlen + len);
|
|
|
|
memcpy(*scriptp + oldlen, mem, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_op(u8 **scriptp, u8 op)
|
|
|
|
{
|
|
|
|
add(scriptp, &op, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_push_bytes(u8 **scriptp, const void *mem, size_t len)
|
|
|
|
{
|
|
|
|
if (len < 76)
|
|
|
|
add_op(scriptp, OP_PUSHBYTES(len));
|
|
|
|
else if (len < 256) {
|
|
|
|
char c = len;
|
|
|
|
add_op(scriptp, OP_PUSHDATA1);
|
|
|
|
add(scriptp, &c, 1);
|
|
|
|
} else if (len < 65536) {
|
|
|
|
le16 v = cpu_to_le16(len);
|
|
|
|
add_op(scriptp, OP_PUSHDATA2);
|
|
|
|
add(scriptp, &v, 2);
|
|
|
|
} else {
|
|
|
|
le32 v = cpu_to_le32(len);
|
|
|
|
add_op(scriptp, OP_PUSHDATA4);
|
|
|
|
add(scriptp, &v, 4);
|
|
|
|
}
|
|
|
|
|
2016-03-08 01:01:15 +01:00
|
|
|
add(scriptp, memcheck(mem, len), len);
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-04 08:50:36 +02:00
|
|
|
static void add_number(u8 **script, u32 num)
|
|
|
|
{
|
|
|
|
if (num == 0)
|
|
|
|
add_op(script, 0);
|
|
|
|
else if (num <= 16)
|
|
|
|
add_op(script, 0x50 + num);
|
|
|
|
else {
|
2017-02-02 01:40:00 +01:00
|
|
|
le64 n = cpu_to_le64(num);
|
2016-01-21 21:08:08 +01:00
|
|
|
|
2017-02-02 01:40:00 +01:00
|
|
|
/* Beware: encoding is signed! */
|
|
|
|
if (num <= 0x0000007F)
|
2016-01-21 21:08:08 +01:00
|
|
|
add_push_bytes(script, &n, 1);
|
2017-02-02 01:40:00 +01:00
|
|
|
else if (num <= 0x00007FFF)
|
2016-01-21 21:08:08 +01:00
|
|
|
add_push_bytes(script, &n, 2);
|
2017-02-02 01:40:00 +01:00
|
|
|
else if (num <= 0x007FFFFF)
|
2016-01-21 21:08:08 +01:00
|
|
|
add_push_bytes(script, &n, 3);
|
2017-02-02 01:40:00 +01:00
|
|
|
else if (num <= 0x7FFFFFFF)
|
2016-01-21 21:08:08 +01:00
|
|
|
add_push_bytes(script, &n, 4);
|
2017-02-02 01:40:00 +01:00
|
|
|
else
|
|
|
|
add_push_bytes(script, &n, 5);
|
2015-06-04 08:50:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
static void add_push_key(u8 **scriptp, const struct pubkey *key)
|
2015-06-02 06:03:21 +02:00
|
|
|
{
|
2016-07-01 03:49:28 +02:00
|
|
|
u8 der[PUBKEY_DER_LEN];
|
2016-12-02 08:42:58 +01:00
|
|
|
pubkey_to_der(der, key);
|
2016-07-01 03:49:28 +02:00
|
|
|
|
|
|
|
add_push_bytes(scriptp, der, sizeof(der));
|
2015-06-02 06:03:21 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
static void add_push_sig(u8 **scriptp, const secp256k1_ecdsa_signature *sig)
|
|
|
|
{
|
|
|
|
u8 der[73];
|
|
|
|
size_t len = signature_to_der(der, sig);
|
|
|
|
|
|
|
|
/* Append sighash type */
|
|
|
|
der[len++] = SIGHASH_ALL;
|
|
|
|
add_push_bytes(scriptp, der, len);
|
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
static u8 *stack_key(const tal_t *ctx, const struct pubkey *key)
|
2016-04-12 05:37:04 +02:00
|
|
|
{
|
2016-07-01 03:49:28 +02:00
|
|
|
u8 der[PUBKEY_DER_LEN];
|
2016-12-02 08:42:58 +01:00
|
|
|
pubkey_to_der(der, key);
|
2016-07-01 03:49:28 +02:00
|
|
|
|
|
|
|
return tal_dup_arr(ctx, u8, der, sizeof(der), 0);
|
2016-04-12 05:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Bitcoin wants DER encoding. */
|
2017-01-25 00:34:23 +01:00
|
|
|
static u8 *stack_sig(const tal_t *ctx, const secp256k1_ecdsa_signature *sig)
|
2015-06-01 04:26:09 +02:00
|
|
|
{
|
2015-06-26 03:51:08 +02:00
|
|
|
u8 der[73];
|
2017-01-25 00:34:23 +01:00
|
|
|
size_t len = signature_to_der(der, sig);
|
2015-06-01 04:26:09 +02:00
|
|
|
|
2015-06-04 08:16:49 +02:00
|
|
|
/* Append sighash type */
|
2017-01-25 00:34:23 +01:00
|
|
|
der[len++] = SIGHASH_ALL;
|
2016-04-12 05:37:04 +02:00
|
|
|
return tal_dup_arr(ctx, u8, der, len, 0);
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
static u8 *stack_preimage(const tal_t *ctx, const struct preimage *preimage)
|
|
|
|
{
|
|
|
|
return tal_dup_arr(ctx, u8, preimage->r, sizeof(preimage->r), 0);
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:24:35 +02:00
|
|
|
/* Bitcoin script stack values are a special, special snowflake.
|
|
|
|
*
|
|
|
|
* They're little endian values, but 0 is an empty value. We only
|
|
|
|
* handle single byte values here. */
|
|
|
|
static u8 *stack_number(const tal_t *ctx, unsigned int num)
|
|
|
|
{
|
|
|
|
u8 val;
|
|
|
|
|
|
|
|
if (num == 0)
|
|
|
|
return tal_arr(ctx, u8, 0);
|
|
|
|
|
|
|
|
val = num;
|
|
|
|
assert(val == num);
|
|
|
|
|
|
|
|
/* We use tal_dup_arr since we want tal_count() to work */
|
|
|
|
return tal_dup_arr(ctx, u8, &val, 1, 0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
2015-06-04 08:50:36 +02:00
|
|
|
add_number(&script, 2);
|
2017-05-14 12:38:59 +02:00
|
|
|
if (pubkey_cmp(key1, key2) < 0) {
|
2016-12-02 08:42:58 +01:00
|
|
|
add_push_key(&script, key1);
|
|
|
|
add_push_key(&script, key2);
|
2015-05-28 23:38:27 +02:00
|
|
|
} else {
|
2016-12-02 08:42:58 +01:00
|
|
|
add_push_key(&script, key2);
|
|
|
|
add_push_key(&script, key1);
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
2015-06-04 08:50:36 +02:00
|
|
|
add_number(&script, 2);
|
2015-05-28 23:38:27 +02:00
|
|
|
add_op(&script, OP_CHECKMULTISIG);
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create p2sh for this redeem script. */
|
|
|
|
u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript)
|
|
|
|
{
|
2015-07-09 08:13:36 +02:00
|
|
|
struct ripemd160 redeemhash;
|
2015-05-28 23:38:27 +02:00
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
|
|
|
|
add_op(&script, OP_HASH160);
|
2016-04-12 05:37:04 +02:00
|
|
|
hash160(&redeemhash, redeemscript, tal_count(redeemscript));
|
2015-07-09 08:13:36 +02:00
|
|
|
add_push_bytes(&script, redeemhash.u.u8, sizeof(redeemhash.u.u8));
|
2015-05-28 23:38:27 +02:00
|
|
|
add_op(&script, OP_EQUAL);
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
/* Create an output script using p2pkh */
|
2017-06-14 10:29:10 +02:00
|
|
|
u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr)
|
2017-02-07 02:44:21 +01:00
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
|
|
|
|
add_op(&script, OP_DUP);
|
|
|
|
add_op(&script, OP_HASH160);
|
2017-06-14 10:29:10 +02:00
|
|
|
add_push_bytes(&script, &addr->addr, sizeof(addr->addr));
|
2017-02-07 02:44:21 +01:00
|
|
|
add_op(&script, OP_EQUALVERIFY);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
|
|
|
|
add_push_sig(&script, sig);
|
|
|
|
add_push_key(&script, pubkey);
|
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
/* Create the redeemscript for a P2SH + P2WPKH (for signing tx) */
|
2017-03-07 02:05:48 +01:00
|
|
|
u8 *bitcoin_redeem_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key)
|
2016-04-12 05:37:04 +02:00
|
|
|
{
|
|
|
|
struct ripemd160 keyhash;
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
|
|
|
|
/* BIP141: BIP16 redeemScript pushed in the scriptSig is exactly a
|
|
|
|
* push of a version byte plus a push of a witness program. */
|
|
|
|
add_number(&script, 0);
|
2017-07-01 13:19:36 +02:00
|
|
|
pubkey_to_hash160(key, &keyhash);
|
2016-04-12 05:37:04 +02:00
|
|
|
add_push_bytes(&script, &keyhash, sizeof(keyhash));
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2017-03-07 02:01:55 +01:00
|
|
|
u8 *bitcoin_scriptsig_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key)
|
|
|
|
{
|
2017-03-07 02:05:48 +01:00
|
|
|
u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(ctx, key), *script;
|
2017-03-07 02:01:55 +01:00
|
|
|
|
|
|
|
/* BIP141: The scriptSig must be exactly a push of the BIP16
|
|
|
|
* redeemScript or validation fails. */
|
|
|
|
script = tal_arr(ctx, u8, 0);
|
|
|
|
add_push_bytes(&script, redeemscript, tal_count(redeemscript));
|
|
|
|
tal_free(redeemscript);
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
/* Create an input which spends the p2sh-p2wpkh. */
|
|
|
|
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)
|
|
|
|
{
|
2017-03-07 02:05:48 +01:00
|
|
|
u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(ctx, key);
|
2016-04-12 05:37:04 +02:00
|
|
|
|
|
|
|
/* BIP141: The scriptSig must be exactly a push of the BIP16 redeemScript
|
|
|
|
* or validation fails. */
|
|
|
|
input->script = tal_arr(ctx, u8, 0);
|
|
|
|
add_push_bytes(&input->script, redeemscript, tal_count(redeemscript));
|
2016-11-04 01:47:03 +01:00
|
|
|
tal_free(redeemscript);
|
2016-04-12 05:37:04 +02:00
|
|
|
|
2017-03-07 02:05:48 +01:00
|
|
|
input->witness = bitcoin_witness_p2wpkh(ctx, sig, key);
|
2016-04-12 05:37:04 +02:00
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
u8 **bitcoin_witness_p2wpkh(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *sig,
|
|
|
|
const struct pubkey *key)
|
|
|
|
{
|
|
|
|
u8 **witness;
|
|
|
|
|
|
|
|
/* BIP141: The witness must consist of exactly 2 items (≤ 520
|
|
|
|
* bytes each). The first one a signature, and the second one
|
|
|
|
* a public key. */
|
|
|
|
witness = tal_arr(ctx, u8 *, 2);
|
|
|
|
witness[0] = stack_sig(witness, sig);
|
|
|
|
witness[1] = stack_key(witness, key);
|
|
|
|
return witness;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:24:35 +02:00
|
|
|
/* Create an output script for a 32-byte witness. */
|
|
|
|
u8 *scriptpubkey_p2wsh(const tal_t *ctx, const u8 *witnessscript)
|
|
|
|
{
|
|
|
|
struct sha256 h;
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-04-24 12:24:35 +02:00
|
|
|
add_op(&script, OP_0);
|
|
|
|
sha256(&h, witnessscript, tal_count(witnessscript));
|
|
|
|
add_push_bytes(&script, h.u.u8, sizeof(h.u.u8));
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:27:35 +02:00
|
|
|
/* Create an output script for a 20-byte witness. */
|
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
|
|
|
{
|
|
|
|
struct ripemd160 h;
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-04-24 12:27:35 +02:00
|
|
|
add_op(&script, OP_0);
|
2017-07-01 13:19:36 +02:00
|
|
|
pubkey_to_hash160(key, &h);
|
2016-04-24 12:27:35 +02:00
|
|
|
add_push_bytes(&script, &h, sizeof(h));
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:49:02 +01:00
|
|
|
u8 *scriptpubkey_p2wpkh_derkey(const tal_t *ctx, const u8 der[33])
|
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
struct ripemd160 h;
|
|
|
|
|
|
|
|
add_op(&script, OP_0);
|
|
|
|
hash160(&h, der, PUBKEY_DER_LEN);
|
|
|
|
add_push_bytes(&script, &h, sizeof(h));
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
u8 **witness = tal_arr(ctx, u8 *, 4);
|
|
|
|
|
|
|
|
/* OP_CHECKMULTISIG has an out-by-one bug, which MBZ */
|
|
|
|
witness[0] = stack_number(witness, 0);
|
|
|
|
|
|
|
|
/* sig order should match key order. */
|
2017-05-14 12:38:59 +02:00
|
|
|
if (pubkey_cmp(key1, key2) < 0) {
|
2016-12-02 08:42:58 +01:00
|
|
|
witness[1] = stack_sig(witness, sig1);
|
|
|
|
witness[2] = stack_sig(witness, sig2);
|
2016-04-24 12:24:35 +02:00
|
|
|
} else {
|
2016-12-02 08:42:58 +01:00
|
|
|
witness[1] = stack_sig(witness, sig2);
|
|
|
|
witness[2] = stack_sig(witness, sig1);
|
2016-04-24 12:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
witness[3] = bitcoin_redeem_2of2(witness, key1, key2);
|
2016-04-24 12:24:35 +02:00
|
|
|
return witness;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
struct ripemd160 pkhash;
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
2017-07-01 13:19:36 +02:00
|
|
|
pubkey_to_hash160(key, &pkhash);
|
2016-04-12 05:36:51 +02:00
|
|
|
|
|
|
|
/* BIP143:
|
|
|
|
*
|
|
|
|
* For P2WPKH witness program, the scriptCode is
|
|
|
|
* 0x1976a914{20-byte-pubkey-hash}88ac.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* PUSH(25): OP_DUP OP_HASH160 PUSH(20) 20-byte-pubkey-hash
|
|
|
|
* OP_EQUALVERIFY OP_CHECKSIG */
|
|
|
|
add_op(&script, OP_DUP);
|
|
|
|
add_op(&script, OP_HASH160);
|
|
|
|
add_push_bytes(&script, &pkhash, sizeof(pkhash));
|
|
|
|
add_op(&script, OP_EQUALVERIFY);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2pkh(const u8 *script)
|
2016-08-31 08:36:32 +02:00
|
|
|
{
|
2017-01-25 00:35:43 +01:00
|
|
|
size_t script_len = tal_len(script);
|
|
|
|
|
2016-08-31 08:36:32 +02:00
|
|
|
if (script_len != 25)
|
|
|
|
return false;
|
|
|
|
if (script[0] != OP_DUP)
|
|
|
|
return false;
|
|
|
|
if (script[1] != OP_HASH160)
|
|
|
|
return false;
|
|
|
|
if (script[2] != OP_PUSHBYTES(20))
|
|
|
|
return false;
|
|
|
|
if (script[23] != OP_EQUALVERIFY)
|
|
|
|
return false;
|
|
|
|
if (script[24] != OP_CHECKSIG)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2sh(const u8 *script)
|
2015-06-02 06:10:00 +02:00
|
|
|
{
|
2017-01-25 00:35:43 +01:00
|
|
|
size_t script_len = tal_len(script);
|
|
|
|
|
2015-06-02 06:10:00 +02:00
|
|
|
if (script_len != 23)
|
|
|
|
return false;
|
|
|
|
if (script[0] != OP_HASH160)
|
|
|
|
return false;
|
|
|
|
if (script[1] != OP_PUSHBYTES(20))
|
|
|
|
return false;
|
|
|
|
if (script[22] != OP_EQUAL)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-24 08:30:10 +02:00
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2wsh(const u8 *script)
|
2016-08-18 06:53:45 +02:00
|
|
|
{
|
2017-01-25 00:35:43 +01:00
|
|
|
size_t script_len = tal_len(script);
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
if (script_len != 1 + 1 + sizeof(struct sha256))
|
|
|
|
return false;
|
|
|
|
if (script[0] != OP_0)
|
|
|
|
return false;
|
|
|
|
if (script[1] != OP_PUSHBYTES(sizeof(struct sha256)))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
bool is_p2wpkh(const u8 *script)
|
2016-08-31 08:36:32 +02:00
|
|
|
{
|
2017-01-25 00:35:43 +01:00
|
|
|
size_t script_len = tal_len(script);
|
|
|
|
|
2016-08-31 08:36:32 +02:00
|
|
|
if (script_len != 1 + 1 + sizeof(struct ripemd160))
|
|
|
|
return false;
|
|
|
|
if (script[0] != OP_0)
|
|
|
|
return false;
|
|
|
|
if (script[1] != OP_PUSHBYTES(sizeof(struct ripemd160)))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-16 01:39:06 +02:00
|
|
|
u8 **bitcoin_witness_sig_and_element(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *sig,
|
|
|
|
const void *elem, size_t elemsize,
|
|
|
|
const u8 *witnessscript)
|
2016-04-24 12:27:35 +02:00
|
|
|
{
|
|
|
|
u8 **witness = tal_arr(ctx, u8 *, 3);
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
witness[0] = stack_sig(witness, sig);
|
2017-09-16 01:39:06 +02:00
|
|
|
witness[1] = tal_dup_arr(witness, u8, elem, elemsize, 0);
|
2016-04-24 12:27:35 +02:00
|
|
|
witness[2] = tal_dup_arr(witness, u8,
|
|
|
|
witnessscript, tal_count(witnessscript), 0);
|
|
|
|
|
|
|
|
return witness;
|
|
|
|
}
|
2016-05-02 08:29:56 +02:00
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* This output sends funds back to the owner of this commitment transaction,
|
|
|
|
* thus must be timelocked using `OP_CSV`. It can be claimed, without delay,
|
|
|
|
* by the other party if they know the revocation key. The output is a version
|
|
|
|
* 0 P2WSH, with a witness script:
|
|
|
|
*
|
|
|
|
* OP_IF
|
|
|
|
* # Penalty transaction
|
2017-06-06 01:48:10 +02:00
|
|
|
* <revocationkey>
|
2017-02-07 02:44:21 +01:00
|
|
|
* OP_ELSE
|
2017-06-06 01:48:10 +02:00
|
|
|
* `to_self_delay`
|
2017-02-07 02:44:21 +01:00
|
|
|
* OP_CSV
|
|
|
|
* OP_DROP
|
2017-06-06 01:48:10 +02:00
|
|
|
* <local_delayedkey>
|
2017-02-07 02:44:21 +01:00
|
|
|
* OP_ENDIF
|
|
|
|
* OP_CHECKSIG
|
|
|
|
*/
|
|
|
|
u8 *bitcoin_wscript_to_local(const tal_t *ctx, u16 to_self_delay,
|
|
|
|
const struct pubkey *revocation_pubkey,
|
|
|
|
const struct pubkey *local_delayedkey)
|
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
add_op(&script, OP_IF);
|
|
|
|
add_push_key(&script, revocation_pubkey);
|
|
|
|
add_op(&script, OP_ELSE);
|
|
|
|
add_number(&script, to_self_delay);
|
|
|
|
add_op(&script, OP_CHECKSEQUENCEVERIFY);
|
|
|
|
add_op(&script, OP_DROP);
|
|
|
|
add_push_key(&script, local_delayedkey);
|
|
|
|
add_op(&script, OP_ENDIF);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 **bitcoin_to_local_spend_revocation(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *revocation_sig,
|
|
|
|
const u8 *wscript)
|
|
|
|
{
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* If a revoked commitment transaction is published, the other party
|
|
|
|
* can spend this output immediately with the following witness:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* <revocation_sig> 1
|
2017-02-07 02:44:21 +01:00
|
|
|
*/
|
|
|
|
u8 **witness = tal_arr(ctx, u8 *, 3);
|
|
|
|
|
|
|
|
witness[0] = stack_sig(witness, revocation_sig);
|
|
|
|
witness[1] = stack_number(witness, 1);
|
|
|
|
witness[2] = tal_dup_arr(witness, u8, wscript, tal_len(wscript), 0);
|
|
|
|
|
|
|
|
return witness;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-03-07 06:46:59 +01:00
|
|
|
* #### Offered HTLC Outputs
|
2017-02-07 02:44:21 +01:00
|
|
|
*
|
2017-03-07 06:46:59 +01:00
|
|
|
* This output sends funds to a HTLC-timeout transaction after the HTLC
|
|
|
|
* timeout, or to the remote peer using the payment preimage or the revocation
|
|
|
|
* key. The output is a P2WSH, with a witness script:
|
|
|
|
*
|
|
|
|
* # To you with revocation key
|
2017-06-06 01:49:10 +02:00
|
|
|
* OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationkey))> OP_EQUAL
|
2017-03-07 06:46:59 +01:00
|
|
|
* OP_IF
|
|
|
|
* OP_CHECKSIG
|
|
|
|
* OP_ELSE
|
|
|
|
* <remotekey> OP_SWAP OP_SIZE 32 OP_EQUAL
|
|
|
|
* OP_NOTIF
|
|
|
|
* # To me via HTLC-timeout transaction (timelocked).
|
|
|
|
* OP_DROP 2 OP_SWAP <localkey> 2 OP_CHECKMULTISIG
|
|
|
|
* OP_ELSE
|
|
|
|
* # To you with preimage.
|
2017-06-06 01:48:10 +02:00
|
|
|
* OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY
|
2017-03-07 06:46:59 +01:00
|
|
|
* OP_CHECKSIG
|
|
|
|
* OP_ENDIF
|
|
|
|
* OP_ENDIF
|
2017-02-07 02:44:21 +01:00
|
|
|
*/
|
2017-08-18 06:43:53 +02:00
|
|
|
u8 *bitcoin_wscript_htlc_offer_ripemd160(const tal_t *ctx,
|
|
|
|
const struct pubkey *localkey,
|
|
|
|
const struct pubkey *remotekey,
|
|
|
|
const struct ripemd160 *payment_ripemd,
|
|
|
|
const struct pubkey *revocationkey)
|
2017-02-07 02:44:21 +01:00
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
struct ripemd160 ripemd;
|
|
|
|
|
2017-03-07 06:46:59 +01:00
|
|
|
add_op(&script, OP_DUP);
|
|
|
|
add_op(&script, OP_HASH160);
|
2017-07-01 13:19:36 +02:00
|
|
|
pubkey_to_hash160(revocationkey, &ripemd);
|
2017-03-07 06:46:59 +01:00
|
|
|
add_push_bytes(&script, &ripemd, sizeof(ripemd));
|
|
|
|
add_op(&script, OP_EQUAL);
|
|
|
|
add_op(&script, OP_IF);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
add_op(&script, OP_ELSE);
|
2017-02-07 02:44:21 +01:00
|
|
|
add_push_key(&script, remotekey);
|
|
|
|
add_op(&script, OP_SWAP);
|
|
|
|
add_op(&script, OP_SIZE);
|
|
|
|
add_number(&script, 32);
|
|
|
|
add_op(&script, OP_EQUAL);
|
|
|
|
add_op(&script, OP_NOTIF);
|
|
|
|
add_op(&script, OP_DROP);
|
|
|
|
add_number(&script, 2);
|
|
|
|
add_op(&script, OP_SWAP);
|
|
|
|
add_push_key(&script, localkey);
|
|
|
|
add_number(&script, 2);
|
|
|
|
add_op(&script, OP_CHECKMULTISIG);
|
|
|
|
add_op(&script, OP_ELSE);
|
|
|
|
add_op(&script, OP_HASH160);
|
2017-08-18 06:43:53 +02:00
|
|
|
add_push_bytes(&script,
|
|
|
|
payment_ripemd->u.u8, sizeof(payment_ripemd->u.u8));
|
2017-02-07 02:44:21 +01:00
|
|
|
add_op(&script, OP_EQUALVERIFY);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
add_op(&script, OP_ENDIF);
|
2017-03-07 06:46:59 +01:00
|
|
|
add_op(&script, OP_ENDIF);
|
2017-02-07 02:44:21 +01:00
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:53 +02:00
|
|
|
u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
|
|
|
|
const struct pubkey *localkey,
|
|
|
|
const struct pubkey *remotekey,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const struct pubkey *revocationkey)
|
|
|
|
{
|
|
|
|
struct ripemd160 ripemd;
|
|
|
|
|
|
|
|
ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u));
|
|
|
|
return bitcoin_wscript_htlc_offer_ripemd160(ctx, localkey, remotekey,
|
|
|
|
&ripemd, revocationkey);
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-03-07 06:46:59 +01:00
|
|
|
* #### Received HTLC Outputs
|
2017-02-07 02:44:21 +01:00
|
|
|
*
|
2017-03-07 06:46:59 +01:00
|
|
|
* This output sends funds to the remote peer after the HTLC timeout or using
|
|
|
|
* the revocation key, or to an HTLC-success transaction with a successful
|
|
|
|
* payment preimage. The output is a P2WSH, with a witness script:
|
|
|
|
*
|
|
|
|
* # To you with revocation key
|
2017-06-06 01:49:10 +02:00
|
|
|
* OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationkey))> OP_EQUAL
|
2017-03-07 06:46:59 +01:00
|
|
|
* OP_IF
|
|
|
|
* OP_CHECKSIG
|
|
|
|
* OP_ELSE
|
|
|
|
* <remotekey> OP_SWAP
|
|
|
|
* OP_SIZE 32 OP_EQUAL
|
|
|
|
* OP_IF
|
|
|
|
* # To me via HTLC-success transaction.
|
2017-06-06 01:48:10 +02:00
|
|
|
* OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY
|
2017-03-07 06:46:59 +01:00
|
|
|
* 2 OP_SWAP <localkey> 2 OP_CHECKMULTISIG
|
|
|
|
* OP_ELSE
|
|
|
|
* # To you after timeout.
|
2017-06-06 01:48:10 +02:00
|
|
|
* OP_DROP <cltv_expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP
|
2017-03-07 06:46:59 +01:00
|
|
|
* OP_CHECKSIG
|
|
|
|
* OP_ENDIF
|
|
|
|
* OP_ENDIF
|
2017-02-07 02:44:21 +01:00
|
|
|
*/
|
2017-08-18 06:43:53 +02:00
|
|
|
u8 *bitcoin_wscript_htlc_receive_ripemd(const tal_t *ctx,
|
|
|
|
const struct abs_locktime *htlc_abstimeout,
|
|
|
|
const struct pubkey *localkey,
|
|
|
|
const struct pubkey *remotekey,
|
|
|
|
const struct ripemd160 *payment_ripemd,
|
|
|
|
const struct pubkey *revocationkey)
|
2017-02-07 02:44:21 +01:00
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
struct ripemd160 ripemd;
|
|
|
|
|
2017-03-07 06:46:59 +01:00
|
|
|
add_op(&script, OP_DUP);
|
|
|
|
add_op(&script, OP_HASH160);
|
2017-07-01 13:19:36 +02:00
|
|
|
pubkey_to_hash160(revocationkey, &ripemd);
|
2017-03-07 06:46:59 +01:00
|
|
|
add_push_bytes(&script, &ripemd, sizeof(ripemd));
|
|
|
|
add_op(&script, OP_EQUAL);
|
|
|
|
add_op(&script, OP_IF);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
add_op(&script, OP_ELSE);
|
2017-02-07 02:44:21 +01:00
|
|
|
add_push_key(&script, remotekey);
|
|
|
|
add_op(&script, OP_SWAP);
|
|
|
|
add_op(&script, OP_SIZE);
|
|
|
|
add_number(&script, 32);
|
|
|
|
add_op(&script, OP_EQUAL);
|
|
|
|
add_op(&script, OP_IF);
|
|
|
|
add_op(&script, OP_HASH160);
|
2017-08-18 06:43:53 +02:00
|
|
|
add_push_bytes(&script,
|
|
|
|
payment_ripemd->u.u8, sizeof(payment_ripemd->u.u8));
|
2017-02-07 02:44:21 +01:00
|
|
|
add_op(&script, OP_EQUALVERIFY);
|
|
|
|
add_number(&script, 2);
|
|
|
|
add_op(&script, OP_SWAP);
|
|
|
|
add_push_key(&script, localkey);
|
|
|
|
add_number(&script, 2);
|
|
|
|
add_op(&script, OP_CHECKMULTISIG);
|
|
|
|
add_op(&script, OP_ELSE);
|
|
|
|
add_op(&script, OP_DROP);
|
|
|
|
add_number(&script, htlc_abstimeout->locktime);
|
|
|
|
add_op(&script, OP_CHECKLOCKTIMEVERIFY);
|
|
|
|
add_op(&script, OP_DROP);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
add_op(&script, OP_ENDIF);
|
2017-03-07 06:46:59 +01:00
|
|
|
add_op(&script, OP_ENDIF);
|
2017-02-07 02:44:21 +01:00
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:53 +02:00
|
|
|
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,
|
|
|
|
const struct pubkey *revocationkey)
|
|
|
|
{
|
|
|
|
struct ripemd160 ripemd;
|
|
|
|
|
|
|
|
ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u));
|
|
|
|
return bitcoin_wscript_htlc_receive_ripemd(ctx, htlc_abstimeout,
|
|
|
|
localkey, remotekey,
|
|
|
|
&ripemd, revocationkey);
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* ## HTLC-Timeout and HTLC-Success Transactions
|
|
|
|
*
|
|
|
|
*...
|
2017-06-06 01:48:10 +02:00
|
|
|
* * `txin[0]` witness stack: `0 <remotesig> <localsig> <payment_preimage>` for HTLC-Success, `0 <remotesig> <localsig> 0` for HTLC-Timeout.
|
2017-02-07 02:44:21 +01:00
|
|
|
*/
|
2017-09-16 01:39:06 +02:00
|
|
|
u8 **bitcoin_witness_htlc_timeout_tx(const tal_t *ctx,
|
|
|
|
const secp256k1_ecdsa_signature *localsig,
|
|
|
|
const secp256k1_ecdsa_signature *remotesig,
|
|
|
|
const u8 *wscript)
|
2017-02-07 02:44:21 +01:00
|
|
|
{
|
|
|
|
u8 **witness = tal_arr(ctx, u8 *, 5);
|
|
|
|
|
|
|
|
witness[0] = stack_number(witness, 0);
|
|
|
|
witness[1] = stack_sig(witness, remotesig);
|
|
|
|
witness[2] = stack_sig(witness, localsig);
|
|
|
|
witness[3] = stack_number(witness, 0);
|
|
|
|
witness[4] = tal_dup_arr(witness, u8, wscript, tal_len(wscript), 0);
|
|
|
|
|
|
|
|
return witness;
|
|
|
|
}
|
|
|
|
|
2017-09-16 01:39:06 +02:00
|
|
|
u8 **bitcoin_witness_htlc_success_tx(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:21 +01:00
|
|
|
{
|
|
|
|
u8 **witness = tal_arr(ctx, u8 *, 5);
|
|
|
|
|
|
|
|
witness[0] = stack_number(witness, 0);
|
|
|
|
witness[1] = stack_sig(witness, remotesig);
|
|
|
|
witness[2] = stack_sig(witness, localsig);
|
|
|
|
witness[3] = stack_preimage(witness, preimage);
|
|
|
|
witness[4] = tal_dup_arr(witness, u8, wscript, tal_len(wscript), 0);
|
|
|
|
|
|
|
|
return witness;
|
|
|
|
}
|
2017-02-07 02:44:22 +01:00
|
|
|
u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx,
|
|
|
|
u16 to_self_delay,
|
|
|
|
const struct pubkey *revocation_pubkey,
|
|
|
|
const struct pubkey *local_delayedkey)
|
|
|
|
{
|
|
|
|
u8 *script = tal_arr(ctx, u8, 0);
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* The witness script for the output is:
|
|
|
|
*
|
|
|
|
* OP_IF
|
|
|
|
* # Penalty transaction
|
2017-06-06 01:48:10 +02:00
|
|
|
* <revocationkey>
|
2017-02-07 02:44:22 +01:00
|
|
|
* OP_ELSE
|
2017-06-06 01:48:10 +02:00
|
|
|
* `to_self_delay`
|
2017-02-07 02:44:22 +01:00
|
|
|
* OP_CSV
|
|
|
|
* OP_DROP
|
2017-06-06 01:48:10 +02:00
|
|
|
* <local_delayedkey>
|
2017-02-07 02:44:22 +01:00
|
|
|
* OP_ENDIF
|
|
|
|
* OP_CHECKSIG
|
|
|
|
*/
|
|
|
|
add_op(&script, OP_IF);
|
|
|
|
add_push_key(&script, revocation_pubkey);
|
|
|
|
add_op(&script, OP_ELSE);
|
|
|
|
add_number(&script, to_self_delay);
|
|
|
|
add_op(&script, OP_CHECKSEQUENCEVERIFY);
|
|
|
|
add_op(&script, OP_DROP);
|
|
|
|
add_push_key(&script, local_delayedkey);
|
|
|
|
add_op(&script, OP_ENDIF);
|
|
|
|
add_op(&script, OP_CHECKSIG);
|
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-01-25 00:35:43 +01:00
|
|
|
memcheck(s1, tal_len(s1));
|
|
|
|
memcheck(s2, tal_len(s2));
|
2016-05-02 08:29:56 +02:00
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
if (tal_len(s1) != tal_len(s2))
|
2016-05-02 08:29:56 +02:00
|
|
|
return false;
|
2017-01-25 00:35:43 +01:00
|
|
|
return memcmp(s1, s2, tal_len(s1)) == 0;
|
2016-05-02 08:29:56 +02:00
|
|
|
}
|