Mnemonic-Seed

在 libwally-core 中呼叫 bip39_mnemonic_from_bytes 返回 NULL

  • August 10, 2018

我究竟做錯了什麼?它總是返回錯誤-2…我將libwallycore包含到我的項目中,我的IDE告訴我它在我也這樣做之後可用include <wally_bib39.h>

我嘗試自己維護一個單詞表,但後來我遇到了更多錯誤,所以我想我錯過了 API。有人可以幫忙嗎?

static void display_mnemonic_word_list(void){
   char **mnemonic_secret=NULL;
   //wordlist_init(bip39_wordlist)
   const unsigned char myArray[] = { 0x00, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22,  0x33 };
   int ret = bip39_mnemonic_from_bytes(NULL,myArray,32,mnemonic_secret);
   if (ret!=WALLY_OK){
       printf("mnemonic did not work. error: %d",ret);
   }
   printf("HSM: your should remember / write down the following words do recover your funds!");
   if (mnemonic_secret){
       int i = 0;
       while ( *mnemonic_secret )
           printf( "%d. %s", ++i, *mnemonic_secret++ );
   }
}

有一些問題,您錯誤地傳遞了 mnemonic_secret 導致 API 呼叫獲得 NULL 指針,因此返回 WALLY_EINVAL。這是一個更正的版本,但請注意您的 myArray 仍然是錯誤的大小:

#include <stdio.h>
#include <wally_bip39.h>

int main()
{
   /* Note that this array is 34 elements long which seems incorrect, it should be 32 */
   const unsigned char myArray[] = { 0x00, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22, 0x11, 0x22,  0x33 };
   char *mnemonic_secret;

   int ret = bip39_mnemonic_from_bytes(0, myArray, 32, &mnemonic_secret);
   if (ret != WALLY_OK) {
       printf("mnemonic did not work. error: %d",ret);
       return 1;
   }
   printf("HSM: your should remember / write down the following words do recover your funds!\n");
   printf("%s", mnemonic_secret);
   wally_free_string(mnemonic_secret);
   return 0;
}

通過在https://github.com/elementsproject/libwally-core在他們的 github 上提問,您可以更快地回答 Wally 的問題。

引用自:https://bitcoin.stackexchange.com/questions/77665