-
Notifications
You must be signed in to change notification settings - Fork 339
/
tx.go
71 lines (58 loc) · 1.5 KB
/
tx.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
package types
import (
"encoding/hex"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const TypeMsgFulfillOrder = "fulfill_order"
var _ = sdk.Msg(&MsgFulfillOrder{})
func NewMsgFulfillOrder(fulfillerAddress string, orderId string) *MsgFulfillOrder {
return &MsgFulfillOrder{
FulfillerAddress: fulfillerAddress,
OrderId: orderId,
}
}
func (msg *MsgFulfillOrder) Route() string {
return RouterKey
}
func (msg *MsgFulfillOrder) Type() string {
return TypeMsgFulfillOrder
}
func (msg *MsgFulfillOrder) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.FulfillerAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgFulfillOrder) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (m *MsgFulfillOrder) ValidateBasic() error {
if !m.isValidOrderId(m.OrderId) {
return ErrInvalidOrderID
}
_, err := sdk.AccAddressFromBech32(m.FulfillerAddress)
if err != nil {
return err
}
return nil
}
func (m *MsgFulfillOrder) isValidOrderId(orderId string) bool {
hashBytes, err := hex.DecodeString(orderId)
if err != nil {
// The string is not a valid hexadecimal string
return false
}
// SHA-256 hashes are 32 bytes long
return len(hashBytes) == 32
}
func (m *MsgFulfillOrder) Validate() error {
if err := m.ValidateBasic(); err != nil {
return err
}
return nil
}
func (m *MsgFulfillOrder) GetFulfillerBech32Address() []byte {
return sdk.MustAccAddressFromBech32(m.FulfillerAddress)
}