-
Notifications
You must be signed in to change notification settings - Fork 29
/
msgs.go
114 lines (90 loc) · 3.14 KB
/
msgs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package types
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ixofoundation/ixo-blockchain/x/did/exported"
ixotypes "github.com/ixofoundation/ixo-blockchain/x/ixo/types"
)
const (
TypeMsgAddDid = "add-did"
TypeMsgAddCredential = "add-credential"
)
var (
_ ixotypes.IxoMsg = &MsgAddDid{}
_ ixotypes.IxoMsg = &MsgAddCredential{}
)
func NewMsgAddDid(did string, publicKey string) *MsgAddDid {
return &MsgAddDid{
Did: did,
PubKey: publicKey,
}
}
func (msg MsgAddDid) Type() string { return TypeMsgAddDid }
func (msg MsgAddDid) Route() string { return RouterKey }
func (msg MsgAddDid) GetSignerDid() exported.Did { return msg.Did }
func (msg MsgAddDid) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{nil} // not used in signature verification in ixo AnteHandler
}
func (msg MsgAddDid) ValidateBasic() error {
// Check that not empty
if strings.TrimSpace(msg.Did) == "" {
return sdkerrors.Wrap(ErrInvalidDid, "did should not be empty")
} else if strings.TrimSpace(msg.PubKey) == "" {
return sdkerrors.Wrap(ErrInvalidPubKey, "pubKey should not be empty")
}
// Check that DID and PubKey valid
if !IsValidDid(msg.Did) {
return sdkerrors.Wrap(ErrInvalidDid, "DID is invalid")
} else if !IsValidPubKey(msg.PubKey) {
return sdkerrors.Wrap(ErrInvalidPubKey, "pubKey is invalid")
}
// Check that DID matches the PubKey
unprefixedDid := exported.UnprefixedDid(msg.Did)
expectedUnprefixedDid := exported.UnprefixedDidFromPubKey(msg.PubKey)
if unprefixedDid != expectedUnprefixedDid {
return sdkerrors.Wrapf(ErrDidPubKeyMismatch,
"did not deducable from pubKey; expected: %s received: %s",
expectedUnprefixedDid, unprefixedDid)
}
return nil
}
func (msg MsgAddDid) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func NewMsgAddCredential(did string, credType []string, issuer string, issued string) *MsgAddCredential {
didCredential := DidCredential{
CredType: credType,
Issuer: issuer,
Issued: issued,
Claim: &Claim{
Id: did,
KYCValidated: true,
},
}
return &MsgAddCredential{
DidCredential: didCredential,
}
}
func (msg MsgAddCredential) Type() string { return TypeMsgAddCredential }
func (msg MsgAddCredential) Route() string { return RouterKey }
func (msg MsgAddCredential) GetSignerDid() exported.Did { return msg.DidCredential.Issuer }
func (msg MsgAddCredential) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{nil} // not used in signature verification in ixo AnteHandler
}
func (msg MsgAddCredential) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.DidCredential.Claim.Id) == "" {
return sdkerrors.Wrap(ErrInvalidClaimId, "claim ID should not be empty")
} else if strings.TrimSpace(msg.DidCredential.Issuer) == "" {
return sdkerrors.Wrap(ErrInvalidIssuer, "issuer should not be empty")
}
// Check that DID valid
if !IsValidDid(msg.DidCredential.Issuer) {
return sdkerrors.Wrap(ErrInvalidDid, "issuer DID is invalid")
}
return nil
}
func (msg MsgAddCredential) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}