-
Notifications
You must be signed in to change notification settings - Fork 41
/
tx_did.go
136 lines (111 loc) · 3.78 KB
/
tx_did.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cli
import (
"crypto/ed25519"
"encoding/base64"
"github.com/cheqd/cheqd-node/x/cheqd/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
)
const FlagVerKey = "ver-key"
func CmdCreateDid() *cobra.Command {
cmd := &cobra.Command{
Use: "create-did [payload-json] [verification-method-id]",
Short: "Creates a new DID.",
Long: "Creates a new DID. [payload-json] is JSON encoded MsgCreateDidPayload. " +
"Key to sign the identity message (verKey) will be taken either from " + FlagVerKey + " flag or interactively." +
"[verification-method-id] is the DID fragment that points to the verKey.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
payloadJson := args[0]
verificationMethodId := args[1]
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// Unmarshal payload
var payload types.MsgCreateDidPayload
err = clientCtx.Codec.UnmarshalJSON([]byte(payloadJson), &payload)
if err != nil {
return err
}
// Get verKey
verKeyPriv, err := getVerKey(cmd, clientCtx)
if err != nil {
return err
}
// Build identity message
signBytes := payload.GetSignBytes()
signatureBytes := ed25519.Sign(verKeyPriv, signBytes)
signInfo := types.SignInfo{
VerificationMethodId: verificationMethodId,
Signature: base64.StdEncoding.EncodeToString(signatureBytes),
}
msg := types.MsgCreateDid{
Payload: &payload,
Signatures: []*types.SignInfo{&signInfo},
}
//Set fee-payer if not set
err = setFeePayerFromSigner(&clientCtx)
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(FlagVerKey, "", "Base64 encoded ed25519 private key to sign identity message with. "+
"Use for testing purposes only because the key will be stored in shell history. Prefer interactive mode.")
return cmd
}
func CmdUpdateDid() *cobra.Command {
cmd := &cobra.Command{
Use: "update-did [payload-json] [verification-method-id]",
Short: "Update a DID.",
Long: "Update a DID. [payload-json] is JSON encoded MsgUpdateDidPayload. " +
"Key to sign the identity message (verKey) will be taken either from " + FlagVerKey + " flag or interactively." +
"[verification-method-id] is the DID fragment that points to the verKey.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
payloadJson := args[0]
verificationMethodId := args[1]
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// Unmarshal payload
var payload types.MsgUpdateDidPayload
err = clientCtx.Codec.UnmarshalJSON([]byte(payloadJson), &payload)
if err != nil {
return err
}
// Get verKey
verKeyPriv, err := getVerKey(cmd, clientCtx)
if err != nil {
return err
}
// Build identity message
signBytes := payload.GetSignBytes()
signatureBytes := ed25519.Sign(verKeyPriv, signBytes)
signInfo := types.SignInfo{
VerificationMethodId: verificationMethodId,
Signature: base64.StdEncoding.EncodeToString(signatureBytes),
}
msg := types.MsgUpdateDid{
Payload: &payload,
Signatures: []*types.SignInfo{&signInfo},
}
//Set fee-payer if not set
err = setFeePayerFromSigner(&clientCtx)
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(FlagVerKey, "", "Base64 encoded ed25519 private key to sign identity message with. "+
"Use for testing purposes only because the key will be stored in shell history. Prefer interactive mode.")
return cmd
}