mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-22 16:44:20 +01:00
bitcoin_script: fix out-by-one error in multisig.
Was using 3 instead of 2. OP_LITERAL() was a bad idea anyway, as it would only handle numbers up to 16 (and we use 32). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
/* Some standard ops */
|
/* Some standard ops */
|
||||||
#define OP_PUSHBYTES(val) (val)
|
#define OP_PUSHBYTES(val) (val)
|
||||||
#define OP_LITERAL(val) (0x51 + (val))
|
|
||||||
#define OP_PUSHDATA1 0x4C
|
#define OP_PUSHDATA1 0x4C
|
||||||
#define OP_PUSHDATA2 0x4D
|
#define OP_PUSHDATA2 0x4D
|
||||||
#define OP_PUSHDATA4 0x4E
|
#define OP_PUSHDATA4 0x4E
|
||||||
@@ -61,6 +60,20 @@ static void add_push_bytes(u8 **scriptp, const void *mem, size_t len)
|
|||||||
add(scriptp, mem, len);
|
add(scriptp, mem, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
u8 n = num;
|
||||||
|
/* We could handle others, but currently unnecessary. */
|
||||||
|
assert(num < 256);
|
||||||
|
add_push_bytes(script, &n, sizeof(n));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void add_push_key(u8 **scriptp, const struct pubkey *key)
|
static void add_push_key(u8 **scriptp, const struct pubkey *key)
|
||||||
{
|
{
|
||||||
add_push_bytes(scriptp, key->key, pubkey_len(key));
|
add_push_bytes(scriptp, key->key, pubkey_len(key));
|
||||||
@@ -194,7 +207,7 @@ u8 *bitcoin_redeem_2of2(const tal_t *ctx,
|
|||||||
const struct pubkey *key2)
|
const struct pubkey *key2)
|
||||||
{
|
{
|
||||||
u8 *script = tal_arr(ctx, u8, 0);
|
u8 *script = tal_arr(ctx, u8, 0);
|
||||||
add_op(&script, OP_LITERAL(2));
|
add_number(&script, 2);
|
||||||
if (key_less(key1, key2)) {
|
if (key_less(key1, key2)) {
|
||||||
add_push_key(&script, key1);
|
add_push_key(&script, key1);
|
||||||
add_push_key(&script, key2);
|
add_push_key(&script, key2);
|
||||||
@@ -202,7 +215,7 @@ u8 *bitcoin_redeem_2of2(const tal_t *ctx,
|
|||||||
add_push_key(&script, key2);
|
add_push_key(&script, key2);
|
||||||
add_push_key(&script, key1);
|
add_push_key(&script, key1);
|
||||||
}
|
}
|
||||||
add_op(&script, OP_LITERAL(2));
|
add_number(&script, 2);
|
||||||
add_op(&script, OP_CHECKMULTISIG);
|
add_op(&script, OP_CHECKMULTISIG);
|
||||||
return script;
|
return script;
|
||||||
}
|
}
|
||||||
@@ -326,7 +339,7 @@ u8 *bitcoin_redeem_revocable(const tal_t *ctx,
|
|||||||
|
|
||||||
/* If the top arg is a hashpreimage. */
|
/* If the top arg is a hashpreimage. */
|
||||||
add_op(&script, OP_SIZE);
|
add_op(&script, OP_SIZE);
|
||||||
add_op(&script, OP_LITERAL(32));
|
add_number(&script, 32);
|
||||||
add_op(&script, OP_EQUAL);
|
add_op(&script, OP_EQUAL);
|
||||||
add_op(&script, OP_IF);
|
add_op(&script, OP_IF);
|
||||||
|
|
||||||
@@ -347,7 +360,7 @@ u8 *bitcoin_redeem_revocable(const tal_t *ctx,
|
|||||||
* will return). */
|
* will return). */
|
||||||
add_op(&script, OP_ELSE);
|
add_op(&script, OP_ELSE);
|
||||||
|
|
||||||
add_op(&script, OP_LITERAL(2));
|
add_number(&script, 2);
|
||||||
/* This obscures whose key is whose. Probably unnecessary? */
|
/* This obscures whose key is whose. Probably unnecessary? */
|
||||||
if (key_less(mykey, theirkey)) {
|
if (key_less(mykey, theirkey)) {
|
||||||
add_push_key(&script, mykey);
|
add_push_key(&script, mykey);
|
||||||
@@ -356,7 +369,7 @@ u8 *bitcoin_redeem_revocable(const tal_t *ctx,
|
|||||||
add_push_key(&script, theirkey);
|
add_push_key(&script, theirkey);
|
||||||
add_push_key(&script, mykey);
|
add_push_key(&script, mykey);
|
||||||
}
|
}
|
||||||
add_op(&script, OP_LITERAL(2));
|
add_number(&script, 2);
|
||||||
add_op(&script, OP_CHECKMULTISIG);
|
add_op(&script, OP_CHECKMULTISIG);
|
||||||
add_op(&script, OP_ENDIF);
|
add_op(&script, OP_ENDIF);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user