@@ -2,125 +2,107 @@ package services
22
33import (
44 "context"
5- "encoding/json"
65 "fmt"
76
87 sdk "github.com/trinsic-id/sdk/go/proto"
98)
109
11- type Document map [ string ] interface {}
12-
13- func NewCredentialService (options * sdk. ServiceOptions ) (CredentialService , error ) {
10+ // NewCredentialService returns a credential service with the base service configured
11+ // using the provided options
12+ func NewCredentialService (options * Options ) (CredentialService , error ) {
1413 base , err := NewServiceBase (options )
1514 if err != nil {
1615 return nil , err
1716 }
18- service := & CredentialBase {
19- ServiceBase : base ,
20- client : sdk .NewVerifiableCredentialClient (base .channel ),
17+ service := & credentialBase {
18+ Service : base ,
19+ client : sdk .NewVerifiableCredentialClient (base .GetChannel () ),
2120 }
2221
2322 return service , nil
2423}
2524
25+ // CredentialService defines the interface for interacting with credentials
2626type CredentialService interface {
2727 Service
28- IssueCredential (userContext context.Context , document Document ) (Document , error )
28+ // IssueCredential using a document json
29+ IssueCredential (userContext context.Context , request * sdk.IssueRequest ) (* sdk.IssueResponse , error )
30+ // IssueFromTemplate issues a credential using a template
2931 IssueFromTemplate (userContext context.Context , request * sdk.IssueFromTemplateRequest ) (* sdk.IssueFromTemplateResponse , error )
32+ // CheckStatus of the credential
3033 CheckStatus (userContext context.Context , request * sdk.CheckStatusRequest ) (* sdk.CheckStatusResponse , error )
34+ // UpdateStatus of the credential (i.e. revoke)
3135 UpdateStatus (userContext context.Context , request * sdk.UpdateStatusRequest ) (* sdk.UpdateStatusResponse , error )
32- CreateProof (userContext context.Context , documentId string , revealDocument Document ) (Document , error )
33- VerifyProof (userContext context.Context , proofDocument Document ) (bool , error )
34- Send (userContext context.Context , document Document , email string ) error
36+ // CreateProof using either a credential in a cloud wallet or based on the json document provided
37+ CreateProof (userContext context.Context , request * sdk.CreateProofRequest ) (* sdk.CreateProofResponse , error )
38+ // VerifyProof presentation
39+ VerifyProof (userContext context.Context , request * sdk.VerifyProofRequest ) (bool , error )
40+ // Send a credential to another use's wallet
41+ Send (userContext context.Context , request * sdk.SendRequest ) error
3542}
3643
37- type CredentialBase struct {
38- * ServiceBase
44+ type credentialBase struct {
45+ Service
3946 client sdk.VerifiableCredentialClient
4047}
4148
42- func (c * CredentialBase ) IssueCredential (userContext context.Context , document Document ) (Document , error ) {
43- jsonBytes , err := json . Marshal ( document )
49+ func (c * credentialBase ) IssueCredential (userContext context.Context , request * sdk. IssueRequest ) (* sdk. IssueResponse , error ) {
50+ md , err := c . GetMetadataContext ( userContext , request )
4451 if err != nil {
4552 return nil , err
4653 }
47- issueRequest := & sdk.IssueRequest {
48- DocumentJson : string (jsonBytes ),
49- }
5054
51- md , err := c .GetMetadataContext (userContext , issueRequest )
52- if err != nil {
53- return nil , err
54- }
55- response , err := c .client .Issue (md , issueRequest )
56- if err != nil {
57- return nil , err
58- }
59- var doc map [string ]interface {}
60- err = json .Unmarshal ([]byte (response .SignedDocumentJson ), & doc )
55+ response , err := c .client .Issue (md , request )
6156 if err != nil {
6257 return nil , err
6358 }
64- return doc , nil
59+
60+ return response , nil
6561}
6662
67- func (c * CredentialBase ) IssueFromTemplate (userContext context.Context , request * sdk.IssueFromTemplateRequest ) (* sdk.IssueFromTemplateResponse , error ) {
63+ func (c * credentialBase ) IssueFromTemplate (userContext context.Context , request * sdk.IssueFromTemplateRequest ) (* sdk.IssueFromTemplateResponse , error ) {
6864 md , err := c .GetMetadataContext (userContext , request )
6965 if err != nil {
7066 return nil , err
7167 }
68+
7269 response , err := c .client .IssueFromTemplate (md , request )
7370 if err != nil {
7471 return nil , err
7572 }
73+
7674 return response , nil
7775}
7876
79- func (c * CredentialBase ) CreateProof (userContext context.Context , documentId string , revealDocument Document ) (Document , error ) {
80- jsonString , err := json .Marshal (revealDocument )
81- if err != nil {
82- return nil , err
83- }
84- request := & sdk.CreateProofRequest {
85- Proof : & sdk.CreateProofRequest_ItemId {ItemId : documentId },
86- RevealDocumentJson : string (jsonString ),
87- }
77+ func (c * credentialBase ) CreateProof (userContext context.Context , request * sdk.CreateProofRequest ) (* sdk.CreateProofResponse , error ) {
8878 md , err := c .GetMetadataContext (userContext , request )
8979 if err != nil {
9080 return nil , err
9181 }
92- proof , err := c .client .CreateProof (md , request )
93- if err != nil {
94- return nil , err
95- }
96- var proofMap map [string ]interface {}
97- err = json .Unmarshal ([]byte (proof .ProofDocumentJson ), & proofMap )
82+
83+ response , err := c .client .CreateProof (md , request )
9884 if err != nil {
9985 return nil , err
10086 }
101- return proofMap , nil
87+
88+ return response , nil
10289}
10390
104- func (c * CredentialBase ) VerifyProof (userContext context.Context , proofDocument Document ) (bool , error ) {
105- jsonString , err := json .Marshal (proofDocument )
106- if err != nil {
107- return false , err
108- }
109- request := & sdk.VerifyProofRequest {
110- ProofDocumentJson : string (jsonString ),
111- }
91+ func (c * credentialBase ) VerifyProof (userContext context.Context , request * sdk.VerifyProofRequest ) (bool , error ) {
11292 md , err := c .GetMetadataContext (userContext , request )
11393 if err != nil {
11494 return false , err
11595 }
96+
11697 proof , err := c .client .VerifyProof (md , request )
11798 if err != nil {
11899 return false , err
119100 }
101+
120102 return proof .IsValid , nil
121103}
122104
123- func (c * CredentialBase ) CheckStatus (userContext context.Context , request * sdk.CheckStatusRequest ) (* sdk.CheckStatusResponse , error ) {
105+ func (c * credentialBase ) CheckStatus (userContext context.Context , request * sdk.CheckStatusRequest ) (* sdk.CheckStatusResponse , error ) {
124106 md , err := c .GetMetadataContext (userContext , request )
125107 if err != nil {
126108 return nil , err
@@ -132,7 +114,7 @@ func (c *CredentialBase) CheckStatus(userContext context.Context, request *sdk.C
132114 return response , nil
133115}
134116
135- func (c * CredentialBase ) UpdateStatus (userContext context.Context , request * sdk.UpdateStatusRequest ) (* sdk.UpdateStatusResponse , error ) {
117+ func (c * credentialBase ) UpdateStatus (userContext context.Context , request * sdk.UpdateStatusRequest ) (* sdk.UpdateStatusResponse , error ) {
136118 md , err := c .GetMetadataContext (userContext , request )
137119 if err != nil {
138120 return nil , err
@@ -147,24 +129,16 @@ func (c *CredentialBase) UpdateStatus(userContext context.Context, request *sdk.
147129 return response , fmt .Errorf ("error - did not run to completion %s" , response .Status )
148130}
149131
150- func (c * CredentialBase ) Send (userContext context.Context , document Document , email string ) error {
151- jsonString , err := json .Marshal (document )
152- if err != nil {
153- return err
154- }
155- request := & sdk.SendRequest {
156- DeliveryMethod : & sdk.SendRequest_Email {
157- Email : email ,
158- },
159- DocumentJson : string (jsonString ),
160- }
132+ func (c * credentialBase ) Send (userContext context.Context , request * sdk.SendRequest ) error {
161133 md , err := c .GetMetadataContext (userContext , request )
162134 if err != nil {
163135 return err
164136 }
137+
165138 _ , err = c .client .Send (md , request )
166139 if err != nil {
167140 return err
168141 }
142+
169143 return nil
170144}
0 commit comments