-
Notifications
You must be signed in to change notification settings - Fork 29
/
self.go
74 lines (60 loc) · 1.29 KB
/
self.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
package node
import (
"net/url"
"strconv"
"time"
"github.com/0chain/gosdk/zmagmacore/wallet"
)
// Node represent self node type.
type Node struct {
url string
wallet *wallet.Wallet
extID string
startTime time.Time
}
var (
self = &Node{}
)
// Start writes to self node current time, sets wallet, external id and url
//
// Start should be used only once while application is starting.
func Start(host string, port int, extID string, wallet *wallet.Wallet) {
self = &Node{
url: makeHostURL(host, port),
wallet: wallet,
extID: extID,
startTime: time.Now(),
}
}
func makeHostURL(host string, port int) string {
if host == "" {
host = "localhost"
}
uri := url.URL{
Scheme: "http",
Host: host + ":" + strconv.Itoa(port),
}
return uri.String()
}
// GetWalletString returns marshaled to JSON string nodes wallet.
func GetWalletString() (string, error) {
return self.wallet.StringJSON()
}
func SetWallet(wall *wallet.Wallet) {
self.wallet = wall
}
// ID returns id of Node.
func ID() string {
return self.wallet.ID()
}
func ExtID() string {
return self.extID
}
// PublicKey returns id of Node.
func PublicKey() string {
return self.wallet.PublicKey()
}
// StartTime returns time when Node is started.
func StartTime() time.Time {
return self.startTime
}