-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TLS support for Electrum. In default, the scheme is a tls connection, which allows connection to electrs. context timeout for wallet operations to enhance reliability.
- Loading branch information
1 parent
103c4f5
commit a2bef11
Showing
13 changed files
with
376 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package electrum | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
|
||
"github.com/checksum0/go-electrum/electrum" | ||
"github.com/elementsproject/peerswap/log" | ||
) | ||
|
||
type electrumClient struct { | ||
client *electrum.Client | ||
endpoint string | ||
isTLS bool | ||
} | ||
|
||
func NewElectrumClient(ctx context.Context, endpoint string, isTLS bool) (RPC, error) { | ||
ec, err := newClient(ctx, endpoint, isTLS) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := &electrumClient{ | ||
client: ec, | ||
endpoint: endpoint, | ||
isTLS: isTLS, | ||
} | ||
return client, nil | ||
} | ||
|
||
// reconnect reconnects to the electrum server if the connection is lost. | ||
func (c *electrumClient) reconnect(ctx context.Context) error { | ||
if err := c.client.Ping(ctx); err != nil { | ||
log.Infof("failed to ping electrum server: %v", err) | ||
log.Infof("reconnecting to electrum server") | ||
client, err := newClient(ctx, c.endpoint, c.isTLS) | ||
if err != nil { | ||
return err | ||
} | ||
c.client = client | ||
} | ||
return nil | ||
} | ||
|
||
func newClient(ctx context.Context, endpoint string, isTLS bool) (*electrum.Client, error) { | ||
if isTLS { | ||
return electrum.NewClientSSL(ctx, endpoint, &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
}) | ||
} | ||
return electrum.NewClientTCP(ctx, endpoint) | ||
} | ||
|
||
func (c *electrumClient) SubscribeHeaders(ctx context.Context) (<-chan *electrum.SubscribeHeadersResult, error) { | ||
if err := c.reconnect(ctx); err != nil { | ||
return nil, err | ||
} | ||
return c.client.SubscribeHeaders(ctx) | ||
} | ||
|
||
func (c *electrumClient) GetHistory(ctx context.Context, scripthash string) ([]*electrum.GetMempoolResult, error) { | ||
if err := c.reconnect(ctx); err != nil { | ||
return nil, err | ||
} | ||
return c.client.GetHistory(ctx, scripthash) | ||
} | ||
|
||
func (c *electrumClient) GetRawTransaction(ctx context.Context, txHash string) (string, error) { | ||
if err := c.reconnect(ctx); err != nil { | ||
return "", err | ||
} | ||
return c.client.GetRawTransaction(ctx, txHash) | ||
} | ||
|
||
func (c *electrumClient) BroadcastTransaction(ctx context.Context, rawTx string) (string, error) { | ||
if err := c.reconnect(ctx); err != nil { | ||
return "", err | ||
} | ||
return c.client.BroadcastTransaction(ctx, rawTx) | ||
} | ||
|
||
func (c *electrumClient) GetFee(ctx context.Context, target uint32) (float32, error) { | ||
if err := c.reconnect(ctx); err != nil { | ||
return 0, err | ||
} | ||
return c.client.GetFee(ctx, target) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.