-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_test.go
More file actions
186 lines (164 loc) · 4.76 KB
/
transfer_test.go
File metadata and controls
186 lines (164 loc) · 4.76 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
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
package tap
import (
"encoding/json"
"errors"
"os"
"testing"
)
func TestNewTransferMessage(t *testing.T) {
body := &TransferBody{
Asset: "eip155:1/slip44:60",
Amount: "1.23",
Originator: &Party{ID: "did:eg:bob"},
Agents: []Agent{
{ID: "did:web:originator.vasp", For: NewForField("did:eg:bob")},
},
}
msg, err := NewTransferMessage("did:web:originator.vasp", []string{"did:web:beneficiary.vasp"}, body)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if msg.Type != TypeTransfer {
t.Errorf("Type: got %q, want %q", msg.Type, TypeTransfer)
}
if msg.From != "did:web:originator.vasp" {
t.Errorf("From: got %q", msg.From)
}
if msg.ID == "" {
t.Error("ID should not be empty")
}
// Verify body was marshaled correctly
var got TransferBody
if err := json.Unmarshal(msg.Body, &got); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if got.Context != TAPContext {
t.Errorf("Context: got %q, want %q", got.Context, TAPContext)
}
if got.Type != TypeTransfer {
t.Errorf("Body Type: got %q, want %q", got.Type, TypeTransfer)
}
if got.Asset != "eip155:1/slip44:60" {
t.Errorf("Asset: got %q", got.Asset)
}
}
func TestNewTransferMessage_MissingAsset(t *testing.T) {
body := &TransferBody{
Agents: []Agent{{ID: "did:web:originator.vasp"}},
}
_, err := NewTransferMessage("did:web:originator.vasp", []string{"did:web:beneficiary.vasp"}, body)
if !errors.Is(err, ErrInvalidBody) {
t.Errorf("expected ErrInvalidBody, got %v", err)
}
}
func TestNewTransferMessage_MissingAgents(t *testing.T) {
body := &TransferBody{
Asset: "eip155:1/slip44:60",
}
_, err := NewTransferMessage("did:web:originator.vasp", []string{"did:web:beneficiary.vasp"}, body)
if !errors.Is(err, ErrInvalidBody) {
t.Errorf("expected ErrInvalidBody, got %v", err)
}
}
func TestTransferBody_JSONRoundTrip(t *testing.T) {
body := TransferBody{
Context: TAPContext,
Type: TypeTransfer,
Asset: "eip155:1/slip44:60",
Amount: "1.23",
Originator: &Party{ID: "did:eg:bob"},
Beneficiary: &Party{ID: "did:eg:alice"},
Agents: []Agent{
{ID: "did:web:originator.vasp", For: NewForField("did:eg:bob")},
{ID: "did:web:beneficiary.vasp", For: NewForField("did:eg:alice")},
},
SettlementID: "eip155:1:tx/0x3edb98c24d46d148eb926c714f4fbaa117c47b0c0821f38bfce9763604457c33",
}
data, err := json.Marshal(body)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got TransferBody
if err := json.Unmarshal(data, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got.Asset != body.Asset || got.Amount != body.Amount || got.SettlementID != body.SettlementID {
t.Errorf("round-trip mismatch: got %+v", got)
}
}
func TestTransferBody_ParseBody(t *testing.T) {
body := &TransferBody{
Asset: "eip155:1/slip44:60",
Amount: "1.23",
Agents: []Agent{{ID: "did:web:originator.vasp", For: NewForField("did:eg:bob")}},
}
msg, err := NewTransferMessage("did:web:originator.vasp", []string{"did:web:beneficiary.vasp"}, body)
if err != nil {
t.Fatalf("create message: %v", err)
}
parsed, err := ParseBody(msg)
if err != nil {
t.Fatalf("parse body: %v", err)
}
tb, ok := parsed.(*TransferBody)
if !ok {
t.Fatalf("expected *TransferBody, got %T", parsed)
}
if tb.Asset != "eip155:1/slip44:60" {
t.Errorf("Asset: got %q", tb.Asset)
}
}
func TestTransfer_TestVectorValid(t *testing.T) {
data, err := os.ReadFile("TAIPs/test-vectors/transfer/valid.json")
if err != nil {
t.Skipf("test vector not available: %v", err)
}
var tv struct {
Message struct {
Body json.RawMessage `json:"body"`
} `json:"message"`
}
if err := json.Unmarshal(data, &tv); err != nil {
t.Fatalf("unmarshal test vector: %v", err)
}
var body TransferBody
if err := json.Unmarshal(tv.Message.Body, &body); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if body.Asset != "eip155:1/slip44:60" {
t.Errorf("Asset: got %q", body.Asset)
}
if body.Amount != "1.23" {
t.Errorf("Amount: got %q", body.Amount)
}
if len(body.Agents) != 3 {
t.Errorf("Agents: got %d, want 3", len(body.Agents))
}
if body.Context != TAPContext {
t.Errorf("Context: got %q", body.Context)
}
}
func TestTransfer_TestVectorMinimal(t *testing.T) {
data, err := os.ReadFile("TAIPs/test-vectors/transfer/minimal.json")
if err != nil {
t.Skipf("test vector not available: %v", err)
}
var tv struct {
Message struct {
Body json.RawMessage `json:"body"`
} `json:"message"`
}
if err := json.Unmarshal(data, &tv); err != nil {
t.Fatalf("unmarshal test vector: %v", err)
}
var body TransferBody
if err := json.Unmarshal(tv.Message.Body, &body); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if body.Asset != "eip155:1/slip44:60" {
t.Errorf("Asset: got %q", body.Asset)
}
if len(body.Agents) != 1 {
t.Errorf("Agents: got %d, want 1", len(body.Agents))
}
}