@@ -206,7 +206,7 @@ static int ctx_add_key_node(ms_ctx *ctx, ms_node *node)
206206{
207207 const char * v = (char * )node ;
208208 return map_add (& ctx -> keys , NULL , ctx -> keys .num_items ,
209- (unsigned char * )v , 1 , false , false);
209+ (unsigned char * )v , 1 , true , false);
210210}
211211
212212/* Built-in miniscript expressions */
@@ -2840,17 +2840,6 @@ int wally_descriptor_get_features(const struct wally_descriptor *descriptor,
28402840 offsetof(struct wally_descriptor , features ));
28412841}
28422842
2843- int wally_descriptor_get_num_keys (const struct wally_descriptor * descriptor ,
2844- uint32_t * value_out )
2845- {
2846- if (value_out )
2847- * value_out = 0 ;
2848- if (!descriptor || !value_out )
2849- return WALLY_EINVAL ;
2850- * value_out = (uint32_t )descriptor -> keys .num_items ;
2851- return WALLY_OK ;
2852- }
2853-
28542843int wally_descriptor_get_num_variants (const struct wally_descriptor * descriptor ,
28552844 uint32_t * value_out )
28562845{
@@ -2887,3 +2876,50 @@ int wally_descriptor_get_depth(const struct wally_descriptor *descriptor,
28872876 * value_out = node_get_depth (descriptor -> top_node ) - 1 ;
28882877 return WALLY_OK ;
28892878}
2879+
2880+ int wally_descriptor_get_num_keys (const struct wally_descriptor * descriptor ,
2881+ uint32_t * value_out )
2882+ {
2883+ if (value_out )
2884+ * value_out = 0 ;
2885+ if (!descriptor || !value_out )
2886+ return WALLY_EINVAL ;
2887+ * value_out = (uint32_t )descriptor -> keys .num_items ;
2888+ return WALLY_OK ;
2889+ }
2890+
2891+ /* Ignore incorrect warnings from the ms_node cast below */
2892+ #pragma GCC diagnostic ignored "-Wcast-align"
2893+ #if defined(__clang__ )
2894+ #pragma clang diagnostic ignored "-Wcast-align"
2895+ #endif
2896+
2897+ int wally_descriptor_get_key (const struct wally_descriptor * descriptor ,
2898+ size_t index , char * * output )
2899+ {
2900+ const ms_node * node ;
2901+
2902+ if (output )
2903+ * output = 0 ;
2904+ if (!descriptor || index >= descriptor -> keys .num_items || !output )
2905+ return WALLY_EINVAL ;
2906+
2907+ node = (ms_node * )descriptor -> keys .items [index ].value ;
2908+ if (node -> kind == KIND_PUBLIC_KEY ) {
2909+ return wally_hex_from_bytes ((const unsigned char * )node -> data ,
2910+ node -> data_len , output );
2911+ }
2912+ if (node -> kind == KIND_PRIVATE_KEY ) {
2913+ uint32_t flags = node -> flags & NF_IS_UNCOMPRESSED ? WALLY_WIF_FLAG_UNCOMPRESSED : 0 ;
2914+ if (!descriptor -> addr_ver )
2915+ return WALLY_EINVAL ; /* Must have a network to fetch private keys */
2916+ return wally_wif_from_bytes ((const unsigned char * )node -> data , node -> data_len ,
2917+ descriptor -> addr_ver -> version_wif ,
2918+ flags , output );
2919+ }
2920+ if ((node -> kind & KIND_BIP32 ) != KIND_BIP32 )
2921+ return WALLY_ERROR ; /* Unknown key type, should not happen */
2922+ if (!(* output = wally_strdup_n (node -> data , node -> data_len )))
2923+ return WALLY_ENOMEM ;
2924+ return WALLY_OK ;
2925+ }
0 commit comments