Skip to content

Commit

Permalink
chore: Add optional parameter to packer.Unpack
Browse files Browse the repository at this point in the history
  • Loading branch information
x1m3 committed Oct 15, 2024
1 parent f2336de commit d8ea652
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packers/anoncrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packers/jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packers/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 33 additions & 3 deletions packers/zkp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d8ea652

Please sign in to comment.