-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlinked_data_proof_test.go
123 lines (102 loc) · 4.08 KB
/
linked_data_proof_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"testing"
"time"
"github.com/stretchr/testify/require"
ldprocessor "github.com/trustbloc/did-go/doc/ld/processor"
"github.com/trustbloc/kms-go/spi/kms"
"github.com/trustbloc/vc-go/proof/testsupport"
"github.com/trustbloc/vc-go/verifiable/lddocument"
)
// This example is generated using https://transmute-industries.github.io/vc-greeting-card
func TestLinkedDataProofSignerAndVerifier(t *testing.T) {
vcJSON := `
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"id": "https://example.com/credentials/1872",
"type": [
"VerifiableCredential",
"UniversityDegreeCredential"
],
"issuer": "did:key:z6Mkj7of2aaooXhTJvJ5oCL9ZVcAS472ZBuSjYyXDa4bWT32",
"issuanceDate": "2020-01-17T15:14:09.724Z",
"credentialSubject": {
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
"degree": {
"type": "BachelorDegree"
},
"name": "Jayden Doe",
"spouse": "did:example:c276e12ec21ebfeb1f712ebc6f1"
}
}
`
proofCreators, proofCheker := testsupport.NewKMSSignersAndVerifier(t, []*testsupport.SigningKey{
{Type: kms.ED25519Type, PublicKeyID: "did:key:z6Mkj7of2aaooXhTJvJ5oCL9ZVcAS472ZBuSjYyXDa4bWT32#key1"},
{Type: kms.ECDSASecp256k1TypeIEEEP1363, PublicKeyID: "did:key:z6Mkj7of2aaooXhTJvJ5oCL9ZVcAS472ZBuSjYyXDa4bWT32#key2"},
})
vcWithEd25519Proof := prepareVCWithEd25519LDP(t, vcJSON, proofCreators[0])
vcWithEd25519ProofBytes, err := vcWithEd25519Proof.MarshalJSON()
require.NoError(t, err)
vcWithSecp256k1Proof := prepareVCWithSecp256k1LDP(t, vcJSON, proofCreators[1])
vcWithSecp256k1ProofBytes, err := vcWithSecp256k1Proof.MarshalJSON()
require.NoError(t, err)
require.NotEmpty(t, vcWithSecp256k1ProofBytes)
t.Run("Single signature suite", func(t *testing.T) {
vcDecoded, err := parseTestCredential(t, vcWithEd25519ProofBytes,
WithProofChecker(proofCheker))
require.NoError(t, err)
require.Equal(t, vcWithEd25519Proof.ToRawJSON(), vcDecoded.ToRawJSON())
})
t.Run("Several signature suites", func(t *testing.T) {
vcDecoded, err := parseTestCredential(t, vcWithEd25519ProofBytes,
WithProofChecker(proofCheker))
require.NoError(t, err)
require.Equal(t, vcWithEd25519Proof.ToRawJSON(), vcDecoded.ToRawJSON())
vcDecoded, err = parseTestCredential(t, vcWithSecp256k1ProofBytes,
WithProofChecker(proofCheker))
require.NoError(t, err)
require.Equal(t, vcWithSecp256k1Proof.ToRawJSON(), vcDecoded.ToRawJSON())
})
}
func prepareVCWithEd25519LDP(t *testing.T, vcJSON string, signer lddocument.ProofCreator) *Credential {
vc, err := ParseCredential([]byte(vcJSON),
WithJSONLDDocumentLoader(createTestDocumentLoader(t)),
WithDisabledProofCheck())
require.NoError(t, err)
created, err := time.Parse(time.RFC3339, "2018-03-15T00:00:00Z")
require.NoError(t, err)
err = vc.AddLinkedDataProof(&LinkedDataProofContext{
SignatureType: "Ed25519Signature2018",
KeyType: kms.ED25519Type,
ProofCreator: signer,
SignatureRepresentation: SignatureJWS,
Created: &created,
VerificationMethod: "did:key:z6Mkj7of2aaooXhTJvJ5oCL9ZVcAS472ZBuSjYyXDa4bWT32#key1",
}, ldprocessor.WithDocumentLoader(createTestDocumentLoader(t)))
require.NoError(t, err)
require.Len(t, vc.Proofs(), 1)
return vc
}
func prepareVCWithSecp256k1LDP(t *testing.T, vcJSON string, signer lddocument.ProofCreator) *Credential {
vc, err := ParseCredential([]byte(vcJSON),
WithJSONLDDocumentLoader(createTestDocumentLoader(t)),
WithDisabledProofCheck())
require.NoError(t, err)
err = vc.AddLinkedDataProof(&LinkedDataProofContext{
SignatureType: "EcdsaSecp256k1Signature2019",
KeyType: kms.ECDSASecp256k1TypeIEEEP1363,
ProofCreator: signer,
SignatureRepresentation: SignatureJWS,
VerificationMethod: "did:key:z6Mkj7of2aaooXhTJvJ5oCL9ZVcAS472ZBuSjYyXDa4bWT32#key2",
}, ldprocessor.WithDocumentLoader(createTestDocumentLoader(t)))
require.NoError(t, err)
require.Len(t, vc.Proofs(), 1)
return vc
}