-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquote.go
More file actions
66 lines (58 loc) · 1.71 KB
/
quote.go
File metadata and controls
66 lines (58 loc) · 1.71 KB
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
package tap
import (
"encoding/json"
"fmt"
didcomm "github.com/Notabene-id/go-didcomm"
"github.com/google/uuid"
)
// QuoteBody represents the body of a TAP Quote message (TAIP-18).
type QuoteBody struct {
Context string `json:"@context"`
Type string `json:"@type"`
FromAsset string `json:"fromAsset"`
ToAsset string `json:"toAsset"`
FromAmount string `json:"fromAmount"`
ToAmount string `json:"toAmount"`
Provider *Party `json:"provider"`
Agents []Agent `json:"agents"`
ExpiresAt string `json:"expiresAt"`
}
func (b *QuoteBody) TAPType() string { return TypeQuote }
// NewQuoteMessage creates a new DIDComm message with a Quote body.
func NewQuoteMessage(from string, to []string, thid string, body *QuoteBody) (*didcomm.Message, error) {
if body.FromAsset == "" {
return nil, fmt.Errorf("%w: missing fromAsset", ErrInvalidBody)
}
if body.ToAsset == "" {
return nil, fmt.Errorf("%w: missing toAsset", ErrInvalidBody)
}
if body.FromAmount == "" {
return nil, fmt.Errorf("%w: missing fromAmount", ErrInvalidBody)
}
if body.ToAmount == "" {
return nil, fmt.Errorf("%w: missing toAmount", ErrInvalidBody)
}
if body.Provider == nil {
return nil, fmt.Errorf("%w: missing provider", ErrInvalidBody)
}
if len(body.Agents) == 0 {
return nil, fmt.Errorf("%w: missing agents", ErrInvalidBody)
}
if body.ExpiresAt == "" {
return nil, fmt.Errorf("%w: missing expiresAt", ErrInvalidBody)
}
body.Context = TAPContext
body.Type = TypeQuote
rawBody, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal body: %w", err)
}
return &didcomm.Message{
ID: uuid.New().String(),
Type: TypeQuote,
From: from,
To: to,
Thid: thid,
Body: rawBody,
}, nil
}