mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 20:54:20 +01:00
* [domain] add reverse boarding inputs in Payment struct * [tx-builder] support reverse boarding script * [wallet] add GetTransaction * [api-spec][application] add reverse boarding support in covenantless * [config] add reverse boarding config * [api-spec] add ReverseBoardingAddress RPC * [domain][application] support empty forfeits txs in EndFinalization events * [tx-builder] optional connector output in round tx * [btc-embedded] fix getTx and taproot finalizer * whitelist ReverseBoardingAddress RPC * [test] add reverse boarding integration test * [client] support reverse boarding * [sdk] support reverse boarding * [e2e] add sleep time after faucet * [test] run using bitcoin-core RPC * [tx-builder] fix GetSweepInput * [application][tx-builder] support reverse onboarding in covenant * [cli] support reverse onboarding in covenant CLI * [test] rework integration tests * [sdk] remove onchain wallet, replace by onboarding address * remove old onboarding protocols * [sdk] Fix RegisterPayment * [e2e] add more funds to covenant ASP * [e2e] add sleeping time * several fixes * descriptor boarding * remove boarding delay from info * [sdk] implement descriptor boarding * go mod tidy * fixes and revert error msgs * move descriptor pkg to common * add replace in go.mod * [sdk] fix unit tests * rename DescriptorInput --> BoardingInput * genrest in SDK * remove boarding input from domain * remove all "reverse boarding" * rename "onboarding" ==> "boarding" * remove outdate payment unit test * use tmpfs docker volument for compose testing files * several fixes
143 lines
2.9 KiB
Go
143 lines
2.9 KiB
Go
package btcwallet
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/ark-network/ark/server/internal/core/ports"
|
|
"github.com/btcsuite/btcd/btcutil"
|
|
"github.com/btcsuite/btcd/wire"
|
|
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) getTx(txid string) (*wire.MsgTx, error) {
|
|
endpoint, err := url.JoinPath(f.url, "tx", txid, "raw")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Get(endpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, errors.New("tx endpoint HTTP error: " + resp.Status)
|
|
}
|
|
|
|
var tx wire.MsgTx
|
|
|
|
if err := tx.Deserialize(resp.Body); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &tx, 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
|
|
}
|