mirror of https://github.com/lightninglabs/loop
add all in one commit for the moment
parent
1dba70eb24
commit
a6c834d929
@ -0,0 +1,44 @@
|
|||||||
|
package withdraw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
"github.com/lightninglabs/loop/fsm"
|
||||||
|
"github.com/lightninglabs/loop/staticaddr/address"
|
||||||
|
"github.com/lightninglabs/loop/staticaddr/deposit"
|
||||||
|
"github.com/lightninglabs/loop/staticaddr/script"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
IdLength = 32
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddressManager handles fetching of address parameters.
|
||||||
|
type AddressManager interface {
|
||||||
|
// GetStaticAddressParameters returns the static address parameters.
|
||||||
|
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
|
||||||
|
error)
|
||||||
|
|
||||||
|
// GetStaticAddress returns the deposit address for the given
|
||||||
|
// client and server public keys.
|
||||||
|
GetStaticAddress(ctx context.Context) (*script.StaticAddress, error)
|
||||||
|
|
||||||
|
// ListUnspent returns a list of utxos at the static address.
|
||||||
|
ListUnspent(ctx context.Context, minConfs,
|
||||||
|
maxConfs int32) ([]*lnwallet.Utxo, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type DepositManager interface {
|
||||||
|
GetActiveDepositsInState(stateFilter fsm.StateType) ([]*deposit.Deposit,
|
||||||
|
error)
|
||||||
|
|
||||||
|
AllOutpointsActiveDeposits(outpoints []wire.OutPoint,
|
||||||
|
stateFilter fsm.StateType) ([]*deposit.Deposit, bool)
|
||||||
|
|
||||||
|
TransitionDeposits(deposits []*deposit.Deposit, event fsm.EventType,
|
||||||
|
expectedFinalState fsm.StateType) error
|
||||||
|
|
||||||
|
UpdateDeposit(d *deposit.Deposit) error
|
||||||
|
}
|
@ -0,0 +1,727 @@
|
|||||||
|
package withdraw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
|
||||||
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
|
"github.com/btcsuite/btcd/txscript"
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
"github.com/btcsuite/btclog"
|
||||||
|
"github.com/lightninglabs/lndclient"
|
||||||
|
"github.com/lightninglabs/loop/staticaddr"
|
||||||
|
"github.com/lightninglabs/loop/staticaddr/deposit"
|
||||||
|
staticaddressrpc "github.com/lightninglabs/loop/swapserverrpc"
|
||||||
|
"github.com/lightningnetwork/lnd/input"
|
||||||
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrWithdrawingInactiveDeposits is returned when the user tries to
|
||||||
|
// withdraw inactive deposits.
|
||||||
|
ErrWithdrawingInactiveDeposits = errors.New("deposits to be " +
|
||||||
|
"withdrawn are unknown or inactive")
|
||||||
|
|
||||||
|
// MinConfs is the minimum number of confirmations we require for a
|
||||||
|
// deposit to be considered withdrawn.
|
||||||
|
MinConfs int32 = 3
|
||||||
|
|
||||||
|
// Is the default confirmation target for the fee estimation of the
|
||||||
|
// withdrawal transaction.
|
||||||
|
defaultConfTarget int32 = 3
|
||||||
|
|
||||||
|
log btclog.Logger
|
||||||
|
)
|
||||||
|
|
||||||
|
// ManagerConfig holds the configuration for the address manager.
|
||||||
|
type ManagerConfig struct {
|
||||||
|
WithdrawalServerClient staticaddressrpc.WithdrawalServerClient
|
||||||
|
|
||||||
|
AddressManager AddressManager
|
||||||
|
|
||||||
|
DepositManager DepositManager
|
||||||
|
|
||||||
|
// WalletKit is the wallet client that is used to derive new keys from
|
||||||
|
// lnd's wallet.
|
||||||
|
WalletKit lndclient.WalletKitClient
|
||||||
|
|
||||||
|
// ChainParams is the chain configuration(mainnet, testnet...) this
|
||||||
|
// manager uses.
|
||||||
|
ChainParams *chaincfg.Params
|
||||||
|
|
||||||
|
// ChainNotifier is the chain notifier that is used to listen for new
|
||||||
|
// blocks.
|
||||||
|
ChainNotifier lndclient.ChainNotifierClient
|
||||||
|
|
||||||
|
// Signer is the signer client that is used to sign transactions.
|
||||||
|
Signer lndclient.SignerClient
|
||||||
|
}
|
||||||
|
|
||||||
|
// Manager manages the address state machines.
|
||||||
|
type Manager struct {
|
||||||
|
cfg *ManagerConfig
|
||||||
|
|
||||||
|
runCtx context.Context
|
||||||
|
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
|
// initChan signals the daemon that the address manager has completed
|
||||||
|
// its initialization.
|
||||||
|
initChan chan struct{}
|
||||||
|
|
||||||
|
// initiationHeight stores the currently best known block height.
|
||||||
|
initiationHeight uint32
|
||||||
|
|
||||||
|
// currentHeight stores the currently best known block height.
|
||||||
|
currentHeight uint32
|
||||||
|
|
||||||
|
activeWithdrawals map[string][]*deposit.Deposit
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewManager creates a new deposit withdrawal manager.
|
||||||
|
func NewManager(cfg *ManagerConfig) *Manager {
|
||||||
|
log = staticaddr.GetLogger()
|
||||||
|
return &Manager{
|
||||||
|
cfg: cfg,
|
||||||
|
initChan: make(chan struct{}),
|
||||||
|
activeWithdrawals: make(map[string][]*deposit.Deposit),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run runs the deposit withdrawal manager.
|
||||||
|
func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
|
||||||
|
m.runCtx = ctx
|
||||||
|
|
||||||
|
m.Lock()
|
||||||
|
m.currentHeight, m.initiationHeight = currentHeight, currentHeight
|
||||||
|
m.Unlock()
|
||||||
|
|
||||||
|
newBlockChan, newBlockErrChan, err := m.cfg.ChainNotifier.RegisterBlockEpochNtfn(m.runCtx) //nolint:lll
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.recover()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Communicate to the caller that the address manager has completed its
|
||||||
|
// initialization.
|
||||||
|
close(m.initChan)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case height := <-newBlockChan:
|
||||||
|
m.Lock()
|
||||||
|
m.currentHeight = uint32(height)
|
||||||
|
m.Unlock()
|
||||||
|
|
||||||
|
err = m.republishWithdrawals()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error republishing withdrawals: %v",
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
case err = <-newBlockErrChan:
|
||||||
|
return err
|
||||||
|
|
||||||
|
case <-m.runCtx.Done():
|
||||||
|
return m.runCtx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) recover() error {
|
||||||
|
// To recover withdrawals we skim through all active deposits and check
|
||||||
|
// if they have a withdrawal address set. For the ones that do we
|
||||||
|
// cluster those with equal withdrawal addresses and kick-off
|
||||||
|
// their withdrawal. Each cluster represents a separate withdrawal
|
||||||
|
// intend by the user.
|
||||||
|
activeDeposits, err := m.cfg.DepositManager.GetActiveDepositsInState(
|
||||||
|
deposit.Deposited,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group the deposits by their withdrawal address.
|
||||||
|
depositsByWithdrawalAddress := make(map[string][]*deposit.Deposit)
|
||||||
|
for _, d := range activeDeposits {
|
||||||
|
sweepAddress := d.WithdrawalSweepAddress
|
||||||
|
if sweepAddress == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
depositsByWithdrawalAddress[sweepAddress] = append(
|
||||||
|
depositsByWithdrawalAddress[sweepAddress], d,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We can now reinstate each cluster of deposits for a withdrawal.
|
||||||
|
for address, deposits := range depositsByWithdrawalAddress {
|
||||||
|
withdrawalAddress, err := btcutil.DecodeAddress(
|
||||||
|
address, m.cfg.ChainParams,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.cfg.DepositManager.TransitionDeposits(
|
||||||
|
deposits, deposit.OnWithdraw, deposit.Withdrawing,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.publishWithdrawal(m.runCtx, deposits, withdrawalAddress)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.handleWithdrawal(deposits, withdrawalAddress)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Lock()
|
||||||
|
m.activeWithdrawals[address] = deposits
|
||||||
|
m.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WaitInitComplete waits until the address manager has completed its setup.
|
||||||
|
func (m *Manager) WaitInitComplete() {
|
||||||
|
defer log.Debugf("Static address withdrawal manager initiation " +
|
||||||
|
"complete.")
|
||||||
|
|
||||||
|
<-m.initChan
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithdrawDeposits starts a deposits withdrawal flow.
|
||||||
|
func (m *Manager) WithdrawDeposits(ctx context.Context,
|
||||||
|
outpoints []wire.OutPoint) error {
|
||||||
|
|
||||||
|
if len(outpoints) == 0 {
|
||||||
|
return fmt.Errorf("no outpoints selected to withdraw")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the deposits are in a state in which they can be
|
||||||
|
// withdrawn.
|
||||||
|
deposits, allActive := m.cfg.DepositManager.AllOutpointsActiveDeposits(
|
||||||
|
outpoints, deposit.Deposited)
|
||||||
|
|
||||||
|
if !allActive {
|
||||||
|
return ErrWithdrawingInactiveDeposits
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate the withdrawal address from our local lnd wallet.
|
||||||
|
withdrawalAddress, err := m.cfg.WalletKit.NextAddr(
|
||||||
|
ctx, lnwallet.DefaultAccountName,
|
||||||
|
walletrpc.AddressType_TAPROOT_PUBKEY, false,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attach the withdrawal address to the deposits. After a client restart
|
||||||
|
// we can use this address as an indicator to continue the withdrawal.
|
||||||
|
// If there are multiple deposits with the same withdrawal address, we
|
||||||
|
// bundle them together in the same withdrawal transaction.
|
||||||
|
for _, d := range deposits {
|
||||||
|
d.Lock()
|
||||||
|
d.WithdrawalSweepAddress = withdrawalAddress.String()
|
||||||
|
d.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transition the deposits to the withdrawing state. This updates each
|
||||||
|
// deposits withdrawal address. If a transition fails, we'll return an
|
||||||
|
// error and abort the withdrawal. An error in transition is likely due
|
||||||
|
// to an error in the state machine. The already transitioned deposits
|
||||||
|
// should be reset to the Deposit state after a restart.
|
||||||
|
err = m.cfg.DepositManager.TransitionDeposits(
|
||||||
|
deposits, deposit.OnWithdraw, deposit.Withdrawing,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.publishWithdrawal(ctx, deposits, withdrawalAddress)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.handleWithdrawal(deposits, withdrawalAddress)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Lock()
|
||||||
|
m.activeWithdrawals[withdrawalAddress.String()] = deposits
|
||||||
|
m.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) publishWithdrawal(ctx context.Context,
|
||||||
|
deposits []*deposit.Deposit, withdrawalAddress btcutil.Address) error {
|
||||||
|
|
||||||
|
// Create a musig2 session for each deposit.
|
||||||
|
withdrawalSessions, clientNonces, err := m.createMusig2Sessions(
|
||||||
|
ctx, deposits,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the fee rate for the withdrawal sweep.
|
||||||
|
withdrawalSweepFeeRate, err := m.cfg.WalletKit.EstimateFeeRate(
|
||||||
|
m.runCtx, defaultConfTarget,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
outpoints := toOutpoints(deposits)
|
||||||
|
resp, err := m.cfg.WithdrawalServerClient.WithdrawDeposits(
|
||||||
|
m.runCtx,
|
||||||
|
&staticaddressrpc.ServerWithdrawRequest{
|
||||||
|
Outpoints: toServerOutpoints(outpoints),
|
||||||
|
ClientNonces: clientNonces,
|
||||||
|
ClientSweepAddr: withdrawalAddress.String(),
|
||||||
|
MusigTxFeeRate: uint64(withdrawalSweepFeeRate),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
|
||||||
|
ctx,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("couldn't get confirmation height for "+
|
||||||
|
"deposit, %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
prevOuts := m.toPrevOuts(deposits, addressParams.PkScript)
|
||||||
|
totalValue := withdrawalValue(prevOuts)
|
||||||
|
withdrawalTx, err := m.createWithdrawalTx(
|
||||||
|
outpoints, totalValue, withdrawalAddress,
|
||||||
|
withdrawalSweepFeeRate,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
coopServerNonces, err := toNonces(resp.ServerNonces)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next we'll get our sweep tx signatures.
|
||||||
|
prevOutFetcher := txscript.NewMultiPrevOutFetcher(prevOuts)
|
||||||
|
_, err = m.signMusig2Tx(
|
||||||
|
m.runCtx, prevOutFetcher, outpoints, m.cfg.Signer, withdrawalTx,
|
||||||
|
withdrawalSessions, coopServerNonces,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now we'll finalize the sweepless sweep transaction.
|
||||||
|
finalizedWithdrawalTx, err := m.finalizeMusig2Transaction(
|
||||||
|
m.runCtx, outpoints, m.cfg.Signer, withdrawalSessions,
|
||||||
|
withdrawalTx, resp.Musig2SweepSigs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
txLabel := fmt.Sprintf("deposit-withdrawal-%v",
|
||||||
|
finalizedWithdrawalTx.TxHash())
|
||||||
|
|
||||||
|
// Publish the withdrawal sweep transaction.
|
||||||
|
err = m.cfg.WalletKit.PublishTransaction(
|
||||||
|
m.runCtx, finalizedWithdrawalTx, txLabel,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if !strings.Contains(err.Error(), "output already spent") {
|
||||||
|
log.Errorf("%v: %v", txLabel, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Debugf("published deposit withdrawal with txid: %v",
|
||||||
|
finalizedWithdrawalTx.TxHash())
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) handleWithdrawal(deposits []*deposit.Deposit,
|
||||||
|
withdrawalAddress btcutil.Address) error {
|
||||||
|
|
||||||
|
withdrawalPkScript, err := txscript.PayToAddrScript(withdrawalAddress)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
confChan, errChan, err := m.cfg.ChainNotifier.RegisterConfirmationsNtfn(
|
||||||
|
m.runCtx, nil, withdrawalPkScript, MinConfs,
|
||||||
|
int32(m.initiationHeight),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-confChan:
|
||||||
|
err = m.cfg.DepositManager.TransitionDeposits(
|
||||||
|
deposits, deposit.OnWithdrawn,
|
||||||
|
deposit.Withdrawn,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error transitioning deposits: %v",
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the withdrawal from the active withdrawals.
|
||||||
|
m.Lock()
|
||||||
|
delete(m.activeWithdrawals, withdrawalAddress.String())
|
||||||
|
m.Unlock()
|
||||||
|
|
||||||
|
case err := <-errChan:
|
||||||
|
log.Errorf("Error waiting for confirmation: %v", err)
|
||||||
|
|
||||||
|
case <-m.runCtx.Done():
|
||||||
|
log.Errorf("Withdrawal tx confirmation wait canceled")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toOutpoints(deposits []*deposit.Deposit) []wire.OutPoint {
|
||||||
|
outpoints := make([]wire.OutPoint, len(deposits))
|
||||||
|
for i, d := range deposits {
|
||||||
|
outpoints[i] = wire.OutPoint{
|
||||||
|
Hash: d.Hash,
|
||||||
|
Index: d.Index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outpoints
|
||||||
|
}
|
||||||
|
|
||||||
|
// signMusig2Tx adds the server nonces to the musig2 sessions and signs the
|
||||||
|
// transaction.
|
||||||
|
func (m *Manager) signMusig2Tx(ctx context.Context,
|
||||||
|
prevOutFetcher *txscript.MultiPrevOutFetcher, outpoints []wire.OutPoint,
|
||||||
|
signer lndclient.SignerClient, tx *wire.MsgTx,
|
||||||
|
musig2sessions []*input.MuSig2SessionInfo,
|
||||||
|
counterPartyNonces [][musig2.PubNonceSize]byte) ([][]byte, error) {
|
||||||
|
|
||||||
|
sigHashes := txscript.NewTxSigHashes(tx, prevOutFetcher)
|
||||||
|
sigs := make([][]byte, len(outpoints))
|
||||||
|
|
||||||
|
for idx, outpoint := range outpoints {
|
||||||
|
if !reflect.DeepEqual(tx.TxIn[idx].PreviousOutPoint,
|
||||||
|
outpoint) {
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("tx input does not match " +
|
||||||
|
"deposits")
|
||||||
|
}
|
||||||
|
|
||||||
|
taprootSigHash, err := txscript.CalcTaprootSignatureHash(
|
||||||
|
sigHashes, txscript.SigHashDefault, tx, idx,
|
||||||
|
prevOutFetcher,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var digest [32]byte
|
||||||
|
copy(digest[:], taprootSigHash)
|
||||||
|
|
||||||
|
// Register the server's nonce before attempting to create our
|
||||||
|
// partial signature.
|
||||||
|
haveAllNonces, err := signer.MuSig2RegisterNonces(
|
||||||
|
ctx, musig2sessions[idx].SessionID,
|
||||||
|
[][musig2.PubNonceSize]byte{counterPartyNonces[idx]},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sanity check that we have all the nonces.
|
||||||
|
if !haveAllNonces {
|
||||||
|
return nil, fmt.Errorf("invalid MuSig2 session: " +
|
||||||
|
"nonces missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since our MuSig2 session has all nonces, we can now create
|
||||||
|
// the local partial signature by signing the sig hash.
|
||||||
|
sig, err := signer.MuSig2Sign(
|
||||||
|
ctx, musig2sessions[idx].SessionID, digest, false,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sigs[idx] = sig
|
||||||
|
}
|
||||||
|
|
||||||
|
return sigs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func withdrawalValue(prevOuts map[wire.OutPoint]*wire.TxOut) btcutil.Amount {
|
||||||
|
var totalValue btcutil.Amount
|
||||||
|
for _, prevOut := range prevOuts {
|
||||||
|
totalValue += btcutil.Amount(prevOut.Value)
|
||||||
|
}
|
||||||
|
return totalValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// toNonces converts a byte slice to a 66 byte slice.
|
||||||
|
func toNonces(nonces [][]byte) ([][66]byte, error) {
|
||||||
|
res := make([][66]byte, 0, len(nonces))
|
||||||
|
for _, n := range nonces {
|
||||||
|
n := n
|
||||||
|
nonce, err := byteSliceTo66ByteSlice(n)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res = append(res, nonce)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// byteSliceTo66ByteSlice converts a byte slice to a 66 byte slice.
|
||||||
|
func byteSliceTo66ByteSlice(b []byte) ([66]byte, error) {
|
||||||
|
if len(b) != 66 {
|
||||||
|
return [66]byte{}, fmt.Errorf("invalid byte slice length")
|
||||||
|
}
|
||||||
|
|
||||||
|
var res [66]byte
|
||||||
|
copy(res[:], b)
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) createWithdrawalTx(outpoints []wire.OutPoint,
|
||||||
|
withdrawlAmount btcutil.Amount, clientSweepAddress btcutil.Address,
|
||||||
|
feeRate chainfee.SatPerKWeight) (*wire.MsgTx,
|
||||||
|
error) {
|
||||||
|
|
||||||
|
// First Create the tx.
|
||||||
|
msgTx := wire.NewMsgTx(2)
|
||||||
|
|
||||||
|
// Add the deposit inputs to the transaction in the order the server
|
||||||
|
// signed them.
|
||||||
|
for _, outpoint := range outpoints {
|
||||||
|
msgTx.AddTxIn(&wire.TxIn{
|
||||||
|
PreviousOutPoint: outpoint,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Estimate the fee
|
||||||
|
weight, err := withdrawalFee(len(outpoints), clientSweepAddress)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pkscript, err := txscript.PayToAddrScript(clientSweepAddress)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fee := feeRate.FeeForWeight(weight)
|
||||||
|
|
||||||
|
// Create the sweep output
|
||||||
|
sweepOutput := &wire.TxOut{
|
||||||
|
Value: int64(withdrawlAmount) - int64(fee),
|
||||||
|
PkScript: pkscript,
|
||||||
|
}
|
||||||
|
|
||||||
|
msgTx.AddTxOut(sweepOutput)
|
||||||
|
|
||||||
|
return msgTx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// withdrawalFee returns the weight for the withdrawal transaction.
|
||||||
|
func withdrawalFee(numInputs int, sweepAddress btcutil.Address) (int64,
|
||||||
|
error) {
|
||||||
|
|
||||||
|
var weightEstimator input.TxWeightEstimator
|
||||||
|
for i := 0; i < numInputs; i++ {
|
||||||
|
weightEstimator.AddTaprootKeySpendInput(
|
||||||
|
txscript.SigHashDefault,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the weight of the sweep output.
|
||||||
|
switch sweepAddress.(type) {
|
||||||
|
case *btcutil.AddressWitnessPubKeyHash:
|
||||||
|
weightEstimator.AddP2WKHOutput()
|
||||||
|
|
||||||
|
case *btcutil.AddressTaproot:
|
||||||
|
weightEstimator.AddP2TROutput()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0, fmt.Errorf("invalid sweep address type %T",
|
||||||
|
sweepAddress)
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(weightEstimator.Weight()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// finalizeMusig2Transaction creates the finalized transactions for either
|
||||||
|
// the htlc or the cooperative close.
|
||||||
|
func (m *Manager) finalizeMusig2Transaction(ctx context.Context,
|
||||||
|
outpoints []wire.OutPoint, signer lndclient.SignerClient,
|
||||||
|
musig2Sessions []*input.MuSig2SessionInfo,
|
||||||
|
tx *wire.MsgTx, serverSigs [][]byte) (*wire.MsgTx, error) {
|
||||||
|
|
||||||
|
for idx := range outpoints {
|
||||||
|
haveAllSigs, finalSig, err := signer.MuSig2CombineSig(
|
||||||
|
ctx, musig2Sessions[idx].SessionID,
|
||||||
|
[][]byte{serverSigs[idx]},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !haveAllSigs {
|
||||||
|
return nil, fmt.Errorf("missing sigs")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.TxIn[idx].Witness = wire.TxWitness{finalSig}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toServerOutpoints(outpoints []wire.OutPoint) []*staticaddressrpc.ServerOutPoint { //nolint:lll
|
||||||
|
var result []*staticaddressrpc.ServerOutPoint
|
||||||
|
for _, o := range outpoints {
|
||||||
|
outP := o
|
||||||
|
outpoint := &staticaddressrpc.ServerOutPoint{
|
||||||
|
TxidBytes: outP.Hash[:],
|
||||||
|
OutputIndex: outP.Index,
|
||||||
|
}
|
||||||
|
result = append(result, outpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// createMusig2Sessions creates a musig2 session for a number of deposits.
|
||||||
|
func (m *Manager) createMusig2Sessions(ctx context.Context,
|
||||||
|
deposits []*deposit.Deposit) ([]*input.MuSig2SessionInfo, [][]byte,
|
||||||
|
error) {
|
||||||
|
|
||||||
|
musig2Sessions := make([]*input.MuSig2SessionInfo, len(deposits))
|
||||||
|
clientNonces := make([][]byte, len(deposits))
|
||||||
|
|
||||||
|
// Create the sessions and nonces from the deposits.
|
||||||
|
for i := 0; i < len(deposits); i++ {
|
||||||
|
session, err := m.createMusig2Session(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
musig2Sessions[i] = session
|
||||||
|
clientNonces[i] = session.PublicNonce[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return musig2Sessions, clientNonces, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Musig2CreateSession creates a musig2 session for the deposit.
|
||||||
|
func (m *Manager) createMusig2Session(ctx context.Context) (
|
||||||
|
*input.MuSig2SessionInfo, error) {
|
||||||
|
|
||||||
|
addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
|
||||||
|
ctx,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("couldn't get confirmation height for "+
|
||||||
|
"deposit, %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
signers := [][]byte{
|
||||||
|
addressParams.ClientPubkey.SerializeCompressed(),
|
||||||
|
addressParams.ServerPubkey.SerializeCompressed(),
|
||||||
|
}
|
||||||
|
|
||||||
|
address, err := m.cfg.AddressManager.GetStaticAddress(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("couldn't get confirmation height for "+
|
||||||
|
"deposit, %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expiryLeaf := address.TimeoutLeaf
|
||||||
|
|
||||||
|
rootHash := expiryLeaf.TapHash()
|
||||||
|
|
||||||
|
return m.cfg.Signer.MuSig2CreateSession(
|
||||||
|
ctx, input.MuSig2Version100RC2, &addressParams.KeyLocator,
|
||||||
|
signers, lndclient.MuSig2TaprootTweakOpt(rootHash[:], false),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) toPrevOuts(deposits []*deposit.Deposit,
|
||||||
|
pkScript []byte) map[wire.OutPoint]*wire.TxOut {
|
||||||
|
|
||||||
|
prevOuts := make(map[wire.OutPoint]*wire.TxOut, len(deposits))
|
||||||
|
for _, d := range deposits {
|
||||||
|
outpoint := wire.OutPoint{
|
||||||
|
Hash: d.Hash,
|
||||||
|
Index: d.Index,
|
||||||
|
}
|
||||||
|
txOut := &wire.TxOut{
|
||||||
|
Value: int64(d.Value),
|
||||||
|
PkScript: pkScript,
|
||||||
|
}
|
||||||
|
prevOuts[outpoint] = txOut
|
||||||
|
}
|
||||||
|
|
||||||
|
return prevOuts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) republishWithdrawals() error {
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
|
for withdrawalAddress, deposits := range m.activeWithdrawals {
|
||||||
|
address, err := btcutil.DecodeAddress(
|
||||||
|
withdrawalAddress, m.cfg.ChainParams,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error decoding withdrawal address: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.publishWithdrawal(m.runCtx, deposits, address)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error republishing withdrawal: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,363 @@
|
|||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// protoc-gen-go v1.30.0
|
||||||
|
// protoc v3.6.1
|
||||||
|
// source: withdraw.proto
|
||||||
|
|
||||||
|
// We can't change this to swapserverrpc, it would be a breaking change because
|
||||||
|
// the package name is also contained in the HTTP URIs and old clients would
|
||||||
|
// call the wrong endpoints. Luckily with the go_package option we can have
|
||||||
|
// different golang and RPC package names to fix protobuf namespace conflicts.
|
||||||
|
|
||||||
|
package swapserverrpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
|
reflect "reflect"
|
||||||
|
sync "sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Verify that this generated code is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||||
|
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServerWithdrawRequest struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// The deposit outpoints the client whishes to close.
|
||||||
|
Outpoints []*ServerOutPoint `protobuf:"bytes,1,rep,name=outpoints,proto3" json:"outpoints,omitempty"`
|
||||||
|
// The nonces that the client used to generate the coop close tx sigs.
|
||||||
|
ClientNonces [][]byte `protobuf:"bytes,2,rep,name=client_nonces,json=clientNonces,proto3" json:"client_nonces,omitempty"`
|
||||||
|
// The address that the client wants to sweep the static address deposits
|
||||||
|
// to.
|
||||||
|
ClientSweepAddr string `protobuf:"bytes,3,opt,name=client_sweep_addr,json=clientSweepAddr,proto3" json:"client_sweep_addr,omitempty"`
|
||||||
|
// The fee rate in sat/kw that the client wants to use for the sweep.
|
||||||
|
MusigTxFeeRate uint64 `protobuf:"varint,4,opt,name=musig_tx_fee_rate,json=musigTxFeeRate,proto3" json:"musig_tx_fee_rate,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawRequest) Reset() {
|
||||||
|
*x = ServerWithdrawRequest{}
|
||||||
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_withdraw_proto_msgTypes[0]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawRequest) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ServerWithdrawRequest) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawRequest) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_withdraw_proto_msgTypes[0]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use ServerWithdrawRequest.ProtoReflect.Descriptor instead.
|
||||||
|
func (*ServerWithdrawRequest) Descriptor() ([]byte, []int) {
|
||||||
|
return file_withdraw_proto_rawDescGZIP(), []int{0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawRequest) GetOutpoints() []*ServerOutPoint {
|
||||||
|
if x != nil {
|
||||||
|
return x.Outpoints
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawRequest) GetClientNonces() [][]byte {
|
||||||
|
if x != nil {
|
||||||
|
return x.ClientNonces
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawRequest) GetClientSweepAddr() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.ClientSweepAddr
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawRequest) GetMusigTxFeeRate() uint64 {
|
||||||
|
if x != nil {
|
||||||
|
return x.MusigTxFeeRate
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerWithdrawResponse struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
// The sweep sigs that the server generated for the htlc.
|
||||||
|
Musig2SweepSigs [][]byte `protobuf:"bytes,1,rep,name=musig2_sweep_sigs,json=musig2SweepSigs,proto3" json:"musig2_sweep_sigs,omitempty"`
|
||||||
|
// The nonces that the server used to generate the sweepless sweep sigs.
|
||||||
|
ServerNonces [][]byte `protobuf:"bytes,2,rep,name=server_nonces,json=serverNonces,proto3" json:"server_nonces,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawResponse) Reset() {
|
||||||
|
*x = ServerWithdrawResponse{}
|
||||||
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_withdraw_proto_msgTypes[1]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawResponse) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ServerWithdrawResponse) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawResponse) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_withdraw_proto_msgTypes[1]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use ServerWithdrawResponse.ProtoReflect.Descriptor instead.
|
||||||
|
func (*ServerWithdrawResponse) Descriptor() ([]byte, []int) {
|
||||||
|
return file_withdraw_proto_rawDescGZIP(), []int{1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawResponse) GetMusig2SweepSigs() [][]byte {
|
||||||
|
if x != nil {
|
||||||
|
return x.Musig2SweepSigs
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerWithdrawResponse) GetServerNonces() [][]byte {
|
||||||
|
if x != nil {
|
||||||
|
return x.ServerNonces
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerOutPoint struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
|
//
|
||||||
|
//Raw bytes representing the transaction id.
|
||||||
|
TxidBytes []byte `protobuf:"bytes,1,opt,name=txid_bytes,json=txidBytes,proto3" json:"txid_bytes,omitempty"`
|
||||||
|
//
|
||||||
|
//Reversed, hex-encoded string representing the transaction id.
|
||||||
|
TxidStr string `protobuf:"bytes,2,opt,name=txid_str,json=txidStr,proto3" json:"txid_str,omitempty"`
|
||||||
|
//
|
||||||
|
//The index of the output on the transaction.
|
||||||
|
OutputIndex uint32 `protobuf:"varint,3,opt,name=output_index,json=outputIndex,proto3" json:"output_index,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerOutPoint) Reset() {
|
||||||
|
*x = ServerOutPoint{}
|
||||||
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_withdraw_proto_msgTypes[2]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerOutPoint) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ServerOutPoint) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *ServerOutPoint) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_withdraw_proto_msgTypes[2]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use ServerOutPoint.ProtoReflect.Descriptor instead.
|
||||||
|
func (*ServerOutPoint) Descriptor() ([]byte, []int) {
|
||||||
|
return file_withdraw_proto_rawDescGZIP(), []int{2}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerOutPoint) GetTxidBytes() []byte {
|
||||||
|
if x != nil {
|
||||||
|
return x.TxidBytes
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerOutPoint) GetTxidStr() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.TxidStr
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *ServerOutPoint) GetOutputIndex() uint32 {
|
||||||
|
if x != nil {
|
||||||
|
return x.OutputIndex
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var File_withdraw_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
|
var file_withdraw_proto_rawDesc = []byte{
|
||||||
|
0x0a, 0x0e, 0x77, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
||||||
|
0x12, 0x07, 0x6c, 0x6f, 0x6f, 0x70, 0x72, 0x70, 0x63, 0x22, 0xca, 0x01, 0x0a, 0x15, 0x53, 0x65,
|
||||||
|
0x72, 0x76, 0x65, 0x72, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77, 0x52, 0x65, 0x71, 0x75,
|
||||||
|
0x65, 0x73, 0x74, 0x12, 0x35, 0x0a, 0x09, 0x6f, 0x75, 0x74, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x73,
|
||||||
|
0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x6c, 0x6f, 0x6f, 0x70, 0x72, 0x70, 0x63,
|
||||||
|
0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4f, 0x75, 0x74, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x52,
|
||||||
|
0x09, 0x6f, 0x75, 0x74, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x73, 0x12, 0x23, 0x0a, 0x0d, 0x63, 0x6c,
|
||||||
|
0x69, 0x65, 0x6e, 0x74, 0x5f, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28,
|
||||||
|
0x0c, 0x52, 0x0c, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4e, 0x6f, 0x6e, 0x63, 0x65, 0x73, 0x12,
|
||||||
|
0x2a, 0x0a, 0x11, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x73, 0x77, 0x65, 0x65, 0x70, 0x5f,
|
||||||
|
0x61, 0x64, 0x64, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x63, 0x6c, 0x69, 0x65,
|
||||||
|
0x6e, 0x74, 0x53, 0x77, 0x65, 0x65, 0x70, 0x41, 0x64, 0x64, 0x72, 0x12, 0x29, 0x0a, 0x11, 0x6d,
|
||||||
|
0x75, 0x73, 0x69, 0x67, 0x5f, 0x74, 0x78, 0x5f, 0x66, 0x65, 0x65, 0x5f, 0x72, 0x61, 0x74, 0x65,
|
||||||
|
0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0e, 0x6d, 0x75, 0x73, 0x69, 0x67, 0x54, 0x78, 0x46,
|
||||||
|
0x65, 0x65, 0x52, 0x61, 0x74, 0x65, 0x22, 0x69, 0x0a, 0x16, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
|
||||||
|
0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
|
||||||
|
0x12, 0x2a, 0x0a, 0x11, 0x6d, 0x75, 0x73, 0x69, 0x67, 0x32, 0x5f, 0x73, 0x77, 0x65, 0x65, 0x70,
|
||||||
|
0x5f, 0x73, 0x69, 0x67, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x0f, 0x6d, 0x75, 0x73,
|
||||||
|
0x69, 0x67, 0x32, 0x53, 0x77, 0x65, 0x65, 0x70, 0x53, 0x69, 0x67, 0x73, 0x12, 0x23, 0x0a, 0x0d,
|
||||||
|
0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x73, 0x18, 0x02, 0x20,
|
||||||
|
0x03, 0x28, 0x0c, 0x52, 0x0c, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4e, 0x6f, 0x6e, 0x63, 0x65,
|
||||||
|
0x73, 0x22, 0x6d, 0x0a, 0x0e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4f, 0x75, 0x74, 0x50, 0x6f,
|
||||||
|
0x69, 0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x74, 0x78, 0x69, 0x64, 0x5f, 0x62, 0x79, 0x74, 0x65,
|
||||||
|
0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x74, 0x78, 0x69, 0x64, 0x42, 0x79, 0x74,
|
||||||
|
0x65, 0x73, 0x12, 0x19, 0x0a, 0x08, 0x74, 0x78, 0x69, 0x64, 0x5f, 0x73, 0x74, 0x72, 0x18, 0x02,
|
||||||
|
0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x74, 0x78, 0x69, 0x64, 0x53, 0x74, 0x72, 0x12, 0x21, 0x0a,
|
||||||
|
0x0c, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x5f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x18, 0x03, 0x20,
|
||||||
|
0x01, 0x28, 0x0d, 0x52, 0x0b, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x49, 0x6e, 0x64, 0x65, 0x78,
|
||||||
|
0x32, 0x67, 0x0a, 0x10, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77, 0x61, 0x6c, 0x53, 0x65,
|
||||||
|
0x72, 0x76, 0x65, 0x72, 0x12, 0x53, 0x0a, 0x10, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77,
|
||||||
|
0x44, 0x65, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x73, 0x12, 0x1e, 0x2e, 0x6c, 0x6f, 0x6f, 0x70, 0x72,
|
||||||
|
0x70, 0x63, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61,
|
||||||
|
0x77, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e, 0x6c, 0x6f, 0x6f, 0x70, 0x72,
|
||||||
|
0x70, 0x63, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61,
|
||||||
|
0x77, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x2d, 0x5a, 0x2b, 0x67, 0x69, 0x74,
|
||||||
|
0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x6e, 0x69, 0x6e,
|
||||||
|
0x67, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x6c, 0x6f, 0x6f, 0x70, 0x2f, 0x73, 0x77, 0x61, 0x70, 0x73,
|
||||||
|
0x65, 0x72, 0x76, 0x65, 0x72, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
file_withdraw_proto_rawDescOnce sync.Once
|
||||||
|
file_withdraw_proto_rawDescData = file_withdraw_proto_rawDesc
|
||||||
|
)
|
||||||
|
|
||||||
|
func file_withdraw_proto_rawDescGZIP() []byte {
|
||||||
|
file_withdraw_proto_rawDescOnce.Do(func() {
|
||||||
|
file_withdraw_proto_rawDescData = protoimpl.X.CompressGZIP(file_withdraw_proto_rawDescData)
|
||||||
|
})
|
||||||
|
return file_withdraw_proto_rawDescData
|
||||||
|
}
|
||||||
|
|
||||||
|
var file_withdraw_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
|
||||||
|
var file_withdraw_proto_goTypes = []interface{}{
|
||||||
|
(*ServerWithdrawRequest)(nil), // 0: looprpc.ServerWithdrawRequest
|
||||||
|
(*ServerWithdrawResponse)(nil), // 1: looprpc.ServerWithdrawResponse
|
||||||
|
(*ServerOutPoint)(nil), // 2: looprpc.ServerOutPoint
|
||||||
|
}
|
||||||
|
var file_withdraw_proto_depIdxs = []int32{
|
||||||
|
2, // 0: looprpc.ServerWithdrawRequest.outpoints:type_name -> looprpc.ServerOutPoint
|
||||||
|
0, // 1: looprpc.WithdrawalServer.WithdrawDeposits:input_type -> looprpc.ServerWithdrawRequest
|
||||||
|
1, // 2: looprpc.WithdrawalServer.WithdrawDeposits:output_type -> looprpc.ServerWithdrawResponse
|
||||||
|
2, // [2:3] is the sub-list for method output_type
|
||||||
|
1, // [1:2] is the sub-list for method input_type
|
||||||
|
1, // [1:1] is the sub-list for extension type_name
|
||||||
|
1, // [1:1] is the sub-list for extension extendee
|
||||||
|
0, // [0:1] is the sub-list for field type_name
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { file_withdraw_proto_init() }
|
||||||
|
func file_withdraw_proto_init() {
|
||||||
|
if File_withdraw_proto != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !protoimpl.UnsafeEnabled {
|
||||||
|
file_withdraw_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*ServerWithdrawRequest); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file_withdraw_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*ServerWithdrawResponse); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file_withdraw_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*ServerOutPoint); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type x struct{}
|
||||||
|
out := protoimpl.TypeBuilder{
|
||||||
|
File: protoimpl.DescBuilder{
|
||||||
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
|
RawDescriptor: file_withdraw_proto_rawDesc,
|
||||||
|
NumEnums: 0,
|
||||||
|
NumMessages: 3,
|
||||||
|
NumExtensions: 0,
|
||||||
|
NumServices: 1,
|
||||||
|
},
|
||||||
|
GoTypes: file_withdraw_proto_goTypes,
|
||||||
|
DependencyIndexes: file_withdraw_proto_depIdxs,
|
||||||
|
MessageInfos: file_withdraw_proto_msgTypes,
|
||||||
|
}.Build()
|
||||||
|
File_withdraw_proto = out.File
|
||||||
|
file_withdraw_proto_rawDesc = nil
|
||||||
|
file_withdraw_proto_goTypes = nil
|
||||||
|
file_withdraw_proto_depIdxs = nil
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
// We can't change this to swapserverrpc, it would be a breaking change because
|
||||||
|
// the package name is also contained in the HTTP URIs and old clients would
|
||||||
|
// call the wrong endpoints. Luckily with the go_package option we can have
|
||||||
|
// different golang and RPC package names to fix protobuf namespace conflicts.
|
||||||
|
package looprpc;
|
||||||
|
|
||||||
|
option go_package = "github.com/lightninglabs/loop/swapserverrpc";
|
||||||
|
|
||||||
|
service WithdrawalServer {
|
||||||
|
// WithdrawDeposits allows to cooperatively sweep deposits that haven't
|
||||||
|
// timed out yet to the client's wallet. The server will generate the
|
||||||
|
// partial sigs for the client's selected deposits.
|
||||||
|
rpc WithdrawDeposits (ServerWithdrawRequest)
|
||||||
|
returns (ServerWithdrawResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
message ServerWithdrawRequest {
|
||||||
|
// The deposit outpoints the client whishes to close.
|
||||||
|
repeated ServerOutPoint outpoints = 1;
|
||||||
|
|
||||||
|
// The nonces that the client used to generate the coop close tx sigs.
|
||||||
|
repeated bytes client_nonces = 2;
|
||||||
|
|
||||||
|
// The address that the client wants to sweep the static address deposits
|
||||||
|
// to.
|
||||||
|
string client_sweep_addr = 3;
|
||||||
|
|
||||||
|
// The fee rate in sat/kw that the client wants to use for the sweep.
|
||||||
|
uint64 musig_tx_fee_rate = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ServerWithdrawResponse {
|
||||||
|
// The sweep sigs that the server generated for the htlc.
|
||||||
|
repeated bytes musig2_sweep_sigs = 1;
|
||||||
|
|
||||||
|
// The nonces that the server used to generate the sweepless sweep sigs.
|
||||||
|
repeated bytes server_nonces = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ServerOutPoint {
|
||||||
|
/*
|
||||||
|
Raw bytes representing the transaction id.
|
||||||
|
*/
|
||||||
|
bytes txid_bytes = 1;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Reversed, hex-encoded string representing the transaction id.
|
||||||
|
*/
|
||||||
|
string txid_str = 2;
|
||||||
|
|
||||||
|
/*
|
||||||
|
The index of the output on the transaction.
|
||||||
|
*/
|
||||||
|
uint32 output_index = 3;
|
||||||
|
}
|
@ -0,0 +1,107 @@
|
|||||||
|
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||||
|
|
||||||
|
package swapserverrpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
|
codes "google.golang.org/grpc/codes"
|
||||||
|
status "google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
// Requires gRPC-Go v1.32.0 or later.
|
||||||
|
const _ = grpc.SupportPackageIsVersion7
|
||||||
|
|
||||||
|
// WithdrawalServerClient is the client API for WithdrawalServer service.
|
||||||
|
//
|
||||||
|
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||||
|
type WithdrawalServerClient interface {
|
||||||
|
// WithdrawDeposits allows to cooperatively sweep deposits that haven't
|
||||||
|
// timed out yet to the client's wallet. The server will generate the
|
||||||
|
// partial sigs for the client's selected deposits.
|
||||||
|
WithdrawDeposits(ctx context.Context, in *ServerWithdrawRequest, opts ...grpc.CallOption) (*ServerWithdrawResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type withdrawalServerClient struct {
|
||||||
|
cc grpc.ClientConnInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWithdrawalServerClient(cc grpc.ClientConnInterface) WithdrawalServerClient {
|
||||||
|
return &withdrawalServerClient{cc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *withdrawalServerClient) WithdrawDeposits(ctx context.Context, in *ServerWithdrawRequest, opts ...grpc.CallOption) (*ServerWithdrawResponse, error) {
|
||||||
|
out := new(ServerWithdrawResponse)
|
||||||
|
err := c.cc.Invoke(ctx, "/looprpc.WithdrawalServer/WithdrawDeposits", in, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithdrawalServerServer is the server API for WithdrawalServer service.
|
||||||
|
// All implementations must embed UnimplementedWithdrawalServerServer
|
||||||
|
// for forward compatibility
|
||||||
|
type WithdrawalServerServer interface {
|
||||||
|
// WithdrawDeposits allows to cooperatively sweep deposits that haven't
|
||||||
|
// timed out yet to the client's wallet. The server will generate the
|
||||||
|
// partial sigs for the client's selected deposits.
|
||||||
|
WithdrawDeposits(context.Context, *ServerWithdrawRequest) (*ServerWithdrawResponse, error)
|
||||||
|
mustEmbedUnimplementedWithdrawalServerServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnimplementedWithdrawalServerServer must be embedded to have forward compatible implementations.
|
||||||
|
type UnimplementedWithdrawalServerServer struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (UnimplementedWithdrawalServerServer) WithdrawDeposits(context.Context, *ServerWithdrawRequest) (*ServerWithdrawResponse, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method WithdrawDeposits not implemented")
|
||||||
|
}
|
||||||
|
func (UnimplementedWithdrawalServerServer) mustEmbedUnimplementedWithdrawalServerServer() {}
|
||||||
|
|
||||||
|
// UnsafeWithdrawalServerServer may be embedded to opt out of forward compatibility for this service.
|
||||||
|
// Use of this interface is not recommended, as added methods to WithdrawalServerServer will
|
||||||
|
// result in compilation errors.
|
||||||
|
type UnsafeWithdrawalServerServer interface {
|
||||||
|
mustEmbedUnimplementedWithdrawalServerServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterWithdrawalServerServer(s grpc.ServiceRegistrar, srv WithdrawalServerServer) {
|
||||||
|
s.RegisterService(&WithdrawalServer_ServiceDesc, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _WithdrawalServer_WithdrawDeposits_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(ServerWithdrawRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(WithdrawalServerServer).WithdrawDeposits(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/looprpc.WithdrawalServer/WithdrawDeposits",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(WithdrawalServerServer).WithdrawDeposits(ctx, req.(*ServerWithdrawRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithdrawalServer_ServiceDesc is the grpc.ServiceDesc for WithdrawalServer service.
|
||||||
|
// It's only intended for direct use with grpc.RegisterService,
|
||||||
|
// and not to be introspected or modified (even as a copy)
|
||||||
|
var WithdrawalServer_ServiceDesc = grpc.ServiceDesc{
|
||||||
|
ServiceName: "looprpc.WithdrawalServer",
|
||||||
|
HandlerType: (*WithdrawalServerServer)(nil),
|
||||||
|
Methods: []grpc.MethodDesc{
|
||||||
|
{
|
||||||
|
MethodName: "WithdrawDeposits",
|
||||||
|
Handler: _WithdrawalServer_WithdrawDeposits_Handler,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Streams: []grpc.StreamDesc{},
|
||||||
|
Metadata: "withdraw.proto",
|
||||||
|
}
|
Loading…
Reference in New Issue