-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
239 lines (189 loc) · 5.43 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/bazo-blockchain/bazo-client/REST"
"github.com/bazo-blockchain/bazo-client/client"
"github.com/bazo-blockchain/bazo-miner/storage"
"io/ioutil"
"log"
"math/big"
"net/http"
"os"
"os/exec"
"strconv"
"time"
)
var (
logger *log.Logger
issuer string
issuerAddress [64]byte
multisig string
openAcc = make(map[int][64]byte)
openFunds = make(map[int][64]byte)
)
const (
CLIENT = "oysyconnect.westeurope.cloudapp.azure.com"
CARMAAPPID = "a63c1f72-8198-4a59-85be-67a96f87ab41"
CARMAPOC = "https://carma-poc.autoidlabs.ch/"
)
type carma_summary struct {
Status string `json:"status"`
Notices interface{} `json:"notices"`
Responses interface{} `json:"response"`
}
type carma_response struct {
Id int `json:"id"`
User_id int `json:"user_id,omitempty"`
Public_key string `json:"public_key,omitempty"`
Amount int `json:"amount,omitempty"`
Status string `json:"status,omitempty"`
Max_amount int `json:"max_amount,omitempty"`
Token string `json:"token,omitempty"`
App_id string `json:"app_id,omitempty"`
}
func main() {
if len(os.Args) != 3 {
log.Fatal("Usage bazo-connect <issuer> <multisig>")
}
issuer = os.Args[1]
multisig = os.Args[2]
issuerPubKey, _, _ := storage.ExtractKeyFromFile(issuer)
issuerAddress = storage.GetAddressFromPubKey(&issuerPubKey)
logger = storage.InitLogger()
for {
processNewAcc()
processNewFunds()
time.Sleep(30 * time.Second)
}
}
func processNewAcc() (err error) {
if err = reqCarmaSummary("open"); err != nil {
return err
}
for id, address := range openAcc {
acc, err := reqAccount(address)
if err != nil {
return err
}
if acc == nil {
create(address)
}
setStatus(id, "pending")
delete(openAcc, id)
}
return nil
}
func processNewFunds() (err error) {
if err = reqCarmaSummary("fundprocessed"); err != nil {
return err
}
issuerAcc, _ := reqAccount(issuerAddress)
for id, address := range openFunds {
acc, err := reqAccount(address)
if err != nil {
return err
}
if acc != nil {
status, err := getStatus(id)
if err != nil {
return err
}
fund(issuerAcc, address, status.Amount)
issuerAcc.TxCnt++
setStatus(id, "processed")
delete(openFunds, id)
}
}
return nil
}
func reqCarmaSummary(status string) (err error) {
response, err := http.Get(CARMAPOC + "bazo/summary?app_id=" + CARMAAPPID)
if err != nil {
return errors.New(fmt.Sprintf("The HTTP request failed with error %s", err))
}
data, _ := ioutil.ReadAll(response.Body)
var responses []carma_response
summary := carma_summary{Responses: &responses}
json.Unmarshal([]byte(data), &summary)
if summary.Status == "OK" {
for _, o := range responses {
if o.Status == status && len(o.Public_key) == 128 && o.Amount > 0 {
var pubKey [64]byte
pubKeyInt, _ := new(big.Int).SetString(o.Public_key, 16)
copy(pubKey[:], pubKeyInt.Bytes())
if status == "open" {
openAcc[o.Id] = pubKey
} else if status == "fundprocessed" {
openFunds[o.Id] = pubKey
}
}
}
}
logger.Println("Status:")
fmt.Println("Open accounts (address)")
for _, address := range openAcc {
fmt.Printf("%x\n", address)
}
fmt.Println("Open funds (address)")
for _, address := range openFunds {
fmt.Printf("%x\n", address)
}
return nil
}
func reqAccount(address [64]byte) (acc *client.Account, err error) {
response, err := http.Get("https://" + CLIENT + "/account/" + hex.EncodeToString(address[:]))
if err != nil {
return nil, errors.New(fmt.Sprintf("The HTTP request failed with error %s", err))
}
data, _ := ioutil.ReadAll(response.Body)
var contents []REST.Content
content := REST.Content{"account", &acc}
contents = append(contents, content)
jsonResponse := REST.JsonResponse{Content: contents}
json.Unmarshal([]byte(data), &jsonResponse)
return acc, nil
}
func getStatus(id int) (status carma_response, err error) {
response, err := http.Get(CARMAPOC + "bazo/request_status?id=" + strconv.Itoa(id) + "&app_id=" + CARMAAPPID)
if err != nil {
return status, errors.New(fmt.Sprintf("The HTTP request failed with error %s", err))
}
data, _ := ioutil.ReadAll(response.Body)
summary := carma_summary{Responses: &status}
json.Unmarshal([]byte(data), &summary)
return status, nil
}
func setStatus(id int, status string) error {
req := carma_response{Id: id, App_id: CARMAAPPID, Status: status}
jsonValue, _ := json.Marshal(req)
response, err := http.Post(CARMAPOC+"bazo/update_request", "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return errors.New(fmt.Sprintf("The HTTP request failed with error %s", err))
}
data, _ := ioutil.ReadAll(response.Body)
var responses []carma_response
summary := carma_summary{Responses: responses}
json.Unmarshal([]byte(data), &summary)
if summary.Status != "OK" {
return errors.New(fmt.Sprintf("Status update failed."))
}
return nil
}
func create(address [64]byte) {
out, err := exec.Command("bazo-client", "accTx", "0", "1", issuer, hex.EncodeToString(address[:])).Output()
if err != nil {
log.Fatal(err)
}
logger.Printf("%s", out)
}
func fund(issuerAcc *client.Account, address [64]byte, amount int) {
out, err := exec.Command("bazo-client", "fundsTx", "0", strconv.Itoa(amount), "1", strconv.Itoa(int(issuerAcc.TxCnt)), issuer, hex.EncodeToString(address[:]), multisig).Output()
if err != nil {
log.Fatal(err)
}
logger.Printf("%s", out)
}