Skip to content

Commit

Permalink
GetSupportedProfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Dec 19, 2024
1 parent f1830f8 commit 71b2ecc
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Packer interface {
Unpack(envelope []byte) (*BasicMessage, error)
// MediaType returns content type of message
MediaType() MediaType
// GetSupportedProfiles returns supported accept profiles
GetSupportedProfiles() []string
}

// PackerParams mock interface for packer params
Expand Down Expand Up @@ -99,6 +101,16 @@ func (r *PackageManager) unpackSafeEnvelope(mediaType MediaType, envelope []byte
return msg, nil
}

// GetSupportedProfiles retrieves all unique supported profiles.
func (r *PackageManager) GetSupportedProfiles() []string {
var acceptProfiles []string
for _, p := range r.packers {
profiles := p.GetSupportedProfiles()
acceptProfiles = append(acceptProfiles, profiles...)
}
return acceptProfiles
}

type envelopeStub struct {
Protected string `json:"protected"`
}
Expand Down
7 changes: 7 additions & 0 deletions packager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ func TestUnpackWithType(t *testing.T) {
assert.Equal(t, unpackedMsg.Typ, packers.MediaTypeZKPMessage)
}

func TestGetSupportedProfiles(t *testing.T) {
pm := initPackageManager(t)
profiles := pm.GetSupportedProfiles()
assert.Contains(t, profiles, "iden3comm/v1;env=application/iden3comm-plain-json")
assert.Contains(t, profiles, "iden3comm/v1;env=application/iden3-zkp-json&alg=groth16&circuitIds=authV2")
}

