-
Notifications
You must be signed in to change notification settings - Fork 0
/
macI.go
173 lines (141 loc) · 3.65 KB
/
macI.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import "C"
import (
"fmt"
"github.com/btcsuite/btcutil/base58"
"github.com/pangolin-lab/atom/ethereum"
"github.com/pangolin-lab/atom/pipeProxy"
"github.com/pangolin-lab/atom/wallet"
"github.com/pangolin-lab/go-node/account"
)
var proxyConf *pipeProxy.ProxyConfig = nil
var curProxy *pipeProxy.PipeProxy = nil
//export LibCreateAccount
func LibCreateAccount(password string) (*C.char, *C.char) {
key, err := account.GenerateKey(password)
if err != nil {
return C.CString(""), C.CString("")
}
address := key.ToNodeId()
cipherTxt := base58.Encode(key.LockedKey)
return C.CString(address.String()), C.CString(cipherTxt)
}
//export LibCreateEthAccount
func LibCreateEthAccount(password, directory string) *C.char {
return C.CString(ethereum.CreateEthAccount(password, directory))
}
//export LibIsInit
func LibIsInit() bool {
return curProxy != nil
}
//export LibVerifyAccount
func LibVerifyAccount(cipherTxt, address, password string) bool {
if _, err := account.AccFromString(address, cipherTxt, password); err != nil {
return false
}
return true
}
//export LibIsProtonAddress
func LibIsProtonAddress(address string) bool {
return account.ID(address).IsValid()
}
//export LibInitProxy
func LibInitProxy(addr, cipher, url, boot, path string) bool {
proxyConf = &pipeProxy.ProxyConfig{
WConfig: &wallet.WConfig{
BCAddr: addr,
Cipher: cipher,
SettingUrl: url,
Saver: nil,
},
BootNodes: boot,
}
mis := proxyConf.FindBootServers(path)
if len(mis) == 0 {
fmt.Println("no valid boot strap node")
return false
}
proxyConf.ServerId = mis[0]
return true
}
//export LibCreateProxy
func LibCreateProxy(password, locSer string) bool {
if proxyConf == nil {
fmt.Println("init the proxy configuration first please")
return false
}
if curProxy != nil {
fmt.Println("stop the old instance first please")
return true
}
fmt.Println(proxyConf.String())
w, err := wallet.NewWallet(proxyConf.WConfig, password)
if err != nil {
fmt.Println(err)
return false
}
proxy, e := pipeProxy.NewProxy(locSer, w, NewTunReader())
if e != nil {
fmt.Println(e)
return false
}
curProxy = proxy
return true
}
//TODO:: inner error call back
//export LibProxyRun
func LibProxyRun() {
if curProxy == nil {
return
}
fmt.Println("start proxy success.....")
curProxy.Proxying()
curProxy.Finish()
curProxy = nil
}
//export LibStopClient
func LibStopClient() {
if curProxy == nil {
return
}
curProxy.Finish()
return
}
//export LibLoadEthAddrByProtonAddr
func LibLoadEthAddrByProtonAddr(protonAddr string) *C.char {
return C.CString(ethereum.BoundEth(protonAddr))
}
//export LibEthBindings
func LibEthBindings(ETHAddr string) (float64, int) {
ethB, no := ethereum.BasicBalance(ETHAddr)
if ethB == nil {
return 0, 0
}
return ethereum.ConvertByDecimal(ethB), no
}
//export LibImportEthAccount
func LibImportEthAccount(file, dir, pwd string) *C.char {
addr := ethereum.ImportEthAccount(file, dir, pwd)
return C.CString(addr)
}
//export LibBindProtonAddr
func LibBindProtonAddr(protonAddr, cipherKey, password string) (*C.char, *C.char) {
tx, err := ethereum.Bind(protonAddr, cipherKey, password)
if err != nil {
fmt.Printf("\nBind proton addr(%s) err:%s", protonAddr, err)
return C.CString(""), C.CString(err.Error())
}
return C.CString(tx), C.CString("")
}
//export LibUnbindProtonAddr
func LibUnbindProtonAddr(protonAddr, cipherKey, password string) (*C.char, *C.char) {
tx, err := ethereum.Unbind(protonAddr, cipherKey, password)
if err != nil {
fmt.Printf("\nBind proton addr(%s) err:%s", protonAddr, err)
return C.CString(""), C.CString(err.Error())
}
return C.CString(tx), C.CString("")
}
//
//func main() {
//}