/
msg.go
94 lines (83 loc) · 2.35 KB
/
msg.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
package paychan
import (
"github.com/iov-one/weave"
"github.com/iov-one/weave/errors"
)
var _ weave.Msg = (*CreatePaymentChannelMsg)(nil)
var _ weave.Msg = (*TransferPaymentChannelMsg)(nil)
var _ weave.Msg = (*ClosePaymentChannelMsg)(nil)
const (
pathCreatePaymentChannelMsg = "paychan/create"
pathTransferPaymentChannelMsg = "paychan/transfer"
pathClosePaymentChannelMsg = "paychan/close"
)
func (m *CreatePaymentChannelMsg) Validate() error {
if m.Src == nil {
return errors.Wrap(errors.ErrInvalidMsg, "missing source")
}
if m.SenderPubkey == nil {
return errors.Wrap(errors.ErrInvalidMsg, "missing sender public key")
}
if m.Recipient == nil {
return errors.Wrap(errors.ErrInvalidMsg, "missing recipient")
}
if m.Total == nil || m.Total.IsZero() {
return errors.Wrap(errors.ErrInvalidMsg, "invalid total amount")
}
if m.Timeout <= 0 {
return errors.Wrap(errors.ErrInvalidMsg, "invalid timeout value")
}
if len(m.Memo) > 128 {
return errors.Wrap(errors.ErrInvalidMsg, "memo too long")
}
return validateAddresses(m.Recipient, m.Src)
}
func (CreatePaymentChannelMsg) Path() string {
return pathCreatePaymentChannelMsg
}
func (m *TransferPaymentChannelMsg) Validate() error {
if m.Signature == nil {
return errors.Wrap(errors.ErrInvalidMsg, "missing signature")
}
if m.Payment == nil {
return errors.Wrap(errors.ErrInvalidMsg, "missing payment")
}
if m.Payment.ChainID == "" {
return errors.Wrap(errors.ErrInvalidMsg, "missing chain ID")
}
if m.Payment.ChannelID == nil {
return errors.Wrap(errors.ErrInvalidMsg, "missing channel ID")
}
if !m.Payment.Amount.IsPositive() {
return errors.Wrap(errors.ErrInvalidMsg, "invalid amount value")
}
return nil
}
func (TransferPaymentChannelMsg) Path() string {
return pathTransferPaymentChannelMsg
}
func (m *ClosePaymentChannelMsg) Validate() error {
if m.ChannelID == nil {
return errors.Wrap(errors.ErrInvalidMsg, "missing channel ID")
}
if len(m.Memo) > 128 {
return errors.Wrap(errors.ErrInvalidMsg, "memo too long")
}
return nil
}
func (ClosePaymentChannelMsg) Path() string {
return pathClosePaymentChannelMsg
}
// validateAddresses returns an error if any non empty address does not
// validate.
func validateAddresses(addrs ...weave.Address) error {
for _, a := range addrs {
if a == nil {
continue
}
if err := a.Validate(); err != nil {
return err
}
}
return nil
}