func createFetchCredentialMessage(typ iden3comm.MediaType, from, to *w3c.DID) ([]byte, error) {

var msg protocol.CredentialFetchRequestMessage
Expand Down
13 changes: 13 additions & 0 deletions packers/anoncrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package packers

import (
"encoding/json"
"fmt"

"github.com/iden3/iden3comm/v2"
"github.com/iden3/iden3comm/v2/protocol"
"github.com/pkg/errors"
"gopkg.in/go-jose/go-jose.v2"
)
Expand Down Expand Up @@ -91,3 +93,14 @@ func (p *AnoncryptPacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, erro
func (p *AnoncryptPacker) MediaType() iden3comm.MediaType {
return MediaTypeEncryptedMessage
}

// GetSupportedProfiles gets packer envelope (supported profiles) with options
func (p *AnoncryptPacker) GetSupportedProfiles() []string {
return []string{
fmt.Sprintf(
"%s;env=%s",
protocol.ProtocolVersionV1,
p.MediaType(),
),
}
}
18 changes: 18 additions & 0 deletions packers/jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"strings"

"github.com/dustinxie/ecc"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/iden3/iden3comm/v2"
"github.com/iden3/iden3comm/v2/packers/providers/bjj"
"github.com/iden3/iden3comm/v2/packers/providers/es256k"
"github.com/iden3/iden3comm/v2/protocol"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
Expand Down Expand Up @@ -332,6 +334,22 @@ func (p *JWSPacker) MediaType() iden3comm.MediaType {
return MediaTypeSignedMessage
}

// GetSupportedProfiles gets packer envelope (supported profiles) with options
func (p *JWSPacker) GetSupportedProfiles() []string {
return []string{
fmt.Sprintf(
"%s;env=%s&alg=%s",
protocol.ProtocolVersionV1,
p.MediaType(),
strings.Join(p.getSupportedAlgorithms(), ","),
),
}
}

func (p *JWSPacker) getSupportedAlgorithms() []string {
return []string{string(protocol.AcceptJwsAlgorithmsES256K), string(protocol.AcceptJwsAlgorithmsES256KR)}
}

// resolveVerificationMethods looks for all verification methods in the DID document.
func resolveVerificationMethods(didDoc *verifiable.DIDDocument) ([]verifiable.CommonVerificationMethod, error) {
vms := make([]verifiable.CommonVerificationMethod, 0,
Expand Down
6 changes: 6 additions & 0 deletions packers/jws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ func TestLookForKid(t *testing.T) {
}
}

func TestJWSSupportedProfiles(t *testing.T) {
p := JWSPacker{}
acceptProfiles := p.GetSupportedProfiles()
require.Equal(t, []string{"iden3comm/v1;env=application/iden3comm-signed-json&alg=ES256K,ES256K-R"}, acceptProfiles)
}

func loadDIDDoc(fileName string) (*verifiable.DIDDocument, error) {
file, err := os.Open(fmt.Sprintf("testdata/jws/%s", fileName))
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions packers/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package packers

import (
"encoding/json"
"fmt"

"github.com/iden3/iden3comm/v2"
"github.com/iden3/iden3comm/v2/protocol"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -46,3 +48,14 @@ func (p *PlainMessagePacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, e
func (p *PlainMessagePacker) MediaType() iden3comm.MediaType {
return MediaTypePlainMessage
}

// GetSupportedProfiles gets packer envelope (supported profiles) with options
func (p *PlainMessagePacker) GetSupportedProfiles() []string {
return []string{
fmt.Sprintf(
"%s;env=%s",
protocol.ProtocolVersionV1,
p.MediaType(),
),
}
}
13 changes: 13 additions & 0 deletions packers/plain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package packers

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestPlainSupportedProfiles(t *testing.T) {
p := PlainMessagePacker{}
acceptProfiles := p.GetSupportedProfiles()
require.Equal(t, []string{"iden3comm/v1;env=application/iden3comm-plain-json"}, acceptProfiles)
}
22 changes: 22 additions & 0 deletions packers/zkp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/iden3/go-jwz/v2"
"github.com/iden3/iden3comm/v2"
"github.com/iden3/iden3comm/v2/protocol"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -239,6 +240,27 @@ func (p *ZKPPacker) MediaType() iden3comm.MediaType {
return MediaTypeZKPMessage
}

// GetSupportedProfiles gets packer envelope (supported profiles) with options
func (p *ZKPPacker) GetSupportedProfiles() []string {
return []string{
fmt.Sprintf(
"%s;env=%s&alg=%s&circuitIds=%s",
protocol.ProtocolVersionV1,
p.MediaType(),
strings.Join(p.getSupportedAlgorithms(), ","),
strings.Join(p.getSupportedCircuitIDs(), ","),
),
}
}

func (p *ZKPPacker) getSupportedAlgorithms() []string {
return []string{string(protocol.AcceptJwzAlgorithmsGroth16)}
}

func (p *ZKPPacker) getSupportedCircuitIDs() []string {
return []string{string(protocol.AcceptAuthCircuitsAuthV2)}
}

// DefaultZKPUnpackerOption is a function that sets the default ZKP unpacker options
type DefaultZKPUnpackerOption func(*defaultZKPUnpacker)

Expand Down
7 changes: 7 additions & 0 deletions packers/zkp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/iden3/iden3comm/v2/mock"
"github.com/iden3/iden3comm/v2/protocol"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestZKPPacker_Pack(t *testing.T) {
Expand Down Expand Up @@ -101,3 +102,9 @@ func TestPlainMessagePacker_Unpack(t *testing.T) {
assert.Len(t, authResponse.Body.Scope, 0)

}

func TestZKPSupportedProfiles(t *testing.T) {
p := ZKPPacker{}
acceptProfiles := p.GetSupportedProfiles()
require.Equal(t, []string{"iden3comm/v1;env=application/iden3-zkp-json&alg=groth16&circuitIds=authV2"}, acceptProfiles)
}
38 changes: 38 additions & 0 deletions protocol/accept.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package protocol

// AcceptProtocolVersion is a type of supported versions of the protocol used in the accept header
type AcceptProtocolVersion string

const (
// ProtocolVersionV1 is a V1 version of the protocol used in the accept header
ProtocolVersionV1 AcceptProtocolVersion = "iden3comm/v1"
)

// AcceptAuthCircuits is a type of accepted authentication circuits
type AcceptAuthCircuits string

const (
// AcceptAuthCircuitsAuthV2 is authV2 accepted circuit
AcceptAuthCircuitsAuthV2 AcceptAuthCircuits = "authV2"
// AcceptAuthCircuitsAuthV3 is authV3 accepted circuit
AcceptAuthCircuitsAuthV3 AcceptAuthCircuits = "authV3"
)

// AcceptJwzAlgorithms is a type of accepted proving algorithms
type AcceptJwzAlgorithms string

const (
// AcceptJwzAlgorithmsGroth16 is a groth16 accepted proving algorithm
AcceptJwzAlgorithmsGroth16 AcceptJwzAlgorithms = "groth16"
)

// AcceptJwsAlgorithms is a type of accepted JWS algorithms
type AcceptJwsAlgorithms string

const (
// AcceptJwsAlgorithmsES256K is a ES256K accepted JWS algorithm
AcceptJwsAlgorithmsES256K AcceptJwsAlgorithms = "ES256K"

// AcceptJwsAlgorithmsES256KR is a ES256K-R accepted JWS algorithm
AcceptJwsAlgorithmsES256KR AcceptJwsAlgorithms = "ES256K-R"
)

0 comments on commit 71b2ecc

Please sign in to comment.