wallit

package
v0.0.0-...-8c3d3b4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2022 License: MIT Imports: 24 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// storage of all utxos. top level is outpoints.
	BKToutpoint = []byte("DuffelBag")
	// storage of all addresses being watched.  top level is pkscripts
	BKTadr = []byte("adr")

	BKTStxos = []byte("SpentTxs")  // for bookkeeping / not sure
	BKTTxns  = []byte("Txns")      // all txs we care about, for replays
	BKTState = []byte("MiscState") // misc states of DB

	//	BKTWatch = []byte("watch") // outpoints we're watching for someone else
	// these are in the state bucket
	KEYNumKeys = []byte("NumKeys") // number of p2pkh keys used

	KEYTipHeight = []byte("TipHeight") // height synced to
)

const strings for db usage

Functions

func EstFee

func EstFee(txins []*portxo.PorTxo, outputByteSize, spB int64) int64

EstFee gives a fee estimate based on an input / output set and a sat/Byte target. It guesses the final tx size based on: Txouts: 8 bytes + pkscript length Total guess on the p2wsh one, see if that's accurate

func GetWalletKeygen

func GetWalletKeygen(idx, cointype uint32) portxo.KeyGen

GetWalletKeygen returns the keygen for a standard wallet address

func NewPorTxo

func NewPorTxo(tx *wire.MsgTx, idx uint32, height int32,
	kg portxo.KeyGen) (*portxo.PorTxo, error)

func NewPorTxoBytesFromKGBytes

func NewPorTxoBytesFromKGBytes(
	tx *wire.MsgTx, idx uint32, height int32, kgb []byte) ([]byte, error)

NewPorTxoBytesFromKGBytes just calls NewPorTxo() and de/serializes quick shortcut for use in the ingest() function

func TxToString

func TxToString(tx *wire.MsgTx) string

TxToString prints out some info about a transaction. for testing / debugging

Types

type FrozenTx

type FrozenTx struct {
	Ins       []*portxo.PorTxo `json:"ins"`
	Outs      []*wire.TxOut    `json:"outs"`
	ChangeOut *wire.TxOut      `json:"changeout"`
	Nlock     uint32           `json:"nlocktime"`
	Txid      chainhash.Hash   `json:"txid"`
}

type Stxo

type Stxo struct {
	PorTxo      portxo.PorTxo  `json:"txo"`    // when it used to be a utxo
	SpendHeight int32          `json:"height"` // height at which it met its demise
	SpendTxid   chainhash.Hash `json:"txid"`   // the tx that consumed it
}

Stxo is a utxo that has moved on.

func StxoFromBytes

func StxoFromBytes(b []byte) (Stxo, error)

StxoFromBytes turns bytes into a Stxo. it's a portxo with a spendHeight and spendTxid at the end.

func (*Stxo) ToBytes

func (s *Stxo) ToBytes() ([]byte, error)

ToBytes turns an Stxo into some bytes. prevUtxo serialization, then spendheight [4], spendtxid [32]

type Wallit

type Wallit struct {
	// could get rid of adr slice, it's just an in-ram cache...
	StateDB *bolt.DB // place to write all this down

	// Set of frozen utxos not to use... they point to the tx using em
	FreezeSet   map[wire.OutPoint]*FrozenTx
	FreezeMutex sync.Mutex

	// OPEventChan sends events to the LN wallet.
	// Gets initialized and activates when called by qln
	OPEventChan chan lnutil.OutPointEvent

	// HeightEventChan sends block height changes to the LN wallet.
	// Gets initialized and activates when called by qln
	HeightEventChan chan lnutil.HeightEvent

	// Params live here...
	Param *coinparam.Params // network parameters (testnet3, segnet, etc)

	// Hook is the connection to a blockchain.
	// imports the uspv interface.  Could put that somewhere else.
	// like an interfaces library, ... lnutil?
	Hook uspv.ChainHook

	// current fee per byte
	FeeRate int64
	// contains filtered or unexported fields
}

The Wallit is lit's main wallet struct. It's got the root key, the dbs, and contains the SPVhooks into the network.

func NewWallit

func NewWallit(
	rootkey *hdkeychain.ExtendedKey, birthHeight int32, resync bool,
	spvhost, path string, proxyURL string, p *coinparam.Params) (*Wallit, int, error)

func (*Wallit) AddPorTxoAdr

func (w *Wallit) AddPorTxoAdr(kg portxo.KeyGen) error

AddPorTxoAdr adds an externally sourced address to the db. Looks at the keygen to derive hash160.

func (*Wallit) AdrDump

func (w *Wallit) AdrDump() ([][20]byte, error)

AdrDump returns all the addresses in the wallit. currently returns 20 byte arrays, which can then be converted somewhere else into bech32 addresses (or old base58)

func (*Wallit) BuildAndSign

