diff --git a/include/wally_descriptor.h b/include/wally_descriptor.h index 80eca4f8a..d182700dc 100644 --- a/include/wally_descriptor.h +++ b/include/wally_descriptor.h @@ -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 '*' */ diff --git a/src/ctest/test_descriptor.c b/src/ctest/test_descriptor.c index b813fe8e3..299f29b80 100644 --- a/src/ctest/test_descriptor.c +++ b/src/ctest/test_descriptor.c @@ -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)", diff --git a/src/descriptor.c b/src/descriptor.c index 7dd70a3df..561df657d 100644 --- a/src/descriptor.c +++ b/src/descriptor.c @@ -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 @@ -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 */ @@ -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; @@ -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, diff --git a/src/wasm_package/src/const.js b/src/wasm_package/src/const.js index bd0efd6b0..902f6a7b7 100755 --- a/src/wasm_package/src/const.js +++ b/src/wasm_package/src/const.js @@ -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 '*' */