Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Adding Bech32 Prefix Support #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*.so
*.dylib

# IDE folders & files
.idea

# Test binary, built with `go test -c`
*.test

Expand Down
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
}
Expand Down
10 changes: 9 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down