This repository has been archived by the owner on Mar 21, 2022. It is now read-only.
/
main.go
135 lines (119 loc) · 2.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
/*
This program is largely based on cmd/genkeys from yggdrasil-go, located at
https://github.com/yggdrasil-network/yggdrasil-go
See cmd/genkeys/main.go@78b5f88e4bb734d0dd6a138ff08d34ca39dcaea3
*/
import (
"crypto/ed25519"
"encoding/hex"
"flag"
"log"
"net"
"os"
"regexp"
"runtime"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
)
var (
programName = "syg_go"
version = "v0.1.4"
copyright = "Copyright (c) 2020 Timur Demin"
)
var (
stdout = log.New(os.Stdout, "", log.Flags())
stderr = log.New(os.Stderr, "", log.Flags())
)
func main() {
rxflag := flag.String("regex", "::", "regex to match addresses against")
threads := flag.Int("threads", runtime.GOMAXPROCS(0), "how many threads to use for mining")
iterationsPerOutput := flag.Uint("iter", 100000, "per how many iterations to output status")
displayVersion := flag.Bool("version", false, "display version")
origCode := flag.Bool("original", false, "use original Yggdrasil code")
highAddressMode := flag.Bool("highaddr", false, "high address mining mode, excludes regex")
flag.Parse()
if *displayVersion {
println(programName, version)
println(copyright)
return
}
if *origCode {
stdout.Println("using unmodified Yggdrasil code")
addrForKey = address.AddrForKey
generateKey = GenerateKeyEd25519
} else {
stdout.Println("using syg_go vendored code")
addrForKey = AddrForKey
generateKey = GenerateKey
}
regex, err := regexp.Compile(*rxflag)
if err != nil {
stderr.Printf("%v\n", err)
os.Exit(1)
}
newKeys := make(chan keySet, *threads)
var currentBest = make(ed25519.PublicKey, ed25519.PublicKeySize)
for i := range currentBest {
currentBest[i] = 0xff
}
for i := 0; i < *threads; i++ {
go doBoxKeys(newKeys)
}
counter := uint64(0)
i := uint64(*iterationsPerOutput)
if !*highAddressMode {
stdout.Printf("starting mining for %v with %v threads\n", regex, *threads)
for {
newKey := <-newKeys
if regex.MatchString(newKey.ip) {
newKey.print()
}
counter++
if counter%i == 0 {
stderr.Printf("reached %v iterations\n", counter)
}
}
} else {
stdout.Printf("starting mining higher addresses with %v threads\n", *threads)
for {
newKey := <-newKeys
if isBetter(currentBest, newKey.pub) {
currentBest = newKey.pub
newKey.print()
}
counter++
if counter%i == 0 {
stderr.Printf("reached %v iterations\n", counter)
}
}
}
}
type keySet struct {
priv []byte
pub []byte
ip string
}
func (k *keySet) print() {
stdout.Printf("priv: %s | pub: %s | ip: %s\n",
hex.EncodeToString(k.priv[:]),
hex.EncodeToString(k.pub[:]),
k.ip)
}
func doBoxKeys(out chan<- keySet) {
for {
pub, priv := generateKey()
ip := net.IP(addrForKey(pub)[:]).String()
out <- keySet{priv[:], pub[:], ip}
}
}
func isBetter(oldPub, newPub ed25519.PublicKey) bool {
for i := range oldPub {
if newPub[i] < oldPub[i] {
return true
}
if newPub[i] > oldPub[i] {
break
}
}
return false
}