forked from eoscanada/eosc
/
txSign.go
50 lines (39 loc) · 1.44 KB
/
txSign.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
// Copyright © 2018 EOS Canada <info@eoscanada.com>
package cmd
import (
"context"
"fmt"
zsw "github.com/zhongshuwen/zswchain-go"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)
var txSignCmd = &cobra.Command{
Use: "sign [transaction.yaml|json]",
Short: "Sign a transaction produced by --write-transaction and submit it to the chain (unless --write-transaction is passed again).",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
var tx *zsw.Transaction
rawTx, err := transactionFromFileOrCLI(args[0], &tx)
errorCheck("reading transaction", err)
api := getAPI()
var chainID zsw.SHA256Bytes
if infileChainID := gjson.Get(string(rawTx), "chain_id").String(); infileChainID != "" {
chainID = toSHA256Bytes(infileChainID, fmt.Sprintf("chain_id field in source transaction"))
} else if cliChainID := viper.GetString("global-offline-chain-id"); cliChainID != "" {
chainID = toSHA256Bytes(cliChainID, "--offline-chain-id")
} else {
// getInfo
resp, err := api.GetInfo(ctx)
errorCheck("get info", err)
chainID = resp.ChainID
}
signedTx, packedTx := optionallySignTransaction(ctx, tx, chainID, api, false)
optionallyPushTransaction(ctx, signedTx, packedTx, chainID, api)
},
}
func init() {
txCmd.AddCommand(txSignCmd)
// txSignCmd.Flags().StringP("hash", "", "", "Hash of the proposition, as defined by the proposition itself")
}