-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_propose_sweep_address.go
79 lines (63 loc) · 2.17 KB
/
message_propose_sweep_address.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
package types
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
volttypes "github.com/twilight-project/nyks/x/volt/types"
)
const TypeMsgProposeSweepAddress = "propose_sweep_address"
var _ sdk.Msg = &MsgProposeSweepAddress{}
func NewMsgProposeSweepAddress(btcAddress string, btcScript string, reserveId uint64, roundId uint64, judgeAddress string) *MsgProposeSweepAddress {
return &MsgProposeSweepAddress{
BtcAddress: btcAddress,
BtcScript: btcScript,
ReserveId: reserveId,
RoundId: roundId,
JudgeAddress: judgeAddress,
}
}
func (msg *MsgProposeSweepAddress) Route() string {
return RouterKey
}
func (msg *MsgProposeSweepAddress) Type() string {
return TypeMsgProposeSweepAddress
}
func (msg *MsgProposeSweepAddress) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.JudgeAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgProposeSweepAddress) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgProposeSweepAddress) ValidateBasic() error {
// Validate BTC Address
if strings.TrimSpace(msg.BtcAddress) == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "BTC address cannot be empty")
}
if ValidateBtcAddress(msg.BtcAddress) != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "Invalid BTC address")
}
// Validate BTC Script
if strings.TrimSpace(msg.BtcScript) == "" {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "BTC script cannot be empty")
}
if len(msg.BtcScript) < 5 || len(msg.BtcScript) > 10000 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Invalid BTC script length")
}
// Validate reserveId (it should be a positive number)
if msg.ReserveId <= 1 && msg.ReserveId >= volttypes.BtcReserveMaxLimit {
return sdkerrors.Wrapf(ErrInvalid, "invalid reserveId")
}
if msg.RoundId < 1 {
return sdkerrors.Wrapf(ErrInvalid, "invalid roundId")
}
// Validate Judge Address
if _, err := sdk.AccAddressFromBech32(msg.JudgeAddress); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "Invalid judge address")
}
return nil
}