This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
103 lines (86 loc) · 2.46 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
package main
import (
"encoding/hex"
"fmt"
"os"
"github.com/kaspanet/faucet/config"
"github.com/kaspanet/faucet/database"
"github.com/kaspanet/faucet/version"
"github.com/kaspanet/go-secp256k1"
"github.com/kaspanet/kaspad/domain/dagconfig"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/profiling"
"github.com/pkg/errors"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/kaspanet/kaspad/infrastructure/os/signal"
"github.com/kaspanet/kaspad/util/panics"
)
var (
faucetAddress util.Address
faucetPrivateKey *secp256k1.SchnorrKeyPair
)
func main() {
defer panics.HandlePanic(log, "main", nil)
interrupt := signal.InterruptListener()
err := config.Parse()
if err != nil {
err := errors.Wrap(err, "Error parsing command-line arguments")
_, err = fmt.Fprintf(os.Stderr, "%s\n", err.Error())
if err != nil {
panic(err)
}
return
}
cfg, err := config.MainConfig()
if err != nil {
panic(err)
}
// Show version at startup.
log.Infof("Version %s", version.Version())
// Enable http profiling server if requested.
if cfg.Profile != "" {
profiling.Start(cfg.Profile, log)
}
if cfg.Migrate {
err := database.Migrate(cfg)
if err != nil {
panic(errors.Errorf("Error migrating database: %s", err))
}
return
}
err = database.Connect(cfg)
if err != nil {
panic(errors.Errorf("Error connecting to database: %s", err))
}
defer func() {
err := database.Close()
if err != nil {
panic(errors.Errorf("Error closing the database: %s", err))
}
}()
privateKeyBytes, err := hex.DecodeString(cfg.PrivateKey)
if err != nil {
panic(errors.Wrap(err, "failed to deserialize private key"))
}
faucetPrivateKey, _ = secp256k1.DeserializeSchnorrPrivateKeyFromSlice(privateKeyBytes)
faucetAddress, err = privateKeyToP2PKHAddress(faucetPrivateKey, config.ActiveNetParams())
if err != nil {
panic(errors.Errorf("Failed to get P2PKH address from private key: %s", err))
}
shutdownServer := startHTTPServer(cfg.HTTPListen)
defer shutdownServer()
<-interrupt
}
// privateKeyToP2PKHAddress generates p2pkh address from private key.
func privateKeyToP2PKHAddress(key *secp256k1.SchnorrKeyPair, net *dagconfig.Params) (util.Address, error) {
publicKey, err := key.SchnorrPublicKey()
if err != nil {
return nil, err
}
serialized, err := publicKey.Serialize()
if err != nil {
return nil, err
}
return util.NewAddressPublicKey(serialized[:], net.Prefix)
}