-
Notifications
You must be signed in to change notification settings - Fork 1
/
signer.go
130 lines (111 loc) · 3.32 KB
/
signer.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
package crypto
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/tendermint/tendermint/crypto"
"sync"
)
//Signer is the signer for posting redeem tx
type Signer struct {
AccAddress sdk.AccAddress
PrivKey crypto.PrivKey
seqLock *sync.Mutex
}
//NewSigner is the constructor for signer
func NewSigner(accAddress sdk.AccAddress, privKey crypto.PrivKey) *Signer {
return &Signer{
AccAddress: accAddress,
PrivKey: privKey,
seqLock: &sync.Mutex{},
}
}
// SendTx calls the test handler with a message.
func (s *Signer) SendTx(cliContext *context.CLIContext, cdc *codec.Codec, msg sdk.Msg) error {
s.seqLock.Lock()
txBldr, err := s.senderTxContext(cliContext, cdc)
if err != nil {
//res = parseResponse(err.Error())
//fmt.Println(err)
return err
}
err = s.completeAndBroadcastTxCLI(*cliContext, txBldr, []sdk.Msg{msg})
s.seqLock.Unlock()
if err != nil {
//res = parseResponse(err.Error())
//fmt.Println(err)
return err
}
return nil
}
// SenderTxContext creates a new TxBuilder
func (s *Signer) senderTxContext(cliContext *context.CLIContext, cdc *codec.Codec) (auth.TxBuilder, error) {
cliContext.FromAddress = s.AccAddress
//cliContext.BroadcastMode = client.BroadcastSync//todo switch back to this setting
cliContext.BroadcastMode = client.BroadcastBlock
txEncoder := utils.GetTxEncoder(cdc)
txBuilder := auth.NewTxBuilderFromCLI().WithTxEncoder(txEncoder)
num, seq, err := auth.NewAccountRetriever(cliContext).GetAccountNumberSequence(s.AccAddress)
if err != nil {
return txBuilder, err
}
return txBuilder.WithAccountNumber(num).WithSequence(seq), nil
}
// nolint[hugeParam]
// Function signature taken from the Cosmos SDK.
// CompleteAndBroadcastTxCLI broadcasts a Tx.
func (s *Signer) completeAndBroadcastTxCLI(cliContext context.CLIContext, txBldr auth.TxBuilder,
msgs []sdk.Msg) error {
txBldr, err := utils.PrepareTxBuilder(txBldr, cliContext)
if err != nil {
return err
}
// build and sign the transaction
txBytes, err := s.buildAndSign(cliContext.Codec, txBldr, msgs)
if err != nil {
return err
}
// broadcast to a Tendermint node
res, err := cliContext.BroadcastTxCommit(txBytes)
if err != nil {
return err
}
if sdk.CodeType(res.Code) != sdk.CodeOK {
return fmt.Errorf(res.RawLog)
}
err = cliContext.PrintOutput(res)
return err
}
//nolint[hugeParam]
// Function signature taken from the Cosmos SDK.
func (s *Signer) buildAndSign(cdc *codec.Codec, bldr auth.TxBuilder, msgs []sdk.Msg) ([]byte, error) {
msg, err := bldr.BuildSignMsg(msgs)
if err != nil {
return nil, err
}
b, _ := json.Marshal(msg)
fmt.Println(string(b))
sig, err := makeSignature(s.PrivKey, msg)
if err != nil {
return nil, err
}
return bldr.TxEncoder()(auth.NewStdTx(msg.Msgs, msg.Fee, []auth.StdSignature{sig}, msg.Memo))
}
//nolint[hugeParam]
// Function signature taken from the Cosmos SDK.
func makeSignature(priv crypto.PrivKey, msg auth.StdSignMsg) (sig auth.StdSignature, err error) {
sigBytes, err := priv.Sign(msg.Bytes())
if err != nil {
return
}
pubkey := priv.PubKey()
return auth.StdSignature{
PubKey: pubkey,
Signature: sigBytes,
}, nil
}