|
1 |
| -# VC JOSE COSE in GO |
| 1 | +[](https://pkg.go.dev/github.com/TBD54566975/vc-jose-cose-go) |
| 2 | +[](https://golang.org/) |
| 3 | +[](https://goreportcard.com/report/github.com/TBD54566975/vc-jose-cose-go) |
| 4 | +[](https://github.com/TBD54566975/vc-jose-cose-go/blob/main/LICENSE) |
| 5 | +[](https://github.com/TBD54566975/vc-jose-cose-go/issues) |
| 6 | + |
| 7 | +[](https://codecov.io/github/TBD54566975/vc-jose-cose-go) |
| 8 | + |
| 9 | +# VC JOSE COSE in go |
2 | 10 |
|
3 | 11 | A lightweight go implementation of the [W3C Verifiable Credentials v2 Data Model](https://www.w3.org/TR/vc-data-model-2.0)
|
4 | 12 | with support for [Securing Verifiable Credentials using JOSE and COSE](https://www.w3.org/TR/vc-jose-cose/).
|
5 | 13 |
|
| 14 | +## Usage |
| 15 | + |
| 16 | +This library provides Go implementations for signing and verifying Verifiable Credentials (VCs) and Verifiable Presentations (VPs) using JOSE, SD-JWT, and COSE formats. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +``` |
| 21 | +go get github.com/TBD54566975/vc-jose-cose-go |
| 22 | +``` |
| 23 | + |
| 24 | +### JOSE (JSON Object Signing and Encryption) |
| 25 | + |
| 26 | +```go |
| 27 | +import ( |
| 28 | + "github.com/TBD54566975/vc-jose-cose-go/jose" |
| 29 | + "github.com/TBD54566975/vc-jose-cose-go/credential" |
| 30 | + "github.com/TBD54566975/vc-jose-cose-go/util" |
| 31 | + "github.com/lestrrat-go/jwx/v2/jwk" |
| 32 | + "github.com/lestrrat-go/jwx/v2/jwa" |
| 33 | +) |
| 34 | + |
| 35 | +func main() { |
| 36 | + // Create a VC |
| 37 | + vc := credential.VerifiableCredential{ |
| 38 | + Context: []string{"https://www.w3.org/2018/credentials/v1"}, |
| 39 | + ID: "https://example.edu/credentials/1872", |
| 40 | + Type: []string{"VerifiableCredential"}, |
| 41 | + Issuer: credential.NewIssuerHolderFromString("did:example:issuer"), |
| 42 | + ValidFrom: "2010-01-01T19:23:24Z", |
| 43 | + CredentialSubject: map[string]any{ |
| 44 | + "id": "did:example:ebfeb1f712ebc6f1c276e12ec21", |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + // Create the issuer's key |
| 49 | + key, _ := util.GenerateJWK(jwa.Ed25519) |
| 50 | + |
| 51 | + // Sign the VC |
| 52 | + jwt, err := jose.SignVerifiableCredential(vc, key) |
| 53 | + if err != nil { |
| 54 | + // Handle error |
| 55 | + } |
| 56 | + |
| 57 | + vc, err := jose.VerifyVerifiableCredential(jwt, key) |
| 58 | + if err != nil { |
| 59 | + // Handle error |
| 60 | + } |
| 61 | + // Use the verified VC |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### SD-JWT (Selective Disclosure JWT) |
| 66 | + |
| 67 | +```go |
| 68 | + import ( |
| 69 | + "github.com/TBD54566975/vc-jose-cose-go/sdjwt" |
| 70 | + "github.com/TBD54566975/vc-jose-cose-go/credential" |
| 71 | + "github.com/TBD54566975/vc-jose-cose-go/util" |
| 72 | + "github.com/lestrrat-go/jwx/v2/jwk" |
| 73 | + "github.com/lestrrat-go/jwx/v2/jwa" |
| 74 | + ) |
| 75 | + |
| 76 | + func main() { |
| 77 | + vc := credential.VerifiableCredential{ |
| 78 | + Context: []string{"https://www.w3.org/2018/credentials/v1"}, |
| 79 | + ID: "https://example.edu/credentials/1872", |
| 80 | + Type: []string{"VerifiableCredential"}, |
| 81 | + Issuer: credential.NewIssuerHolderFromString("did:example:issuer"), |
| 82 | + ValidFrom: "2010-01-01T19:23:24Z", |
| 83 | + CredentialSubject: map[string]any{ |
| 84 | + "id": "did:example:ebfeb1f712ebc6f1c276e12ec21", |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + // Define disclosure paths |
| 89 | + disclosurePaths := []sdjwt.DisclosurePath{ |
| 90 | + "issuer", |
| 91 | + "credentialSubject.id", |
| 92 | + } |
| 93 | + |
| 94 | + // Create the issuer's key |
| 95 | + key, _ := util.GenerateJWK(jwa.Ed25519) |
| 96 | + |
| 97 | + // Create SD-JWT |
| 98 | + sdJWT, err := sdjwt.SignVerifiableCredential(vc, disclosurePaths, issuerKey) |
| 99 | + if err != nil { |
| 100 | + // Handle error |
| 101 | + } |
| 102 | + |
| 103 | + verifiedVC, err := sdjwt.VerifyVerifiableCredential(*sdJWT, issuerKey) |
| 104 | + if err != nil { |
| 105 | + // Handle error |
| 106 | + } |
| 107 | + } |
| 108 | +``` |
| 109 | + |
| 110 | +### COSE (CBOR Object Signing and Encryption) |
| 111 | + |
| 112 | +```go |
| 113 | +import ( |
| 114 | + "github.com/TBD54566975/vc-jose-cose-go/cose" |
| 115 | + "github.com/TBD54566975/vc-jose-cose-go/credential" |
| 116 | + "github.com/TBD54566975/vc-jose-cose-go/util" |
| 117 | + "github.com/lestrrat-go/jwx/v2/jwk" |
| 118 | + "github.com/lestrrat-go/jwx/v2/jwa" |
| 119 | +) |
| 120 | + |
| 121 | +func main() { |
| 122 | + // Create a VC |
| 123 | + vc := credential.VerifiableCredential{ |
| 124 | + Context: []string{"https://www.w3.org/2018/credentials/v1"}, |
| 125 | + ID: "https://example.edu/credentials/1872", |
| 126 | + Type: []string{"VerifiableCredential"}, |
| 127 | + Issuer: credential.NewIssuerHolderFromString("did:example:issuer"), |
| 128 | + ValidFrom: "2010-01-01T19:23:24Z", |
| 129 | + CredentialSubject: map[string]any{ |
| 130 | + "id": "did:example:ebfeb1f712ebc6f1c276e12ec21", |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + // Create the issuer's key |
| 135 | + key, _ := util.GenerateJWK(jwa.Ed25519) |
| 136 | + |
| 137 | + // Sign the VC |
| 138 | + cs1, err := cose.SignVerifiableCredential(vc, key) |
| 139 | + if err != nil { |
| 140 | + // Handle error |
| 141 | + } |
| 142 | + |
| 143 | + vc, err := cose.VerifyVerifiableCredential(cs1, key) |
| 144 | + if err != nil { |
| 145 | + // Handle error |
| 146 | + } |
| 147 | + // Use the verified VC |
| 148 | +} |
| 149 | +``` |
| 150 | + |
6 | 151 | ## Project Resources
|
7 | 152 |
|
8 | 153 | | Resource | Description |
|
9 | 154 | | ------------------------------------------ | ------------------------------------------------------------------------------ |
|
10 | 155 | | [CODEOWNERS](./CODEOWNERS) | Outlines the project lead(s) |
|
11 | 156 | | [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md) | Expected behavior for project contributors, promoting a welcoming environment |
|
12 |
| -| [CONTRIBUTING.md](./CONTRIBUTING.md) | Developer guide to build, test, run, access CI, chat, discuss, file issues | |
| 157 | +| [CONTRIBUTING.md](./CONTRIBUTING.md) | Developer guide to build, test, run, access CI, chat, discuss, file issues | |
13 | 158 | | [GOVERNANCE.md](./GOVERNANCE.md) | Project governance |
|
14 |
| -| [LICENSE](./LICENSE) | Apache License, Version 2.0 | |
| 159 | +| [LICENSE](./LICENSE) | Apache License, Version 2.0 | |
0 commit comments