-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Add optional parameter to packer.Unpack #60
base: main
Are you sure you want to change the base?
Changes from 6 commits
d8ea652
14c5977
011c8a1
5e9f07e
d51f162
6f0b289
d7f61e7
c7b4b02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, opts ...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, opts ...DefaultZKPUnpackerOption) error { | ||
return f(id, pubsignals, opts...) | ||
} | ||
|
||
// VerificationParams defined the verification function and the verification key for ZKP full verification | ||
|
@@ -133,14 +133,31 @@ 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 | ||
iden3comm.PackerParams | ||
} | ||
|
||
// NewZKPPUnpackerParams creates new zkp unpacker params | ||
func NewZKPPUnpackerParams(authVerifyDelay time.Duration) ZKPPUnpackerParams { | ||
return ZKPPUnpackerParams{ | ||
authVerifyDelay: authVerifyDelay, | ||
} | ||
} | ||
|
||
// 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") | ||
} | ||
|
||
token, err := jwz.Parse(string(envelope)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
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, | ||
|
@@ -155,7 +172,15 @@ func (p *ZKPPacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, error) { | |
return nil, errors.New("message proof is invalid") | ||
} | ||
|
||
err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals) | ||
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") | ||
} | ||
verifyOpts = append(verifyOpts, WithAuthVerifyDelay(zkParams.authVerifyDelay)) | ||
} | ||
err = verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals, verifyOpts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -238,32 +263,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you will remove opts from defaultZKPUnpacker, you can refactor the method to function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. It is using d.resolvers inside the method body. |
||
options := d.opts | ||
for _, opt := range opts { | ||
opt(&options) | ||
} | ||
|
||
if id != circuits.AuthV2CircuitID { | ||
return errors.Errorf("circuit ID '%s' is not supported", id) | ||
} | ||
|
@@ -291,7 +328,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}) | ||
|
@@ -306,7 +346,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()) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open question. Is it ok to use DefaultZKPUnpackerOption or we should create a new Option type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proposal it's add
UnpackerParams
in the same way how we did withPackerParams
and use them instead ofDefaultZKPUnpackerOption
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the function
verificationKey.VerificationFn.Verify(circuits.CircuitID(token.CircuitID), token.ZkProof.PubSignals, verifyOpts...)
should acceptZKPPUnpackerParams