-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.go
More file actions
55 lines (47 loc) · 1.55 KB
/
connect.go
File metadata and controls
55 lines (47 loc) · 1.55 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
package tap
import (
"encoding/json"
"fmt"
didcomm "github.com/Notabene-id/go-didcomm"
"github.com/google/uuid"
)
// ConnectBody represents the body of a TAP Connect message (TAIP-15).
type ConnectBody struct {
Context string `json:"@context"`
Type string `json:"@type"`
Requester *Party `json:"requester"`
Principal *Party `json:"principal"`
Agents []Agent `json:"agents"`
Constraints *TransactionConstraints `json:"constraints"`
Agreement string `json:"agreement,omitempty"`
Expiry string `json:"expiry,omitempty"`
}
func (b *ConnectBody) TAPType() string { return TypeConnect }
// NewConnectMessage creates a new DIDComm message with a Connect body.
func NewConnectMessage(from string, to []string, body *ConnectBody) (*didcomm.Message, error) {
if body.Requester == nil {
return nil, fmt.Errorf("%w: missing requester", ErrInvalidBody)
}
if body.Principal == nil {
return nil, fmt.Errorf("%w: missing principal", ErrInvalidBody)
}
if len(body.Agents) == 0 {
return nil, fmt.Errorf("%w: missing agents", ErrInvalidBody)
}
if body.Constraints == nil {
return nil, fmt.Errorf("%w: missing constraints", ErrInvalidBody)
}
body.Context = TAPContext
body.Type = TypeConnect
rawBody, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal body: %w", err)
}
return &didcomm.Message{
ID: uuid.New().String(),
Type: TypeConnect,
From: from,
To: to,
Body: rawBody,
}, nil
}