Skip to content

Commit

Permalink
feat(core/btc): add btc account derviate
Browse files Browse the repository at this point in the history
  • Loading branch information
Gkirito committed Apr 15, 2024
1 parent 9bb9214 commit 2835c18
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions core/btc/account_derivation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package btc

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

func Derivation(mnemonic string, path string) (*btcec.PrivateKey, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
if err != nil {
return nil, err
}
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
return nil, err
}
dPath, err := accounts.ParseDerivationPath(path)
if err != nil {
return nil, err
}
key := masterKey
for _, n := range dPath {
key, err = key.Derive(n)
if err != nil {
return nil, err
}
}
return key.ECPrivKey()
}

0 comments on commit 2835c18

Please sign in to comment.