Skip to content

Commit

Permalink
use zkp unpacker params only (#63)
Browse files Browse the repository at this point in the history
use zkp unpacker params only
  • Loading branch information
ilya-korotya authored Nov 13, 2024
1 parent d7f61e7 commit c7b4b02
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions packers/zkp.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,45 +263,21 @@ 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)
return NewZKPPacker(nil, verifications)
}

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)
}
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit c7b4b02

Please sign in to comment.