/
bch.go
240 lines (216 loc) · 7.39 KB
/
bch.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package bch
import (
"bytes"
"encoding/json"
"fmt"
"math"
"decred.org/dcrdex/client/asset"
"decred.org/dcrdex/client/asset/btc"
"decred.org/dcrdex/dex"
dexbch "decred.org/dcrdex/dex/networks/bch"
dexbtc "decred.org/dcrdex/dex/networks/btc"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/gcash/bchd/bchec"
bchscript "github.com/gcash/bchd/txscript"
bchwire "github.com/gcash/bchd/wire"
)
const (
version = 0
// BipID is the Bip 44 coin ID for Bitcoin Cash.
BipID = 145
// The default fee is passed to the user as part of the asset.WalletInfo
// structure.
defaultFee = 100
minNetworkVersion = 221100
walletTypeRPC = "bitcoindRPC"
walletTypeLegacy = ""
)
var (
netPorts = dexbtc.NetPorts{
Mainnet: "8332",
Testnet: "28332",
Simnet: "18443",
}
fallbackFeeKey = "fallbackfee"
configOpts = []*asset.ConfigOption{
{
Key: "walletname",
DisplayName: "Wallet Name",
Description: "The wallet name",
},
{
Key: "rpcuser",
DisplayName: "JSON-RPC Username",
Description: "Bitcoin Cash 'rpcuser' setting",
},
{
Key: "rpcpassword",
DisplayName: "JSON-RPC Password",
Description: "Bitcoin Cash 'rpcpassword' setting",
NoEcho: true,
},
{
Key: "rpcbind",
DisplayName: "JSON-RPC Address",
Description: "<addr> or <addr>:<port> (default 'localhost')",
},
{
Key: "rpcport",
DisplayName: "JSON-RPC Port",
Description: "Port for RPC connections (if not set in Address)",
},
{
Key: fallbackFeeKey,
DisplayName: "Fallback fee rate",
Description: "Bitcoin Cash 'fallbackfee' rate. Units: BCH/kB",
DefaultValue: defaultFee * 1000 / 1e8,
},
{
Key: "txsplit",
DisplayName: "Pre-split funding inputs",
Description: "When placing an order, create a \"split\" transaction to fund the order without locking more of the wallet balance than " +
"necessary. Otherwise, excess funds may be reserved to fund the order until the first swap contract is broadcast " +
"during match settlement, or the order is canceled. This an extra transaction for which network mining fees are paid. " +
"Used only for standing-type orders, e.g. limit orders without immediate time-in-force.",
IsBoolean: true,
},
}
// WalletInfo defines some general information about a Bitcoin Cash wallet.
WalletInfo = &asset.WalletInfo{
Name: "Bitcoin Cash",
Version: version,
// Same as bitcoin. That's dumb.
UnitInfo: dexbch.UnitInfo,
AvailableWallets: []*asset.WalletDefinition{{
Type: walletTypeRPC,
Tab: "External",
Description: "Connect to bitcoind",
DefaultConfigPath: dexbtc.SystemConfigPath("bitcoin"), // Same as bitcoin. That's dumb.
ConfigOpts: configOpts,
}},
}
)
func init() {
asset.Register(BipID, &Driver{})
}
// Driver implements asset.Driver.
type Driver struct{}
// Check that Driver implements asset.Driver.
var _ asset.Driver = (*Driver)(nil)
// Open creates the BCH exchange wallet. Start the wallet with its Run method.
func (d *Driver) Open(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
return NewWallet(cfg, logger, network)
}
// DecodeCoinID creates a human-readable representation of a coin ID for
// Bitcoin Cash.
func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
// Bitcoin Cash and Bitcoin have the same tx hash and output format.
return (&btc.Driver{}).DecodeCoinID(coinID)
}
// Info returns basic information about the wallet and asset.
func (d *Driver) Info() *asset.WalletInfo {
return WalletInfo
}
// NewWallet is the exported constructor by which the DEX will import the
// exchange wallet.
func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
var params *chaincfg.Params
switch network {
case dex.Mainnet:
params = dexbch.MainNetParams
case dex.Testnet:
params = dexbch.TestNet4Params
case dex.Regtest:
params = dexbch.RegressionNetParams
default:
return nil, fmt.Errorf("unknown network ID %v", network)
}
// Designate the clone ports. These will be overwritten by any explicit
// settings in the configuration file. Bitcoin Cash uses the same default
// ports as Bitcoin.
cloneCFG := &btc.BTCCloneCFG{
WalletCFG: cfg,
MinNetworkVersion: minNetworkVersion,
WalletInfo: WalletInfo,
Symbol: "bch",
Logger: logger,
Network: network,
ChainParams: params,
Ports: netPorts,
DefaultFallbackFee: defaultFee,
Segwit: false,
LegacyBalance: true,
// Bitcoin Cash uses the Cash Address encoding, which is Bech32, but not
// indicative of segwit. We provide a custom encoder and decode to go
// to/from a btcutil.Address and a string.
AddressDecoder: dexbch.DecodeCashAddress,
AddressStringer: dexbch.EncodeCashAddress,
// Bitcoin Cash has a custom signature hash algorithm. Since they don't
// have segwit, Bitcoin Cash implemented a variation of the withdrawn
// BIP0062 that utilizes Shnorr signatures.
// https://gist.github.com/markblundeberg/a3aba3c9d610e59c3c49199f697bc38b#making-unmalleable-smart-contracts
// https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
NonSegwitSigner: rawTxInSigner,
OmitAddressType: true,
// Bitcoin Cash uses estimatefee instead of estimatesmartfee, and even
// then, they modified it from the old Bitcoin Core estimatefee by
// removing the confirmation target argument.
FeeEstimator: estimateFee,
}
return btc.BTCCloneWallet(cloneCFG)
}
// rawTxSigner signs the transaction using Bitcoin Cash's custom signature
// hash and signing algorithm.
func rawTxInSigner(btcTx *wire.MsgTx, idx int, subScript []byte, hashType txscript.SigHashType,
btcKey *btcec.PrivateKey, vals []int64, _ [][]byte) ([]byte, error) {
bchTx, err := translateTx(btcTx)
if err != nil {
return nil, fmt.Errorf("btc->bch wire.MsgTx translation error: %v", err)
}
bchKey, _ := bchec.PrivKeyFromBytes(bchec.S256(), btcKey.Serialize())
return bchscript.RawTxInECDSASignature(bchTx, idx, subScript, bchscript.SigHashType(uint32(hashType)), bchKey, vals[idx])
}
// serializeBtcTx serializes the wire.MsgTx.
func serializeBtcTx(msgTx *wire.MsgTx) ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, msgTx.SerializeSize()))
err := msgTx.Serialize(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// estimateFee uses Bitcoin Cash's estimatefee RPC, since estimatesmartfee
// is not implemented.
func estimateFee(node btc.RawRequester, confTarget uint64) (uint64, error) {
resp, err := node.RawRequest("estimatefee", nil)
if err != nil {
return 0, err
}
var feeRate float64
err = json.Unmarshal(resp, &feeRate)
if err != nil {
return 0, err
}
if feeRate <= 0 {
return 0, fmt.Errorf("fee could not be estimated")
}
return uint64(math.Round(feeRate * 1e5)), nil
}
// translateTx converts the btcd/*wire.MsgTx into a bchd/*wire.MsgTx.
func translateTx(btcTx *wire.MsgTx) (*bchwire.MsgTx, error) {
txB, err := serializeBtcTx(btcTx)
if err != nil {
return nil, err
}
bchTx := new(bchwire.MsgTx)
err = bchTx.Deserialize(bytes.NewBuffer(txB))
if err != nil {
return nil, err
}
return bchTx, nil
}