Skip to content

Commit 6acdbc4

Browse files
committed
fixed btc chain configuration
1 parent 3fc5b57 commit 6acdbc4

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

internal/bridge/chains/btc.go

+26-21
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ func (c Chain) Bitcoin() Bitcoin {
2828
}
2929

3030
chain := Bitcoin{Id: c.Id, Confirmations: c.Confirmations}
31+
if c.Network == NetworkMainnet {
32+
chain.Params = &chaincfg.MainNetParams
33+
} else if c.Network == NetworkTestnet {
34+
chain.Params = &chaincfg.TestNet3Params
35+
} else {
36+
panic("invalid network")
37+
}
3138

32-
if err := figure.Out(&chain.Receivers).FromInterface(c.BridgeAddresses).With(bitcoinHooks).Please(); err != nil {
39+
if err := figure.Out(&chain.Receivers).FromInterface(c.BridgeAddresses).With(btcClientHook).Please(); err != nil {
3340
panic(errors.Wrap(err, "failed to decode bitcoin receivers"))
3441
}
35-
if err := figure.Out(&chain.Rpc).FromInterface(c.Rpc).With(bitcoinHooks).Please(); err != nil {
42+
if err := figure.Out(&chain.Rpc).FromInterface(c.Rpc).With(bitcoinAddrHook(chain.Params)).Please(); err != nil {
3643
panic(errors.Wrap(err, "failed to init bitcoin chain rpc"))
3744
}
3845

@@ -42,30 +49,28 @@ func (c Chain) Bitcoin() Bitcoin {
4249
panic(errors.Wrap(err, "failed to get wallet info"))
4350
}
4451

45-
if c.Network == NetworkMainnet {
46-
chain.Params = &chaincfg.MainNetParams
47-
}
48-
if c.Network == NetworkTestnet {
49-
chain.Params = &chaincfg.TestNet3Params
50-
}
51-
5252
return chain
5353
}
5454

55-
var bitcoinHooks = figure.Hooks{
56-
"btcutil.Address": func(value interface{}) (reflect.Value, error) {
57-
switch v := value.(type) {
58-
case string:
59-
addr, err := btcutil.DecodeAddress(v, &chaincfg.MainNetParams)
60-
if err != nil {
61-
return reflect.Value{}, errors.Wrap(err, "failed to decode address")
55+
func bitcoinAddrHook(params *chaincfg.Params) figure.Hooks {
56+
return figure.Hooks{
57+
"btcutil.Address": func(value interface{}) (reflect.Value, error) {
58+
switch v := value.(type) {
59+
case string:
60+
addr, err := btcutil.DecodeAddress(v, params)
61+
if err != nil {
62+
return reflect.Value{}, errors.Wrap(err, "failed to decode address")
63+
}
64+
65+
return reflect.ValueOf(addr), nil
66+
default:
67+
return reflect.Value{}, errors.Errorf("unsupported conversion from %T", value)
6268
}
69+
},
70+
}
71+
}
6372

64-
return reflect.ValueOf(addr), nil
65-
default:
66-
return reflect.Value{}, errors.Errorf("unsupported conversion from %T", value)
67-
}
68-
},
73+
var btcClientHook = figure.Hooks{
6974
"*rpcclient.Client": func(value interface{}) (reflect.Value, error) {
7075
switch v := value.(type) {
7176
case map[string]interface{}:

0 commit comments

Comments
 (0)