Skip to content

Commit

Permalink
Refactor NewBtcClient
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhenchun committed Oct 28, 2021
1 parent 5ffda00 commit afd0d2d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
34 changes: 27 additions & 7 deletions btc/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,45 @@ import (
"errors"
"github.com/btcsuite/btcd/rpcclient"
"github.com/lizc2003/hdwallet/wallet"
"net/url"
)

type BtcClient struct {
RpcClient *rpcclient.Client
}

func NewBtcClient(host string, user string, pass string, chainId int) (*BtcClient, error) {
func NewBtcClient(URL string, user string, pass string, chainId int) (*BtcClient, error) {
chainCfg, err := wallet.GetBtcChainConfig(chainId)
if err != nil {
return nil, err
}

connCfg := &rpcclient.ConnConfig{
Host: host,
User: user,
Pass: pass,
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
DisableTLS: true, // Bitcoin core does not provide TLS by default
Params: chainCfg.Name,
User: user,
Pass: pass,
Params: chainCfg.Name,
}

u, err := url.Parse(URL)
if err != nil {
return nil, err
}
if u.Scheme == "http" || u.Scheme == "https" {
connCfg.HTTPPostMode = true
connCfg.Host = u.Host + u.Path
if u.Scheme == "http" {
connCfg.DisableTLS = true
}
} else if u.Scheme == "ws" || u.Scheme == "wss" {
connCfg.Host = u.Host
if u.Path != "" {
connCfg.Endpoint = u.Path[1:]
}
if u.Scheme == "ws" {
connCfg.DisableTLS = true
}
}

// Notice the notification parameter is nil since notifications are
// not supported in HTTP POST mode.
client, err := rpcclient.New(connCfg, nil)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ github.com/btcsuite/btcwallet/wallet/txrules v1.0.0/go.mod h1:UwQE78yCerZ313EXZw
github.com/btcsuite/btcwallet/wallet/txrules v1.1.0/go.mod h1:Zn9UTqpiTH+HOd5BLzSBzULzlOPmcoeyQIA0cp0WbQQ=
github.com/btcsuite/btcwallet/wallet/txsizes v1.0.0/go.mod h1:pauEU8UuMFiThe5PB3EO+gO5kx87Me5NvdQDsTuq6cs=
github.com/btcsuite/btcwallet/wallet/txsizes v1.1.0/go.mod h1:pauEU8UuMFiThe5PB3EO+gO5kx87Me5NvdQDsTuq6cs=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd h1:R/opQEbFEy9JGkIguV40SvRY1uliPX8ifOvi6ICsFCw=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I=
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 h1:R8vQdOQdZ9Y3SkEwmHoWBmX1DNXhXZqlTpq6s4tyJGc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34=
Expand Down

0 comments on commit afd0d2d

Please sign in to comment.