github.com/cosmos/cosmos-sdk@v0.50.10/fuzz/tests/crypto_hd_deriveprivatekeyforpath_test.go (about)

     1  //go:build gofuzz || go1.18
     2  
     3  package tests
     4  
     5  import (
     6  	"bytes"
     7  	"testing"
     8  
     9  	bip39 "github.com/cosmos/go-bip39"
    10  
    11  	"github.com/cosmos/cosmos-sdk/crypto/hd"
    12  )
    13  
    14  func mnemonicToSeed(mnemonic string) []byte {
    15  	return bip39.NewSeed(mnemonic, "" /* Default passphrase */)
    16  }
    17  
    18  func FuzzCryptoHDDerivePrivateKeyForPath(f *testing.F) {
    19  	f.Fuzz(func(t *testing.T, in []byte) {
    20  		splits := bytes.Split(in, []byte("*"))
    21  		if len(splits) == 1 {
    22  			return
    23  		}
    24  		mnemonic, path := splits[0], splits[1]
    25  		if len(path) > 1e5 {
    26  			// Deriving a private key takes non-trivial time proportional
    27  			// to the path length. Skip the longer ones that trigger timeouts
    28  			// on fuzzing infrastructure.
    29  			return
    30  		}
    31  		seed := mnemonicToSeed(string(mnemonic))
    32  		master, ch := hd.ComputeMastersFromSeed(seed)
    33  		hd.DerivePrivateKeyForPath(master, ch, string(path))
    34  	})
    35  }