Skip to content

Commit

Permalink
Format code with goimports.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Dec 18, 2020
1 parent 408be0b commit dad283c
Showing 1 changed file with 143 additions and 142 deletions.
285 changes: 143 additions & 142 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package main

import (
"flag"
"fmt"
"os"
"strings"
"time"

"database/sql"
_ "github.com/lib/pq"
"github.com/stellar/go/keypair"
"github.com/dustin/go-humanize"
"github.com/dustin/go-humanize/english"
"github.com/hako/durafmt"
"flag"
"fmt"
"os"
"strings"
"time"

"database/sql"

"github.com/dustin/go-humanize"
"github.com/dustin/go-humanize/english"
"github.com/hako/durafmt"
_ "github.com/lib/pq"
"github.com/stellar/go/keypair"
)

var databaseUrl string = os.Getenv("DATABASE_URL")
Expand Down Expand Up @@ -43,169 +44,169 @@ const findSql = `
`

func main() {
flag.IntVar(&maxConcurrency, "concurrency", 10, "Specify the concurrency")
flag.BoolVar(&printKeys, "print", false, "Output saved keys")

flag.Parse()
words = flag.Args()
throttle = make(chan bool, maxConcurrency)

if saveToDatabase {
connectToDatabase()
}

if printKeys {
runPrintKeys()
} else {
runFindKeys()
}
flag.IntVar(&maxConcurrency, "concurrency", 10, "Specify the concurrency")
flag.BoolVar(&printKeys, "print", false, "Output saved keys")

flag.Parse()
words = flag.Args()
throttle = make(chan bool, maxConcurrency)

if saveToDatabase {
connectToDatabase()
}

if printKeys {
runPrintKeys()
} else {
runFindKeys()
}
}

func connectToDatabase() {
var err error
db, err = sql.Open("postgres", databaseUrl)
panicWithError(err)
var err error
db, err = sql.Open("postgres", databaseUrl)
panicWithError(err)
}

func runPrintKeys() {
var timeCondition time.Time
var err error
timeCondition, _ = time.Parse(time.RFC3339, "1900-01-1")
var timeCondition time.Time
var err error
timeCondition, _ = time.Parse(time.RFC3339, "1900-01-1")

panicWithError(err)
total := 0
panicWithError(err)
total := 0

for {
rows, err := db.Query(findSql, encryptionKey, timeCondition)
panicWithError(err)
for {
rows, err := db.Query(findSql, encryptionKey, timeCondition)
panicWithError(err)

count := 0
count := 0

for rows.Next() {
count += 1
total += 1
for rows.Next() {
count += 1
total += 1

var suffix string
var address string
var seed string
var createdAt time.Time
var suffix string
var address string
var seed string
var createdAt time.Time

err := rows.Scan(&suffix, &address, &seed, &createdAt)
panicWithError(err)
err := rows.Scan(&suffix, &address, &seed, &createdAt)
panicWithError(err)

formattedAddress := formatAddress(address, suffix)
template := "\rπŸ”‘ %s\nπŸ” %s\n⏱ %s\n\n"
fmt.Printf(template, formattedAddress, seed, humanize.Time(createdAt))
formattedAddress := formatAddress(address, suffix)
template := "\rπŸ”‘ %s\nπŸ” %s\n⏱ %s\n\n"
fmt.Printf(template, formattedAddress, seed, humanize.Time(createdAt))

timeCondition = createdAt
}
timeCondition = createdAt
}

if count == 0 {
break
}
}
if count == 0 {
break
}
}

if total == 0 {
fmt.Printf("\n😞 No keys found so far.\n")
} else {
fmt.Printf("πŸ™‚ %s %s found so far.\n", humanize.Comma(int64(total)), english.PluralWord(total, "key", ""))
}
if total == 0 {
fmt.Printf("\n😞 No keys found so far.\n")
} else {
fmt.Printf("πŸ™‚ %s %s found so far.\n", humanize.Comma(int64(total)), english.PluralWord(total, "key", ""))
}
}

