/
wallet.go
120 lines (95 loc) · 2.28 KB
/
wallet.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
package rpc
import (
"sync"
"fyne.io/fyne/v2/widget"
"github.com/civilware/Gnomon/structures"
"github.com/deroproject/derohe/cryptography/crypto"
"github.com/deroproject/derohe/rpc"
"github.com/deroproject/derohe/walletapi"
"github.com/sirupsen/logrus"
)
type wallet struct {
UserPass string
IdHash string
Rpc string
Address string
ClientKey string
Balance uint64
TokenBal map[string]uint64
Height int
Connect bool
KeyLock bool
MuC sync.RWMutex
MuB sync.RWMutex
File *walletapi.Wallet_Disk
LogEntry *widget.Entry
Display struct {
Balance map[string]string
Height string
}
}
var Wallet wallet
var logger = structures.Logger.WithFields(logrus.Fields{})
// Check if wallet is connected
func (w *wallet) IsConnected() bool {
w.MuC.RLock()
defer w.MuC.RUnlock()
return w.Connect
}
// Set wallet connection
func (w *wallet) Connected(b bool) {
w.MuC.Lock()
w.Connect = b
w.MuC.Unlock()
}
// Get Wallet.Balance, 0 if not connected
func (w *wallet) GetBalance() {
w.MuB.Lock()
defer w.MuB.Unlock()
if w.IsConnected() {
rpcClientW, ctx, cancel := SetWalletClient(w.Rpc, w.UserPass)
defer cancel()
var result *rpc.GetBalance_Result
if err := rpcClientW.CallFor(ctx, &result, "GetBalance"); err != nil {
logger.Errorln("[GetBalance]", err)
w.Balance = 0
return
}
w.Balance = result.Unlocked_Balance
return
}
w.Balance = 0
}
// Get single balance of Wallet.TokenBal[name], 0 if not connected
func (w *wallet) GetTokenBalance(name, scid string) {
w.MuB.Lock()
defer w.MuB.Unlock()
if w.IsConnected() {
rpcClientW, ctx, cancel := SetWalletClient(w.Rpc, w.UserPass)
defer cancel()
params := &rpc.GetBalance_Params{
SCID: crypto.HashHexToHash(scid),
}
var result *rpc.GetBalance_Result
if err := rpcClientW.CallFor(ctx, &result, "GetBalance", params); err != nil {
logger.Errorln("[GetTokenBalance]", err)
w.TokenBal[name] = 0
return
}
w.TokenBal[name] = result.Unlocked_Balance
return
}
w.TokenBal[name] = 0
}
// Read Wallet.Balance
func (w *wallet) ReadBalance() uint64 {
w.MuB.RLock()
defer w.MuB.RUnlock()
return w.Balance
}
// Read Wallet.TokenBal[name]
func (w *wallet) ReadTokenBalance(name string) uint64 {
w.MuB.RLock()
defer w.MuB.RUnlock()
return w.TokenBal[name]
}