Skip to content

Commit 366c8e2

Browse files
authored
make generators versioned, add input interface for validator context (#1258)
Signed-off-by: Arne Rutjes <arne123@gmail.com>
1 parent b6c190f commit 366c8e2

File tree

12 files changed

+53
-32
lines changed

12 files changed

+53
-32
lines changed

integration/nwo/token/generators/pp/fabtokenv1/gen.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,27 @@ import (
1313
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1414
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections"
1515
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/topology"
16-
fabtokenv1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/setup"
16+
"github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/setup"
17+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
1718
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity"
1819
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/storage/kvs"
1920
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/x509"
2021
)
2122

22-
type FabTokenPublicParamsGenerator struct{}
23+
const DefaultDriverVersion = setup.ProtocolV1
2324

24-
func NewFabTokenPublicParamsGenerator() *FabTokenPublicParamsGenerator {
25-
return &FabTokenPublicParamsGenerator{}
25+
type FabTokenPublicParamsGenerator struct {
26+
DriverVersion driver.TokenDriverVersion
27+
}
28+
29+
func NewFabTokenPublicParamsGenerator(version driver.TokenDriverVersion) *FabTokenPublicParamsGenerator {
30+
return &FabTokenPublicParamsGenerator{
31+
DriverVersion: version,
32+
}
2633
}
2734

2835
func (f *FabTokenPublicParamsGenerator) Generate(tms *topology.TMS, wallets *topology.Wallets, args ...interface{}) ([]byte, error) {
29-
precision := fabtokenv1.DefaultPrecision
36+
precision := setup.DefaultPrecision
3037
if len(args) == 2 {
3138
// First is empty
3239

@@ -41,7 +48,7 @@ func (f *FabTokenPublicParamsGenerator) Generate(tms *topology.TMS, wallets *top
4148
return nil, errors.Wrapf(err, "failed to parse max token value [%s] to uint64", precisionStr)
4249
}
4350
}
44-
pp, err := fabtokenv1.Setup(precision)
51+
pp, err := setup.WithVersion(precision, f.DriverVersion)
4552
if err != nil {
4653
return nil, err
4754
}

integration/nwo/token/generators/pp/zkatdlognoghv1/gen.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@ import (
1818
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections"
1919
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/zkatdlognoghv1"
2020
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/topology"
21-
dlognoghv1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
21+
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
22+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
2223
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity"
2324
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/storage/kvs"
2425
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/x509"
2526
)
2627

28+
const DefaultDriverVersion = setup.ProtocolV1
29+
2730
type DLogPublicParamsGenerator struct {
2831
DefaultCurveID math3.CurveID
32+
DriverVersion driver.TokenDriverVersion
2933
}
3034

31-
func NewDLogPublicParamsGenerator(defaultCurveID math3.CurveID) *DLogPublicParamsGenerator {
32-
return &DLogPublicParamsGenerator{DefaultCurveID: defaultCurveID}
35+
// NewDLogPublicParamsGenerator creates a new generator. The version is optional and defaults to v1.
36+
func NewDLogPublicParamsGenerator(defaultCurveID math3.CurveID, version driver.TokenDriverVersion) *DLogPublicParamsGenerator {
37+
return &DLogPublicParamsGenerator{
38+
DefaultCurveID: defaultCurveID,
39+
DriverVersion: version,
40+
}
3341
}
3442

3543
func (d *DLogPublicParamsGenerator) Generate(tms *topology.TMS, wallets *topology.Wallets, args ...interface{}) ([]byte, error) {
@@ -63,7 +71,7 @@ func (d *DLogPublicParamsGenerator) Generate(tms *topology.TMS, wallets *topolog
6371
return nil, err
6472
}
6573
}
66-
pp, err := dlognoghv1.Setup(bits, ipkBytes, curveID)
74+
pp, err := setup.WithVersion(bits, ipkBytes, curveID, d.DriverVersion)
6775
if err != nil {
6876
return nil, err
6977
}

integration/nwo/token/platform.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators"
2626
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/fabtokenv1"
2727
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/zkatdlognoghv1"
28-
fabtokenv2 "github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/pp/fabtokenv1"
29-
common2 "github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/pp/zkatdlognoghv1"
28+
fabtokengen "github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/pp/fabtokenv1"
29+
zkatgen "github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/pp/zkatdlognoghv1"
3030
topology2 "github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/topology"
3131
"github.com/hyperledger-labs/fabric-token-sdk/token"
3232
"github.com/hyperledger-labs/fabric-token-sdk/token/services/logging"
@@ -84,8 +84,8 @@ func NewPlatform(ctx api2.Context, t api2.Topology, builder api2.Builder) *Platf
8484
TokenGenPath: DefaultTokenGenPath,
8585
NetworkHandlers: map[string]NetworkHandler{},
8686
}
87-
p.PublicParamsGenerators[fabtokenv1.DriverIdentifier] = fabtokenv2.NewFabTokenPublicParamsGenerator()
88-
p.PublicParamsGenerators[zkatdlognoghv1.DriverIdentifier] = common2.NewDLogPublicParamsGenerator(math3.BN254)
87+
p.PublicParamsGenerators[fabtokenv1.DriverIdentifier] = fabtokengen.NewFabTokenPublicParamsGenerator(fabtokengen.DefaultDriverVersion)
88+
p.PublicParamsGenerators[zkatdlognoghv1.DriverIdentifier] = zkatgen.NewDLogPublicParamsGenerator(math3.BN254, zkatgen.DefaultDriverVersion)
8989

9090
return p
9191
}

token/core/common/validator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
TokenRequestSignatures driver.ValidationAttributeID = "sigs"
2323
)
2424

25-
type Context[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] struct {
25+
type Context[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] struct {
2626
Logger logging.Logger
2727
PP P
2828
Anchor driver.TokenRequestAnchor
@@ -42,17 +42,17 @@ func (c *Context[P, T, TA, IA, DS]) CountMetadataKey(key string) {
4242
c.MetadataCounter[key] = c.MetadataCounter[key] + 1
4343
}
4444

45-
type ValidateTransferFunc[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(c context.Context, ctx *Context[P, T, TA, IA, DS]) error
45+
type ValidateTransferFunc[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(c context.Context, ctx *Context[P, T, TA, IA, DS]) error
4646

47-
type ValidateIssueFunc[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(c context.Context, ctx *Context[P, T, TA, IA, DS]) error
47+
type ValidateIssueFunc[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(c context.Context, ctx *Context[P, T, TA, IA, DS]) error
4848

49-
type ValidateAuditingFunc[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(c context.Context, ctx *Context[P, T, TA, IA, DS]) error
49+
type ValidateAuditingFunc[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] func(c context.Context, ctx *Context[P, T, TA, IA, DS]) error
5050

5151
type ActionDeserializer[TA driver.TransferAction, IA driver.IssueAction] interface {
5252
DeserializeActions(tr *driver.TokenRequest) ([]IA, []TA, error)
5353
}
5454

55-
type Validator[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] struct {
55+
type Validator[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer] struct {
5656
Logger logging.Logger
5757
PublicParams P
5858
Deserializer DS
@@ -63,7 +63,7 @@ type Validator[P driver.PublicParameters, T any, TA driver.TransferAction, IA dr
6363
IssueValidators []ValidateIssueFunc[P, T, TA, IA, DS]
6464
}
6565

66-
func NewValidator[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](
66+
func NewValidator[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](
6767
Logger logging.Logger,
6868
publicParams P,
6969
deserializer DS,

token/core/common/validator_auditing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
ErrAuditorSignaturesPresent = errors.New("auditor signatures present")
2020
)
2121

22-
func AuditingSignaturesValidate[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](c context.Context, ctx *Context[P, T, TA, IA, DS]) error {
22+
func AuditingSignaturesValidate[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](c context.Context, ctx *Context[P, T, TA, IA, DS]) error {
2323
if len(ctx.PP.Auditors()) == 0 {
2424
// enforce no auditor signatures are attached
2525
if len(ctx.TokenRequest.AuditorSignatures) != 0 {

token/core/common/validator_auditing_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
type (
20-
TestContext = Context[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer]
20+
TestContext = Context[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer]
2121
TestCheck = func() bool
2222
)
2323

token/core/common/validator_issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
// IssueApplicationDataValidate accepts any metadata in the "pub" namespace.
1818
// This gives the user of the Token SDK the option to attach public data to the token transaction.
19-
func IssueApplicationDataValidate[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](c context.Context, ctx *Context[P, T, TA, IA, DS]) error {
19+
func IssueApplicationDataValidate[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](c context.Context, ctx *Context[P, T, TA, IA, DS]) error {
2020
for key := range ctx.IssueAction.GetMetadata() {
2121
if strings.HasPrefix(key, meta.PublicMetadataPrefix) {
2222
ctx.CountMetadataKey(key)

token/core/common/validator_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@ import (
2121
func TestAnchorInContext(t *testing.T) {
2222
anchor := driver.TokenRequestAnchor("hello world")
2323
anotherAnchor := driver.TokenRequestAnchor("another anchor")
24-
v := NewValidator[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer](
24+
v := NewValidator[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer](
2525
nil,
2626
nil,
2727
nil,
2828
nil,
29-
[]ValidateTransferFunc[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer]{
30-
func(c context.Context, ctx *Context[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer]) error {
29+
[]ValidateTransferFunc[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer]{
30+
func(c context.Context, ctx *Context[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer]) error {
3131
if anchor != ctx.Anchor {
3232
return fmt.Errorf("transfer, anchor does not match, expected %s, got %s", anchor, ctx.Anchor)
3333
}
3434
return nil
3535
},
3636
},
37-
[]ValidateIssueFunc[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer]{
38-
func(c context.Context, ctx *Context[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer]) error {
37+
[]ValidateIssueFunc[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer]{
38+
func(c context.Context, ctx *Context[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer]) error {
3939
if anchor != ctx.Anchor {
4040
return fmt.Errorf("issue, anchor does not match, expected %s, got %s", anchor, ctx.Anchor)
4141
}
4242
return nil
4343
},
4444
},
45-
[]ValidateAuditingFunc[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer]{
46-
func(c context.Context, ctx *Context[driver.PublicParameters, any, driver.TransferAction, driver.IssueAction, driver.Deserializer]) error {
45+
[]ValidateAuditingFunc[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer]{
46+
func(c context.Context, ctx *Context[driver.PublicParameters, driver.Input, driver.TransferAction, driver.IssueAction, driver.Deserializer]) error {
4747
if anchor != ctx.Anchor {
4848
return fmt.Errorf("audit, anchor does not match, expected %s, got %s", anchor, ctx.Anchor)
4949
}

token/core/common/validator_transfer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
// TransferApplicationDataValidate accepts any metadata in the "pub" namespace.
1818
// This gives the user of the Token SDK the option to attach public data to the token transaction.
19-
func TransferApplicationDataValidate[P driver.PublicParameters, T any, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](c context.Context, ctx *Context[P, T, TA, IA, DS]) error {
19+
func TransferApplicationDataValidate[P driver.PublicParameters, T driver.Input, TA driver.TransferAction, IA driver.IssueAction, DS driver.Deserializer](c context.Context, ctx *Context[P, T, TA, IA, DS]) error {
2020
for key := range ctx.TransferAction.GetMetadata() {
2121
if strings.HasPrefix(key, meta.PublicMetadataPrefix) {
2222
ctx.CountMetadataKey(key)

0 commit comments

Comments
 (0)