This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
197 lines (177 loc) · 4.89 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
package main
import (
"bytes"
"encoding/hex"
"fmt"
"os"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
log "github.com/inconshreveable/log15"
"github.com/levigross/grequests"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/icook/ngpool/pkg/common"
"github.com/icook/ngpool/pkg/service"
)
func sign(config *service.ChainConfig, urlbase string,
addresses map[string]*btcec.PrivateKey) error {
resp, err := grequests.Get(urlbase+"/v1/createpayout/"+config.Code, nil)
if err != nil {
return err
}
type Payout struct {
Errors []interface{}
Data struct {
TX string
PayoutMeta common.PayoutMeta `json:"payout_meta"`
}
}
var vals Payout
err = resp.JSON(&vals)
if err != nil {
return err
}
if len(vals.Errors) > 0 {
log.Error("Error from server", "errors", vals.Errors)
return errors.New("Error from remote")
}
if vals.Data.TX == "" {
log.Info("No transaction to process at this time", "curr", config.Code)
return nil
}
var payout = vals.Data
log.Info("Got pending payout",
"outputs", len(payout.PayoutMeta.PayoutMaps),
"tx_size_wo_sigs", len(payout.TX))
redeemTx, err := common.HexStringToTX(payout.TX)
if err != nil {
return err
}
lookupKey := func(a btcutil.Address) (*btcec.PrivateKey, bool, error) {
addr, ok := addresses[a.EncodeAddress()]
return addr, ok, nil
}
for i, input := range redeemTx.TxIn {
var inputAddress string
cmp := input.PreviousOutPoint.Hash.String()
for _, inputUTXO := range payout.PayoutMeta.Inputs {
if inputUTXO.Hash == cmp && input.PreviousOutPoint.Index == inputUTXO.Vout {
inputAddress = inputUTXO.Address
break
}
}
if inputAddress == "" {
log.Error("Failed linking input to UTXO", "i", i, "prevout", input.PreviousOutPoint)
return errors.New("Failed to find UTXO linking to input")
}
addr, err := btcutil.DecodeAddress(inputAddress, config.Params)
if err != nil {
return errors.New("Invalid address in UTXO from server")
}
pkScript, err := txscript.PayToAddrScript(addr)
if err != nil {
return err
}
sigScript, err := txscript.SignTxOutput(config.Params,
redeemTx, i, pkScript, txscript.SigHashAll,
txscript.KeyClosure(lookupKey), nil, nil)
redeemTx.TxIn[i].SignatureScript = sigScript
}
// Push back to server
out := bytes.Buffer{}
redeemTx.SerializeNoWitness(&out)
log.Info("Signed tx", "tx_size", out.Len())
resp, err = grequests.Post(urlbase+"/v1/payout",
&grequests.RequestOptions{
JSON: map[string]interface{}{
"currency": config.Code,
"payout_meta": payout.PayoutMeta,
"tx": hex.EncodeToString(out.Bytes()),
},
})
if err != nil {
return err
}
if resp.StatusCode != 200 {
log.Error("Payout submission failed", "ret", resp.String())
return errors.New("Got non-200 from submission")
}
return nil
}
// Loads currency config from the remote
func loadCommon(urlbase string) {
resp, err := grequests.Get(urlbase+"/v1/common", nil)
if err != nil {
log.Crit("Failed to get common config", "urlbase", urlbase)
os.Exit(1)
}
type Resp struct {
Data struct {
Currencies map[string]interface{} `json:"raw_currencies"`
}
}
var vals Resp
resp.JSON(&vals)
service.SetupCurrencies(vals.Data.Currencies)
}
func loadAddresses(addrs []string, params *chaincfg.Params) (map[string]*btcec.PrivateKey, error) {
var ret = map[string]*btcec.PrivateKey{}
for idx, privkey := range addrs {
wif, err := btcutil.DecodeWIF(privkey)
if err != nil {
return nil, errors.Wrapf(err, "invalid idx %v", idx)
}
addr, err := btcutil.NewAddressPubKey(wif.PrivKey.PubKey().SerializeCompressed(), params)
if err != nil {
return nil, errors.Wrapf(err, "invalid idx %v", idx)
}
ret[addr.EncodeAddress()] = wif.PrivKey
}
return ret, nil
}
var RootCmd = &cobra.Command{
Use: "ngsign [urlbase] [keyfile]",
Short: "Sign raw transactions",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
urlbase := args[0]
keyfile := args[1]
config := viper.New()
config.AddConfigPath(".")
config.AddConfigPath("../../")
config.SetConfigType("yaml")
config.SetConfigName(keyfile)
err := config.ReadInConfig()
if err != nil {
log.Crit("error parsing configuration file", "err", err)
os.Exit(1)
}
loadCommon(urlbase)
for _, curr := range service.CurrencyConfig {
logger := log.New("currency", curr.Code)
subcfg := config.Sub(curr.Code)
if subcfg == nil {
logger.Info("Skipping unconfigured")
continue
}
addresses, err := loadAddresses(subcfg.GetStringSlice("keys"), curr.Params)
if err != nil {
logger.Crit("Invalid address", "err", err)
os.Exit(1)
}
err = sign(curr, args[0], addresses)
if err != nil {
logger.Crit("Failed signing", "err", err)
}
}
},
}
func main() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}