Skip to content

Commit

Permalink
Document --count flag. Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkinj1 committed Jan 3, 2020
1 parent 90d561a commit e298fa3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
## Features
* Generate Cosmos bech32 vanity addresses
* Use all CPU cores
* Specify a substring that the address must
* Specify a substring that the addresses must
* start with
* end with
* contain
* Set required minimum amount of letters (a-z) or digits (0-9) in the address
* Set required minimum amount of letters (a-z) or digits (0-9) in the addresses
* Binaries built for Linux, macOS and Windows

## Installing
Expand Down Expand Up @@ -47,6 +47,11 @@ Find an address with at least 26 digits (e.g. cosmos1r573c4086585u084926726x535y
./cosmosvanity --digits 26
```

Generate 5 addresses (the default is 1)
```bash
./cosmosvanity -n 5
```

Combine flags introduced above
```bash
./cosmosvanity --contains 8888 --startswith a --endswith c
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.5.0/go.mod h1:AkYRkVJF8TkSG/xet6PzXX+l39KhhXa2pdqVSxnTcn4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
18 changes: 4 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,21 @@ func generateWallet() wallet {
return wallet{bech32Addr, pubkey, privkey}
}

// TODO: this function is now unused. Remove it and adapt its test to do something useful
func findMatchingWallet(m matcher) wallet {
for {
w := generateWallet()
if m.Match(w.Address) {
return w
}
}
}

func findMatchingWallets(ch chan wallet, quit chan struct{}, m matcher) {
for {
select {
case <-quit:
return
default:
w := generateWallet()
if m.Match(w.Address) {
ch <- w
}
case <-quit:
return
}
}
}

func findMatchingWalletConcurrently(m matcher, goroutines int) wallet {
func findMatchingWalletConcurrent(m matcher, goroutines int) wallet {
ch := make(chan wallet)
quit := make(chan struct{})
defer close(quit)
Expand Down Expand Up @@ -168,7 +158,7 @@ func main() {

var matchingWallet wallet
for i := 0; i < *walletsToFind; i++ {
matchingWallet = findMatchingWalletConcurrently(m, runtime.NumCPU())
matchingWallet = findMatchingWalletConcurrent(m, runtime.NumCPU())
fmt.Printf(":::: Matching wallet %d/%d found ::::\n", i+1, *walletsToFind)
fmt.Println(matchingWallet)
}
Expand Down
39 changes: 31 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,40 @@ func TestGenerateWallet(t *testing.T) {
require.Equal(t, len(w.Privkey), 32, "Incorrect privkey length")
}

func TestFindMatchingWallet(t *testing.T) {
firstChars := "aa"
m := matcher{StartsWith: firstChars}
w := findMatchingWallet(m)
require.Equal(t, w.Address[:7+len(firstChars)], "cosmos1"+firstChars, "Incorrect address prefix")
func TestStartsWith(t *testing.T) {
m := matcher{StartsWith: "aaaa"}
require.True(t, m.Match("cosmos1aaaaqztg6eu45nlljp0wp947juded46aln83kr"))
require.False(t, m.Match("cosmos1aaa9qztg6eu45nlljp0wp947juded46aln83kr"))
}

func TestFindMatchingWalletConcurrently(t *testing.T) {
goroutineCount := 4
func TestEndsWith(t *testing.T) {
m := matcher{EndsWith: "8888"}
require.True(t, m.Match("cosmos14sy657pp6tgclhgqnl3dkwzwu3ewt4cf3f8888"))
require.False(t, m.Match("cosmos14sy657pp6tgclhgqnl3dkwzwu3ewt4cf3ff888"))
}

func TestContains(t *testing.T) {
m := matcher{Contains: "k2k2k"}
require.True(t, m.Match("cosmos1s6rlmknaj3swdd7hua6s852sk2k2k409a3z9f9"))
require.False(t, m.Match("cosmos14sy657pp6tgclhgqnl3dkwzwu3ewt4cf3ff888"))
}

func TestLetters(t *testing.T) {
m := matcher{Letters: 38}
require.True(t, m.Match("cosmos1gcjsgsglhacarlumkjzywedykkvkuvrzqlnlxd"))
require.False(t, m.Match("cosmos1gcjsgsglhacarlumkjzywedykkvkuvrzqlnlx8"))
}

func TestDigits(t *testing.T) {
m := matcher{Digits: 26}
require.True(t, m.Match("cosmos1j666m3qz66t786s48t540536465p56zrve5893"))
require.False(t, m.Match("cosmos1j666m3qz66t786s48t540536465p56zrve589z"))
}

func TestFindMatchingWalletConcurrent(t *testing.T) {
goroutineCount := 5
lastChars := "zz"
m := matcher{EndsWith: lastChars}
w := findMatchingWalletConcurrently(m, goroutineCount)
w := findMatchingWalletConcurrent(m, goroutineCount)
require.Equal(t, w.Address[len(w.Address)-len(lastChars):], lastChars, "Incorrect address suffix")
}

0 comments on commit e298fa3

Please sign in to comment.