func (w *Wallit) BuildAndSign(
	utxos []*portxo.PorTxo, txos []*wire.TxOut, nlt uint32) (*wire.MsgTx, error)

BuildAndSign builds a tx from a slice of utxos and txOuts. It then signs all the inputs and returns the tx. Should pretty much always work for any inputs.

func (*Wallit) BuildDontSign

func (w *Wallit) BuildDontSign(
	utxos []*portxo.PorTxo, txos []*wire.TxOut) (*wire.MsgTx, error)

Builds tx from inputs and outputs, returns tx. Sorts. Doesn't sign.

func (*Wallit) CurrentHeight

func (w *Wallit) CurrentHeight() int32

func (*Wallit) DirectSendTx

func (w *Wallit) DirectSendTx(tx *wire.MsgTx) error

Directly send out a tx. For things that plug in to the uspv wallet.

func (*Wallit) ExportHook

func (w *Wallit) ExportHook() uspv.ChainHook

func (*Wallit) ExportUtxo

func (w *Wallit) ExportUtxo(u *portxo.PorTxo)

ExportUtxo is really *IM*port utxo on this side. Not implemented yet. Fix "ingest many" at the same time eh?

func (*Wallit) Fee

func (w *Wallit) Fee() int64

func (*Wallit) FindFreezeTx

func (w *Wallit) FindFreezeTx(txid *chainhash.Hash) (*FrozenTx, error)

FindFreezeTx looks through the frozen map to find a tx. Error if it can't find it

func (*Wallit) GainUtxo

func (w *Wallit) GainUtxo(u portxo.PorTxo) error

GainUtxo registers the utxo in the duffel bag don't register address; they shouldn't be re-used ever anyway.

func (*Wallit) GetAllUtxos

func (w *Wallit) GetAllUtxos() ([]*portxo.PorTxo, error)

GetAllUtxos returns a slice of all portxos in the db. empty slice is OK. Doesn't return watch only outpoints

func (*Wallit) GetDBSyncHeight

func (w *Wallit) GetDBSyncHeight() (int32, error)

SyncHeight returns the chain height to which the db has synced

func (*Wallit) GetPriv

func (w *Wallit) GetPriv(k portxo.KeyGen) (*koblitz.PrivateKey, error)

func (*Wallit) GetPub

func (w *Wallit) GetPub(k portxo.KeyGen) *koblitz.PublicKey

func (*Wallit) GetUsePriv

func (w *Wallit) GetUsePriv(kg portxo.KeyGen, use uint32) *koblitz.PrivateKey

GetUsePrive generates a private key for the given use case & keypath

func (*Wallit) GetUsePub

func (w *Wallit) GetUsePub(kg portxo.KeyGen, use uint32) [33]byte

GetUsePub generates a pubkey for the given use case & keypath

func (*Wallit) GetWalletPrivkey

func (w *Wallit) GetWalletPrivkey(idx uint32) *koblitz.PrivateKey

get a private key from the regular wallet

func (*Wallit) GrabAll

func (w *Wallit) GrabAll() error

GrabAll makes first-party justice txs.

func (*Wallit) HeightHandler

func (w *Wallit) HeightHandler(incomingHeight chan int32)

func (*Wallit) Ingest

func (w *Wallit) Ingest(tx *wire.MsgTx, height int32) (uint32, error)

Ingest -- take in a tx from the ChainHook

func (*Wallit) IngestMany

func (w *Wallit) IngestMany(txs []*wire.MsgTx, height int32) (uint32, error)

This should always work; there shouldn't be false positives getting to here, as those should be handled on the ChainHook level. IngestMany can probably work OK even if the txs are out of order. But don't do that, that's weird and untested. also it'll error if you give it more than 1M txs, so don't.

func (*Wallit) LetMeKnow

func (w *Wallit) LetMeKnow() chan lnutil.OutPointEvent

func (*Wallit) LetMeKnowHeight

func (w *Wallit) LetMeKnowHeight() chan lnutil.HeightEvent

func (*Wallit) MaybeSend

func (w *Wallit) MaybeSend(txos []*wire.TxOut, ow bool) ([]*wire.OutPoint, error)

Build a tx, kindof like with SendCoins, but don't sign or broadcast. Segwit inputs only. Freeze the utxos used so the tx can be signed and broadcast later. Use only segwit utxos. Return the txid, and indexes of where the txouts in the argument slice ended up in the final tx. Bunch of redundancy with SendMany, maybe move that to a shared function... NOTE this does not support multiple txouts with identical pkscripts in one tx. The code would be trivial; it's not supported on purpose. Use unique pkscripts.

func (*Wallit) NahDontSend

func (w *Wallit) NahDontSend(txid *chainhash.Hash) error

Cancel the hold on a tx previously built with MaybeSend. Clears freeze on utxos so they can be used somewhere else.

func (*Wallit) NewAdr

func (w *Wallit) NewAdr() ([20]byte, error)

