github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/wallit/basewalletif.go (about)

     1  package wallit
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/mit-dci/lit/btcutil/chaincfg/chainhash"
     8  	"github.com/mit-dci/lit/coinparam"
     9  	"github.com/mit-dci/lit/crypto/koblitz"
    10  	"github.com/mit-dci/lit/lnutil"
    11  	"github.com/mit-dci/lit/logging"
    12  	"github.com/mit-dci/lit/portxo"
    13  	"github.com/mit-dci/lit/uspv"
    14  	"github.com/mit-dci/lit/wire"
    15  )
    16  
    17  /*
    18  type UWallet interface {
    19  	GetPub(k portxo.KeyGen) *koblitz.PublicKey
    20  
    21  	GetPriv(k portxo.KeyGen) *koblitz.PrivateKey
    22  
    23  	PushTx(tx *wire.MsgTx) error
    24  	ExportUtxo(txo *portxo.PorTxo)
    25  	MaybeSend(txos []*wire.TxOut) ([]*wire.OutPoint, error)
    26  	ReallySend(txid *chainhash.Hash) error
    27  	NahDontSend(txid *chainhash.Hash) error
    28  	WatchThis(wire.OutPoint) error
    29  	LetMeKnow() chan lnutil.OutPointEvent
    30  	LetMeKnowHeight() chan lnutil.HeightEvent
    31  	BlockMonitor() chan *wire.MsgBlock
    32  
    33  	Params() *coinparam.Params
    34  }
    35  */
    36  
    37  // --- implementation of BaseWallet interface ----
    38  
    39  func (w *Wallit) GetPriv(k portxo.KeyGen) (*koblitz.PrivateKey, error) {
    40  	if w.PathPrivkey(k) != nil {
    41  		return w.PathPrivkey(k), nil
    42  	} else {
    43  		return nil, fmt.Errorf("Nil Wallet Error")
    44  	}
    45  }
    46  
    47  func (w *Wallit) GetPub(k portxo.KeyGen) *koblitz.PublicKey {
    48  	return w.PathPubkey(k)
    49  }
    50  
    51  func (w *Wallit) PushTx(tx *wire.MsgTx) error {
    52  	return w.Hook.PushTx(tx)
    53  }
    54  
    55  func (w *Wallit) Params() *coinparam.Params {
    56  	return w.Param
    57  }
    58  
    59  func (w *Wallit) LetMeKnow() chan lnutil.OutPointEvent {
    60  	w.OPEventChan = make(chan lnutil.OutPointEvent, 1)
    61  	return w.OPEventChan
    62  }
    63  
    64  func (w *Wallit) LetMeKnowHeight() chan lnutil.HeightEvent {
    65  	w.HeightEventChan = make(chan lnutil.HeightEvent, 1)
    66  	return w.HeightEventChan
    67  }
    68  
    69  func (w *Wallit) CurrentHeight() int32 {
    70  	h, err := w.GetDBSyncHeight()
    71  	if err != nil {
    72  		logging.Errorf("can't get height from db...")
    73  		return -99
    74  	}
    75  	return h
    76  }
    77  
    78  func (w *Wallit) NewAdr() ([20]byte, error) {
    79  	return w.NewAdr160()
    80  }
    81  
    82  func (w *Wallit) ExportHook() uspv.ChainHook {
    83  	return w.Hook
    84  }
    85  
    86  // ExportUtxo is really *IM*port utxo on this side.
    87  // Not implemented yet.  Fix "ingest many" at the same time eh?
    88  func (w *Wallit) ExportUtxo(u *portxo.PorTxo) {
    89  
    90  	// zero value utxo counts as an address exort, not utxo export.
    91  	if u.Value == 0 {
    92  		err := w.AddPorTxoAdr(u.KeyGen)
    93  		if err != nil {
    94  			logging.Errorf(err.Error())
    95  		}
    96  	} else {
    97  		err := w.GainUtxo(*u)
    98  		if err != nil {
    99  			logging.Errorf(err.Error())
   100  		}
   101  	}
   102  
   103  	// Register new address with chainhook
   104  	adr160 := w.PathPubHash160(u.KeyGen)
   105  	err := w.Hook.RegisterAddress(adr160)
   106  	if err != nil {
   107  		logging.Errorf(err.Error())
   108  	}
   109  }
   110  
   111  // WatchThis registers an outpoint to watch.  Register as watched OP, and
   112  // passes to chainhook.
   113  func (w *Wallit) WatchThis(op wire.OutPoint) error {
   114  
   115  	// first, tell the chainhook
   116  	err := w.Hook.RegisterOutPoint(op)
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	// then register in the wallit
   122  	err = w.RegisterWatchOP(op)
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	return nil
   128  }
   129  
   130  // StopWatchingThis removes an outpoint to watch.
   131  func (w *Wallit) StopWatchingThis(op wire.OutPoint) error {
   132  
   133  	// first, tell the chainhook
   134  	err := w.Hook.RegisterOutPoint(op)
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	// then unregister from the wallit
   140  	err = w.UnregisterWatchOP(op)
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	return nil
   146  }
   147  
   148  func (w *Wallit) Fee() int64 {
   149  	return w.FeeRate
   150  }
   151  
   152  func (w *Wallit) SetFee(set int64) int64 {
   153  	w.FeeRate = set
   154  	return set
   155  }
   156  
   157  // ********* sweep is for testing / spamming, remove for real use
   158  func (w *Wallit) Sweep(outScript []byte, n uint32) ([]*chainhash.Hash, error) {
   159  	var err error
   160  	var txids []*chainhash.Hash
   161  
   162  	var utxos portxo.TxoSliceByAmt
   163  	utxos, err = w.GetAllUtxos()
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	// smallest and unconfirmed last (because it's reversed)
   169  	sort.Sort(sort.Reverse(utxos))
   170  
   171  	for _, u := range utxos {
   172  		if n < 1 {
   173  			return txids, nil
   174  		}
   175  
   176  		// this doesn't really work with maybeSend huh...
   177  		if u.Height != 0 && u.Value > 20000 {
   178  			tx, err := w.SendOne(*u, outScript)
   179  			if err != nil {
   180  				return nil, err
   181  			}
   182  
   183  			_, err = w.Ingest(tx, 0)
   184  			if err != nil {
   185  				return nil, err
   186  			}
   187  
   188  			err = w.PushTx(tx)
   189  			if err != nil {
   190  				return nil, err
   191  			}
   192  			txid := tx.TxHash()
   193  			txids = append(txids, &txid)
   194  
   195  			n--
   196  		}
   197  	}
   198  
   199  	return txids, nil
   200  }