Skip to content

Commit

Permalink
add: support derive extended master key from mnemonic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Nov 9, 2024
1 parent 26bde0b commit d48c740
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ buildAllSDKIOS:

# If you want to quickly verify some features of the gomobile, you can just package a little file test.
buildIOSBase:
GOOS=ios gomobile bind -ldflags "-s -w" -v -target=ios/arm64 -o=${outdir}/${iosPackageName} ${pkgBase} ${pkgBtc}
GOOS=ios gomobile bind -ldflags "-s -w" -v -target=ios/arm64 -o=${outdir}/${iosPackageName} ${pkgBase} ${pkgBtc} ${pkgWallet}

# 使用: make packageAll v=1.4
# 结果: out 目录下将产生两个压缩包 wallet-SDK-ios.1.4.zip 和 wallet-SDK-android.1.4.zip
Expand Down
14 changes: 14 additions & 0 deletions core/wallet/mnemonic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wallet

import (
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/tyler-smith/go-bip39"
)

Expand All @@ -17,3 +19,15 @@ func IsValidMnemonic(mnemonic string) bool {
_, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
return err == nil
}

func ExtendMasterKey(mnemonic string) (string, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
if err != nil {
return "", err
}
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
return "", err
}
return masterKey.String(), nil
}
12 changes: 11 additions & 1 deletion core/wallet/mnemonic_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package wallet

import (
"testing"

"github.com/ChainSafe/go-schnorrkel"
"github.com/stretchr/testify/require"
"github.com/tyler-smith/go-bip39"
"testing"
)

// const (
Expand Down Expand Up @@ -37,3 +39,11 @@ func TestSeed(t *testing.T) {
t.Log(seed)
t.Log(fromMnemonic)
}

func TestExtendMasterKey(t *testing.T) {
mn := "unaware oxygen allow method allow property predict various slice travel please priority"
extendedMasterKey, err := ExtendMasterKey(mn)
require.NoError(t, err)
want := "xprv9s21ZrQH143K2gM869SUAeSEGDM81UnyWG2tM7ZygYwN3PkveZmY799G6q7zkQYSQCMn7M7AMpP8Z6etKUoZo8x4hH1WVcKBCMZnVTNM8AD"
require.Equal(t, want, extendedMasterKey)
}

0 comments on commit d48c740

Please sign in to comment.