-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (174 loc) · 4.78 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"encoding/hex"
"flag"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/pkg/errors"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
"github.com/stellar/go/txnbuild"
)
var (
inputFile string
outputFile string
preview bool
submit bool
walletsecret string
)
func init() {
flag.StringVar(&inputFile, "inputfile", "payouts_to_sign.txt", "File with transactions to sign")
flag.StringVar(&outputFile, "outputfile", "payouts_signed.txt", "File to place signed transactions")
flag.BoolVar(&preview, "preview", false, "Print transactions before signing")
flag.BoolVar(&submit, "submit", false, "Submit transactions to the network after signing")
flag.StringVar(&walletsecret, "wallet-secret", "", "Secret key of the wallet to sign the transactions with")
}
func main() {
flag.Parse()
if walletsecret == "" {
panic("wallet secret is required")
}
wallet, err := newWallet(walletsecret)
if err != nil {
panic("wallet secret is not valid")
}
horizon := horizonclient.DefaultPublicNetClient
infile, err := os.Open(inputFile)
if err != nil {
panic(fmt.Sprintf("Failed to open payouts input file %s", err))
}
reader := bufio.NewReader(infile)
defer infile.Close()
outFile, err := os.Create(outputFile)
if err != nil {
panic(fmt.Sprintf("Failed to open payouts output file %s", err))
}
defer outFile.Close()
defer outFile.Sync()
for {
line, err := reader.ReadString('\n')
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
if line == "" {
break
}
xdr := strings.TrimSpace(line)
txn := &txnbuild.Transaction{}
if err = txn.UnmarshalText([]byte(xdr)); err != nil {
panic("Cant unmarshal transaction")
}
if err = basicValidation(txn); err != nil {
panic("transaction validation failed")
}
if preview {
previewTxn(txn)
continue
}
txn, err = wallet.Sign(txn)
if err != nil {
panic("could not sign transaction")
}
if submit {
for {
previewTxn(txn)
_, err := horizon.SubmitTransaction(txn)
if err != nil {
if err, ok := err.(*horizonclient.Error); ok {
// Horizon timeout
if err.Response.StatusCode == 504 {
fmt.Println("Horizon timeout, retry request in 15 seconds")
time.Sleep(time.Second * 15)
continue
} else if err.Response.StatusCode == 404 {
fmt.Println("ERROR: Account does not exist", err)
break
}
if resCodes, ok := err.Problem.Extras["result_codes"]; ok {
if resMap, ok := resCodes.(map[string]string); ok {
if v, exists := resMap["operations"]; exists {
if strings.Contains(v, "tx_insufficient_fee") {
fmt.Println("Tx insufficient fee, retry in 30 seconds")
time.Sleep(time.Second * 30)
continue
} else if strings.Contains(v, "op_no_destination") {
fmt.Println("ERROR: Account does not exsit")
break
}
}
}
}
fmt.Println(err)
time.Sleep(time.Second * 60)
continue
}
fmt.Println("ERROR", err)
time.Sleep(time.Second * 60)
continue
}
break
}
} else {
xdr, err := txn.MarshalText()
if err != nil {
panic(err)
}
outFile.WriteString(string(xdr))
outFile.WriteString("\n")
}
}
}
func basicValidation(txn *txnbuild.Transaction) error {
if _, ok := txn.Memo().(txnbuild.MemoHash); !ok {
if _, ok = txn.Memo().(txnbuild.MemoReturn); !ok {
return errors.New("memo must be of type memo_hash or memo_return")
}
}
if len(txn.Operations()) != 1 {
return errors.New("transaction must contain a single operation")
}
if _, ok := txn.Operations()[0].(*txnbuild.Payment); !ok {
return errors.New("transaction operation must be of type Payment")
}
return nil
}
func previewTxn(txn *txnbuild.Transaction) {
var rawMemo [32]byte
var ok bool
rawMemo, ok = txn.Memo().(txnbuild.MemoHash)
if !ok {
rawMemo, ok = txn.Memo().(txnbuild.MemoReturn)
}
if !ok {
panic("memo is not of type MemoHash or MemoReturn")
}
memo := hex.EncodeToString(rawMemo[:])
sequence := txn.SequenceNumber()
paymentOp := txn.Operations()[0].(*txnbuild.Payment)
asset := paymentOp.Asset
amount := paymentOp.Amount
dest := paymentOp.Destination
sigs := len(txn.Signatures())
fmt.Printf("Sending %s %s to %s with memo %s (%d signatures, %d seqno)\n", amount, asset, dest, memo, sigs, sequence)
}
type Wallet struct {
keypair *keypair.Full
}
func newWallet(privateKey string) (*Wallet, error) {
kp, err := keypair.ParseFull(privateKey)
if err != nil {
return nil, errors.Wrap(err, "could not parse private key")
}
return &Wallet{keypair: kp}, nil
}
func (w *Wallet) Sign(txn *txnbuild.Transaction) (*txnbuild.Transaction, error) {
return txn.Sign(network.PublicNetworkPassphrase, w.keypair)
}