-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
130 lines (105 loc) · 3.32 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package request
import (
"context"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
messagingpb "github.com/code-payments/code-protobuf-api/generated/go/messaging/v1"
micropaymentpb "github.com/code-payments/code-protobuf-api/generated/go/micropayment/v1"
userpb "github.com/code-payments/code-protobuf-api/generated/go/user/v1"
"github.com/code-payments/code-server/pkg/code/common"
)
// todo: Migrate to a generic HTTP -> gRPC with signed proto strategy
func (s *Server) createTrustlessRequest(ctx context.Context, request *trustlessRequest) (err error) {
return s.createRequest(
ctx,
&messagingpb.SendMessageRequest{
RendezvousKey: &messagingpb.RendezvousKey{
Value: request.GetPublicRendezvousKey().PublicKey().ToBytes(),
},
Message: request.ToProtoMessage(),
Signature: &commonpb.Signature{
Value: request.clientSignature[:],
},
},
request.webhookUrl,
)
}
func (s *Server) createRequest(
ctx context.Context,
signedCreateRequest *messagingpb.SendMessageRequest,
webhookUrl *string,
) error {
messagingClient := messagingpb.NewMessagingClient(s.cc)
microPaymentClient := micropaymentpb.NewMicroPaymentClient(s.cc)
//
// Part 1: Create the request.
//
// Note that retries are acceptable
createResp, err := messagingClient.SendMessage(ctx, signedCreateRequest)
if err != nil {
return err
} else if createResp.Result != messagingpb.SendMessageResponse_OK {
return errors.Errorf("send message result %s", createResp.Result)
}
//
// Part 2: Register webhook if URL was provided
//
if webhookUrl != nil {
registerWebhookReq := µpaymentpb.RegisterWebhookRequest{
IntentId: &commonpb.IntentId{
Value: signedCreateRequest.RendezvousKey.Value,
},
Url: *webhookUrl,
}
registerWebhookResp, err := microPaymentClient.RegisterWebhook(ctx, registerWebhookReq)
if err != nil {
return err
}
switch registerWebhookResp.Result {
case micropaymentpb.RegisterWebhookResponse_OK, micropaymentpb.RegisterWebhookResponse_ALREADY_REGISTERED:
default:
return errors.Errorf("register webhook result %s", createResp.Result)
}
}
return nil
}
type intentStatusResp struct {
Status string
}
func (s *Server) getIntentStatus(ctx context.Context, intentId *common.Account) (*intentStatusResp, error) {
microPaymentClient := micropaymentpb.NewMicroPaymentClient(s.cc)
getStatusReq := µpaymentpb.GetStatusRequest{
IntentId: &commonpb.IntentId{
Value: intentId.PublicKey().ToBytes(),
},
}
getStatusResp, err := microPaymentClient.GetStatus(ctx, getStatusReq)
if err != nil {
return nil, err
}
res := intentStatusResp{
Status: "UNKNOWN",
}
if getStatusResp.IntentSubmitted {
res.Status = "SUBMITTED"
} else if getStatusResp.Exists {
res.Status = "PENDING"
}
return &res, nil
}
type getUserIdResp struct {
User string
}
func (s *Server) getUserId(ctx context.Context, protoReq *userpb.GetLoginForThirdPartyAppRequest) (*getUserIdResp, error) {
userIdentityClient := userpb.NewIdentityClient(s.cc)
getLoginResp, err := userIdentityClient.GetLoginForThirdPartyApp(ctx, protoReq)
if err != nil {
return nil, err
}
res := &getUserIdResp{}
if getLoginResp.Result == userpb.GetLoginForThirdPartyAppResponse_OK {
res.User = base58.Encode(getLoginResp.UserId.Value)
}
return res, nil
}