From d8ea652107310a5d6769dd3c8adbcc2c51da322e Mon Sep 17 00:00:00 2001 From: x1m3 Date: Tue, 15 Oct 2024 18:05:31 +0200 Subject: [PATCH 1/8] chore: Add optional parameter to packer.Unpack --- packager.go | 10 +++++----- packers/anoncrypt.go | 2 +- packers/jws.go | 2 +- packers/plain.go | 2 +- packers/zkp.go | 36 +++++++++++++++++++++++++++++++++--- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/packager.go b/packager.go index 8b7765d..12c92f5 100644 --- a/packager.go +++ b/packager.go @@ -16,7 +16,7 @@ type Packer interface { // Pack a payload of type ContentType in an Iden3 compliant format using the packer identity Pack(payload []byte, params PackerParams) ([]byte, error) // Unpack an envelope in Iden3 compliant format. - Unpack(envelope []byte) (*BasicMessage, error) + Unpack(envelope []byte, params ...PackerParams) (*BasicMessage, error) // MediaType returns content type of message MediaType() MediaType } @@ -65,14 +65,14 @@ func (r *PackageManager) Pack(mediaType MediaType, payload []byte, params Packer // Unpack returns iden3 message method from envelope // if it's not valid or can't be decrypted error is returned -func (r *PackageManager) Unpack(envelope []byte) (*BasicMessage, MediaType, error) { +func (r *PackageManager) Unpack(envelope []byte, params ...PackerParams) (*BasicMessage, MediaType, error) { safeEnvelope := strings.Trim(strings.TrimSpace(string(envelope)), "\"") mediaType, err := r.GetMediaType([]byte(safeEnvelope)) if err != nil { return nil, "", err } - msg, err := r.unpackSafeEnvelope(mediaType, []byte(safeEnvelope)) + msg, err := r.unpackSafeEnvelope(mediaType, []byte(safeEnvelope), params...) if err != nil { return nil, mediaType, err } @@ -85,14 +85,14 @@ func (r *PackageManager) UnpackWithType(mediaType MediaType, envelope []byte) (* return r.unpackSafeEnvelope(mediaType, []byte(safeEnvelope)) } -func (r *PackageManager) unpackSafeEnvelope(mediaType MediaType, envelope []byte) (*BasicMessage, error) { +func (r *PackageManager) unpackSafeEnvelope(mediaType MediaType, envelope []byte, params ...PackerParams) (*BasicMessage, error) { p, ok := r.packers[mediaType] if !ok { return nil, errors.Errorf("packer for media type %s doesn't exist", mediaType) } // safeEnvelope can be rather base64 encoded or valid json - msg, err := p.Unpack(envelope) + msg, err := p.Unpack(envelope, params...) if err != nil { return nil, err } diff --git a/packers/anoncrypt.go b/packers/anoncrypt.go index f0fb353..df6cf56 100644 --- a/packers/anoncrypt.go +++ b/packers/anoncrypt.go @@ -65,7 +65,7 @@ func (p *AnoncryptPacker) Pack(payload []byte, params iden3comm.PackerParams) ([ } // Unpack returns unpacked message from transport envelope -func (p *AnoncryptPacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, error) { +func (p *AnoncryptPacker) Unpack(envelope []byte, _ ...iden3comm.PackerParams) (*iden3comm.BasicMessage, error) { jwe, err := jose.ParseEncrypted(string(envelope)) if err != nil { diff --git a/packers/jws.go b/packers/jws.go index 0d271b9..ca1c698 100644 --- a/packers/jws.go +++ b/packers/jws.go @@ -200,7 +200,7 @@ func (p *JWSPacker) Pack( } // Unpack returns unpacked message from transport envelope with verification of signature -func (p *JWSPacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, error) { +func (p *JWSPacker) Unpack(envelope []byte, _ ...iden3comm.PackerParams) (*iden3comm.BasicMessage, error) { token, err := jws.Parse(envelope) if err != nil { diff --git a/packers/plain.go b/packers/plain.go index 99eb589..10a4312 100644 --- a/packers/plain.go +++ b/packers/plain.go @@ -32,7 +32,7 @@ func (p *PlainMessagePacker) Pack(payload []byte, _ iden3comm.PackerParams) ([]b } // Unpack returns unpacked message from transport envelope -func (p *PlainMessagePacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, error) { +func (p *PlainMessagePacker) Unpack(envelope []byte, _ ...iden3comm.PackerParams) (*iden3comm.BasicMessage, error) { var msg iden3comm.BasicMessage err := json.Unmarshal(envelope, &msg) diff --git a/packers/zkp.go b/packers/zkp.go index ec95640..b187ccd 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -133,15 +133,45 @@ func (p *ZKPPacker) Pack(payload []byte, params iden3comm.PackerParams) ([]byte, return []byte(tokenStr), nil } -// Unpack returns unpacked message from transport envelope with verification of zeroknowledge proof -func (p *ZKPPacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, error) { +// ZKPPUnpackerParams is params for zkp unpacker +type ZKPPUnpackerParams struct { + authVerifyDelay time.Duration + ethResolver map[int]eth.Resolver + verificationKey []byte + iden3comm.PackerParams +} + +// NewZKPPUnpackerParams creates new zkp unpacker params +func NewZKPPUnpackerParams(verificationKey []byte, resolvers map[int]eth.Resolver, authVerifyDelay time.Duration) ZKPPUnpackerParams { + return ZKPPUnpackerParams{ + authVerifyDelay: authVerifyDelay, + ethResolver: resolvers, + verificationKey: verificationKey, + } +} + +// Unpack returns unpacked message from transport envelope with verification of zero knowledge proof +// params is variadic but only none or one is accepted +func (p *ZKPPacker) Unpack(envelope []byte, params ...iden3comm.PackerParams) (*iden3comm.BasicMessage, error) { + + if len(params) > 1 { + return nil, errors.New("expecting no more than one parameter in ZKPPacker Unpack") + } + unpacker := p + if len(params) == 1 { + zkParams, ok := (params[0]).(ZKPPUnpackerParams) + if !ok { + return nil, errors.New("can't cast params to zkp unpacker params") + } + unpacker = DefaultZKPUnpacker(zkParams.verificationKey, zkParams.ethResolver, WithAuthVerifyDelay(zkParams.authVerifyDelay)) + } token, err := jwz.Parse(string(envelope)) if err != nil { return nil, err } - verificationKey, ok := p.Verification[jwz.ProvingMethodAlg{Alg: token.Alg, CircuitID: token.CircuitID}] + verificationKey, ok := unpacker.Verification[jwz.ProvingMethodAlg{Alg: token.Alg, CircuitID: token.CircuitID}] if !ok { return nil, fmt.Errorf("message was packed with unsupported circuit `%s` and alg `%s`", token.CircuitID, token.Alg) From 14c597734e9db3b77133adf979af5c84abcbfb15 Mon Sep 17 00:00:00 2001 From: x1m3 Date: Thu, 17 Oct 2024 12:20:52 +0200 Subject: [PATCH 2/8] chore: Check ethereum resolver not found --- packers/zkp.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packers/zkp.go b/packers/zkp.go index b187ccd..9d7ca57 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -321,7 +321,10 @@ func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.Circui userDID.String(), err) } - resolver := d.resolvers[int(chainID)] + resolver, found := d.resolvers[int(chainID)] + if !found { + return errors.Errorf("resolver for chainID '%d' not found", chainID) + } globalState := authPubSignals.GISTRoot.BigInt() globalStateInfo, err := resolver.ResolveGist(context.Background(), &services.ResolverOpts{GistRoot: globalState}) From 011c8a15266f86fa5d65a913499460863d54d136 Mon Sep 17 00:00:00 2001 From: x1m3 Date: Fri, 18 Oct 2024 18:18:06 +0200 Subject: [PATCH 3/8] wip: Do not create instance of new unpacker --- packers/zkp.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/packers/zkp.go b/packers/zkp.go index 9d7ca57..9fbf059 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -136,17 +136,13 @@ func (p *ZKPPacker) Pack(payload []byte, params iden3comm.PackerParams) ([]byte, // ZKPPUnpackerParams is params for zkp unpacker type ZKPPUnpackerParams struct { authVerifyDelay time.Duration - ethResolver map[int]eth.Resolver - verificationKey []byte iden3comm.PackerParams } // NewZKPPUnpackerParams creates new zkp unpacker params -func NewZKPPUnpackerParams(verificationKey []byte, resolvers map[int]eth.Resolver, authVerifyDelay time.Duration) ZKPPUnpackerParams { +func NewZKPPUnpackerParams(authVerifyDelay time.Duration) ZKPPUnpackerParams { return ZKPPUnpackerParams{ authVerifyDelay: authVerifyDelay, - ethResolver: resolvers, - verificationKey: verificationKey, } } @@ -157,21 +153,12 @@ func (p *ZKPPacker) Unpack(envelope []byte, params ...iden3comm.PackerParams) (* if len(params) > 1 { return nil, errors.New("expecting no more than one parameter in ZKPPacker Unpack") } - unpacker := p - if len(params) == 1 { - zkParams, ok := (params[0]).(ZKPPUnpackerParams) - if !ok { - return nil, errors.New("can't cast params to zkp unpacker params") - } - unpacker = DefaultZKPUnpacker(zkParams.verificationKey, zkParams.ethResolver, WithAuthVerifyDelay(zkParams.authVerifyDelay)) - } token, err := jwz.Parse(string(envelope)) if err != nil { return nil, err } - - verificationKey, ok := unpacker.Verification[jwz.ProvingMethodAlg{Alg: token.Alg, CircuitID: token.CircuitID}] + verificationKey, ok := p.Verification[jwz.ProvingMethodAlg{Alg: token.Alg, CircuitID: token.CircuitID}] if !ok { return nil, fmt.Errorf("message was packed with unsupported circuit `%s` and alg `%s`", token.CircuitID, token.Alg) @@ -185,7 +172,16 @@ func (p *ZKPPacker) Unpack(envelope []byte, params ...iden3comm.PackerParams) (* return nil, errors.New("message proof is invalid") } - err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals) + var authVerifDelay time.Duration + if len(params) == 1 { + zkParams, ok := (params[0]).(ZKPPUnpackerParams) + if !ok { + return nil, errors.New("can't cast params to zkp unpacker params") + } + authVerifDelay = zkParams.authVerifyDelay + } + _ = authVerifDelay + err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals) // TODO: Pass as a config func the value of authVerifDelay to Verify() if err != nil { return nil, err } From 5e9f07e61f86cace53122515de83226d1907a730 Mon Sep 17 00:00:00 2001 From: x1m3 Date: Mon, 21 Oct 2024 11:44:43 +0200 Subject: [PATCH 4/8] feat: Parametrize VerificationHandlerFunc --- mock/proving.go | 5 ----- packager_test.go | 8 +++++++- packers/zkp.go | 34 +++++++++++++++++++++++----------- packers/zkp_test.go | 8 ++++++-- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/mock/proving.go b/mock/proving.go index b6be26a..fe45c6e 100644 --- a/mock/proving.go +++ b/mock/proving.go @@ -82,11 +82,6 @@ func PrepareAuthInputs(hash []byte, _ *w3c.DID, _ circuits.CircuitID) ([]byte, e return j, err } -// VerifyState return no error always -func VerifyState(_ circuits.CircuitID, _ []string) error { - return nil -} - // MockRecipientKeyID is mocked key id for recipient const MockRecipientKeyID = "123456789" diff --git a/packager_test.go b/packager_test.go index b00adc9..9216d45 100644 --- a/packager_test.go +++ b/packager_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/google/uuid" + "github.com/iden3/go-circuits/v2" "github.com/iden3/go-iden3-core/v2/w3c" "github.com/iden3/go-jwz/v2" "github.com/iden3/iden3comm/v2" @@ -182,7 +183,7 @@ func initPackageManager(t *testing.T) *iden3comm.PackageManager { mockVerificationParam := make(map[jwz.ProvingMethodAlg]packers.VerificationParams) mockVerificationParam[mockedProvingMethod.ProvingMethodAlg] = packers.NewVerificationParams([]byte(""), - mock.VerifyState) + verifyStateMock) mockProvingParamMap := make(map[jwz.ProvingMethodAlg]packers.ProvingParams) mockProvingParamMap[mockedProvingMethod.ProvingMethodAlg] = packers.NewProvingParams(mock.PrepareAuthInputs, @@ -193,3 +194,8 @@ func initPackageManager(t *testing.T) *iden3comm.PackageManager { return pm } + +// VerifyState return no error always +func verifyStateMock(_ circuits.CircuitID, _ []string, _ ...packers.DefaultZKPUnpackerOption) error { + return nil +} diff --git a/packers/zkp.go b/packers/zkp.go index 9fbf059..2a46d59 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -30,11 +30,11 @@ func (f DataPreparerHandlerFunc) Prepare(hash []byte, id *w3c.DID, circuitID cir } // VerificationHandlerFunc registers the handler function for state verification. -type VerificationHandlerFunc func(id circuits.CircuitID, pubsignals []string) error +type VerificationHandlerFunc func(id circuits.CircuitID, pubsignals []string, confFuncs ...DefaultZKPUnpackerOption) error // Verify function is responsible to call provided handler for outputs verification -func (f VerificationHandlerFunc) Verify(id circuits.CircuitID, pubsignals []string) error { - return f(id, pubsignals) +func (f VerificationHandlerFunc) Verify(id circuits.CircuitID, pubsignals []string, confFuncs ...DefaultZKPUnpackerOption) error { + return f(id, pubsignals, confFuncs...) } // VerificationParams defined the verification function and the verification key for ZKP full verification @@ -264,32 +264,44 @@ func (p *ZKPPacker) MediaType() iden3comm.MediaType { } // DefaultZKPUnpackerOption is a function that sets the default ZKP unpacker options -type DefaultZKPUnpackerOption func(*defaultZKPUnpacker) +type DefaultZKPUnpackerOption func(*zkUnpackerOpts) // WithAuthVerifyDelay sets the delay for the auth verification func WithAuthVerifyDelay(delay time.Duration) DefaultZKPUnpackerOption { - return func(p *defaultZKPUnpacker) { + return func(p *zkUnpackerOpts) { p.authVerifyDelay = delay } } -type defaultZKPUnpacker struct { - resolvers map[int]eth.Resolver +type zkUnpackerOpts struct { authVerifyDelay time.Duration } +type defaultZKPUnpacker struct { + resolvers map[int]eth.Resolver + opts zkUnpackerOpts +} + // DefaultZKPUnpacker creates a default ZKP unpacker with the provided verification key and resolvers func DefaultZKPUnpacker(verificationKey []byte, resolvers map[int]eth.Resolver, opts ...DefaultZKPUnpackerOption) *ZKPPacker { - def := &defaultZKPUnpacker{resolvers, time.Minute * 5} + def := &defaultZKPUnpacker{ + resolvers: resolvers, + opts: zkUnpackerOpts{authVerifyDelay: time.Minute * 5}, + } for _, opt := range opts { - opt(def) + opt(&def.opts) } verifications := make(map[jwz.ProvingMethodAlg]VerificationParams) verifications[jwz.AuthV2Groth16Alg] = NewVerificationParams(verificationKey, def.defaultZkpUnpackerVerificationFn) return NewZKPPacker(nil, verifications) } -func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.CircuitID, pubsignals []string) error { +func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.CircuitID, pubsignals []string, opts ...DefaultZKPUnpackerOption) error { + options := d.opts + for _, opt := range opts { + opt(&options) + } + if id != circuits.AuthV2CircuitID { return errors.Errorf("circuit ID '%s' is not supported", id) } @@ -335,7 +347,7 @@ func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.Circui } if (big.NewInt(0)).Cmp(globalStateInfo.ReplacedByRoot) != 0 && - time.Since(time.Unix(globalStateInfo.ReplacedAtTimestamp.Int64(), 0)) > d.authVerifyDelay { + time.Since(time.Unix(globalStateInfo.ReplacedAtTimestamp.Int64(), 0)) > options.authVerifyDelay { return errors.Errorf("global state is too old, replaced timestamp is %v", globalStateInfo.ReplacedAtTimestamp.Int64()) } diff --git a/packers/zkp_test.go b/packers/zkp_test.go index dbe39f7..204de54 100644 --- a/packers/zkp_test.go +++ b/packers/zkp_test.go @@ -28,7 +28,7 @@ func TestZKPPacker_Pack(t *testing.T) { }) mockVerificationParam := make(map[jwz.ProvingMethodAlg]VerificationParams) - mockVerificationParam[mockedProvingMethod.ProvingMethodAlg] = NewVerificationParams([]byte(""), mock.VerifyState) + mockVerificationParam[mockedProvingMethod.ProvingMethodAlg] = NewVerificationParams([]byte(""), verifyStateMock) mockProvingParamMap := make(map[jwz.ProvingMethodAlg]ProvingParams) mockProvingParamMap[mockedProvingMethod.ProvingMethodAlg] = @@ -77,7 +77,7 @@ func TestPlainMessagePacker_Unpack(t *testing.T) { }) mockVerificationParam := make(map[jwz.ProvingMethodAlg]VerificationParams) - mockVerificationParam[mockedProvingMethod.ProvingMethodAlg] = NewVerificationParams([]byte(""), mock.VerifyState) + mockVerificationParam[mockedProvingMethod.ProvingMethodAlg] = NewVerificationParams([]byte(""), verifyStateMock) mockProvingParamMap := make(map[jwz.ProvingMethodAlg]ProvingParams) mockProvingParamMap[mockedProvingMethod.ProvingMethodAlg] = @@ -101,3 +101,7 @@ func TestPlainMessagePacker_Unpack(t *testing.T) { assert.Len(t, authResponse.Body.Scope, 0) } + +func verifyStateMock(_ circuits.CircuitID, _ []string, _ ...DefaultZKPUnpackerOption) error { + return nil +} From d51f162a3c4d3c754f0514bae85e7da1f878b17a Mon Sep 17 00:00:00 2001 From: x1m3 Date: Mon, 21 Oct 2024 11:48:55 +0200 Subject: [PATCH 5/8] chore: parameter rename --- packers/zkp.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packers/zkp.go b/packers/zkp.go index 2a46d59..846ff4c 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -30,11 +30,11 @@ func (f DataPreparerHandlerFunc) Prepare(hash []byte, id *w3c.DID, circuitID cir } // VerificationHandlerFunc registers the handler function for state verification. -type VerificationHandlerFunc func(id circuits.CircuitID, pubsignals []string, confFuncs ...DefaultZKPUnpackerOption) error +type VerificationHandlerFunc func(id circuits.CircuitID, pubsignals []string, opts ...DefaultZKPUnpackerOption) error // Verify function is responsible to call provided handler for outputs verification -func (f VerificationHandlerFunc) Verify(id circuits.CircuitID, pubsignals []string, confFuncs ...DefaultZKPUnpackerOption) error { - return f(id, pubsignals, confFuncs...) +func (f VerificationHandlerFunc) Verify(id circuits.CircuitID, pubsignals []string, opts ...DefaultZKPUnpackerOption) error { + return f(id, pubsignals, opts...) } // VerificationParams defined the verification function and the verification key for ZKP full verification From 6f0b28947c20bb1e397553fce64604aa8ea42cc0 Mon Sep 17 00:00:00 2001 From: x1m3 Date: Mon, 21 Oct 2024 12:32:50 +0200 Subject: [PATCH 6/8] fix: Pass authVerifDelay to Verify function --- packers/zkp.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packers/zkp.go b/packers/zkp.go index 846ff4c..60e30f3 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -172,16 +172,15 @@ func (p *ZKPPacker) Unpack(envelope []byte, params ...iden3comm.PackerParams) (* return nil, errors.New("message proof is invalid") } - var authVerifDelay time.Duration + var verifyOpts []DefaultZKPUnpackerOption if len(params) == 1 { zkParams, ok := (params[0]).(ZKPPUnpackerParams) if !ok { return nil, errors.New("can't cast params to zkp unpacker params") } - authVerifDelay = zkParams.authVerifyDelay + verifyOpts = append(verifyOpts, WithAuthVerifyDelay(zkParams.authVerifyDelay)) } - _ = authVerifDelay - err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals) // TODO: Pass as a config func the value of authVerifDelay to Verify() + err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals, verifyOpts...) if err != nil { return nil, err } From d7f61e7479dad3f1507a5d912a0aca7605b7637a Mon Sep 17 00:00:00 2001 From: x1m3 Date: Wed, 6 Nov 2024 18:19:05 +0100 Subject: [PATCH 7/8] feat: VerificationHandlerFunc.Verify now accept ZKPPUnpacker params instead of DefaultZKPUnpackerOption --- packager_test.go | 2 +- packers/zkp.go | 31 ++++++++++++++++--------------- packers/zkp_test.go | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packager_test.go b/packager_test.go index 9216d45..13f8b95 100644 --- a/packager_test.go +++ b/packager_test.go @@ -196,6 +196,6 @@ func initPackageManager(t *testing.T) *iden3comm.PackageManager { } // VerifyState return no error always -func verifyStateMock(_ circuits.CircuitID, _ []string, _ ...packers.DefaultZKPUnpackerOption) error { +func verifyStateMock(_ circuits.CircuitID, _ []string, _ ...packers.ZKPPUnpackerParams) error { return nil } diff --git a/packers/zkp.go b/packers/zkp.go index 60e30f3..54431ad 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -30,11 +30,14 @@ func (f DataPreparerHandlerFunc) Prepare(hash []byte, id *w3c.DID, circuitID cir } // VerificationHandlerFunc registers the handler function for state verification. -type VerificationHandlerFunc func(id circuits.CircuitID, pubsignals []string, opts ...DefaultZKPUnpackerOption) error +type VerificationHandlerFunc func(id circuits.CircuitID, pubsignals []string, opts ...ZKPPUnpackerParams) error // Verify function is responsible to call provided handler for outputs verification -func (f VerificationHandlerFunc) Verify(id circuits.CircuitID, pubsignals []string, opts ...DefaultZKPUnpackerOption) error { - return f(id, pubsignals, opts...) +func (f VerificationHandlerFunc) Verify(id circuits.CircuitID, pubsignals []string, params ...ZKPPUnpackerParams) error { + if len(params) > 1 { + return errors.New("expecting no more than one parameter in VerificationHandlerFunc.Verify") + } + return f(id, pubsignals, params...) } // VerificationParams defined the verification function and the verification key for ZKP full verification @@ -172,15 +175,13 @@ func (p *ZKPPacker) Unpack(envelope []byte, params ...iden3comm.PackerParams) (* return nil, errors.New("message proof is invalid") } - var verifyOpts []DefaultZKPUnpackerOption - if len(params) == 1 { - zkParams, ok := (params[0]).(ZKPPUnpackerParams) - if !ok { - return nil, errors.New("can't cast params to zkp unpacker params") + var zkParams []ZKPPUnpackerParams + for _, param := range params { + if zkpParam, ok := param.(ZKPPUnpackerParams); ok { + zkParams = append(zkParams, zkpParam) } - verifyOpts = append(verifyOpts, WithAuthVerifyDelay(zkParams.authVerifyDelay)) } - err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals, verifyOpts...) + err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals, zkParams...) if err != nil { return nil, err } @@ -295,10 +296,10 @@ func DefaultZKPUnpacker(verificationKey []byte, resolvers map[int]eth.Resolver, return NewZKPPacker(nil, verifications) } -func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.CircuitID, pubsignals []string, opts ...DefaultZKPUnpackerOption) error { - options := d.opts - for _, opt := range opts { - opt(&options) +func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.CircuitID, pubsignals []string, opts ...ZKPPUnpackerParams) error { + authVerifyDelay := d.opts.authVerifyDelay + if len(opts) == 1 { + authVerifyDelay = opts[0].authVerifyDelay } if id != circuits.AuthV2CircuitID { @@ -346,7 +347,7 @@ func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.Circui } if (big.NewInt(0)).Cmp(globalStateInfo.ReplacedByRoot) != 0 && - time.Since(time.Unix(globalStateInfo.ReplacedAtTimestamp.Int64(), 0)) > options.authVerifyDelay { + time.Since(time.Unix(globalStateInfo.ReplacedAtTimestamp.Int64(), 0)) > authVerifyDelay { return errors.Errorf("global state is too old, replaced timestamp is %v", globalStateInfo.ReplacedAtTimestamp.Int64()) } diff --git a/packers/zkp_test.go b/packers/zkp_test.go index 204de54..cf3faf7 100644 --- a/packers/zkp_test.go +++ b/packers/zkp_test.go @@ -102,6 +102,6 @@ func TestPlainMessagePacker_Unpack(t *testing.T) { } -func verifyStateMock(_ circuits.CircuitID, _ []string, _ ...DefaultZKPUnpackerOption) error { +func verifyStateMock(_ circuits.CircuitID, _ []string, _ ...ZKPPUnpackerParams) error { return nil } From c7b4b0251865fbaec06996abde316f3ce6664c8c Mon Sep 17 00:00:00 2001 From: Ilya Date: Wed, 13 Nov 2024 12:39:27 +0100 Subject: [PATCH 8/8] use zkp unpacker params only (#63) use zkp unpacker params only --- packers/zkp.go | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/packers/zkp.go b/packers/zkp.go index 54431ad..4570932 100644 --- a/packers/zkp.go +++ b/packers/zkp.go @@ -263,33 +263,14 @@ func (p *ZKPPacker) MediaType() iden3comm.MediaType { return MediaTypeZKPMessage } -// DefaultZKPUnpackerOption is a function that sets the default ZKP unpacker options -type DefaultZKPUnpackerOption func(*zkUnpackerOpts) - -// WithAuthVerifyDelay sets the delay for the auth verification -func WithAuthVerifyDelay(delay time.Duration) DefaultZKPUnpackerOption { - return func(p *zkUnpackerOpts) { - p.authVerifyDelay = delay - } -} - -type zkUnpackerOpts struct { - authVerifyDelay time.Duration -} - type defaultZKPUnpacker struct { resolvers map[int]eth.Resolver - opts zkUnpackerOpts } // DefaultZKPUnpacker creates a default ZKP unpacker with the provided verification key and resolvers -func DefaultZKPUnpacker(verificationKey []byte, resolvers map[int]eth.Resolver, opts ...DefaultZKPUnpackerOption) *ZKPPacker { +func DefaultZKPUnpacker(verificationKey []byte, resolvers map[int]eth.Resolver) *ZKPPacker { def := &defaultZKPUnpacker{ resolvers: resolvers, - opts: zkUnpackerOpts{authVerifyDelay: time.Minute * 5}, - } - for _, opt := range opts { - opt(&def.opts) } verifications := make(map[jwz.ProvingMethodAlg]VerificationParams) verifications[jwz.AuthV2Groth16Alg] = NewVerificationParams(verificationKey, def.defaultZkpUnpackerVerificationFn) @@ -297,11 +278,6 @@ func DefaultZKPUnpacker(verificationKey []byte, resolvers map[int]eth.Resolver, } func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.CircuitID, pubsignals []string, opts ...ZKPPUnpackerParams) error { - authVerifyDelay := d.opts.authVerifyDelay - if len(opts) == 1 { - authVerifyDelay = opts[0].authVerifyDelay - } - if id != circuits.AuthV2CircuitID { return errors.Errorf("circuit ID '%s' is not supported", id) } @@ -346,6 +322,11 @@ func (d *defaultZKPUnpacker) defaultZkpUnpackerVerificationFn(id circuits.Circui globalState.String(), globalStateInfo.Root.String()) } + authVerifyDelay := time.Minute * 5 + if len(opts) > 0 { + authVerifyDelay = opts[0].authVerifyDelay + } + if (big.NewInt(0)).Cmp(globalStateInfo.ReplacedByRoot) != 0 && time.Since(time.Unix(globalStateInfo.ReplacedAtTimestamp.Int64(), 0)) > authVerifyDelay { return errors.Errorf("global state is too old, replaced timestamp is %v",