Skip to content

Commit 151fafc

Browse files
committed
commnets
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent bfe5e0d commit 151fafc

6 files changed

Lines changed: 376 additions & 15 deletions

File tree

token/core/common/auditor.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,11 @@ func RetrieveAuditTokens(
293293
return nil, errors.WithMessagef(err, "failed to retrieve audit tokens for tx [%s]", anchor)
294294
}
295295

296-
// Build the token map using token ID string as key
296+
// Build the token map using token ID string as key.
297+
// Tokens is in order of the ids.
297298
auditTokens := make(map[string]*token.Token, len(tokens))
298-
for i, tok := range tokens {
299-
if tok != nil && i < len(tokenIDs) {
300-
auditTokens[tokenIDs[i].String()] = tok
301-
}
299+
for i, id := range tokenIDs {
300+
auditTokens[id.String()] = tokens[i]
302301
}
303302
logger.DebugfContext(ctx, "[%s] retrieved [%d] audit tokens", anchor, len(auditTokens))
304303

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"encoding/json"
1313
"maps"
1414
"os"
15-
"strconv"
1615

1716
math "github.com/IBM/mathlib"
1817
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
@@ -717,10 +716,7 @@ func prepareTransfer(
717716
if err != nil {
718717
return nil, nil, nil, nil, errors.Wrap(err, "failed to serialize metadata for loader")
719718
}
720-
key := ids[i].TxId
721-
if ids[i].Index != 0 {
722-
key = ids[i].TxId + ":" + strconv.FormatUint(ids[i].Index, 10)
723-
}
719+
key := ids[i].String()
724720
tokenLoaderMap[key] = v1.LoadedToken{
725721
Token: tokenRaw,
726722
Metadata: metadataRaw,

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package testutils
99
import (
1010
"context"
1111
"math/big"
12-
"strconv"
1312

1413
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1514
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1"
@@ -163,10 +162,7 @@ type testTokenLoader struct {
163162
func (t *testTokenLoader) LoadTokens(ctx context.Context, ids []*token2.ID) ([]v1.LoadedToken, error) {
164163
result := make([]v1.LoadedToken, len(ids))
165164
for i, id := range ids {
166-
key := id.TxId
167-
if id.Index != 0 {
168-
key = id.TxId + ":" + strconv.FormatUint(id.Index, 10)
169-
}
165+
key := id.String()
170166
tok, ok := t.tokens[key]
171167
if !ok {
172168
return nil, errors.Errorf("token not found: %s", key)

token/driver/request.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,60 @@ func (r *TokenRequest) FromProtos(tr *request.TokenRequest) error {
340340
r.Signatures = append(r.Signatures, requestSignature)
341341
}
342342

343+
// Validate the structural integrity of the token request
344+
if err := r.Validate(); err != nil {
345+
return errors.Wrap(err, "token request validation failed")
346+
}
347+
348+
return nil
349+
}
350+
351+
// Validate checks the structural validity of the TokenRequest.
352+
// It ensures that all required fields are present and non-empty.
353+
// This method should be called after unmarshalling to verify the request is well-formed.
354+
//
355+
// Special case: Empty requests (nil Actions or empty Actions slice) are allowed
356+
// to support test scenarios where requests are created but not yet populated.
357+
// This is necessary because ToProtos() sets Version=ProtocolV1 even for empty requests.
358+
//
359+
// Returns an error if:
360+
// - Any action in Actions is nil
361+
// - Any action has empty Raw bytes
362+
// - Any signature in Signatures is nil
363+
// - Any signature has empty signature bytes
364+
func (r *TokenRequest) Validate() error {
365+
// If we have actions, validate them
366+
367+
for i, action := range r.Actions {
368+
if action == nil {
369+
return errors.Errorf("action at index %d is nil", i)
370+
}
371+
if len(action.Raw) == 0 {
372+
return errors.Errorf("action at index %d has empty Raw bytes", i)
373+
}
374+
}
375+
376+
// Validate signatures (if present)
377+
for i, sig := range r.Signatures {
378+
if sig == nil {
379+
return errors.Errorf("signature at index %d is nil", i)
380+
}
381+
382+
// Check that signature has either Action or Auditor signature with non-empty bytes
383+
switch {
384+
case sig.Action != nil:
385+
if len(sig.Action.Signature) == 0 {
386+
return errors.Errorf("action signature at index %d has empty signature bytes", i)
387+
}
388+
case sig.Auditor != nil:
389+
if len(sig.Auditor.Signature) == 0 {
390+
return errors.Errorf("auditor signature at index %d has empty signature bytes", i)
391+
}
392+
default:
393+
return errors.Errorf("signature at index %d has neither action nor auditor signature", i)
394+
}
395+
}
396+
343397
return nil
344398
}
345399

0 commit comments

Comments
 (0)