Skip to content

Commit 6e8a3da

Browse files
committed
not validate the issuer for transfers
Signed-off-by: Angelo De Caro <[email protected]>
1 parent 709c838 commit 6e8a3da

File tree

5 files changed

+91
-28
lines changed

5 files changed

+91
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (s *IssueService) VerifyIssue(ctx context.Context, ia driver.IssueAction, o
224224
if err := metadata.Deserialize(outputMetadata[i].OutputMetadata); err != nil {
225225
return errors.Wrap(err, "failed unmarshalling metadata")
226226
}
227-
if err := metadata.Validate(); err != nil {
227+
if err := metadata.Validate(true); err != nil {
228228
return errors.Wrap(err, "invalid metadata")
229229
}
230230

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package token
8+
9+
import "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
10+
11+
var (
12+
// ErrEmptyType is returned when a token type is empty
13+
ErrEmptyType = errors.New("missing Type")
14+
// ErrEmptyValue is returned when a token value is nil
15+
ErrEmptyValue = errors.New("missing Value")
16+
// ErrEmptyBlindingFactor is returned when a token blinding factor is nil
17+
ErrEmptyBlindingFactor = errors.New("missing BlindingFactor")
18+
// ErrMissingIssuer is returned when an issuer is required but missing
19+
ErrMissingIssuer = errors.New("missing Issuer")
20+
// ErrUnexpectedIssuer is returned when an issuer is present but should not be
21+
ErrUnexpectedIssuer = errors.New("issuer should not be there")
22+
// ErrEmptyOwner is returned when a token owner is empty
23+
ErrEmptyOwner = errors.New("token owner cannot be empty")
24+
// ErrEmptyTokenData is returned when token data is nil
25+
ErrEmptyTokenData = errors.New("token data cannot be empty")
26+
// ErrNilCommitElement is returned when trying to commit a nil element
27+
ErrNilCommitElement = errors.New("cannot commit a nil element")
28+
// ErrTokenMismatch is returned when a token does not match its metadata
29+
ErrTokenMismatch = errors.New("cannot retrieve token in the clear: output does not match provided opening")
30+
// ErrMissingFabToken is returned when FabToken is nil in UpgradeWitness
31+
ErrMissingFabToken = errors.New("missing FabToken")
32+
// ErrMissingFabTokenOwner is returned when FabToken owner is empty
33+
ErrMissingFabTokenOwner = errors.New("missing FabToken.Owner")
34+
// ErrMissingFabTokenType is returned when FabToken type is empty
35+
ErrMissingFabTokenType = errors.New("missing FabToken.Type")
36+
// ErrMissingFabTokenQuantity is returned when FabToken quantity is empty
37+
ErrMissingFabTokenQuantity = errors.New("missing FabToken.Quantity")
38+
// ErrMissingUpgradeBlindingFactor is returned when upgrade blinding factor is nil
39+
ErrMissingUpgradeBlindingFactor = errors.New("missing BlindingFactor")
40+
)

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (t *Token) ToClear(meta *Metadata, pp *noghv1.PublicParams) (*token.Token,
7777
}
7878
// check that token matches meta
7979
if !com.Equals(t.Data) {
80-
return nil, errors.New("cannot retrieve token in the clear: output does not match provided opening")
80+
return nil, ErrTokenMismatch
8181
}
8282
return &token.Token{
8383
Type: meta.Type,
@@ -88,10 +88,10 @@ func (t *Token) ToClear(meta *Metadata, pp *noghv1.PublicParams) (*token.Token,
8888

8989
func (t *Token) Validate(checkOwner bool) error {
9090
if checkOwner && len(t.Owner) == 0 {
91-
return errors.Errorf("token owner cannot be empty")
91+
return ErrEmptyOwner
9292
}
9393
if t.Data == nil {
94-
return errors.Errorf("token data cannot be empty")
94+
return ErrEmptyTokenData
9595
}
9696
return nil
9797
}
@@ -202,18 +202,24 @@ func (m *Metadata) Clone() *Metadata {
202202
}
203203
}
204204

205-
func (m *Metadata) Validate() error {
205+
// Validate checks that Metadata is well-formed.
206+
// If checkIssuer is true, it checks that the Issuer field is set.
207+
// If checkIssuer is false, it checks that the Issuer field is not set.
208+
func (m *Metadata) Validate(checkIssuer bool) error {
206209
if len(m.Type) == 0 {
207-
return errors.New("missing Type")
210+
return ErrEmptyType
208211
}
209212
if m.Value == nil {
210-
return errors.New("missing Value")
213+
return ErrEmptyValue
211214
}
212215
if m.BlindingFactor == nil {
213-
return errors.New("missing BlindingFactor")
216+
return ErrEmptyBlindingFactor
214217
}
215-
if len(m.Issuer) == 0 {
216-
return errors.New("missing Issuer")
218+
if checkIssuer && len(m.Issuer) == 0 {
219+
return ErrMissingIssuer
220+
}
221+
if !checkIssuer && len(m.Issuer) != 0 {
222+
return ErrUnexpectedIssuer
217223
}
218224
return nil
219225
}
@@ -222,7 +228,7 @@ func commit(vector []*math.Zr, generators []*math.G1, c *math.Curve) (*math.G1,
222228
com := c.NewG1()
223229
for i := range vector {
224230
if vector[i] == nil {
225-
return nil, errors.New("cannot commit a nil element")
231+
return nil, ErrNilCommitElement
226232
}
227233
com.Add(generators[i].Mul(vector[i]))
228234
}
@@ -237,19 +243,19 @@ type UpgradeWitness struct {
237243

238244
func (u *UpgradeWitness) Validate() error {
239245
if u.FabToken == nil {
240-
return errors.New("missing FabToken")
246+
return ErrMissingFabToken
241247
}
242248
if len(u.FabToken.Owner) == 0 {
243-
return errors.New("missing FabToken.Owner")
249+
return ErrMissingFabTokenOwner
244250
}
245251
if len(u.FabToken.Type) == 0 {
246-
return errors.New("missing FabToken.Type")
252+
return ErrMissingFabTokenType
247253
}
248254
if len(u.FabToken.Quantity) == 0 {
249-
return errors.New("missing FabToken.Quantity")
255+
return ErrMissingFabTokenQuantity
250256
}
251257
if u.BlindingFactor == nil {
252-
return errors.New("missing BlindingFactor")
258+
return ErrMissingUpgradeBlindingFactor
253259
}
254260
return nil
255261
}

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ func TestMetadata_Validate(t *testing.T) {
2828
}
2929

3030
tests := []struct {
31-
name string
32-
meta *Metadata
33-
wantErr string
31+
name string
32+
meta *Metadata
33+
checkIssuer bool
34+
wantErr string
3435
}{
3536
{
36-
name: "valid metadata",
37-
meta: validMetadata,
38-
wantErr: "",
37+
name: "valid metadata",
38+
meta: validMetadata,
39+
checkIssuer: true,
40+
wantErr: "",
3941
},
4042
{
4143
name: "missing type",
@@ -45,7 +47,8 @@ func TestMetadata_Validate(t *testing.T) {
4547
BlindingFactor: validMetadata.BlindingFactor,
4648
Issuer: validMetadata.Issuer,
4749
},
48-
wantErr: "missing Type",
50+
checkIssuer: true,
51+
wantErr: ErrEmptyType.Error(),
4952
},
5053
{
5154
name: "missing value",
@@ -55,7 +58,8 @@ func TestMetadata_Validate(t *testing.T) {
5558
BlindingFactor: validMetadata.BlindingFactor,
5659
Issuer: validMetadata.Issuer,
5760
},
58-
wantErr: "missing Value",
61+
checkIssuer: true,
62+
wantErr: ErrEmptyValue.Error(),
5963
},
6064
{
6165
name: "missing blinding factor",
@@ -65,7 +69,8 @@ func TestMetadata_Validate(t *testing.T) {
6569
BlindingFactor: nil,
6670
Issuer: validMetadata.Issuer,
6771
},
68-
wantErr: "missing BlindingFactor",
72+
checkIssuer: true,
73+
wantErr: ErrEmptyBlindingFactor.Error(),
6974
},
7075
{
7176
name: "missing issuer",
@@ -75,13 +80,25 @@ func TestMetadata_Validate(t *testing.T) {
7580
BlindingFactor: validMetadata.BlindingFactor,
7681
Issuer: nil,
7782
},
78-
wantErr: "missing Issuer",
83+
checkIssuer: true,
84+
wantErr: ErrMissingIssuer.Error(),
85+
},
86+
{
87+
name: "should not have the issuer",
88+
meta: &Metadata{
89+
Type: validMetadata.Type,
90+
Value: validMetadata.Value,
91+
BlindingFactor: validMetadata.BlindingFactor,
92+
Issuer: validMetadata.Issuer,
93+
},
94+
checkIssuer: false,
95+
wantErr: ErrUnexpectedIssuer.Error(),
7996
},
8097
}
8198

8299
for _, tt := range tests {
83100
t.Run(tt.name, func(t *testing.T) {
84-
err := tt.meta.Validate()
101+
err := tt.meta.Validate(tt.checkIssuer)
85102
if tt.wantErr == "" {
86103
assert.NoError(t, err)
87104
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func (s *TransferService) VerifyTransfer(ctx context.Context, transferAction dri
291291
if err := metadata.Deserialize(outputMetadata[i].OutputMetadata); err != nil {
292292
return errors.Wrap(err, "failed unmarshalling metadata")
293293
}
294-
if err := metadata.Validate(); err != nil {
294+
if err := metadata.Validate(false); err != nil {
295295
return errors.Wrap(err, "invalid metadata")
296296
}
297297

0 commit comments

Comments
 (0)