mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-22 16:44:20 +01:00
script: Consolidate scripts to use pubkey_to_hash160
This commit is contained in:
committed by
Rusty Russell
parent
f371b6df20
commit
7dc693963d
@@ -47,13 +47,6 @@ static void hash160(struct ripemd160 *redeemhash, const void *mem, size_t len)
|
|||||||
ripemd160(redeemhash, h.u.u8, sizeof(h));
|
ripemd160(redeemhash, h.u.u8, sizeof(h));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hash160_key(struct ripemd160 *khash, const struct pubkey *key)
|
|
||||||
{
|
|
||||||
u8 der[PUBKEY_DER_LEN];
|
|
||||||
pubkey_to_der(der, key);
|
|
||||||
hash160(khash, der, sizeof(der));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void add(u8 **scriptp, const void *mem, size_t len)
|
static void add(u8 **scriptp, const void *mem, size_t len)
|
||||||
{
|
{
|
||||||
size_t oldlen = tal_count(*scriptp);
|
size_t oldlen = tal_count(*scriptp);
|
||||||
@@ -245,7 +238,7 @@ u8 *bitcoin_redeem_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key)
|
|||||||
/* BIP141: BIP16 redeemScript pushed in the scriptSig is exactly a
|
/* BIP141: BIP16 redeemScript pushed in the scriptSig is exactly a
|
||||||
* push of a version byte plus a push of a witness program. */
|
* push of a version byte plus a push of a witness program. */
|
||||||
add_number(&script, 0);
|
add_number(&script, 0);
|
||||||
hash160_key(&keyhash, key);
|
pubkey_to_hash160(key, &keyhash);
|
||||||
add_push_bytes(&script, &keyhash, sizeof(keyhash));
|
add_push_bytes(&script, &keyhash, sizeof(keyhash));
|
||||||
return script;
|
return script;
|
||||||
}
|
}
|
||||||
@@ -313,7 +306,7 @@ u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key)
|
|||||||
u8 *script = tal_arr(ctx, u8, 0);
|
u8 *script = tal_arr(ctx, u8, 0);
|
||||||
|
|
||||||
add_op(&script, OP_0);
|
add_op(&script, OP_0);
|
||||||
hash160_key(&h, key);
|
pubkey_to_hash160(key, &h);
|
||||||
add_push_bytes(&script, &h, sizeof(h));
|
add_push_bytes(&script, &h, sizeof(h));
|
||||||
return script;
|
return script;
|
||||||
}
|
}
|
||||||
@@ -470,8 +463,8 @@ u8 *p2wpkh_scriptcode(const tal_t *ctx, const struct pubkey *key)
|
|||||||
{
|
{
|
||||||
struct ripemd160 pkhash;
|
struct ripemd160 pkhash;
|
||||||
u8 *script = tal_arr(ctx, u8, 0);
|
u8 *script = tal_arr(ctx, u8, 0);
|
||||||
|
pubkey_to_hash160(key, &pkhash);
|
||||||
|
|
||||||
hash160_key(&pkhash, key);
|
|
||||||
/* BIP143:
|
/* BIP143:
|
||||||
*
|
*
|
||||||
* For P2WPKH witness program, the scriptCode is
|
* For P2WPKH witness program, the scriptCode is
|
||||||
@@ -726,7 +719,7 @@ u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
|
|||||||
|
|
||||||
add_op(&script, OP_DUP);
|
add_op(&script, OP_DUP);
|
||||||
add_op(&script, OP_HASH160);
|
add_op(&script, OP_HASH160);
|
||||||
hash160_key(&ripemd, revocationkey);
|
pubkey_to_hash160(revocationkey, &ripemd);
|
||||||
add_push_bytes(&script, &ripemd, sizeof(ripemd));
|
add_push_bytes(&script, &ripemd, sizeof(ripemd));
|
||||||
add_op(&script, OP_EQUAL);
|
add_op(&script, OP_EQUAL);
|
||||||
add_op(&script, OP_IF);
|
add_op(&script, OP_IF);
|
||||||
@@ -794,7 +787,7 @@ u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx,
|
|||||||
|
|
||||||
add_op(&script, OP_DUP);
|
add_op(&script, OP_DUP);
|
||||||
add_op(&script, OP_HASH160);
|
add_op(&script, OP_HASH160);
|
||||||
hash160_key(&ripemd, revocationkey);
|
pubkey_to_hash160(revocationkey, &ripemd);
|
||||||
add_push_bytes(&script, &ripemd, sizeof(ripemd));
|
add_push_bytes(&script, &ripemd, sizeof(ripemd));
|
||||||
add_op(&script, OP_EQUAL);
|
add_op(&script, OP_EQUAL);
|
||||||
add_op(&script, OP_IF);
|
add_op(&script, OP_IF);
|
||||||
|
|||||||
@@ -203,11 +203,11 @@ struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id)
|
|||||||
struct peer *find_peer_by_pkhash(struct lightningd_state *dstate, const u8 *pkhash)
|
struct peer *find_peer_by_pkhash(struct lightningd_state *dstate, const u8 *pkhash)
|
||||||
{
|
{
|
||||||
struct peer *peer;
|
struct peer *peer;
|
||||||
u8 addr[20];
|
struct ripemd160 addr;
|
||||||
|
|
||||||
list_for_each(&dstate->peers, peer, list) {
|
list_for_each(&dstate->peers, peer, list) {
|
||||||
pubkey_hash160(addr, peer->id);
|
pubkey_to_hash160(peer->id, &addr);
|
||||||
if (memcmp(addr, pkhash, sizeof(addr)) == 0)
|
if (memcmp(&addr, pkhash, sizeof(addr)) == 0)
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <bitcoin/address.h>
|
||||||
|
|
||||||
#include <ccan/crypto/ripemd160/ripemd160.h>
|
#include <ccan/crypto/ripemd160/ripemd160.h>
|
||||||
#include <ccan/crypto/sha256/sha256.h>
|
#include <ccan/crypto/sha256/sha256.h>
|
||||||
#include <ccan/mem/mem.h>
|
#include <ccan/mem/mem.h>
|
||||||
@@ -263,26 +265,6 @@ bool onion_shared_secret(
|
|||||||
privkey->secret.data);
|
privkey->secret.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pubkey_hash160(
|
|
||||||
u8 *dst,
|
|
||||||
const struct pubkey *pubkey)
|
|
||||||
{
|
|
||||||
struct ripemd160 r;
|
|
||||||
struct sha256 h;
|
|
||||||
u8 der[33];
|
|
||||||
size_t outputlen = 33;
|
|
||||||
|
|
||||||
secp256k1_ec_pubkey_serialize(secp256k1_ctx,
|
|
||||||
der,
|
|
||||||
&outputlen,
|
|
||||||
&pubkey->pubkey,
|
|
||||||
SECP256K1_EC_COMPRESSED);
|
|
||||||
sha256(&h, der, sizeof(der));
|
|
||||||
ripemd160(&r, h.u.u8, sizeof(h));
|
|
||||||
|
|
||||||
memcpy(dst, r.u.u8, sizeof(r));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void generate_key_set(const u8 secret[SHARED_SECRET_SIZE],
|
static void generate_key_set(const u8 secret[SHARED_SECRET_SIZE],
|
||||||
struct keyset *keys)
|
struct keyset *keys)
|
||||||
{
|
{
|
||||||
@@ -372,7 +354,8 @@ struct onionpacket *create_onionpacket(
|
|||||||
u8 filler[2 * (num_hops - 1) * SECURITY_PARAMETER];
|
u8 filler[2 * (num_hops - 1) * SECURITY_PARAMETER];
|
||||||
u8 hopfiller[(num_hops - 1) * HOP_PAYLOAD_SIZE];
|
u8 hopfiller[(num_hops - 1) * HOP_PAYLOAD_SIZE];
|
||||||
struct keyset keys;
|
struct keyset keys;
|
||||||
u8 nextaddr[20], nexthmac[SECURITY_PARAMETER];
|
struct bitcoin_address nextaddr;
|
||||||
|
u8 nexthmac[SECURITY_PARAMETER];
|
||||||
u8 stream[ROUTING_INFO_SIZE], hopstream[TOTAL_HOP_PAYLOAD_SIZE];
|
u8 stream[ROUTING_INFO_SIZE], hopstream[TOTAL_HOP_PAYLOAD_SIZE];
|
||||||
struct hop_params *params = generate_hop_params(ctx, sessionkey, path);
|
struct hop_params *params = generate_hop_params(ctx, sessionkey, path);
|
||||||
u8 binhoppayloads[tal_count(path)][HOP_PAYLOAD_SIZE];
|
u8 binhoppayloads[tal_count(path)][HOP_PAYLOAD_SIZE];
|
||||||
@@ -383,7 +366,7 @@ struct onionpacket *create_onionpacket(
|
|||||||
if (!params)
|
if (!params)
|
||||||
return NULL;
|
return NULL;
|
||||||
packet->version = 1;
|
packet->version = 1;
|
||||||
memset(nextaddr, 0, 20);
|
memset(&nextaddr, 0, 20);
|
||||||
memset(nexthmac, 0, 20);
|
memset(nexthmac, 0, 20);
|
||||||
memset(packet->routinginfo, 0, ROUTING_INFO_SIZE);
|
memset(packet->routinginfo, 0, ROUTING_INFO_SIZE);
|
||||||
|
|
||||||
@@ -399,7 +382,7 @@ struct onionpacket *create_onionpacket(
|
|||||||
/* Rightshift mix-header by 2*SECURITY_PARAMETER */
|
/* Rightshift mix-header by 2*SECURITY_PARAMETER */
|
||||||
memmove(packet->routinginfo + 2 * SECURITY_PARAMETER, packet->routinginfo,
|
memmove(packet->routinginfo + 2 * SECURITY_PARAMETER, packet->routinginfo,
|
||||||
ROUTING_INFO_SIZE - 2 * SECURITY_PARAMETER);
|
ROUTING_INFO_SIZE - 2 * SECURITY_PARAMETER);
|
||||||
memcpy(packet->routinginfo, nextaddr, SECURITY_PARAMETER);
|
memcpy(packet->routinginfo, &nextaddr, SECURITY_PARAMETER);
|
||||||
memcpy(packet->routinginfo + SECURITY_PARAMETER, nexthmac, SECURITY_PARAMETER);
|
memcpy(packet->routinginfo + SECURITY_PARAMETER, nexthmac, SECURITY_PARAMETER);
|
||||||
xorbytes(packet->routinginfo, packet->routinginfo, stream, ROUTING_INFO_SIZE);
|
xorbytes(packet->routinginfo, packet->routinginfo, stream, ROUTING_INFO_SIZE);
|
||||||
|
|
||||||
@@ -420,7 +403,7 @@ struct onionpacket *create_onionpacket(
|
|||||||
|
|
||||||
compute_packet_hmac(packet, assocdata, assocdatalen, keys.mu,
|
compute_packet_hmac(packet, assocdata, assocdatalen, keys.mu,
|
||||||
nexthmac);
|
nexthmac);
|
||||||
pubkey_hash160(nextaddr, &path[i]);
|
pubkey_to_hash160(&path[i], &nextaddr.addr);
|
||||||
}
|
}
|
||||||
memcpy(packet->mac, nexthmac, sizeof(nexthmac));
|
memcpy(packet->mac, nexthmac, sizeof(nexthmac));
|
||||||
memcpy(&packet->ephemeralkey, ¶ms[0].ephemeralkey, sizeof(secp256k1_pubkey));
|
memcpy(&packet->ephemeralkey, ¶ms[0].ephemeralkey, sizeof(secp256k1_pubkey));
|
||||||
|
|||||||
@@ -133,8 +133,4 @@ struct onionpacket *parse_onionpacket(
|
|||||||
const size_t srclen
|
const size_t srclen
|
||||||
);
|
);
|
||||||
|
|
||||||
void pubkey_hash160(
|
|
||||||
u8 *dst,
|
|
||||||
const struct pubkey *pubkey);
|
|
||||||
|
|
||||||
#endif /* LIGHTNING_DAEMON_SPHINX_H */
|
#endif /* LIGHTNING_DAEMON_SPHINX_H */
|
||||||
|
|||||||
@@ -236,26 +236,6 @@ bool onion_shared_secret(
|
|||||||
privkey->secret.data);
|
privkey->secret.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pubkey_hash160(
|
|
||||||
u8 *dst,
|
|
||||||
const struct pubkey *pubkey)
|
|
||||||
{
|
|
||||||
struct ripemd160 r;
|
|
||||||
struct sha256 h;
|
|
||||||
u8 der[33];
|
|
||||||
size_t outputlen = 33;
|
|
||||||
|
|
||||||
secp256k1_ec_pubkey_serialize(secp256k1_ctx,
|
|
||||||
der,
|
|
||||||
&outputlen,
|
|
||||||
&pubkey->pubkey,
|
|
||||||
SECP256K1_EC_COMPRESSED);
|
|
||||||
sha256(&h, der, sizeof(der));
|
|
||||||
ripemd160(&r, h.u.u8, sizeof(h));
|
|
||||||
|
|
||||||
memcpy(dst, r.u.u8, sizeof(r));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void generate_key_set(const u8 secret[SHARED_SECRET_SIZE],
|
static void generate_key_set(const u8 secret[SHARED_SECRET_SIZE],
|
||||||
struct keyset *keys)
|
struct keyset *keys)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -156,10 +156,6 @@ struct onionpacket *parse_onionpacket(
|
|||||||
const size_t srclen
|
const size_t srclen
|
||||||
);
|
);
|
||||||
|
|
||||||
void pubkey_hash160(
|
|
||||||
u8 *dst,
|
|
||||||
const struct pubkey *pubkey);
|
|
||||||
|
|
||||||
struct onionreply {
|
struct onionreply {
|
||||||
/* Node index in the path that is replying */
|
/* Node index in the path that is replying */
|
||||||
int origin_index;
|
int origin_index;
|
||||||
|
|||||||
Reference in New Issue
Block a user