mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-03 20:44:54 +01:00
7283efa5b5
Currently the only source for amount_asset is the value getter on a tx output, and we don't hand it too far around (mainly ignoring it if it isn't the chain's main currency). Eventually we could bubble them up to the wallet, use them to select outputs or actually support assets in the channels. Since we don't hand them around too widely I thought it was ok for them to be pass-by-value rather than having to allocate them and pass them around by reference. They're just 41 bytes currently so the overhead should be ok. Signed-off-by: Christian Decker <@cdecker>
211 lines
7.7 KiB
C
211 lines
7.7 KiB
C
#include "../derive_basepoints.c"
|
|
#include <ccan/err/err.h>
|
|
#include <ccan/mem/mem.h>
|
|
#include <ccan/str/hex/hex.h>
|
|
#include <ccan/structeq/structeq.h>
|
|
#include <common/utils.h>
|
|
#include <stdio.h>
|
|
#include <wally_core.h>
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
|
/* Generated stub for amount_asset_is_main */
|
|
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
|
|
{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); }
|
|
/* Generated stub for amount_asset_to_sat */
|
|
struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED)
|
|
{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); }
|
|
/* Generated stub for amount_sat_add */
|
|
bool amount_sat_add(struct amount_sat *val UNNEEDED,
|
|
struct amount_sat a UNNEEDED,
|
|
struct amount_sat b UNNEEDED)
|
|
{ fprintf(stderr, "amount_sat_add called!\n"); abort(); }
|
|
/* Generated stub for amount_sat_eq */
|
|
bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
|
|
{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
|
|
/* Generated stub for amount_sat_sub */
|
|
bool amount_sat_sub(struct amount_sat *val UNNEEDED,
|
|
struct amount_sat a UNNEEDED,
|
|
struct amount_sat b UNNEEDED)
|
|
{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); }
|
|
/* Generated stub for fromwire_fail */
|
|
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
|
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
|
|
/* Generated stub for fromwire_privkey */
|
|
void fromwire_privkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct privkey *privkey UNNEEDED)
|
|
{ fprintf(stderr, "fromwire_privkey called!\n"); abort(); }
|
|
/* Generated stub for fromwire_pubkey */
|
|
void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED)
|
|
{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); }
|
|
/* Generated stub for fromwire_secret */
|
|
void fromwire_secret(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct secret *secret UNNEEDED)
|
|
{ fprintf(stderr, "fromwire_secret called!\n"); abort(); }
|
|
/* Generated stub for towire_privkey */
|
|
void towire_privkey(u8 **pptr UNNEEDED, const struct privkey *privkey UNNEEDED)
|
|
{ fprintf(stderr, "towire_privkey called!\n"); abort(); }
|
|
/* Generated stub for towire_pubkey */
|
|
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
|
|
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
|
|
/* Generated stub for towire_secret */
|
|
void towire_secret(u8 **pptr UNNEEDED, const struct secret *secret UNNEEDED)
|
|
{ fprintf(stderr, "towire_secret called!\n"); abort(); }
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
STRUCTEQ_DEF(basepoints, 0,
|
|
revocation.pubkey,
|
|
payment.pubkey,
|
|
htlc.pubkey,
|
|
delayed_payment.pubkey);
|
|
STRUCTEQ_DEF(secrets, 0,
|
|
funding_privkey.secret.data,
|
|
revocation_basepoint_secret.data,
|
|
payment_basepoint_secret.data,
|
|
htlc_basepoint_secret.data,
|
|
delayed_payment_basepoint_secret.data);
|
|
STRUCTEQ_DEF(privkey, 0,
|
|
secret.data);
|
|
|
|
struct info {
|
|
struct secret seed;
|
|
struct pubkey funding_pubkey;
|
|
struct basepoints basepoints;
|
|
struct secrets secrets;
|
|
struct sha256 shaseed;
|
|
};
|
|
|
|
/* We get a fresh one each time, to catch uninitialized fields */
|
|
static struct info *new_info(const tal_t *ctx)
|
|
{
|
|
struct info *info = tal(ctx, struct info);
|
|
memset(&info->seed, 7, sizeof(info->seed));
|
|
|
|
return info;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
setup_locale();
|
|
|
|
const tal_t *ctx = tal(NULL, char);
|
|
struct info *baseline, *info;
|
|
|
|
wally_init(0);
|
|
secp256k1_ctx = wally_get_secp_context();
|
|
baseline = new_info(ctx);
|
|
assert(derive_basepoints(&baseline->seed, &baseline->funding_pubkey,
|
|
&baseline->basepoints,
|
|
&baseline->secrets,
|
|
&baseline->shaseed));
|
|
|
|
/* Same seed, same result. */
|
|
info = new_info(ctx);
|
|
assert(derive_basepoints(&info->seed, &info->funding_pubkey,
|
|
&info->basepoints,
|
|
&info->secrets,
|
|
&info->shaseed));
|
|
assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey));
|
|
assert(basepoints_eq(&baseline->basepoints, &info->basepoints));
|
|
assert(secrets_eq(&baseline->secrets, &info->secrets));
|
|
assert(sha256_eq(&baseline->shaseed, &info->shaseed));
|
|
|
|
/* Different seed, different result. */
|
|
for (size_t i = 0; i < sizeof(info->seed); i++) {
|
|
for (size_t b = 0; b < CHAR_BIT; b++) {
|
|
info = new_info(ctx);
|
|
info->seed.data[i] ^= (1 << b);
|
|
|
|
assert(derive_basepoints(&info->seed,
|
|
&info->funding_pubkey,
|
|
&info->basepoints,
|
|
&info->secrets,
|
|
&info->shaseed));
|
|
assert(!pubkey_eq(&baseline->funding_pubkey,
|
|
&info->funding_pubkey));
|
|
assert(!basepoints_eq(&baseline->basepoints,
|
|
&info->basepoints));
|
|
assert(!secrets_eq(&baseline->secrets, &info->secrets));
|
|
assert(!sha256_eq(&baseline->shaseed, &info->shaseed));
|
|
}
|
|
}
|
|
|
|
/* Any field can be NULL (except seed). */
|
|
info = new_info(ctx);
|
|
assert(derive_basepoints(&info->seed, NULL,
|
|
&info->basepoints,
|
|
&info->secrets,
|
|
&info->shaseed));
|
|
assert(basepoints_eq(&baseline->basepoints, &info->basepoints));
|
|
assert(secrets_eq(&baseline->secrets, &info->secrets));
|
|
assert(sha256_eq(&baseline->shaseed, &info->shaseed));
|
|
|
|
info = new_info(ctx);
|
|
assert(derive_basepoints(&info->seed, &info->funding_pubkey,
|
|
NULL,
|
|
&info->secrets,
|
|
&info->shaseed));
|
|
assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey));
|
|
assert(secrets_eq(&baseline->secrets, &info->secrets));
|
|
assert(sha256_eq(&baseline->shaseed, &info->shaseed));
|
|
|
|
info = new_info(ctx);
|
|
assert(derive_basepoints(&info->seed, &info->funding_pubkey,
|
|
&info->basepoints,
|
|
NULL,
|
|
&info->shaseed));
|
|
assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey));
|
|
assert(basepoints_eq(&baseline->basepoints, &info->basepoints));
|
|
assert(sha256_eq(&baseline->shaseed, &info->shaseed));
|
|
|
|
info = new_info(ctx);
|
|
assert(derive_basepoints(&info->seed, &info->funding_pubkey,
|
|
&info->basepoints,
|
|
&info->secrets,
|
|
NULL));
|
|
assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey));
|
|
assert(basepoints_eq(&baseline->basepoints, &info->basepoints));
|
|
assert(secrets_eq(&baseline->secrets, &info->secrets));
|
|
|
|
/* derive_payment_basepoint should give same results. */
|
|
info = new_info(ctx);
|
|
assert(derive_payment_basepoint(&info->seed, &info->basepoints.payment,
|
|
&info->secrets.payment_basepoint_secret));
|
|
assert(pubkey_eq(&baseline->basepoints.payment,
|
|
&info->basepoints.payment));
|
|
assert(secret_eq_consttime(&baseline->secrets.payment_basepoint_secret,
|
|
&info->secrets.payment_basepoint_secret));
|
|
|
|
/* derive_funding_key should give same results. */
|
|
info = new_info(ctx);
|
|
assert(derive_funding_key(&info->seed, &info->funding_pubkey,
|
|
&info->secrets.funding_privkey));
|
|
assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey));
|
|
assert(privkey_eq(&baseline->secrets.funding_privkey,
|
|
&info->secrets.funding_privkey));
|
|
|
|
/* derive_shaseed should give same results. */
|
|
info = new_info(ctx);
|
|
assert(derive_shaseed(&info->seed, &info->shaseed));
|
|
assert(sha256_eq(&baseline->shaseed, &info->shaseed));
|
|
|
|
/* derive_revocation_basepoint should give same results. */
|
|
info = new_info(ctx);
|
|
assert(derive_revocation_basepoint(&info->seed, &info->basepoints.revocation,
|
|
&info->secrets.revocation_basepoint_secret));
|
|
assert(pubkey_eq(&baseline->basepoints.revocation,
|
|
&info->basepoints.revocation));
|
|
assert(secret_eq_consttime(&baseline->secrets.revocation_basepoint_secret,
|
|
&info->secrets.revocation_basepoint_secret));
|
|
|
|
/* derive_htlc_basepoint should give same results. */
|
|
info = new_info(ctx);
|
|
assert(derive_htlc_basepoint(&info->seed, &info->basepoints.htlc,
|
|
&info->secrets.htlc_basepoint_secret));
|
|
assert(pubkey_eq(&baseline->basepoints.htlc,
|
|
&info->basepoints.htlc));
|
|
assert(secret_eq_consttime(&baseline->secrets.htlc_basepoint_secret,
|
|
&info->secrets.htlc_basepoint_secret));
|
|
|
|
tal_free(ctx);
|
|
wally_cleanup(0);
|
|
return 0;
|
|
}
|