github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/sdjwt/issuer/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 issuer 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 ) 17 18 func ExampleNew() { 19 signer, _, err := setUp() 20 if err != nil { 21 fmt.Println("failed to set-up test: %w", err.Error()) 22 } 23 24 claims := map[string]interface{}{ 25 "last_name": "Smith", 26 "address": map[string]interface{}{ 27 "street_address": "123 Main St", 28 "country": "US", 29 }, 30 } 31 32 // Issuer will issue SD-JWT for specified claims. Salt function is only provided to keep example outcome the same. 33 token, err := New("https://example.com/issuer", claims, nil, signer, 34 WithStructuredClaims(true), 35 WithNonSelectivelyDisclosableClaims([]string{"address.country"}), 36 WithSaltFnc(func() (string, error) { 37 return sampleSalt, nil 38 })) 39 if err != nil { 40 fmt.Println("failed to issue SD-JWT: %w", err.Error()) 41 } 42 43 var decoded map[string]interface{} 44 45 err = token.DecodeClaims(&decoded) 46 if err != nil { 47 fmt.Println("failed to decode SD-JWT claims: %w", err.Error()) 48 } 49 50 issuerClaimsJSON, err := marshalObj(decoded) 51 if err != nil { 52 fmt.Println("verifier failed to marshal verified claims: %w", err.Error()) 53 } 54 55 fmt.Println(issuerClaimsJSON) 56 57 // Output: { 58 // "_sd": [ 59 // "V9-Eiizd3iJpdlxojQuwps44Zba7z6R08S7rPCDg_wU" 60 // ], 61 // "_sd_alg": "sha-256", 62 // "address": { 63 // "_sd": [ 64 // "tD1XVFffEo0KTGuvHn9UlXCBgt3vot5xAanqXMdvVMg" 65 // ], 66 // "country": "US" 67 // }, 68 // "iss": "https://example.com/issuer" 69 //} 70 } 71 72 func setUp() (*afjwt.JoseED25519Signer, *afjwt.JoseEd25519Verifier, error) { 73 issuerPublicKey, issuerPrivateKey, err := ed25519.GenerateKey(rand.Reader) 74 if err != nil { 75 return nil, nil, err 76 } 77 78 signer := afjwt.NewEd25519Signer(issuerPrivateKey) 79 80 signatureVerifier, err := afjwt.NewEd25519Verifier(issuerPublicKey) 81 if err != nil { 82 return nil, nil, err 83 } 84 85 return signer, signatureVerifier, nil 86 } 87 88 func marshalObj(obj interface{}) (string, error) { 89 objBytes, err := json.Marshal(obj) 90 if err != nil { 91 fmt.Println("failed to marshal object: %w", err.Error()) 92 } 93 94 return prettyPrint(objBytes) 95 }