Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Small refactors for cryptosuites; provable (#403)
Browse files Browse the repository at this point in the history
* move suites to their own directories

* small refactors

* fix util bug

* pr comments
  • Loading branch information
decentralgabe authored May 31, 2023
1 parent 7878abd commit 70d1269
Show file tree
Hide file tree
Showing 34 changed files with 339 additions and 241 deletions.
31 changes: 16 additions & 15 deletions credential/exchange/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/TBD54566975/ssi-sdk/cryptosuite/jws2020"
"github.com/goccy/go-json"
"github.com/oliveagle/jsonpath"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -51,7 +52,7 @@ func TestBuildPresentationSubmission(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
submissionBytes, err := BuildPresentationSubmission(*signer, signer.ID, def, []PresentationClaim{presentationClaim}, JWTVPTarget)
assert.NoError(tt, err)
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestBuildPresentationSubmissionVP(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
assert.NoError(tt, err)
Expand Down Expand Up @@ -230,7 +231,7 @@ func TestBuildPresentationSubmissionVP(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
assert.NoError(tt, err)
Expand Down Expand Up @@ -301,7 +302,7 @@ func TestBuildPresentationSubmissionVP(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
testVCJWT := getTestJWTVerifiableCredential()
presentationClaimJWT := PresentationClaim{
Expand Down Expand Up @@ -367,7 +368,7 @@ func TestProcessInputDescriptor(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
assert.NoError(tt, err)
Expand Down Expand Up @@ -400,7 +401,7 @@ func TestProcessInputDescriptor(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
assert.NoError(tt, err)
Expand All @@ -427,7 +428,7 @@ func TestProcessInputDescriptor(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
assert.NoError(tt, err)
Expand All @@ -450,15 +451,15 @@ func TestProcessInputDescriptor(t *testing.T) {
},
Format: &ClaimFormat{
LDP: &LDPType{
ProofType: []cryptosuite.SignatureType{cryptosuite.JSONWebSignature2020},
ProofType: []cryptosuite.SignatureType{jws2020.JSONWebSignature2020},
},
},
}
testVC := getTestVerifiableCredential("test-issuer", "test-subject")
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
assert.NoError(tt, err)
Expand All @@ -481,15 +482,15 @@ func TestProcessInputDescriptor(t *testing.T) {
},
Format: &ClaimFormat{
LDPVC: &LDPType{
ProofType: []cryptosuite.SignatureType{cryptosuite.JSONWebSignature2020},
ProofType: []cryptosuite.SignatureType{jws2020.JSONWebSignature2020},
},
},
}
testVC := getTestVerifiableCredential("test-issuer", "test-subject")
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
assert.NoError(tt, err)
Expand Down Expand Up @@ -758,7 +759,7 @@ func TestNormalizePresentationClaims(t *testing.T) {
presentationClaim := PresentationClaim{
Presentation: &vpClaim,
LDPFormat: LDPVP.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}

normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
Expand All @@ -767,7 +768,7 @@ func TestNormalizePresentationClaims(t *testing.T) {
assert.True(tt, len(normalized) == 1)
assert.NotEmpty(tt, normalized[0].Data)
assert.EqualValues(tt, LDPVP, normalized[0].Format)
assert.EqualValues(tt, string(cryptosuite.JSONWebSignature2020), normalized[0].AlgOrProofType)
assert.EqualValues(tt, string(jws2020.JSONWebSignature2020), normalized[0].AlgOrProofType)
})

t.Run("Normalize VC Claim", func(tt *testing.T) {
Expand All @@ -777,7 +778,7 @@ func TestNormalizePresentationClaims(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &vcClaim,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}

normalized, err := normalizePresentationClaims([]PresentationClaim{presentationClaim})
Expand All @@ -786,7 +787,7 @@ func TestNormalizePresentationClaims(t *testing.T) {
assert.True(tt, len(normalized) == 1)
assert.NotEmpty(tt, normalized[0].Data)
assert.EqualValues(tt, LDPVC, normalized[0].Format)
assert.EqualValues(tt, string(cryptosuite.JSONWebSignature2020), normalized[0].AlgOrProofType)
assert.EqualValues(tt, string(jws2020.JSONWebSignature2020), normalized[0].AlgOrProofType)
})
}

Expand Down
6 changes: 3 additions & 3 deletions credential/exchange/verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/TBD54566975/ssi-sdk/cryptosuite/jws2020"
"github.com/stretchr/testify/assert"

"github.com/TBD54566975/ssi-sdk/crypto/jwx"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/TBD54566975/ssi-sdk/util"

"github.com/TBD54566975/ssi-sdk/credential"
"github.com/TBD54566975/ssi-sdk/cryptosuite"
)

func TestVerifyPresentationSubmission(t *testing.T) {
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestVerifyPresentationSubmission(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
submissionBytes, err := BuildPresentationSubmission(*signer, verifier.ID, def, []PresentationClaim{presentationClaim}, JWTVPTarget)
assert.NoError(tt, err)
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestVerifyPresentationSubmissionVP(t *testing.T) {
presentationClaim := PresentationClaim{
Credential: &testVC,
LDPFormat: LDPVC.Ptr(),
SignatureAlgorithmOrProofType: string(cryptosuite.JSONWebSignature2020),
SignatureAlgorithmOrProofType: string(jws2020.JSONWebSignature2020),
}
submissionBytes, err := BuildPresentationSubmission(*signer, "requester", def, []PresentationClaim{presentationClaim}, JWTVPTarget)
assert.NoError(tt, err)
Expand Down
3 changes: 2 additions & 1 deletion credential/manifest/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/TBD54566975/ssi-sdk/crypto"
"github.com/TBD54566975/ssi-sdk/crypto/jwx"
"github.com/TBD54566975/ssi-sdk/cryptosuite"
"github.com/TBD54566975/ssi-sdk/cryptosuite/jws2020"
"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -215,7 +216,7 @@ func TestIsValidCredentialApplicationForManifest(t *testing.T) {
cm, ca := getValidTestCredManifestCredApplication(tt)

cm.PresentationDefinition.InputDescriptors[0].Format = &exchange.ClaimFormat{
LDP: &exchange.LDPType{ProofType: []cryptosuite.SignatureType{cryptosuite.JSONWebSignature2020}},
LDP: &exchange.LDPType{ProofType: []cryptosuite.SignatureType{jws2020.JSONWebSignature2020}},
}

credAppRequestBytes, err := json.Marshal(ca)
Expand Down
10 changes: 5 additions & 5 deletions credential/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func VerifyCredentialSignature(ctx context.Context, genericCred any, r resolutio
if r == nil {
return false, errors.New("resolution cannot be empty")
}
switch genericCred.(type) {
switch typedCred := genericCred.(type) {
case *VerifiableCredential, VerifiableCredential, map[string]any:
_, token, cred, err := ToCredential(genericCred)
_, token, cred, err := ToCredential(typedCred)
if err != nil {
return false, errors.Wrap(err, "error converting credential from generic type")
}
Expand All @@ -40,16 +40,16 @@ func VerifyCredentialSignature(ctx context.Context, genericCred any, r resolutio
return false, errors.New("data integrity signature verification not yet implemented")
case []byte:
// turn it into a string and try again
return VerifyCredentialSignature(ctx, string(genericCred.([]byte)), r)
return VerifyCredentialSignature(ctx, string(typedCred), r)
case string:
// could be a Data Integrity credential
var cred VerifiableCredential
if err := json.Unmarshal([]byte(genericCred.(string)), &cred); err == nil {
if err := json.Unmarshal([]byte(typedCred), &cred); err == nil {
return VerifyCredentialSignature(ctx, cred, r)
}

// could be a JWT
return VerifyJWTCredential(genericCred.(string), r)
return VerifyJWTCredential(typedCred, r)
}
return false, fmt.Errorf("invalid credential type: %s", reflect.TypeOf(genericCred).Kind().String())
}
Expand Down
49 changes: 28 additions & 21 deletions credential/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

// ToCredential turn a generic cred into its known object model
func ToCredential(genericCred any) (jws.Headers, jwt.Token, *VerifiableCredential, error) {
switch genericCred.(type) {
switch typedCred := genericCred.(type) {
case []byte:
// could be a JWT
headers, token, vcFromJWT, err := ToCredential(string(genericCred.([]byte)))
headers, token, vcFromJWT, err := ToCredential(string(typedCred))
if err == nil {
return headers, token, vcFromJWT, err
}
Expand All @@ -28,18 +28,21 @@ func ToCredential(genericCred any) (jws.Headers, jwt.Token, *VerifiableCredentia
}
return ToCredential(cred)
case *VerifiableCredential:
return nil, nil, genericCred.(*VerifiableCredential), nil
return nil, nil, typedCred, nil
case VerifiableCredential:
verifiableCredential := genericCred.(VerifiableCredential)
return nil, nil, &verifiableCredential, nil
return nil, nil, &typedCred, nil
case string:
// TODO(gabe) support the case where the string is a json representation of an LD credential
// JWT
return ParseVerifiableCredentialFromJWT(genericCred.(string))
// first try the case where the string is JSON of a VC object
var cred VerifiableCredential
if err := json.Unmarshal([]byte(typedCred), &cred); err == nil {
return nil, nil, &cred, nil
}

// next try it as a JWT
return ParseVerifiableCredentialFromJWT(typedCred)
case map[string]any:
// VC or JWTVC JSON
credJSON := genericCred.(map[string]any)
credMapBytes, err := json.Marshal(credJSON)
credMapBytes, err := json.Marshal(typedCred)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "marshalling credential map")
}
Expand All @@ -62,26 +65,31 @@ func ToCredential(genericCred any) (jws.Headers, jwt.Token, *VerifiableCredentia

// ToCredentialJSONMap turn a generic cred into a JSON object
func ToCredentialJSONMap(genericCred any) (map[string]any, error) {
switch genericCred.(type) {
switch typedCred := genericCred.(type) {
case []byte:
// could be a JWT
credJSON, err := ToCredentialJSONMap(string(genericCred.([]byte)))
credJSON, err := ToCredentialJSONMap(string(typedCred))
if err == nil {
return credJSON, err
}

// could also be a vc
var cred VerifiableCredential
if err = json.Unmarshal(genericCred.([]byte), &cred); err != nil {
if err = json.Unmarshal(typedCred, &cred); err != nil {
return nil, errors.Wrap(err, "unmarshalling credential object")
}
return ToCredentialJSONMap(cred)
case map[string]any:
return genericCred.(map[string]any), nil
return typedCred, nil
case string:
// TODO(gabe) support the case where the string is a json representation of an LD credential
// JWT
_, token, _, err := ParseVerifiableCredentialFromJWT(genericCred.(string))
// first try the case where the string is JSON of a VC object
var credJSON map[string]any
if err := json.Unmarshal([]byte(typedCred), &credJSON); err == nil {
return credJSON, nil
}

// next try it as a JWT
_, token, _, err := ParseVerifiableCredentialFromJWT(typedCred)
if err != nil {
return nil, errors.Wrap(err, "parsing credential from JWT")
}
Expand All @@ -90,18 +98,17 @@ func ToCredentialJSONMap(genericCred any) (map[string]any, error) {
if err != nil {
return nil, errors.Wrap(err, "marshaling credential JWT")
}
var credJSON map[string]any
if err := json.Unmarshal(tokenJSONBytes, &credJSON); err != nil {
if err = json.Unmarshal(tokenJSONBytes, &credJSON); err != nil {
return nil, errors.Wrap(err, "unmarshalling credential JWT")
}
return credJSON, nil
case VerifiableCredential, *VerifiableCredential:
credJSONBytes, err := json.Marshal(genericCred)
credJSONBytes, err := json.Marshal(typedCred)
if err != nil {
return nil, errors.Wrap(err, "marshalling credential object")
}
var credJSON map[string]any
if err := json.Unmarshal(credJSONBytes, &credJSON); err != nil {
if err = json.Unmarshal(credJSONBytes, &credJSON); err != nil {
return nil, errors.Wrap(err, "unmarshalling credential object")
}
return credJSON, nil
Expand Down
Loading

0 comments on commit 70d1269

Please sign in to comment.