func runFindKeys() {
if len(words) == 0 {
fmt.Fprintf(os.Stderr, "πŸ’£ \x1b[31mERROR: You need to provide at least one word.\x1b[0m\n")
os.Exit(1)
}

if len(words) == 1 {
words = strings.Split(words[0], " ")
}

if saveToDatabase {
fmt.Printf("\x1b[32mβœ… Matching keys will be saved to the database. Use `lumenaddr --print` to view them.\x1b[0m\n\n")
} else {
fmt.Fprintf(os.Stderr, "\x1b[31m⚠️ DATABASE_URL and ENCRYPTION_KEY config vars not set; outputting keys instead.\x1b[0m\n\n")
}

index := 0
starts = time.Now()

print("πŸ”Ž Searching. Press CTRL-C to stop...")

for {
index += 1
throttle <- true
go generatePair(words)
}
if len(words) == 0 {
fmt.Fprintf(os.Stderr, "πŸ’£ \x1b[31mERROR: You need to provide at least one word.\x1b[0m\n")
os.Exit(1)
}

if len(words) == 1 {
words = strings.Split(words[0], " ")
}

if saveToDatabase {
fmt.Printf("\x1b[32mβœ… Matching keys will be saved to the database. Use `lumenaddr --print` to view them.\x1b[0m\n\n")
} else {
fmt.Fprintf(os.Stderr, "\x1b[31m⚠️ DATABASE_URL and ENCRYPTION_KEY config vars not set; outputting keys instead.\x1b[0m\n\n")
}

index := 0
starts = time.Now()

print("πŸ”Ž Searching. Press CTRL-C to stop...")

for {
index += 1
throttle <- true
go generatePair(words)
}
}

func panicWithError(err error) {
if err != nil {
fmt.Printf("\rπŸ’£ \x1b[31mERROR: %s\x1b[0m\n", err)
os.Exit(1)
}
if err != nil {
fmt.Printf("\rπŸ’£ \x1b[31mERROR: %s\x1b[0m\n", err)
os.Exit(1)
}
}

func matchingWord(address string, words []string)(string) {
var match string
func matchingWord(address string, words []string) string {
var match string

for _, suffix := range words {
suffix := strings.ToUpper(suffix)
for _, suffix := range words {
suffix := strings.ToUpper(suffix)

if strings.HasSuffix(address, suffix) {
match = suffix
break
}
}
if strings.HasSuffix(address, suffix) {
match = suffix
break
}
}

return match
return match
}

func generatePair(words []string) {
var err error
pair, err := keypair.Random()

panicWithError(err)

address := pair.Address()
seed := pair.Seed()
elapsed := time.Since(starts)
totalKeys += 1
match := matchingWord(address, words)

if match == "" {
if totalKeys % 1000 == 0 {
printStatsMessage()
}

<-throttle
return
}

matchedKeys += 1

if saveToDatabase {
_, err = db.Exec("INSERT INTO addresses (word, public_key, encrypted_private_key) VALUES ($1, $2, encrypt($3, $4, 'aes'))", match, address, seed, encryptionKey)
panicWithError(err)
printStatsMessage()
} else {
formattedAddress := formatAddress(address, match)
duration, _ := durafmt.ParseString(elapsed.String())
_ = duration
template := "\rπŸ”‘ %s\nπŸ” %s\n\n"
fmt.Printf(template, formattedAddress, seed)
}

<-throttle
var err error
pair, err := keypair.Random()

panicWithError(err)

address := pair.Address()
seed := pair.Seed()
elapsed := time.Since(starts)
totalKeys += 1
match := matchingWord(address, words)

if match == "" {
if totalKeys%1000 == 0 {
printStatsMessage()
}

<-throttle
return
}

matchedKeys += 1

if saveToDatabase {
_, err = db.Exec("INSERT INTO addresses (word, public_key, encrypted_private_key) VALUES ($1, $2, encrypt($3, $4, 'aes'))", match, address, seed, encryptionKey)
panicWithError(err)
printStatsMessage()
} else {
formattedAddress := formatAddress(address, match)
duration, _ := durafmt.ParseString(elapsed.String())
_ = duration
template := "\rπŸ”‘ %s\nπŸ” %s\n\n"
fmt.Printf(template, formattedAddress, seed)
}

<-throttle
}

func printStatsMessage() {
template := "\rπŸ”Ž Found %s out of %s %s. Press CTRL-C to stop..."
fmt.Printf(template, humanize.Comma(matchedKeys), humanize.Comma(totalKeys), english.PluralWord(int(totalKeys), "key", ""))
template := "\rπŸ”Ž Found %s out of %s %s. Press CTRL-C to stop..."
fmt.Printf(template, humanize.Comma(matchedKeys), humanize.Comma(totalKeys), english.PluralWord(int(totalKeys), "key", ""))
}

func formatAddress(address string, suffix string)(string) {
lastIndex := strings.LastIndex(address, suffix)
return fmt.Sprintf("\x1b[34m%s\x1b[44m\x1b[37m%s\x1b[0m", address[0:lastIndex], suffix)
func formatAddress(address string, suffix string) string {
lastIndex := strings.LastIndex(address, suffix)
return fmt.Sprintf("\x1b[34m%s\x1b[44m\x1b[37m%s\x1b[0m", address[0:lastIndex], suffix)
}

0 comments on commit dad283c

Please sign in to comment.