github.com/turingchain2020/turingchain@v1.1.21/wallet/seedtool/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"log"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/turingchain2020/turingchain/common/address"
    10  
    11  	"github.com/turingchain2020/turingchain/types"
    12  	"github.com/turingchain2020/turingchain/wallet"
    13  	"github.com/turingchain2020/turingchain/wallet/bipwallet"
    14  
    15  	"net/http"
    16  	_ "net/http/pprof"
    17  )
    18  
    19  var seed = flag.String("seed", "", "source seed")
    20  var targetaddr = flag.String("addr", "", "address of target")
    21  var lang = flag.Int("lang", 1, "lang: 0 englist, 1 chinese")
    22  var oldseed = flag.Bool("oldseed", false, "is seed old")
    23  var accountnum = flag.Int("nacc", 5, "gen account count")
    24  
    25  func main() {
    26  	flag.Parse()
    27  	wallet.InitSeedLibrary()
    28  	log.Println("seed", *seed)
    29  	log.Println("target", *targetaddr)
    30  	go http.ListenAndServe("localhost:6060", nil)
    31  	seedlist := strings.Split(*seed, " ")
    32  	//第一种情况,用户写错一个字
    33  	n := 0
    34  	wordlist := wallet.ChineseSeedCache
    35  	if *lang == 0 {
    36  		wordlist = wallet.EnglishSeedCache
    37  	}
    38  	checkseed(*seed)
    39  	for k := range wordlist {
    40  		log.Println("change ", k, int(100*(float64(n)/float64(2048))))
    41  		n++
    42  		var seeds []string
    43  		for i := 0; i < len(seedlist); i++ {
    44  			item := seedlist[i]
    45  			seedlist[i] = k
    46  			newseed := strings.Join(seedlist, " ")
    47  			seeds = append(seeds, newseed)
    48  			seedlist[i] = item
    49  		}
    50  		checkmultithread(seeds)
    51  	}
    52  	log.Println("not found")
    53  }
    54  
    55  func checkmultithread(seeds []string) {
    56  	done := make(chan struct{}, len(seeds))
    57  	for i := 0; i < len(seeds); i++ {
    58  		go func(seed string) {
    59  			checkseed(seed)
    60  			done <- struct{}{}
    61  		}(seeds[i])
    62  	}
    63  	for i := 0; i < len(seeds); i++ {
    64  		<-done
    65  	}
    66  }
    67  
    68  func checkseed(newseed string) {
    69  	addrlist, err := genaddrlist(newseed)
    70  	if err != nil {
    71  		return
    72  	}
    73  	if _, ok := addrlist[*targetaddr]; ok {
    74  		log.Println("find new seed", newseed)
    75  		os.Exit(0)
    76  	}
    77  }
    78  func genaddrlist(seed string) (map[string]bool, error) {
    79  	var wallet *bipwallet.HDWallet
    80  	var err error
    81  	if *oldseed {
    82  		wallet, err = bipwallet.NewWalletFromSeed(bipwallet.TypeTrc, uint32(types.SECP256K1), []byte(seed))
    83  		if err != nil {
    84  			log.Println("GetPrivkeyBySeed NewWalletFromSeed", "err", err)
    85  			return nil, types.ErrNewWalletFromSeed
    86  		}
    87  	} else {
    88  		wallet, err = bipwallet.NewWalletFromMnemonic(bipwallet.TypeTrc, uint32(types.SECP256K1), seed)
    89  		if err != nil {
    90  			//log.Println("GetPrivkeyBySeed NewWalletFromMnemonic", "err", err)
    91  			wallet, err = bipwallet.NewWalletFromSeed(bipwallet.TypeTrc, uint32(types.SECP256K1), []byte(seed))
    92  			if err != nil {
    93  				log.Println("GetPrivkeyBySeed NewWalletFromSeed", "err", err)
    94  				return nil, types.ErrNewWalletFromSeed
    95  			}
    96  		}
    97  	}
    98  	addrlist := make(map[string]bool)
    99  	for index := 0; index <= *accountnum; index++ {
   100  		//通过索引生成Key pair
   101  		_, pub, err := childkey(wallet, uint32(index))
   102  		if err != nil {
   103  			log.Println("GetPrivkeyBySeed NewKeyPair", "err", err)
   104  			return nil, types.ErrNewKeyPair
   105  		}
   106  		addr := address.PubKeyToAddress(pub)
   107  		addrlist[addr.String()] = true
   108  	}
   109  	return addrlist, err
   110  }
   111  
   112  func childkey(w *bipwallet.HDWallet, index uint32) (priv, pub []byte, err error) {
   113  	if *oldseed {
   114  		key, err := w.MasterKey.NewChildKey(index)
   115  		if err != nil {
   116  			return nil, nil, err
   117  		}
   118  		return key.Key, key.PublicKey().Key, err
   119  	}
   120  	return w.NewKeyPair(index)
   121  }