-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescrow.go
More file actions
63 lines (55 loc) · 1.7 KB
/
escrow.go
File metadata and controls
63 lines (55 loc) · 1.7 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
package tap
import (
"encoding/json"
"fmt"
didcomm "github.com/Notabene-id/go-didcomm"
"github.com/google/uuid"
)
// EscrowBody represents the body of a TAP Escrow message (TAIP-17).
type EscrowBody struct {
Context string `json:"@context"`
Type string `json:"@type"`
Asset string `json:"asset,omitempty"`
Currency string `json:"currency,omitempty"`
Amount string `json:"amount"`
Originator *Party `json:"originator"`
Beneficiary *Party `json:"beneficiary"`
Expiry string `json:"expiry"`
Agents []Agent `json:"agents"`
Agreement string `json:"agreement,omitempty"`
}
func (b *EscrowBody) TAPType() string { return TypeEscrow }
// NewEscrowMessage creates a new DIDComm message with an Escrow body.
func NewEscrowMessage(from string, to []string, body *EscrowBody) (*didcomm.Message, error) {
if body.Amount == "" {
return nil, fmt.Errorf("%w: missing amount", ErrInvalidBody)
}
if body.Originator == nil {
return nil, fmt.Errorf("%w: missing originator", ErrInvalidBody)
}
if body.Beneficiary == nil {
return nil, fmt.Errorf("%w: missing beneficiary", ErrInvalidBody)
}
if body.Expiry == "" {
return nil, fmt.Errorf("%w: missing expiry", ErrInvalidBody)
}
if len(body.Agents) == 0 {
return nil, fmt.Errorf("%w: missing agents", ErrInvalidBody)
}
if body.Asset == "" && body.Currency == "" {
return nil, fmt.Errorf("%w: missing asset or currency", ErrInvalidBody)
}
body.Context = TAPContext
body.Type = TypeEscrow
rawBody, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal body: %w", err)
}
return &didcomm.Message{
ID: uuid.New().String(),
Type: TypeEscrow,
From: from,
To: to,
Body: rawBody,
}, nil
}