mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 20:24:21 +01:00
* scaffolding wallet * remove wallet db, add loader instead * wip * implement some wallet methods * signing and utxos * renaming * fee estimator * chain source options * config * application service * clark docker-compose * CLI refactor * v0 clark * v0.1 clark * fix SignTapscriptInput (btcwallet) * wallet.Broadcast, send via explora * fix ASP pubkey * Use lnd's btcwallet & Add rpc to get wallet staus * wip * unilateral exit * Fixes on watching for notifications and cli init * handle non-final BIP68 errors * Fixes * Fixes * Fix * a * fix onboard cosigners + revert tree validation * fix covenant e2e tests * fix covenantless e2e tests * fix container naming * fix lint error * update REAME.md * Add env var for wallet password --------- Co-authored-by: altafan <18440657+altafan@users.noreply.github.com>
117 lines
2.3 KiB
Go
117 lines
2.3 KiB
Go
package btcwallet
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/ark-network/ark/internal/core/ports"
|
|
"github.com/btcsuite/btcd/btcutil"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type esploraClient struct {
|
|
url string
|
|
}
|
|
|
|
type esploraTx struct {
|
|
Status struct {
|
|
Confirmed bool `json:"confirmed"`
|
|
BlockTime int64 `json:"block_time"`
|
|
} `json:"status"`
|
|
}
|
|
|
|
func (f *esploraClient) broadcast(txhex string) error {
|
|
endpoint, err := url.JoinPath(f.url, "tx")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := http.Post(endpoint, "text/plain", strings.NewReader(txhex))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
content, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if strings.Contains(strings.ToLower(string(content)), "non-BIP68-final") {
|
|
return ports.ErrNonFinalBIP68
|
|
}
|
|
|
|
return fmt.Errorf("failed to broadcast transaction: %s (%s, %s)", txhex, resp.Status, content)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *esploraClient) getTxStatus(txid string) (isConfirmed bool, blocktime int64, err error) {
|
|
endpoint, err := url.JoinPath(f.url, "tx", txid)
|
|
if err != nil {
|
|
return false, 0, err
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Get(endpoint)
|
|
if err != nil {
|
|
return false, 0, err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return false, 0, err
|
|
}
|
|
|
|
var response esploraTx
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
|
return false, 0, err
|
|
}
|
|
|
|
return response.Status.Confirmed, response.Status.BlockTime, nil
|
|
}
|
|
|
|
func (f *esploraClient) getFeeRate() (btcutil.Amount, error) {
|
|
endpoint, err := url.JoinPath(f.url, "fee-estimates")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Get(endpoint)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return 0, errors.New("fee-estimates endpoint HTTP error: " + resp.Status)
|
|
}
|
|
|
|
response := make(map[string]float64)
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if len(response) == 0 {
|
|
log.Warn("empty response from esplora fee-estimates endpoint, default to 2 sat/vbyte")
|
|
return 2.0, nil
|
|
}
|
|
|
|
feeRate, ok := response["1"]
|
|
if !ok {
|
|
return 0, errors.New("failed to get fee rate for 1 block")
|
|
}
|
|
|
|
return btcutil.Amount(feeRate * 1000), nil
|
|
}
|