This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
indyledger.go
213 lines (164 loc) · 4.58 KB
/
indyledger.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
package addons
import (
"errors"
"github.com/findy-network/findy-wrapper-go/dto"
"github.com/findy-network/findy-wrapper-go/internal/c2go"
"github.com/findy-network/findy-wrapper-go/ledger"
"github.com/findy-network/findy-wrapper-go/plugin"
"github.com/findy-network/findy-wrapper-go/pool"
"github.com/golang/glog"
"github.com/lainio/err2"
"github.com/lainio/err2/assert"
"github.com/lainio/err2/try"
)
const indyLedgerAddonName = "FINDY_LEDGER"
// Indy is a ledger addon which implements real Indy ledger pool client.
// The machine which uses it must have indy pool named FINDY_LEDGER.
// Also it's important that memory or file ledger is used same time because both
// writing and reading to addon is done asynchronously.
type Indy struct {
handle int
}
func (ao *Indy) Close() {
c2go.PoolCloseLedger(ao.handle)
}
func (ao *Indy) Open(name ...string) (ok bool) {
poolName := name[0]
if poolName == "" {
poolName = indyLedgerAddonName
}
defer err2.Catch(err2.Err(func(err error) {
ok = false
glog.Errorln("cannot open", indyLedgerAddonName, "by name", poolName)
glog.Errorln(err)
}))
r := <-c2go.PoolOpenLedger(poolName)
try.To(r.Err())
ao.handle = r.Handle()
return true
}
func (ao *Indy) Write(tx plugin.TxInfo, ID, data string) error {
switch tx.TxType {
case plugin.TxTypeDID:
return ao.WriteDID(tx, ID, data)
case plugin.TxTypeSchema:
return ao.WriteSchema(tx, ID, data)
case plugin.TxTypeCredDef:
return ao.WriteCredDef(tx, ID, data)
}
return nil
}
func (ao *Indy) Read(tx plugin.TxInfo, ID string) (name string, value string, err error) {
switch tx.TxType {
case plugin.TxTypeDID:
assert.That(false, "we don't support DID reading from ledger")
case plugin.TxTypeSchema:
return ao.ReadSchema(tx, ID)
case plugin.TxTypeCredDef:
return ao.ReadCredDef(tx, ID)
}
return
}
var indyAddonLedger = new(Indy)
func init() {
pool.RegisterPlugin(indyLedgerAddonName, indyAddonLedger)
}
func (ao *Indy) ReadCredDef(
tx plugin.TxInfo,
credDefID string,
) (name string, value string, err error) {
defer err2.Handle(&err)
glog.V(100).Infoln("submitter:", tx.SubmitterDID)
r := <-ledger.BuildGetCredDefRequest(tx.SubmitterDID, credDefID)
try.To(r.Err())
r = <-ledger.SubmitRequest(ao.handle, r.Str1())
try.To(r.Err())
lresp := r.Str1()
// parse ledger response to credDefID and credDef
r = <-ledger.ParseGetCredDefResponse(lresp)
try.To(r.Err())
name = r.Str1()
assert.That(name == credDefID)
credDef := r.Str2()
return credDefID, credDef, nil
}
func (ao *Indy) ReadSchema(
tx plugin.TxInfo,
ID string,
) (name string, value string, err error) {
defer err2.Handle(&err)
glog.V(100).Infoln("submitter:", tx.SubmitterDID)
r := <-ledger.BuildGetSchemaRequest(tx.SubmitterDID, ID)
try.To(r.Err())
gsr := r.Str1()
r = <-ledger.SubmitRequest(ao.handle, gsr)
try.To(r.Err())
gsr = r.Str1()
r = <-ledger.ParseGetSchemaResponse(gsr)
try.To(r.Err())
readSchemaID := r.Str1()
scJSON := r.Str2()
return readSchemaID, scJSON, nil
}
func (ao *Indy) WriteDID(
tx plugin.TxInfo,
_ string,
data string,
) (err error) {
defer err2.Handle(&err)
glog.V(1).Infoln("submitter:", tx.SubmitterDID)
r := <-ledger.BuildNymRequest(tx.SubmitterDID, data, tx.VerKey, tx.Alias, tx.Role)
try.To(r.Err())
req := r.Str1()
r = <-ledger.SignAndSubmitRequest(ao.handle, tx.Wallet, tx.SubmitterDID, req)
try.To(r.Err())
try.To(checkWriteResponse(r.Str1()))
return nil
}
func (ao *Indy) WriteSchema(
tx plugin.TxInfo,
_ string,
data string,
) (err error) {
defer err2.Handle(&err)
glog.V(1).Infoln("submitter:", tx.SubmitterDID)
r := <-ledger.BuildSchemaRequest(tx.SubmitterDID, data)
try.To(r.Err())
srq := r.Str1()
r = <-ledger.SignAndSubmitRequest(ao.handle, tx.Wallet, tx.SubmitterDID, srq)
try.To(r.Err())
try.To(checkWriteResponse(r.Str1()))
return nil
}
func (ao *Indy) WriteCredDef(
tx plugin.TxInfo,
_ string,
data string,
) (err error) {
defer err2.Handle(&err)
glog.V(1).Infoln("submitter:", tx.SubmitterDID)
r := <-ledger.BuildCredDefRequest(tx.SubmitterDID, data)
try.To(r.Err())
r = <-ledger.SignAndSubmitRequest(ao.handle, tx.Wallet, tx.SubmitterDID, r.Str1())
try.To(r.Err())
try.To(checkWriteResponse(r.Str1()))
return nil
}
func checkWriteResponse(r string) error {
type response struct {
Op string `json:"op"`
Identifier string `json:"identifier"`
ReqID uint64 `json:"reqId"`
Reason string `json:"reason"`
}
var res response
dto.FromJSONStr(r, &res)
switch res.Op {
case "REJECT":
return errors.New(res.Reason)
case "REPLY": // we know this one, it's here for debugging
return nil
default:
return nil
}
}