Skip to content

Commit 49dea13

Browse files
committed
adjust validator
Signed-off-by: Angelo De Caro <angelo.decaro@gmail.com>
1 parent 6bdf93f commit 49dea13

File tree

12 files changed

+432
-125
lines changed

12 files changed

+432
-125
lines changed

token/core/common/validator.go

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
type Context[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] struct {
2626
Logger logging.Logger
2727
PP P
28+
TokenRequest *driver.TokenRequest
2829
Deserializer DS
2930
SignatureProvider driver.SignatureProvider
3031
Signatures [][]byte
@@ -44,6 +45,8 @@ type ValidateTransferFunc[P driver.PublicParameters, T any, TA driver.TransferAc
4445

4546
type ValidateIssueFunc[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(ctx *Context[P, T, TA, IA, DS]) error
4647

48+
type ValidateAuditingFunc[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(ctx *Context[P, T, TA, IA, DS]) error
49+
4750
type ActionDeserializer[TA driver.TransferAction, IA driver.IssueAction] interface {
4851
DeserializeActions(tr *driver.TokenRequest) ([]IA, []TA, error)
4952
}
@@ -53,6 +56,8 @@ type Validator[P driver.PublicParameters, T any, TA driver.TransferAction, IA dr
5356
PublicParams P
5457
Deserializer DS
5558
ActionDeserializer ActionDeserializer[TA, IA]
59+
60+
AuditingValidators []ValidateAuditingFunc[P, T, TA, IA, DS]
5661
TransferValidators []ValidateTransferFunc[P, T, TA, IA, DS]
5762
IssueValidators []ValidateIssueFunc[P, T, TA, IA, DS]
5863
}
@@ -64,6 +69,7 @@ func NewValidator[P driver.PublicParameters, T any, TA driver.TransferAction, IA
6469
actionDeserializer ActionDeserializer[TA, IA],
6570
transferValidators []ValidateTransferFunc[P, T, TA, IA, DS],
6671
issueValidators []ValidateIssueFunc[P, T, TA, IA, DS],
72+
auditingValidators []ValidateAuditingFunc[P, T, TA, IA, DS],
6773
) *Validator[P, T, TA, IA, DS] {
6874
return &Validator[P, T, TA, IA, DS]{
6975
Logger: Logger,
@@ -72,6 +78,7 @@ func NewValidator[P driver.PublicParameters, T any, TA driver.TransferAction, IA
7278
ActionDeserializer: actionDeserializer,
7379
TransferValidators: transferValidators,
7480
IssueValidators: issueValidators,
81+
AuditingValidators: auditingValidators,
7582
}
7683
}
7784

@@ -90,13 +97,11 @@ func (v *Validator[P, T, TA, IA, DS]) VerifyTokenRequestFromRaw(ctx context.Cont
9097
if err != nil {
9198
return nil, nil, errors.Wrap(err, "failed to marshal signed token request")
9299
}
93-
var signatures [][]byte
94-
if len(v.PublicParams.Auditors()) != 0 {
95-
signatures = append(signatures, tr.AuditorSignatures...)
96-
signatures = append(signatures, tr.Signatures...)
97-
} else {
98-
signatures = tr.Signatures
100+
signatures := make([][]byte, 0, len(tr.AuditorSignatures)+len(tr.Signatures))
101+
for _, sig := range tr.AuditorSignatures {
102+
signatures = append(signatures, sig.Signature)
99103
}
104+
signatures = append(signatures, tr.Signatures...)
100105

101106
attributes := make(driver.ValidationAttributes)
102107
attributes[TokenRequestToSign] = signed
@@ -110,18 +115,18 @@ func (v *Validator[P, T, TA, IA, DS]) VerifyTokenRequestFromRaw(ctx context.Cont
110115
}
111116

112117
func (v *Validator[P, T, TA, IA, DS]) VerifyTokenRequest(ledger driver.Ledger, signatureProvider driver.SignatureProvider, anchor string, tr *driver.TokenRequest, attributes driver.ValidationAttributes) ([]interface{}, driver.ValidationAttributes, error) {
113-
if err := v.verifyAuditorSignature(signatureProvider, attributes); err != nil {
118+
if err := v.verifyAuditing(tr, ledger, signatureProvider, attributes); err != nil {
114119
return nil, nil, errors.Wrapf(err, "failed to verifier auditor's signature [%s]", anchor)
115120
}
116121
ia, ta, err := v.ActionDeserializer.DeserializeActions(tr)
117122
if err != nil {
118123
return nil, nil, errors.Wrapf(err, "failed to unmarshal actions [%s]", anchor)
119124
}
120-
err = v.verifyIssues(ledger, ia, signatureProvider, attributes)
125+
err = v.verifyIssues(tr, ledger, ia, signatureProvider, attributes)
121126
if err != nil {
122127
return nil, nil, errors.Wrapf(err, "failed to verify issue actions [%s]", anchor)
123128
}
124-
err = v.verifyTransfers(ledger, ta, signatureProvider, attributes)
129+
err = v.verifyTransfers(tr, ledger, ta, signatureProvider, attributes)
125130
if err != nil {
126131
return nil, nil, errors.Wrapf(err, "failed to verify transfer actions [%s]", anchor)
127132
}
@@ -157,35 +162,34 @@ func (v *Validator[P, T, TA, IA, DS]) UnmarshalActions(raw []byte) ([]interface{
157162
return res, nil
158163
}
159164

160-
func (v *Validator[P, T, TA, IA, DS]) verifyAuditorSignature(signatureProvider driver.SignatureProvider, attributes driver.ValidationAttributes) error {
161-
if len(v.PublicParams.Auditors()) != 0 {
162-
auditor := v.PublicParams.Auditors()[0]
163-
verifier, err := v.Deserializer.GetAuditorVerifier(auditor)
164-
if err != nil {
165-
return errors.Errorf("failed to deserialize auditor's public key")
166-
}
167-
v.Logger.Infof("verify auditor signature for [%s]", auditor)
168-
_, err = signatureProvider.HasBeenSignedBy(auditor, verifier)
169-
return err
170-
}
171-
return nil
172-
}
173-
174-
func (v *Validator[P, T, TA, IA, DS]) verifyIssues(ledger driver.Ledger, issues []IA, signatureProvider driver.SignatureProvider, attributes driver.ValidationAttributes) error {
165+
func (v *Validator[P, T, TA, IA, DS]) verifyIssues(
166+
tokenRequest *driver.TokenRequest,
167+
ledger driver.Ledger,
168+
issues []IA,
169+
signatureProvider driver.SignatureProvider,
170+
attributes driver.ValidationAttributes,
171+
) error {
175172
for i, issue := range issues {
176-
if err := v.verifyIssue(issue, ledger, signatureProvider, attributes); err != nil {
173+
if err := v.verifyIssue(tokenRequest, issue, ledger, signatureProvider, attributes); err != nil {
177174
return errors.Wrapf(err, "failed to verify issue action at [%d]", i)
178175
}
179176
}
180177
return nil
181178
}
182179

183-
func (v *Validator[P, T, TA, IA, DS]) verifyIssue(tr IA, ledger driver.Ledger, signatureProvider driver.SignatureProvider, attributes driver.ValidationAttributes) error {
180+
func (v *Validator[P, T, TA, IA, DS]) verifyIssue(
181+
tokenRequest *driver.TokenRequest,
182+
action IA,
183+
ledger driver.Ledger,
184+
signatureProvider driver.SignatureProvider,
185+
attributes driver.ValidationAttributes,
186+
) error {
184187
context := &Context[P, T, TA, IA, DS]{
185188
Logger: v.Logger,
186189
PP: v.PublicParams,
190+
TokenRequest: tokenRequest,
187191
Deserializer: v.Deserializer,
188-
IssueAction: tr,
192+
IssueAction: action,
189193
Ledger: ledger,
190194
SignatureProvider: signatureProvider,
191195
MetadataCounter: map[string]int{},
@@ -205,30 +209,43 @@ func (v *Validator[P, T, TA, IA, DS]) verifyIssue(tr IA, ledger driver.Ledger, s
205209
}
206210
counter += c
207211
}
208-
if len(tr.GetMetadata()) != counter {
209-
return errors.Errorf("more metadata than those validated [%d]!=[%d], [%v]!=[%v]", len(tr.GetMetadata()), counter, tr.GetMetadata(), context.MetadataCounter)
212+
if len(action.GetMetadata()) != counter {
213+
return errors.Errorf("more metadata than those validated [%d]!=[%d], [%v]!=[%v]", len(action.GetMetadata()), counter, action.GetMetadata(), context.MetadataCounter)
210214
}
211215

212216
return nil
213217
}
214218

215-
func (v *Validator[P, T, TA, IA, DS]) verifyTransfers(ledger driver.Ledger, transferActions []TA, signatureProvider driver.SignatureProvider, attributes driver.ValidationAttributes) error {
219+
func (v *Validator[P, T, TA, IA, DS]) verifyTransfers(
220+
tokenRequest *driver.TokenRequest,
221+
ledger driver.Ledger,
222+
transferActions []TA,
223+
signatureProvider driver.SignatureProvider,
224+
attributes driver.ValidationAttributes,
225+
) error {
216226
v.Logger.Debugf("check sender start...")
217227
defer v.Logger.Debugf("check sender finished.")
218228
for i, action := range transferActions {
219-
if err := v.verifyTransfer(action, ledger, signatureProvider, attributes); err != nil {
229+
if err := v.verifyTransfer(tokenRequest, action, ledger, signatureProvider, attributes); err != nil {
220230
return errors.Wrapf(err, "failed to verify transfer action at [%d]", i)
221231
}
222232
}
223233
return nil
224234
}
225235

226-
func (v *Validator[P, T, TA, IA, DS]) verifyTransfer(tr TA, ledger driver.Ledger, signatureProvider driver.SignatureProvider, attributes driver.ValidationAttributes) error {
236+
func (v *Validator[P, T, TA, IA, DS]) verifyTransfer(
237+
tokenRequest *driver.TokenRequest,
238+
action TA,
239+
ledger driver.Ledger,
240+
signatureProvider driver.SignatureProvider,
241+
attributes driver.ValidationAttributes,
242+
) error {
227243
context := &Context[P, T, TA, IA, DS]{
228244
Logger: v.Logger,
229245
PP: v.PublicParams,
246+
TokenRequest: tokenRequest,
230247
Deserializer: v.Deserializer,
231-
TransferAction: tr,
248+
TransferAction: action,
232249
Ledger: ledger,
233250
SignatureProvider: signatureProvider,
234251
MetadataCounter: map[MetadataCounterID]int{},
@@ -248,13 +265,36 @@ func (v *Validator[P, T, TA, IA, DS]) verifyTransfer(tr TA, ledger driver.Ledger
248265
}
249266
counter += c
250267
}
251-
if len(tr.GetMetadata()) != counter {
252-
return errors.Errorf("more metadata than those validated [%d]!=[%d], [%v]!=[%v]", len(tr.GetMetadata()), counter, tr.GetMetadata(), context.MetadataCounter)
268+
if len(action.GetMetadata()) != counter {
269+
return errors.Errorf("more metadata than those validated [%d]!=[%d], [%v]!=[%v]", len(action.GetMetadata()), counter, action.GetMetadata(), context.MetadataCounter)
253270
}
254271

255272
return nil
256273
}
257274

275+
func (v *Validator[P, T, TA, IA, DS]) verifyAuditing(
276+
tokenRequest *driver.TokenRequest,
277+
ledger driver.Ledger,
278+
signatureProvider driver.SignatureProvider,
279+
attributes driver.ValidationAttributes,
280+
) error {
281+
context := &Context[P, T, TA, IA, DS]{
282+
Logger: v.Logger,
283+
PP: v.PublicParams,
284+
TokenRequest: tokenRequest,
285+
Deserializer: v.Deserializer,
286+
Ledger: ledger,
287+
SignatureProvider: signatureProvider,
288+
Attributes: attributes,
289+
}
290+
for _, v := range v.AuditingValidators {
291+
if err := v(context); err != nil {
292+
return err
293+
}
294+
}
295+
return nil
296+
}
297+
258298
func IsAnyNil[T any](args ...*T) bool {
259299
for _, arg := range args {
260300
if arg == nil {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package common
8+
9+
import (
10+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
11+
"github.com/pkg/errors"
12+
)
13+
14+
func AuditingSignaturesValidate[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](ctx *Context[P, T, TA, IA, DS]) error {
15+
if len(ctx.PP.Auditors()) == 0 {
16+
// enforce no auditor signatures are attached
17+
if len(ctx.TokenRequest.AuditorSignatures) != 0 {
18+
return errors.New("auditor signatures are not empty")
19+
}
20+
}
21+
22+
for _, auditorSignature := range ctx.TokenRequest.AuditorSignatures {
23+
auditor := auditorSignature.Identity
24+
verifier, err := ctx.Deserializer.GetAuditorVerifier(auditor)
25+
if err != nil {
26+
return errors.Errorf("failed to deserialize auditor's public key")
27+
}
28+
_, err = ctx.SignatureProvider.HasBeenSignedBy(auditor, verifier)
29+
if err != nil {
30+
return errors.Errorf("failed to verify auditor's signature")
31+
}
32+
}
33+
return nil
34+
35+
}

token/core/fabtoken/v1/validator/validator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type ValidateTransferFunc = common.ValidateTransferFunc[*setup.PublicParams, *ac
1818

1919
type ValidateIssueFunc = common.ValidateIssueFunc[*setup.PublicParams, *actions.Output, *actions.TransferAction, *actions.IssueAction, driver.Deserializer]
2020

21+
type ValidateAuditingFunc = common.ValidateAuditingFunc[*setup.PublicParams, *actions.Output, *actions.TransferAction, *actions.IssueAction, driver.Deserializer]
22+
2123
type ActionDeserializer struct{}
2224

2325
func (a *ActionDeserializer) DeserializeActions(tr *driver.TokenRequest) ([]*actions.IssueAction, []*actions.TransferAction, error) {
@@ -59,12 +61,17 @@ func NewValidator(logger logging.Logger, pp *setup.PublicParams, deserializer dr
5961
IssueValidate,
6062
}
6163

64+
auditingValidators := []ValidateAuditingFunc{
65+
common.AuditingSignaturesValidate[*setup.PublicParams, *actions.Output, *actions.TransferAction, *actions.IssueAction, driver.Deserializer],
66+
}
67+
6268
return common.NewValidator[*setup.PublicParams, *actions.Output, *actions.TransferAction, *actions.IssueAction, driver.Deserializer](
6369
logger,
6470
pp,
6571
deserializer,
6672
&ActionDeserializer{},
6773
transferValidators,
6874
issueValidators,
75+
auditingValidators,
6976
)
7077
}

token/core/zkatdlog/nogh/v1/validator/validator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type ValidateTransferFunc = common.ValidateTransferFunc[*v1.PublicParams, *token
2020

2121
type ValidateIssueFunc = common.ValidateIssueFunc[*v1.PublicParams, *token.Token, *transfer.Action, *issue.Action, driver.Deserializer]
2222

23+
type ValidateAuditingFunc = common.ValidateAuditingFunc[*v1.PublicParams, *token.Token, *transfer.Action, *issue.Action, driver.Deserializer]
24+
2325
type Context = common.Context[*v1.PublicParams, *token.Token, *transfer.Action, *issue.Action, driver.Deserializer]
2426

2527
type ActionDeserializer struct {
@@ -69,12 +71,17 @@ func New(
6971
IssueValidate,
7072
}
7173

74+
auditingValidators := []ValidateAuditingFunc{
75+
common.AuditingSignaturesValidate[*v1.PublicParams, *token.Token, *transfer.Action, *issue.Action, driver.Deserializer],
76+
}
77+
7278
return common.NewValidator[*v1.PublicParams, *token.Token, *transfer.Action, *issue.Action, driver.Deserializer](
7379
logger,
7480
pp,
7581
deserializer,
7682
&ActionDeserializer{},
7783
transferValidators,
7884
issueValidators,
85+
auditingValidators,
7986
)
8087
}

token/core/zkatdlog/nogh/v1/validator/validator_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ var _ = Describe("validator", func() {
134134
Expect(err).NotTo(HaveOccurred())
135135
sigma, err := auditor.Endorse(ar, "2")
136136
Expect(err).NotTo(HaveOccurred())
137-
ar.AuditorSignatures = append(ar.AuditorSignatures, sigma)
137+
ar.AuditorSignatures = append(ar.AuditorSignatures, &driver.AuditorSignature{
138+
Identity: araw,
139+
Signature: sigma,
140+
})
138141

139142
ar.Signatures = append(ar.Signatures, signatures...)
140143
})
@@ -474,7 +477,12 @@ func prepareIssue(auditor *audit.Auditor, issuer *issue2.Issuer, issuerIdentity
474477
Expect(err).NotTo(HaveOccurred())
475478
sigma, err := auditor.Endorse(ir, "1")
476479
Expect(err).NotTo(HaveOccurred())
477-
ir.AuditorSignatures = append(ir.AuditorSignatures, sigma)
480+
araw, err := auditor.Signer.Serialize()
481+
Expect(err).NotTo(HaveOccurred())
482+
ir.AuditorSignatures = append(ir.AuditorSignatures, &driver.AuditorSignature{
483+
Identity: araw,
484+
Signature: sigma,
485+
})
478486

479487
return ir, issueMetadata
480488
}
@@ -571,7 +579,12 @@ func prepareTransfer(pp *v1.PublicParams, signer driver.SigningIdentity, auditor
571579

572580
sigma, err := auditor.Endorse(tr, "1")
573581
Expect(err).NotTo(HaveOccurred())
574-
tr.AuditorSignatures = append(tr.AuditorSignatures, sigma)
582+
araw, err := auditor.Signer.Serialize()
583+
Expect(err).NotTo(HaveOccurred())
584+
tr.AuditorSignatures = append(tr.AuditorSignatures, &driver.AuditorSignature{
585+
Identity: araw,
586+
Signature: sigma,
587+
})
575588

576589
signatures, err := sender.SignTokenActions(raw)
577590
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)