Skip to content

Commit

Permalink
MERGEBACK: descriptor: add a flag to require a checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
jgriffiths committed Feb 10, 2023
1 parent 31ad972 commit 8ad25e0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
9 changes: 5 additions & 4 deletions include/wally_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ struct wally_map;
/** An opaque type holding a parsed minscript/descriptor expression */
struct wally_descriptor;

/* Miniscript type flag */
#define WALLY_MINISCRIPT_WITNESS_SCRIPT 0x00 /** Witness script */
#define WALLY_MINISCRIPT_TAPSCRIPT 0x01 /** Tapscript */
#define WALLY_MINISCRIPT_ONLY 0x02 /** Only allow miniscript (not descriptor) expressions */
/* Flags for parsing miniscript/descriptors */
#define WALLY_MINISCRIPT_WITNESS_SCRIPT 0x00 /** Witness script */
#define WALLY_MINISCRIPT_TAPSCRIPT 0x01 /** Tapscript */
#define WALLY_MINISCRIPT_ONLY 0x02 /** Only allow miniscript (not descriptor) expressions */
#define WALLY_MINISCRIPT_REQUIRE_CHECKSUM 0x04 /** Require a checksum to be present */

#define WALLY_MS_IS_RANGED 0x01 /** Allows key ranges via '*' */

Expand Down
6 changes: 6 additions & 0 deletions src/ctest/test_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,12 @@ static const struct descriptor_test {
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, 0, NULL, 0,
NULL,
""
},{
"descriptor errchk - missing required checksum",
"wpkh(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9)",
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, 0, NULL, WALLY_MINISCRIPT_REQUIRE_CHECKSUM,
NULL,
""
},{
"descriptor errchk - upper case hardened indicator",
"pkh(xprvA2YKGLieCs6cWCiczALiH1jzk3VCCS5M1pGQfWPkamCdR9UpBgE2Gb8AKAyVjKHkz8v37avcfRjdcnP19dVAmZrvZQfvTcXXSAiFNQ6tTtU/1H/2)",
Expand Down
12 changes: 9 additions & 3 deletions src/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#define NUM_ELEMS(a) (sizeof(a) / sizeof(a[0]))
#define MS_FLAGS_ALL (WALLY_MINISCRIPT_WITNESS_SCRIPT | \
WALLY_MINISCRIPT_TAPSCRIPT | WALLY_MINISCRIPT_ONLY)
WALLY_MINISCRIPT_TAPSCRIPT | WALLY_MINISCRIPT_ONLY | \
WALLY_MINISCRIPT_REQUIRE_CHECKSUM)

/* Properties and expressions definition */
#define TYPE_NONE 0x00
Expand Down Expand Up @@ -375,7 +376,7 @@ static int canonicalize(const char *descriptor,
if (output)
*output = NULL;

if (!descriptor || flags || !output)
if (!descriptor || (flags & ~WALLY_MINISCRIPT_REQUIRE_CHECKSUM) || !output)
return WALLY_EINVAL;

/* First, find the length of the canonicalized descriptor */
Expand All @@ -400,6 +401,9 @@ static int canonicalize(const char *descriptor,
}
}

if (!*p && (flags & WALLY_MINISCRIPT_REQUIRE_CHECKSUM))
return WALLY_EINVAL; /* Checksum required but not present */

if (!(*output = wally_malloc(required_len + 1 + DESCRIPTOR_CHECKSUM_LENGTH + 1)))
return WALLY_ENOMEM;

Expand Down Expand Up @@ -2387,7 +2391,9 @@ int wally_descriptor_parse(const char *miniscript,
return WALLY_ENOMEM;
ctx = *output;
ctx->addr_ver = addr_ver;
ret = canonicalize(miniscript, vars_in, 0, &ctx->src);
ret = canonicalize(miniscript, vars_in,
flags & WALLY_MINISCRIPT_REQUIRE_CHECKSUM,
&ctx->src);
if (ret == WALLY_OK) {
ctx->src_len = strlen(ctx->src);
ret = analyze_miniscript(ctx, ctx->src, ctx->src_len, kind,
Expand Down
1 change: 1 addition & 0 deletions src/wasm_package/src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const WALLY_ERROR = -1; /** General error */
export const WALLY_HOST_COMMITMENT_LEN = 32;
export const WALLY_MAX_OP_RETURN_LEN = 80; /* Maximum length of OP_RETURN data push */
export const WALLY_MINISCRIPT_ONLY = 0x02; /** Only allow miniscript (not descriptor) expressions */
export const WALLY_MINISCRIPT_REQUIRE_CHECKSUM = 0x04; /** Require a checksum to be present */
export const WALLY_MINISCRIPT_TAPSCRIPT = 0x01; /** Tapscript */
export const WALLY_MINISCRIPT_WITNESS_SCRIPT = 0x00; /** Witness script */
export const WALLY_MS_IS_RANGED = 0x01; /** Allows key ranges via '*' */
Expand Down

0 comments on commit 8ad25e0

Please sign in to comment.