Skip to content

Commit 5d4cae9

Browse files
authored
Update to Golang SDK (#478)
* update to golang sdk * remove document type * fix lint * update comments Co-authored-by: Seth Back <[email protected]>
1 parent 5972c9c commit 5d4cae9

13 files changed

+383
-341
lines changed

go/services/account_service.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212

1313
// NewAccountService returns an account servcie with the base service configured
1414
// using the provided options
15-
func NewAccountService(options *sdk.ServiceOptions) (AccountService, error) {
15+
func NewAccountService(options *Options) (AccountService, error) {
1616
base, err := NewServiceBase(options)
1717
if err != nil {
1818
return nil, err
1919
}
20-
service := &AccountBase{
21-
ServiceBase: base,
22-
client: sdk.NewAccountClient(base.channel),
20+
service := &accountBase{
21+
Service: base,
22+
client: sdk.NewAccountClient(base.GetChannel()),
2323
}
2424

2525
return service, nil
@@ -29,9 +29,9 @@ func NewAccountService(options *sdk.ServiceOptions) (AccountService, error) {
2929
type AccountService interface {
3030
Service
3131
// SignIn returns an encoded auth token
32-
SignIn(userContext context.Context, details *sdk.AccountDetails, inviteCode, ecosystemID string) (string, sdk.ConfirmationMethod, error)
32+
SignIn(userContext context.Context, request *sdk.SignInRequest) (string, sdk.ConfirmationMethod, error)
3333
// Unprotect takes an authtoken that has been protected using a pin code
34-
// and returns an ulocked token
34+
// and returns an unlocked token
3535
Unprotect(authtoken, securityCode string) (string, error)
3636
// Protect will apply the given security code blind to the provided token
3737
Protect(authtoken, securityCode string) (string, error)
@@ -43,37 +43,35 @@ type AccountService interface {
4343
RevokeDevice(userContext context.Context, request *sdk.RevokeDeviceRequest) (*sdk.RevokeDeviceResponse, error)
4444
}
4545

46-
// AccountBase implements the AccountService interface
47-
type AccountBase struct {
48-
*ServiceBase
46+
type accountBase struct {
47+
Service
4948
client sdk.AccountClient
5049
}
5150

5251
// SignIn to a given account
53-
func (a *AccountBase) SignIn(userContext context.Context, details *sdk.AccountDetails, inviteCode, ecosystemID string) (string, sdk.ConfirmationMethod, error) {
54-
if details == nil {
55-
details = &sdk.AccountDetails{}
56-
}
57-
58-
if len(ecosystemID) == 0 {
59-
ecosystemID = a.options.DefaultEcosystem
52+
func (a *accountBase) SignIn(userContext context.Context, request *sdk.SignInRequest) (string, sdk.ConfirmationMethod, error) {
53+
if len(request.EcosystemId) == 0 {
54+
request.EcosystemId = a.GetServiceOptions().DefaultEcosystem
6055
}
6156

62-
response, err := a.client.SignIn(userContext, &sdk.SignInRequest{Details: details, InvitationCode: inviteCode, EcosystemId: ecosystemID})
57+
resp, err := a.client.SignIn(userContext, request)
6358
if err != nil {
6459
return "", sdk.ConfirmationMethod_None, err
6560
}
6661

67-
tkn, err := ProfileToToken(response.Profile)
62+
tkn, err := ProfileToToken(resp.Profile)
6863
if err != nil {
6964
return "", sdk.ConfirmationMethod_None, err
7065
}
7166

72-
return tkn, response.ConfirmationMethod, nil
67+
return tkn, resp.ConfirmationMethod, nil
7368
}
7469

7570
// Unprotect an authtoken using the given security code
76-
func (a *AccountBase) Unprotect(authtoken, securityCode string) (string, error) {
71+
//
72+
// This method can be called multiple times on an individual token
73+
// to "unwrap" the blindings that have been applied
74+
func (a *accountBase) Unprotect(authtoken, securityCode string) (string, error) {
7775
profile, err := ProfileFromToken(authtoken)
7876
if err != nil {
7977
return "", err
@@ -101,8 +99,8 @@ func (a *AccountBase) Unprotect(authtoken, securityCode string) (string, error)
10199
// Protect an authtoken with the given security code. Must be unprotected before use
102100
//
103101
// This method can be called as many times as you want, but each code must be "unwrapped"
104-
// in the reverse order before use
105-
func (a *AccountBase) Protect(authtoken, securityCode string) (string, error) {
102+
// by calling Unprotect in the reverse order before use
103+
func (a *accountBase) Protect(authtoken, securityCode string) (string, error) {
106104
profile, err := ProfileFromToken(authtoken)
107105
if err != nil {
108106
return "", err
@@ -128,7 +126,7 @@ func (a *AccountBase) Protect(authtoken, securityCode string) (string, error) {
128126
}
129127

130128
// GetInfo associated with a given wallet
131-
func (a *AccountBase) GetInfo(userContext context.Context) (*sdk.InfoResponse, error) {
129+
func (a *accountBase) GetInfo(userContext context.Context) (*sdk.InfoResponse, error) {
132130
request := &sdk.InfoRequest{}
133131
md, err := a.GetMetadataContext(userContext, request)
134132
if err != nil {
@@ -142,7 +140,7 @@ func (a *AccountBase) GetInfo(userContext context.Context) (*sdk.InfoResponse, e
142140
}
143141

144142
// ListDevices that can access the cloud wallet
145-
func (a *AccountBase) ListDevices(userContext context.Context, request *sdk.ListDevicesRequest) (*sdk.ListDevicesResponse, error) {
143+
func (a *accountBase) ListDevices(userContext context.Context, request *sdk.ListDevicesRequest) (*sdk.ListDevicesResponse, error) {
146144
md, err := a.GetMetadataContext(userContext, request)
147145
if err != nil {
148146
return nil, err
@@ -155,7 +153,7 @@ func (a *AccountBase) ListDevices(userContext context.Context, request *sdk.List
155153
}
156154

157155
// RevokeDevice from the cloud wallet
158-
func (a *AccountBase) RevokeDevice(userContext context.Context, request *sdk.RevokeDeviceRequest) (*sdk.RevokeDeviceResponse, error) {
156+
func (a *accountBase) RevokeDevice(userContext context.Context, request *sdk.RevokeDeviceRequest) (*sdk.RevokeDeviceResponse, error) {
159157
md, err := a.GetMetadataContext(userContext, request)
160158
if err != nil {
161159
return nil, err

go/services/account_service_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8+
sdk "github.com/trinsic-id/sdk/go/proto"
89
)
910

1011
func TestProtectUnprotectProfile(t *testing.T) {
@@ -19,7 +20,8 @@ func TestProtectUnprotectProfile(t *testing.T) {
1920
if !assert2.Nil(err) {
2021
return
2122
}
22-
profile, _, err := accountService.SignIn(context.Background(), nil, "", "default")
23+
24+
profile, _, err := accountService.SignIn(context.Background(), &sdk.SignInRequest{})
2325
if !assert2.Nil(err) {
2426
return
2527
}
@@ -35,7 +37,7 @@ func TestProtectUnprotectProfile(t *testing.T) {
3537
}
3638

3739
t.Run("Protected profile should fail", func(t *testing.T) {
38-
accountService.SetProfile(protectedProfile)
40+
accountService.SetToken(protectedProfile)
3941
info2, err2 := accountService.GetInfo(context.Background())
4042
if !assert2.NotNil(err2) {
4143
t.FailNow()
@@ -46,7 +48,7 @@ func TestProtectUnprotectProfile(t *testing.T) {
4648
})
4749

4850
t.Run("Unprotected profile should work", func(t *testing.T) {
49-
accountService.SetProfile(unprotectedProfile)
51+
accountService.SetToken(unprotectedProfile)
5052
info2, err2 := accountService.GetInfo(context.Background())
5153
if !assert2.Nil(err2) {
5254
t.FailNow()

go/services/credential_service.go

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,125 +2,107 @@ package services
22

33
import (
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
2626
type 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

Comments
 (0)