diff --git a/.gitignore b/.gitignore index 201e3fe..c594794 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,9 @@ *.so *.dylib +# IDE folders & files +.idea + # Test binary, built with `go test -c` *.test diff --git a/main.go b/main.go index 1bc61a2..6f077b0 100644 --- a/main.go +++ b/main.go @@ -17,12 +17,16 @@ type matcher struct { StartsWith string EndsWith string Contains string + Prefix string Letters int Digits int } func (m matcher) Match(candidate string) bool { - candidate = strings.TrimPrefix(candidate, "cosmos1") + var builder strings.Builder + builder.WriteString(m.Prefix) + builder.WriteString("1") // build prefix string + candidate = strings.TrimPrefix(candidate, builder.String()) if !strings.HasPrefix(candidate, m.StartsWith) { return false } @@ -70,10 +74,10 @@ func (w wallet) String() string { "Private key:\t" + hex.EncodeToString(w.Privkey) } -func generateWallet() wallet { +func generateWallet(prefix string) wallet { var privkey secp256k1.PrivKey = secp256k1.GenPrivKey() var pubkey secp256k1.PubKey = privkey.PubKey().(secp256k1.PubKey) - bech32Addr, err := bech32.ConvertAndEncode("cosmos", pubkey.Address()) + bech32Addr, err := bech32.ConvertAndEncode(prefix, pubkey.Address()) if err != nil { panic(err) } @@ -87,7 +91,7 @@ func findMatchingWallets(ch chan wallet, quit chan struct{}, m matcher) { case <-quit: return default: - w := generateWallet() + w := generateWallet(m.Prefix) if m.Match(w.Address) { // Do a non-blocking write instead of simple `ch <- w` to prevent // blocking when it's time to quit and ch is full. @@ -140,6 +144,8 @@ func main() { var mustEndWith = flag.StringP("endswith", "e", "", "A string that the address must end with") var letters = flag.IntP("letters", "l", 0, "Amount of letters (a-z) that the address must contain") var digits = flag.IntP("digits", "d", 0, "Amount of digits (0-9) that the address must contain") + var bechPrefix = flag.StringP("prefix", "p", "cosmos", "The bech32 prefix the address should have") + flag.Parse() if *walletsToFind < 1 { @@ -155,6 +161,7 @@ func main() { StartsWith: strings.ToLower(*mustStartWith), EndsWith: strings.ToLower(*mustEndWith), Contains: strings.ToLower(*mustContain), + Prefix: strings.ToLower(*bechPrefix), Letters: *letters, Digits: *digits, } diff --git a/main_test.go b/main_test.go index e060837..d524f33 100644 --- a/main_test.go +++ b/main_test.go @@ -7,13 +7,21 @@ import ( ) func TestGenerateWallet(t *testing.T) { - w := generateWallet() + w := generateWallet("cosmos") require.Equal(t, w.Address[:7], "cosmos1", "Incorrect bech32 prefix") require.Equal(t, len(w.Address), 45, "Incorrect privkey length") require.Equal(t, len(w.Pubkey), 33, "Incorrect pubkey length") require.Equal(t, len(w.Privkey), 32, "Incorrect privkey length") } +func TestGenerateWalletWithPrefix(t *testing.T) { + w := generateWallet("osmo") + require.Equal(t, w.Address[:5], "osmo1", "Incorrect bech32 prefix") + require.Equal(t, len(w.Address), 43, "Incorrect privkey length") + require.Equal(t, len(w.Pubkey), 33, "Incorrect pubkey length") + require.Equal(t, len(w.Privkey), 32, "Incorrect privkey length") +} + func TestStartsWith(t *testing.T) { m := matcher{StartsWith: "aaaa"} require.True(t, m.Match("cosmos1aaaaqztg6eu45nlljp0wp947juded46aln83kr"))