@@ -463,17 +463,20 @@ int wally_map_find_bip32_public_key_from(const struct wally_map *map_in, size_t
463463 return ret ;
464464}
465465
466- int wally_map_keypath_get_bip32_key_from_alloc (const struct wally_map * map_in ,
467- size_t index , const struct ext_key * hdkey ,
468- struct ext_key * * output )
466+ int wally_map_keypath_get_bip32_key_from (const struct wally_map * map_in ,
467+ size_t index , const struct ext_key * hdkey ,
468+ struct ext_key * output , size_t * written )
469469{
470470 uint32_t path [BIP32_PATH_MAX_LEN ];
471471 struct ext_key derived ;
472472 size_t i , path_len , idx = 0 ;
473473 int ret = WALLY_OK ;
474474
475- OUTPUT_CHECK ;
476- if (!map_in || !hdkey )
475+ if (written )
476+ * written = 0 ;
477+ if (output )
478+ memset (output , 0 , sizeof (* output ));
479+ if (!map_in || !hdkey || !written || !output )
477480 return WALLY_EINVAL ;
478481
479482 if (mem_is_zero (hdkey -> chain_code , sizeof (hdkey -> chain_code ))) {
@@ -516,14 +519,33 @@ int wally_map_keypath_get_bip32_key_from_alloc(const struct wally_map *map_in,
516519 }
517520 }
518521 if (ret == WALLY_OK && idx ) {
519- /* Found, return the matching key */
520- * output = wally_calloc (sizeof (struct ext_key ));
521- if (!* output )
522+ /* Found, return the matching key and its 1-based index */
523+ * written = idx ;
524+ memcpy (output , hdkey , sizeof (* hdkey ));
525+ }
526+ wally_clear (& derived , sizeof (derived ));
527+ return ret ;
528+ }
529+
530+
531+ int wally_map_keypath_get_bip32_key_from_alloc (const struct wally_map * map_in ,
532+ size_t index , const struct ext_key * hdkey ,
533+ struct ext_key * * output )
534+ {
535+ struct ext_key found ;
536+ size_t found_index ;
537+ int ret ;
538+
539+ OUTPUT_CHECK ;
540+ ret = wally_map_keypath_get_bip32_key_from (map_in , index , hdkey ,
541+ & found , & found_index );
542+ if (ret == WALLY_OK && found_index ) {
543+ if (!(* output = wally_calloc (sizeof (struct ext_key ))))
522544 ret = WALLY_ENOMEM ;
523545 else
524- memcpy (* output , hdkey , sizeof (* hdkey ));
546+ memcpy (* output , & found , sizeof (found ));
525547 }
526- wally_clear (& derived , sizeof (derived ));
548+ wally_clear (& found , sizeof (found ));
527549 return ret ;
528550}
529551
0 commit comments