|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "math" |
| 9 | + "runtime" |
| 10 | + "strconv" |
| 11 | + "sync" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/guggero/chantools/btc/fasthd" |
| 15 | + "github.com/guggero/chantools/lnd" |
| 16 | + "github.com/lightningnetwork/lnd/aezeed" |
| 17 | + "github.com/lightningnetwork/lnd/keychain" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + nodeKeyDerivationPath = "m/1017'/%d'/%d'/0/0" |
| 22 | +) |
| 23 | + |
| 24 | +type vanityGenCommand struct { |
| 25 | + Prefix string `long:"prefix" description:"Hex encoded prefix to find in node public key."` |
| 26 | + Threads int `long:"threads" description:"Number of parallel threads." default:"4"` |
| 27 | +} |
| 28 | + |
| 29 | +func (c *vanityGenCommand) Execute(_ []string) error { |
| 30 | + setupChainParams(cfg) |
| 31 | + |
| 32 | + prefixBytes, err := hex.DecodeString(c.Prefix) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("hex decoding of prefix failed: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + if len(prefixBytes) < 2 { |
| 38 | + return fmt.Errorf("prefix must be at least 2 bytes") |
| 39 | + } |
| 40 | + if !(prefixBytes[0] == 0x02 || prefixBytes[0] == 0x03) { |
| 41 | + return fmt.Errorf("prefix must start with 02 or 03 because " + |
| 42 | + "it's an EC public key") |
| 43 | + } |
| 44 | + |
| 45 | + path, err := lnd.ParsePath(fmt.Sprintf( |
| 46 | + nodeKeyDerivationPath, chainParams.HDCoinType, |
| 47 | + keychain.KeyFamilyNodeKey, |
| 48 | + )) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + numBits := ((len(prefixBytes) - 1) * 8) + 1 |
| 54 | + numTries := math.Pow(2, float64(numBits)) |
| 55 | + fmt.Printf("Running vanitygen on %d threads. Prefix bit length is %d, "+ |
| 56 | + "expecting to approach\nprobability p=1.0 after %s seeds.\n", |
| 57 | + c.Threads, numBits, format(int64(numTries))) |
| 58 | + runtime.GOMAXPROCS(c.Threads) |
| 59 | + var ( |
| 60 | + mtx sync.Mutex |
| 61 | + globalCount uint64 |
| 62 | + abort = make(chan struct{}) |
| 63 | + start = time.Now() |
| 64 | + ) |
| 65 | + |
| 66 | + for i := 0; i < c.Threads; i++ { |
| 67 | + go func() { |
| 68 | + var ( |
| 69 | + entropy [16]byte |
| 70 | + count uint64 |
| 71 | + ) |
| 72 | + for { |
| 73 | + select { |
| 74 | + case <-abort: |
| 75 | + return |
| 76 | + default: |
| 77 | + } |
| 78 | + |
| 79 | + if _, err := rand.Read(entropy[:]); err != nil { |
| 80 | + log.Error(err) |
| 81 | + } |
| 82 | + rootKey, err := fasthd.NewFastDerivation( |
| 83 | + entropy[:], chainParams, |
| 84 | + ) |
| 85 | + if err != nil { |
| 86 | + log.Error(err) |
| 87 | + } |
| 88 | + err = rootKey.ChildPath(path) |
| 89 | + if err != nil { |
| 90 | + log.Error(err) |
| 91 | + } |
| 92 | + pubKeyBytes := rootKey.PubKeyBytes() |
| 93 | + |
| 94 | + if bytes.HasPrefix( |
| 95 | + pubKeyBytes, prefixBytes, |
| 96 | + ) { |
| 97 | + seed, err := aezeed.New( |
| 98 | + aezeed.CipherSeedVersion, |
| 99 | + &entropy, time.Now(), |
| 100 | + ) |
| 101 | + if err != nil { |
| 102 | + log.Error(err) |
| 103 | + } |
| 104 | + mnemonic, err := seed.ToMnemonic(nil) |
| 105 | + if err != nil { |
| 106 | + log.Error(err) |
| 107 | + } |
| 108 | + fmt.Printf("\nLooking for %x, found "+ |
| 109 | + "pubkey: %x\nwith seed: %v\n", |
| 110 | + prefixBytes, pubKeyBytes, |
| 111 | + mnemonic) |
| 112 | + |
| 113 | + close(abort) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + if count > 0 && count%100 == 0 { |
| 118 | + mtx.Lock() |
| 119 | + globalCount += count |
| 120 | + count = 0 |
| 121 | + mtx.Unlock() |
| 122 | + } |
| 123 | + count++ |
| 124 | + } |
| 125 | + }() |
| 126 | + } |
| 127 | + |
| 128 | + lastCount := uint64(0) |
| 129 | + for { |
| 130 | + select { |
| 131 | + case <-abort: |
| 132 | + return nil |
| 133 | + case <-time.After(1 * time.Second): |
| 134 | + mtx.Lock() |
| 135 | + currentCount := globalCount |
| 136 | + mtx.Unlock() |
| 137 | + |
| 138 | + msg := fmt.Sprintf("Tested %sk seeds, p=%.5f, "+ |
| 139 | + "speed=%dk/s, elapsed=%v", |
| 140 | + format(int64(currentCount/1000)), |
| 141 | + float64(currentCount)/numTries, |
| 142 | + (currentCount-lastCount)/1000, |
| 143 | + time.Since(start).Truncate(time.Second), |
| 144 | + ) |
| 145 | + fmt.Printf("\r%-80s", msg) |
| 146 | + |
| 147 | + lastCount = currentCount |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func format(n int64) string { |
| 153 | + in := strconv.FormatInt(n, 10) |
| 154 | + numOfDigits := len(in) |
| 155 | + if n < 0 { |
| 156 | + numOfDigits-- // First character is the - sign (not a digit) |
| 157 | + } |
| 158 | + numOfCommas := (numOfDigits - 1) / 3 |
| 159 | + |
| 160 | + out := make([]byte, len(in)+numOfCommas) |
| 161 | + if n < 0 { |
| 162 | + in, out[0] = in[1:], '-' |
| 163 | + } |
| 164 | + |
| 165 | + for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 { |
| 166 | + out[j] = in[i] |
| 167 | + if i == 0 { |
| 168 | + return string(out) |
| 169 | + } |
| 170 | + if k++; k == 3 { |
| 171 | + j, k = j-1, 0 |
| 172 | + out[j] = ',' |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments