forked from eoscanada/eosc
/
txPush.go
87 lines (67 loc) · 2.23 KB
/
txPush.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 (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
eos "github.com/sebastianmontero/eos-go"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
)
var txPushCmd = &cobra.Command{
Use: "push [transaction.json | string]",
Short: "Push a signed transaction to the chain. Must be done online.",
Long: `
The first argument interpretation is heuristics based, and depends on its
content.
If it's an existing file on disk, the first argument is assumed to be a
filename containing a transaction in JSON format and is used right away.
Otherwise, if the first argument looks like a JSON string (basic checks
just enough to avoid ambiguity with a plain filename), then the input is
considered a JSON transaction.
Otherwise, if the argument is not deem being a valid JSON input, it's
assumed to be a filename that is not present and a proper error message
is displayed.
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cnt, err := readTransaction(args[0])
errorCheck("reading transaction", err)
chainID := gjson.GetBytes(cnt, "chain_id").String()
hexChainID, _ := hex.DecodeString(chainID)
var signedTx *eos.SignedTransaction
errorCheck("json unmarshal transaction", json.Unmarshal(cnt, &signedTx))
api := getAPI()
packedTx, err := signedTx.Pack(eos.CompressionNone)
errorCheck("packing transaction", err)
pushTransaction(context.Background(), api, packedTx, eos.SHA256Bytes(hexChainID))
},
}
func readTransaction(argument string) ([]byte, error) {
fileInfo, err := os.Stat(argument)
if err == nil {
if fileInfo.IsDir() {
return nil, fmt.Errorf("argument %q is a directory", argument)
}
return ioutil.ReadFile(argument)
}
if looksLikeTransactionJSON(argument) {
return []byte(argument), nil
}
if os.IsNotExist(err) {
return nil, fmt.Errorf("file %q does not exist", argument)
}
return nil, fmt.Errorf("unable to check file %q: %s", argument, err)
}
var jsonFieldRegexp = regexp.MustCompile(`".+"\s*:\s*".*"`)
func looksLikeTransactionJSON(input string) bool {
return strings.Contains(input, "{") && jsonFieldRegexp.MatchString(input)
}
func init() {
txCmd.AddCommand(txPushCmd)
}