-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote.go
265 lines (219 loc) · 7.33 KB
/
quote.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package quote
import (
"encoding/json"
"fmt"
"time"
"github.com/TBD54566975/tbdex-go/tbdex/closemsg"
"github.com/TBD54566975/tbdex-go/tbdex/crypto"
"github.com/TBD54566975/tbdex-go/tbdex/message"
"github.com/TBD54566975/tbdex-go/tbdex/order"
"github.com/TBD54566975/tbdex-go/tbdex/validator"
"github.com/tbd54566975/web5-go/dids/did"
"go.jetpack.io/typeid"
)
// Kind identifies this message kind
const Kind = "quote"
// ValidNext returns the valid message kinds that can follow a Quote.
func ValidNext() []string {
return []string{order.Kind, closemsg.Kind}
}
// Quote represents a quote message within the exchange.
type Quote struct {
Metadata message.Metadata `json:"metadata,omitempty"`
Data Data `json:"data,omitempty"`
Signature string `json:"signature,omitempty"`
}
// GetMetadata returns the metadata of the message
func (q Quote) GetMetadata() message.Metadata {
return q.Metadata
}
// GetKind returns the kind of message
func (q Quote) GetKind() string {
return Kind
}
// GetValidNext returns the valid message kinds that can follow a Quote.
func (q Quote) GetValidNext() []string {
return ValidNext()
}
// Data encapsulates the data content of a quote.
type Data struct {
ExpiresAt string `json:"expiresAt,omitmepty"`
Payin QuoteDetails `json:"payin,omitempty"`
Payout QuoteDetails `json:"payout,omitempty"`
}
// QuoteDetails describes the relevant information of a currency that is being sent or received
type QuoteDetails struct {
CurrencyCode string `json:"currencyCode,omitempty"`
Amount string `json:"amount,omitempty"`
Fee string `json:"fee,omitempty,omitempty"`
PaymentInstruction *PaymentInstruction `json:"paymentInstruction,omitempty"`
}
// PaymentInstruction contains instructions with plain text and/or a link
type PaymentInstruction struct {
Link string `json:"link,omitempty"`
Instruction string `json:"instruction,omitempty"`
}
// Digest computes a hash of the quote
func (q Quote) Digest() ([]byte, error) {
payload := map[string]any{"metadata": q.Metadata, "data": q.Data}
hashed, err := crypto.DigestJSON(payload)
if err != nil {
return nil, fmt.Errorf("failed to digest quote: %w", err)
}
return hashed, nil
}
// Verify verifies the signature of the quote.
func (q *Quote) Verify() error {
decoded, err := crypto.VerifySignature(q, q.Signature)
if err != nil {
return fmt.Errorf("failed to verify quote signature: %w", err)
}
if decoded.SignerDID.URI != q.Metadata.From {
return fmt.Errorf("signer: %s does not match message metadata from: %s", decoded.SignerDID.URI, q.Metadata.From)
}
return nil
}
// UnmarshalJSON validates and unmarshals the input data into an Quote.
func (q *Quote) UnmarshalJSON(data []byte) error {
err := validator.Validate(validator.TypeMessage, data, validator.WithKind(Kind))
if err != nil {
return fmt.Errorf("invalid quote: %w", err)
}
ret := quote{}
err = json.Unmarshal(data, &ret)
if err != nil {
return fmt.Errorf("failed to JSON unmarshal quote: %w", err)
}
*q = Quote(ret)
return nil
}
// Parse validates, parses input data into an Quote, and verifies the signature.
func Parse(data []byte) (Quote, error) {
q := Quote{}
if err := json.Unmarshal(data, &q); err != nil {
return Quote{}, fmt.Errorf("failed to unmarshal Quote: %w", err)
}
if err := q.Verify(); err != nil {
return Quote{}, fmt.Errorf("failed to verify Quote: %w", err)
}
return q, nil
}
// Create generates a new Quote with the specified parameters and options.
func Create(fromDID did.BearerDID, to, exchangeID, expiresAt string, payin, payout QuoteDetails, opts ...CreateOption) (Quote, error) {
q := createOptions{
id: typeid.Must(typeid.WithPrefix(Kind)).String(),
createdAt: time.Now(),
protocol: "1.0",
}
for _, opt := range opts {
opt(&q)
}
quote := Quote{
Metadata: message.Metadata{
From: fromDID.URI,
To: to,
Kind: Kind,
ID: q.id,
ExchangeID: exchangeID,
CreatedAt: q.createdAt.UTC().Format(time.RFC3339),
ExternalID: q.externalID,
Protocol: q.protocol,
},
Data: Data{ExpiresAt: expiresAt, Payin: payin, Payout: payout},
}
signature, err := crypto.Sign(quote, fromDID)
if err != nil {
return Quote{}, fmt.Errorf("failed to sign quote: %w", err)
}
quote.Signature = signature
return quote, nil
}
type createOptions struct {
id string
createdAt time.Time
protocol string
externalID string
}
// CreateOption defines a type for functions that can modify the createOptions struct.
type CreateOption func(*createOptions)
// ID can be passed to [Create] to provide a custom id.
func ID(id string) CreateOption {
return func(o *createOptions) {
o.id = id
}
}
// CreatedAt can be passed to [Create] to provide a custom created at time.
func CreatedAt(t time.Time) CreateOption {
return func(o *createOptions) {
o.createdAt = t
}
}
// ExternalID can be passed to [Create] to provide a custom external id.
func ExternalID(externalID string) CreateOption {
return func(o *createOptions) {
o.externalID = externalID
}
}
type quoteDetailsOptions struct {
Fee string
PaymentInstruction *PaymentInstruction
}
// QuoteDetailsOption defines a type for functions that can modify the quoteDetailsOptions struct.
type QuoteDetailsOption func(*quoteDetailsOptions)
// DetailsFee is an option for [NewQuoteDetails] that allows setting a custom fee for a [QuoteDetails].
func DetailsFee(fee string) QuoteDetailsOption {
return func(q *quoteDetailsOptions) {
q.Fee = fee
}
}
// DetailsInstruction is an option for NewQuoteDetails that allows setting a custom [PaymentInstruction]
// for a [QuoteDetails].
func DetailsInstruction(p *PaymentInstruction) QuoteDetailsOption {
return func(q *quoteDetailsOptions) {
q.PaymentInstruction = p
}
}
// NewQuoteDetails creates a [QuoteDetails] object with the specified currency code, amount,
// and optional modifications provided through [QuoteDetailsOption] functions.
func NewQuoteDetails(currencyCode string, amount string, opts ...QuoteDetailsOption) QuoteDetails {
q := quoteDetailsOptions{}
for _, opt := range opts {
opt(&q)
}
return QuoteDetails{
CurrencyCode: currencyCode,
Amount: amount,
Fee: q.Fee,
PaymentInstruction: q.PaymentInstruction,
}
}
type paymentInstructionOptions struct {
Link string
Instruction string
}
// PaymentInstructionOptions defines a type for functions that can modify the paymentInstructionOptions struct.
type PaymentInstructionOptions func(*paymentInstructionOptions)
// InstructionLink is an option for [NewPaymentInstruction] that allows setting a custom link.
func InstructionLink(link string) PaymentInstructionOptions {
return func(p *paymentInstructionOptions) {
p.Link = link
}
}
// Instruction is an option for [NewPaymentInstruction] that allows setting custom text.
func Instruction(instruction string) PaymentInstructionOptions {
return func(p *paymentInstructionOptions) {
p.Instruction = instruction
}
}
// NewPaymentInstruction creates a new [PaymentInstruction] using the provided options.
func NewPaymentInstruction(opts ...PaymentInstructionOptions) *PaymentInstruction {
p := paymentInstructionOptions{}
for _, opt := range opts {
opt(&p)
}
return &PaymentInstruction{
Link: p.Link,
Instruction: p.Instruction,
}
}
type quote Quote