Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Sep 22, 2018
0 parents commit 1943689
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/main
/lumenaddr
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: lumenaddr --database-url "$DATABASE_URL" --encryption-key "$ENCRYPTION_KEY" $SUFFIXES
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```sql
CREATE TABLE addresses (
word text NOT NULL,
public_key text NOT NULL,
encrypted_private_key text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
```

```sql
SELECT
word,
public_key,
convert_from(decrypt(encrypted_private_key::bytea, 'sekret', 'aes'), 'SQL_ASCII') private_key
FROM
addresses
ORDER BY
length(word) DESC,
created_at DESC
LIMIT 10;
```
127 changes: 127 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main

import (
"fmt"
"log"
"flag"
"strings"
"os"

"database/sql"
_ "github.com/lib/pq"
"github.com/stellar/go/keypair"
)

var databaseUrl string
var encryptionKey string
var saveToDatabase bool
var maxConcurrency int
var db *sql.DB
var throttle chan bool

func main() {
var err error
var words []string
var verbose bool

flag.StringVar(&databaseUrl, "database-url", "", "Specify PostgreSQL's connection string")
flag.StringVar(&encryptionKey, "encryption-key", "", "Specify the encryption key")
flag.IntVar(&maxConcurrency, "concurrency", 10, "Specify the concurrency")
flag.BoolVar(&verbose, "verbose", false, "Output iterations count")

flag.Parse()
words = flag.Args()

if len(words) == 0 {
fmt.Fprintf(os.Stderr, "ERROR: You need to provide at least one word.\n")
os.Exit(1)
}

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

throttle = make(chan bool, maxConcurrency)
saveToDatabase = databaseUrl != "" && encryptionKey != ""

if saveToDatabase {
if verbose {
fmt.Printf("\x1b[32m=> NOTICE: saving keys to the database. To view them, use the following SQL:\x1b[0m\n")
fmt.Printf("\x1b[1;30m SELECT word, public_key, convert_from(decrypt(encrypted_private_key::bytea, '<encryption key>', 'aes'), 'SQL_ASCII') private_key FROM addresses ORDER BY length(word) DESC LIMIT 10;\x1b[0m\n\n")
}

db, err = sql.Open("postgres", databaseUrl)
panicWithError(err)
} else {
if verbose {
fmt.Fprintf(os.Stderr, "\x1b[31mNOTICE: --database-url and --encryption-key not set; outputting keys instead.\x1b[0m\n\n")
}
}

index := 0

for {
index += 1

if verbose {
fmt.Printf("\r=> Lookups %d", index)
}

throttle <- true
go generatePair(words)
}
}

func panicWithError(err error) {
if err != nil {
panic(err)
}
}

func logError(err error) {
if err != nil {
log.Fatal(err)
}
}

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

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

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

return match
}

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

logError(err)

address := pair.Address()
seed := pair.Seed()
match := matchingWord(address, words)

if match == "" {
<-throttle
return
}

if saveToDatabase {
result := db.QueryRow("INSERT INTO addresses (word, public_key, encrypted_private_key) VALUES ($1, $2, encrypt($3, $4, 'aes'))", match, address, seed, encryptionKey)
_ = result
panicWithError(err)
} else {
lastIndex := strings.LastIndex(address, match)
fmt.Printf("\n\n%s\x1b[32m%s\x1b[0m\n%s\n\n", address[0:lastIndex], match, seed)
}

<-throttle
}

0 comments on commit 1943689

Please sign in to comment.