forked from eoscanada/eosc
/
txCreate.go
87 lines (71 loc) · 2.2 KB
/
txCreate.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
// Copyright © 2018 EOS Canada <info@eoscanada.com>
package cmd
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"io"
eos "github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/system"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var txCreateCmd = &cobra.Command{
Use: "create [contract] [action] [payload]",
Short: "Create a transaction with a single action",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
contract := toAccount(args[0], "contract")
action := toActionName(args[1], "action")
payload := args[2]
forceUnique := viper.GetBool("tx-create-cmd-force-unique")
var dump map[string]interface{}
err := json.Unmarshal([]byte(payload), &dump)
errorCheck("[payload] is not valid JSON", err)
api := getAPI()
actionBinary, err := api.ABIJSONToBin(contract, eos.Name(action), dump)
errorCheck("unable to retrieve action binary from JSON via API", err)
actions := []*eos.Action{
&eos.Action{
Account: contract,
Name: action,
ActionData: eos.NewActionDataFromHexData([]byte(actionBinary)),
}}
var contextFreeActions []*eos.Action
if forceUnique {
contextFreeActions = append(contextFreeActions, newNonceAction())
}
pushEOSCActionsAndContextFreeActions(api, contextFreeActions, actions)
},
}
func newNonceAction() *eos.Action {
return &eos.Action{
Account: eos.AN("eosio.null"),
Name: eos.ActN("nonce"),
ActionData: eos.NewActionData(system.Nonce{
Value: hex.EncodeToString(generateRandomNonce()),
}),
}
}
func generateRandomNonce() []byte {
// Use 48 bits of entropy to generate a valid random
nonce := make([]byte, 6)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
errorCheck("unable to correctly generate nonce", err)
}
return nonce
}
func init() {
txCmd.AddCommand(txCreateCmd)
txCreateCmd.Flags().BoolP(
"force-unique",
"f",
false,
"force the transaction to be unique. this will consume extra bandwidth and remove any protections against accidently issuing the same transaction multiple times",
)
for _, flag := range []string{"force-unique"} {
if err := viper.BindPFlag("tx-create-cmd-"+flag, txCreateCmd.Flags().Lookup(flag)); err != nil {
panic(err)
}
}
}