/
txUnpack.go
65 lines (51 loc) · 1.58 KB
/
txUnpack.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
// Copyright © 2018 EOS Canada <info@eoscanada.com>
package cmd
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
eos "github.com/eoscanada/eos-go"
"github.com/spf13/cobra"
)
var txUnpackCmd = &cobra.Command{
Use: "unpack [transaction.yaml|json]",
Short: "Unpack a transaction produced by --write-transaction and display all its actions (for review). This does not submit anything to the chain.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filename := args[0]
cnt, err := ioutil.ReadFile(filename)
errorCheck("reading transaction file", err)
var tx *eos.SignedTransaction
errorCheck("json unmarshal transaction", json.Unmarshal(cnt, &tx))
api := getAPI()
for _, act := range tx.ContextFreeActions {
errorCheck("context free action unpack", txUnpackAction(api, act))
}
for _, act := range tx.Actions {
errorCheck("action unpack", txUnpackAction(api, act))
}
cnt, err = json.MarshalIndent(tx, "", " ")
errorCheck("marshalling signed transaction", err)
fmt.Println(string(cnt))
},
}
func txUnpackAction(api *eos.API, act *eos.Action) error {
hexBytes, ok := act.Data.(string)
if !ok {
return fmt.Errorf("action data expected to be hex bytes as string, was %T", act.Data)
}
bytes, err := hex.DecodeString(hexBytes)
if err != nil {
return fmt.Errorf("invalid hex bytes stream: %s", err)
}
data, err := api.ABIBinToJSON(act.Account, eos.Name(act.Name), bytes)
if err != nil {
return fmt.Errorf("chain abi_bin_to_json: %s", err)
}
act.Data = data
return nil
}
func init() {
txCmd.AddCommand(txUnpackCmd)
}