func (*Wallit) NewAdr160

func (w *Wallit) NewAdr160() ([20]byte, error)

NewAdr creates a new, never before seen address, and increments the DB counter, and returns the hash160 of the pubkey.

func (*Wallit) NewChangeOut

func (w *Wallit) NewChangeOut(amt int64) (*wire.TxOut, error)

make a new change output. I guess this is supposed to be on a different branch than regular addresses...

func (*Wallit) NewOutgoingTx

func (w *Wallit) NewOutgoingTx(tx *wire.MsgTx) error

NewOutgoingTx runs a tx though the db first, then sends it out to the network.

func (*Wallit) OpenDB

func (w *Wallit) OpenDB(filename string) error

OpenDB starts up the database. Creates the file if it doesn't exist.

func (*Wallit) Params

func (w *Wallit) Params() *coinparam.Params

func (*Wallit) PathPrivkey

func (w *Wallit) PathPrivkey(kg portxo.KeyGen) *koblitz.PrivateKey

PathPrivkey returns a private key by descending the given path Returns nil if there's an error.

func (*Wallit) PathPubHash160

func (w *Wallit) PathPubHash160(kg portxo.KeyGen) [20]byte

PathPubHash160 returns a 20 byte pubkey hash for the given path It'll always return 20 bytes, or a nil if there's an error.

func (*Wallit) PathPubkey

func (w *Wallit) PathPubkey(kg portxo.KeyGen) *koblitz.PublicKey

PathPubkey returns a public key by descending the given path. Returns nil if there's an error.

func (*Wallit) PickUtxos

func (w *Wallit) PickUtxos(
	amtWanted, outputByteSize, feePerByte int64,
	ow bool) (portxo.TxoSliceByBip69, int64, error)

PickUtxos Picks Utxos for spending. Tell it how much money you want. It returns a tx-sortable utxoslice, and the overshoot amount. Also errors. if "ow" is true, only gives witness utxos (for channel funding) The overshoot amount is *after* fees, so can be used directly for a change output.

func (*Wallit) PushTx

func (w *Wallit) PushTx(tx *wire.MsgTx) error

func (*Wallit) ReallySend

func (w *Wallit) ReallySend(txid *chainhash.Hash) error

Sign and broadcast a tx previously built with MaybeSend. This clears the freeze on the utxos but they're not utxos anymore anyway.

func (*Wallit) RegisterWatchOP

func (w *Wallit) RegisterWatchOP(op wire.OutPoint) error

RegisterWatchOP registers an outpoint to watch. Called from ReallySend()

func (*Wallit) RollBack

func (w *Wallit) RollBack(rollHeight int32) error

Rollback rewinds the wallet state to a previous height. It removes new UTXOs

func (*Wallit) SaveTx

func (w *Wallit) SaveTx(tx *wire.MsgTx) error

SaveTx unconditionally saves a tx in the DB, usually for sending out to nodes

func (*Wallit) SendOne

func (w *Wallit) SendOne(u portxo.PorTxo, outScript []byte) (*wire.MsgTx, error)

SendOne is for the sweep function, and doesn't do change. Probably can get rid of this for real txs.

func (*Wallit) SetDBSyncHeight

func (w *Wallit) SetDBSyncHeight(n int32) error

SetDBSyncHeight sets sync height of the db, indicated the latest block of which it has ingested all the transactions.

func (*Wallit) SetFee

func (w *Wallit) SetFee(set int64) int64

func (*Wallit) SignMyInputs

func (w *Wallit) SignMyInputs(tx *wire.MsgTx) error

SignMyInputs finds the inputs in a transaction that came from our own wallet, and signs them with our private keys. Will modify the transaction in place, but will ignore inputs that we can't sign and leave them unsigned.

func (*Wallit) StopWatchingThis

func (w *Wallit) StopWatchingThis(op wire.OutPoint) error

StopWatchingThis removes an outpoint to watch.

func (*Wallit) Sweep

func (w *Wallit) Sweep(outScript []byte, n uint32) ([]*chainhash.Hash, error)

********* sweep is for testing / spamming, remove for real use

func (*Wallit) TxHandler

func (w *Wallit) TxHandler(incomingTxAndHeight chan lnutil.TxAndHeight)

TxHandler is the goroutine that receives & ingests new txs for the wallit.

func (*Wallit) UnregisterWatchOP

func (w *Wallit) UnregisterWatchOP(op wire.OutPoint) error

UnregisterWatchOP unregisters an outpoint to watch. Used to remove watched HTLC OPs if we claim them ourselves.

func (*Wallit) UtxoDump

func (w *Wallit) UtxoDump() ([]*portxo.PorTxo, error)

func (*Wallit) WatchThis

func (w *Wallit) WatchThis(op wire.OutPoint) error

WatchThis registers an outpoint to watch. Register as watched OP, and passes to chainhook.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL