github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/sdjwt/verifier/example_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package verifier 8 9 import ( 10 "crypto/ed25519" 11 "crypto/rand" 12 "encoding/json" 13 "fmt" 14 15 afjwt "github.com/hyperledger/aries-framework-go/pkg/doc/jwt" 16 "github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/common" 17 "github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/holder" 18 "github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/issuer" 19 ) 20 21 func ExampleParse() { 22 signer, signatureVerifier, err := setUp() 23 if err != nil { 24 fmt.Println("failed to set-up test: %w", err.Error()) 25 } 26 27 claims := map[string]interface{}{ 28 "given_name": "Albert", 29 "last_name": "Smith", 30 } 31 32 // Issuer will issue SD-JWT for specified claims. 33 token, err := issuer.New(testIssuer, claims, nil, signer) 34 if err != nil { 35 fmt.Println("failed to issue SD-JWT: %w", err.Error()) 36 } 37 38 combinedFormatForIssuance, err := token.Serialize(false) 39 if err != nil { 40 fmt.Println("failed to issue SD-JWT: %w", err.Error()) 41 } 42 43 // Holder will parse combined format for issuance for verification purposes. 44 _, err = holder.Parse(combinedFormatForIssuance, holder.WithSignatureVerifier(signatureVerifier)) 45 if err != nil { 46 fmt.Println("holder failed to parse SD-JWT: %w", err.Error()) 47 } 48 49 // The Holder will disclose all claims. 50 combinedFormatForPresentation := combinedFormatForIssuance + common.CombinedFormatSeparator 51 52 // Verifier will validate combined format for presentation and create verified claims. 53 verifiedClaims, err := Parse(combinedFormatForPresentation, 54 WithSignatureVerifier(signatureVerifier)) 55 if err != nil { 56 fmt.Println("verifier failed to parse holder presentation: %w", err.Error()) 57 } 58 59 verifiedClaimsJSON, err := marshalObj(verifiedClaims) 60 if err != nil { 61 fmt.Println("verifier failed to marshal verified claims: %w", err.Error()) 62 } 63 64 fmt.Println(verifiedClaimsJSON) 65 66 // Output: { 67 // "given_name": "Albert", 68 // "iss": "https://example.com/issuer", 69 // "last_name": "Smith" 70 //} 71 } 72 73 func setUp() (*afjwt.JoseED25519Signer, *afjwt.JoseEd25519Verifier, error) { 74 issuerPublicKey, issuerPrivateKey, err := ed25519.GenerateKey(rand.Reader) 75 if err != nil { 76 return nil, nil, err 77 } 78 79 signer := afjwt.NewEd25519Signer(issuerPrivateKey) 80 81 signatureVerifier, err := afjwt.NewEd25519Verifier(issuerPublicKey) 82 if err != nil { 83 return nil, nil, err 84 } 85 86 return signer, signatureVerifier, nil 87 } 88 89 func marshalObj(obj interface{}) (string, error) { 90 objBytes, err := json.Marshal(obj) 91 if err != nil { 92 fmt.Println("failed to marshal object: %w", err.Error()) 93 } 94 95 return prettyPrint(objBytes) 96 }