/
orion.go
304 lines (265 loc) · 9.8 KB
/
orion.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package orion
import (
"bytes"
"io"
"os"
"text/template"
"time"
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/dlog"
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/fabtoken"
math3 "github.com/IBM/mathlib"
api2 "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/api"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc"
sfcnode "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc/node"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators"
topology2 "github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/topology"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/fabric/tcc"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/vault/translator"
"github.com/hyperledger-labs/orion-sdk-go/pkg/bcdb"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
var logger = flogging.MustGetLogger("token-sdk.integration.token.orion")
type tokenPlatform interface {
TokenGen(keygen common.Command) (*gexec.Session, error)
PublicParametersFile(tms *topology2.TMS) string
GetContext() api2.Context
PublicParameters(tms *topology2.TMS) []byte
GetPublicParamsGenerators(driver string) generators.PublicParamsGenerator
PublicParametersDir() string
GetBuilder() api2.Builder
TokenDir() string
}
type orionPlatform interface {
CreateDBInstance() bcdb.BCDB
CreateUserSession(bcdb bcdb.BCDB, user string) bcdb.DBSession
}
type Entry struct {
TMS *topology2.TMS
Wallets map[string]*generators.Wallets
}
type NetworkHandler struct {
TokenPlatform tokenPlatform
EventuallyTimeout time.Duration
Entries map[string]*Entry
CryptoMaterialGenerators map[string]generators.CryptoMaterialGenerator
}
func NewNetworkHandler(tokenPlatform tokenPlatform, builder api2.Builder) *NetworkHandler {
return &NetworkHandler{
TokenPlatform: tokenPlatform,
EventuallyTimeout: 10 * time.Minute,
Entries: map[string]*Entry{},
CryptoMaterialGenerators: map[string]generators.CryptoMaterialGenerator{
"fabtoken": fabtoken.NewCryptoMaterialGenerator(tokenPlatform, builder),
"dlog": dlog.NewCryptoMaterialGenerator(tokenPlatform, math3.BN254, builder),
},
}
}
func (p *NetworkHandler) GenerateArtifacts(tms *topology2.TMS) {
entry := p.GetEntry(tms)
// Generate crypto material
cmGenerator := p.CryptoMaterialGenerators[tms.Driver]
Expect(cmGenerator).NotTo(BeNil(), "Crypto material generator for driver %s not found", tms.Driver)
// - Setup
root, err := cmGenerator.Setup(tms)
Expect(err).NotTo(HaveOccurred())
// - Generate crypto material for each FSC node
fscTopology := p.TokenPlatform.GetContext().TopologyByName(fsc.TopologyName).(*fsc.Topology)
for _, node := range fscTopology.Nodes {
p.GenerateCryptoMaterial(cmGenerator, tms, node)
}
// Generate public parameters
var ppRaw []byte
ppGenerator := p.TokenPlatform.GetPublicParamsGenerators(tms.Driver)
Expect(ppGenerator).NotTo(BeNil(), "No public params generator for driver %s", tms.Driver)
args := []interface{}{root}
for _, arg := range tms.PublicParamsGenArgs {
args = append(args, arg)
}
logger.Debugf("Generating public parameters for [%s:%s] with args [%+v]", tms.ID(), args)
wallets := &generators.Wallets{}
for _, w := range entry.Wallets {
wallets.Issuers = append(wallets.Issuers, w.Issuers...)
wallets.Auditors = append(wallets.Auditors, w.Auditors...)
wallets.Certifiers = append(wallets.Certifiers, w.Certifiers...)
}
ppRaw, err = ppGenerator.Generate(tms, wallets, args...)
Expect(err).ToNot(HaveOccurred())
// - Store pp
Expect(os.MkdirAll(p.TokenPlatform.PublicParametersDir(), 0766)).ToNot(HaveOccurred())
Expect(os.WriteFile(p.TokenPlatform.PublicParametersFile(tms), ppRaw, 0766)).ToNot(HaveOccurred())
}
func (p *NetworkHandler) GenerateExtension(tms *topology2.TMS, node *sfcnode.Node) string {
t, err := template.New("peer").Funcs(template.FuncMap{
"TMSID": func() string { return tms.ID() },
"TMS": func() *topology2.TMS { return tms },
"Wallets": func() *generators.Wallets { return p.GetEntry(tms).Wallets[node.Name] },
"IsCustodian": func() bool {
custodianNode, ok := tms.BackendParams[Custodian]
if !ok {
return false
}
return custodianNode.(*sfcnode.Node).Name == node.Name
},
"CustodianID": func() string {
return tms.BackendParams[Custodian].(*sfcnode.Node).Name
},
}).Parse(Extension)
Expect(err).NotTo(HaveOccurred())
ext := bytes.NewBufferString("")
err = t.Execute(io.MultiWriter(ext), p)
Expect(err).NotTo(HaveOccurred())
return ext.String()
}
func (p *NetworkHandler) PostRun(load bool, tms *topology2.TMS) {
if load {
return
}
// Store the public parameters in orion
orion, ok := p.TokenPlatform.GetContext().PlatformByName(tms.BackendTopology.Name()).(orionPlatform)
Expect(ok).To(BeTrue(), "No orion platform found for topology %s", tms.BackendTopology.Name())
db := orion.CreateDBInstance()
custodianID := tms.BackendParams[Custodian].(*sfcnode.Node).Name
session := orion.CreateUserSession(db, custodianID)
tx, err := session.DataTx()
Expect(err).ToNot(HaveOccurred(), "Failed to create data transaction")
rwset := &RWSWrapper{
db: tms.Namespace,
me: custodianID,
tx: tx,
}
w := translator.New("", rwset, "")
ppRaw, err := os.ReadFile(p.TokenPlatform.PublicParametersFile(tms))
Expect(err).ToNot(HaveOccurred(), "Failed to read public parameters file %s", p.TokenPlatform.PublicParametersFile(tms))
action := &tcc.SetupAction{
SetupParameters: ppRaw,
}
Expect(w.Write(action)).ToNot(HaveOccurred(), "Failed to store public parameters for namespace %s", tms.Namespace)
_, _, err = tx.Commit(true)
Expect(err).ToNot(HaveOccurred(), "Failed to commit transaction")
}
func (p *NetworkHandler) Cleanup() {
}
func (p *NetworkHandler) UpdateChaincodePublicParams(tms *topology2.TMS, ppRaw []byte) {
panic("Should not be invoked")
}
func (p *NetworkHandler) GenIssuerCryptoMaterial(tms *topology2.TMS, nodeID string, walletID string) string {
cmGenerator := p.CryptoMaterialGenerators[tms.Driver]
Expect(cmGenerator).NotTo(BeNil(), "Crypto material generator for driver %s not found", tms.Driver)
fscTopology := p.TokenPlatform.GetContext().TopologyByName(fsc.TopologyName).(*fsc.Topology)
for _, node := range fscTopology.Nodes {
if node.ID() == nodeID {
ids := cmGenerator.GenerateIssuerIdentities(tms, node, walletID)
return ids[0].Path
}
}
Expect(false).To(BeTrue(), "cannot find FSC node [%s:%s]", tms.Network, nodeID)
return ""
}
func (p *NetworkHandler) GenOwnerCryptoMaterial(tms *topology2.TMS, nodeID string, walletID string, useCAIfAvailable bool) string {
cmGenerator := p.CryptoMaterialGenerators[tms.Driver]
Expect(cmGenerator).NotTo(BeNil(), "Crypto material generator for driver %s not found", tms.Driver)
fscTopology := p.TokenPlatform.GetContext().TopologyByName(fsc.TopologyName).(*fsc.Topology)
for _, node := range fscTopology.Nodes {
if node.ID() == nodeID {
ids := cmGenerator.GenerateOwnerIdentities(tms, node, walletID)
return ids[0].Path
}
}
Expect(false).To(BeTrue(), "cannot find FSC node [%s:%s]", tms.Network, nodeID)
return ""
}
func (p *NetworkHandler) SetCryptoMaterialGenerator(driver string, generator generators.CryptoMaterialGenerator) {
p.CryptoMaterialGenerators[driver] = generator
}
func (p *NetworkHandler) GenerateCryptoMaterial(cmGenerator generators.CryptoMaterialGenerator, tms *topology2.TMS, node *sfcnode.Node) {
entry := p.GetEntry(tms)
o := node.PlatformOpts()
opts := topology2.ToOptions(o)
wallet := &generators.Wallets{
Certifiers: []generators.Identity{},
Issuers: []generators.Identity{},
Owners: []generators.Identity{},
Auditors: []generators.Identity{},
}
entry.Wallets[node.Name] = wallet
// Issuer identities
issuers := opts.Issuers()
if len(issuers) != 0 {
var index int
found := false
for i, issuer := range issuers {
if issuer == node.ID() || issuer == "_default_" {
index = i
found = true
issuers[i] = node.ID()
break
}
}
if !found {
issuers = append(issuers, node.ID())
index = len(issuers) - 1
}
ids := cmGenerator.GenerateIssuerIdentities(tms, node, issuers...)
if len(ids) > 0 {
wallet.Issuers = append(wallet.Issuers, ids...)
wallet.Issuers[index].Default = true
}
}
// Owner identities
owners := opts.Owners()
if len(owners) != 0 {
var index int
found := false
for i, owner := range owners {
if owner == node.ID() || owner == "_default_" {
index = i
found = true
owners[i] = node.ID()
break
}
}
if !found {
owners = append(owners, node.ID())
index = len(owners) - 1
}
ids := cmGenerator.GenerateOwnerIdentities(tms, node, owners...)
if len(ids) > 0 {
wallet.Owners = append(wallet.Owners, ids...)
wallet.Owners[index].Default = true
}
}
// Auditor identity
if opts.Auditor() {
ids := cmGenerator.GenerateAuditorIdentities(tms, node, node.Name)
if len(ids) > 0 {
wallet.Auditors = append(wallet.Auditors, ids...)
wallet.Auditors[len(wallet.Auditors)-1].Default = true
}
}
// Certifier identities
if opts.Certifier() {
ids := cmGenerator.GenerateCertifierIdentities(tms, node, node.Name)
if len(ids) > 0 {
wallet.Certifiers = append(wallet.Certifiers, ids...)
wallet.Certifiers[len(wallet.Certifiers)-1].Default = true
}
}
}
func (p *NetworkHandler) GetEntry(tms *topology2.TMS) *Entry {
entry, ok := p.Entries[tms.Network+tms.Channel+tms.Namespace]
if !ok {
entry = &Entry{
TMS: tms,
Wallets: map[string]*generators.Wallets{},
}
p.Entries[tms.Network+tms.Channel+tms.Namespace] = entry
}
return entry
}