dcr

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2022 License: BlueOak-1.0.0 Imports: 55 Imported by: 9

Documentation

Index

Constants

View Source
const (

	// BipID is the BIP-0044 asset ID.
	BipID = 42
)

Variables

View Source
var (
	// ContractSearchLimit is how far back in time AuditContract in SPV mode
	// will search for a contract if no txData is provided. This should be a
	// positive duration.
	ContractSearchLimit = 48 * time.Hour

	// WalletInfo defines some general information about a Decred wallet.
	WalletInfo = &asset.WalletInfo{
		Name:     "Decred",
		Version:  version,
		UnitInfo: dexdcr.UnitInfo,
		AvailableWallets: []*asset.WalletDefinition{
			{
				Type:        walletTypeSPV,
				Tab:         "Native",
				Description: "Use the built-in SPV wallet",
				ConfigOpts:  append(spvOpts, walletOpts...),
				Seeded:      true,
			},
			{
				Type:              walletTypeDcrwRPC,
				Tab:               "External",
				Description:       "Connect to dcrwallet",
				DefaultConfigPath: defaultConfigPath,
				ConfigOpts:        append(rpcOpts, walletOpts...),
			},
		},
	}
)

Functions

func RegisterCustomWallet added in v0.4.0

func RegisterCustomWallet(constructor WalletConstructor, def *asset.WalletDefinition) error

RegisterCustomWallet registers a function that should be used in creating a Wallet implementation that the ExchangeWallet of the specified type will use in place of the default rpcWallet implementation. External consumers can use this function to provide alternative Wallet implementations, and must do so before attempting to create an ExchangeWallet instance of this type.

Types

type AddressInfo added in v0.5.0

type AddressInfo struct {
	Account string
	Branch  uint32
}

AddressInfo is the source account and branch info for an address.

type BlockHeader added in v0.5.0

type BlockHeader struct {
	*wire.BlockHeader
	MedianTime    int64
	Confirmations int64
	NextHash      *chainhash.Hash
}

BlockHeader is a wire.BlockHeader with the addition of a MedianTime field. Implementations must fill in the MedianTime field when returning a BlockHeader.

type Driver

type Driver struct{}

Driver implements asset.Driver.

func (*Driver) Create added in v0.5.0

func (d *Driver) Create(params *asset.CreateWalletParams) error

Create creates a new SPV wallet.

func (*Driver) DecodeCoinID

func (d *Driver) DecodeCoinID(coinID []byte) (string, error)

DecodeCoinID creates a human-readable representation of a coin ID for Decred.

func (*Driver) Exists added in v0.5.0

func (d *Driver) Exists(walletType, dataDir string, _ map[string]string, net dex.Network) (bool, error)

Exists checks the existence of the wallet. Part of the Creator interface.

func (*Driver) Info

func (d *Driver) Info() *asset.WalletInfo

Info returns basic information about the wallet and asset.

func (*Driver) Open added in v0.4.0

func (d *Driver) Open(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error)

Open creates the DCR exchange wallet. Start the wallet with its Run method.

type ExchangeWallet

type ExchangeWallet struct {
	// contains filtered or unexported fields
}

ExchangeWallet is a wallet backend for Decred. The backend is how the DEX client app communicates with the Decred blockchain and wallet. ExchangeWallet satisfies the dex.Wallet interface.

func NewWallet

func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (*ExchangeWallet, error)

NewWallet is the exported constructor by which the DEX will import the exchange wallet.

func (*ExchangeWallet) AuditContract

func (dcr *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, rebroadcast bool) (*asset.AuditInfo, error)

AuditContract retrieves information about a swap contract from the provided txData if it represents a valid transaction that pays to the contract at the specified coinID. The txData may be empty to attempt retrieval of the transaction output from the network, but it is only ensured to succeed for a full node or, if the tx is confirmed, an SPV wallet. Normally the server should communicate this txData, and the caller can decide to require it. The ability to work with an empty txData is a convenience for recovery tools and testing, and it may change in the future if a GetTxData method is added for this purpose. Optionally, attempt is also made to broadcasted the txData to the blockchain network but it is not necessary that the broadcast succeeds since the contract may have already been broadcasted.

func (*ExchangeWallet) Balance

func (dcr *ExchangeWallet) Balance() (*asset.Balance, error)

Balance should return the total available funds in the wallet. Note that after calling Fund, the amount returned by Balance may change by more than the value funded. Part of the asset.Wallet interface. TODO: Since this includes potentially untrusted 0-conf utxos, consider prioritizing confirmed utxos when funding an order.

func (*ExchangeWallet) Connect

func (dcr *ExchangeWallet) Connect(ctx context.Context) (*sync.WaitGroup, error)

Connect connects the wallet to the RPC server. Satisfies the dex.Connector interface.

func (*ExchangeWallet) DepositAddress added in v0.5.0

func (dcr *ExchangeWallet) DepositAddress() (string, error)

DepositAddress returns an address for depositing funds into the exchange wallet.

func (*ExchangeWallet) EstimateRegistrationTxFee added in v0.4.3

func (dcr *ExchangeWallet) EstimateRegistrationTxFee(feeRate uint64) uint64

EstimateRegistrationTxFee returns an estimate for the tx fee needed to pay the registration fee using the provided feeRate.

func (*ExchangeWallet) FeeRate added in v0.4.3

func (dcr *ExchangeWallet) FeeRate() uint64

FeeRate satisfies asset.FeeRater.

func (*ExchangeWallet) FindRedemption

func (dcr *ExchangeWallet) FindRedemption(ctx context.Context, coinID, _ dex.Bytes) (redemptionCoin, secret dex.Bytes, err error)

FindRedemption watches for the input that spends the specified contract coin, and returns the spending input and the contract's secret key when it finds a spender.

This method blocks until the redemption is found, an error occurs or the provided context is canceled.

func (*ExchangeWallet) FundOrder

func (dcr *ExchangeWallet) FundOrder(ord *asset.Order) (asset.Coins, []dex.Bytes, error)

FundOrder selects coins for use in an order. The coins will be locked, and will not be returned in subsequent calls to FundOrder or calculated in calls to Available, unless they are unlocked with ReturnCoins. The returned []dex.Bytes contains the redeem scripts for the selected coins. Equal number of coins and redeemed scripts must be returned. A nil or empty dex.Bytes should be appended to the redeem scripts collection for coins with no redeem script.

func (*ExchangeWallet) FundingCoins

func (dcr *ExchangeWallet) FundingCoins(ids []dex.Bytes) (asset.Coins, error)

FundingCoins gets funding coins for the coin IDs. The coins are locked. This method might be called to reinitialize an order from data stored externally. This method will only return funding coins, e.g. unspent transaction outputs.

func (*ExchangeWallet) Info

func (dcr *ExchangeWallet) Info() *asset.WalletInfo

Info returns basic information about the wallet and asset.

func (*ExchangeWallet) Lock

func (dcr *ExchangeWallet) Lock() error

Lock locks the exchange wallet.

func (*ExchangeWallet) Locked

func (dcr *ExchangeWallet) Locked() bool

Locked will be true if the wallet is currently locked. Q: why are we ignoring RPC errors in this?

func (*ExchangeWallet) LocktimeExpired

func (dcr *ExchangeWallet) LocktimeExpired(ctx context.Context, contract dex.Bytes) (bool, time.Time, error)

LocktimeExpired returns true if the specified contract's locktime has expired, making it possible to issue a Refund.

func (*ExchangeWallet) MaxOrder added in v0.2.0

func (dcr *ExchangeWallet) MaxOrder(ord *asset.MaxOrderForm) (*asset.SwapEstimate, error)

MaxOrder generates information about the maximum order size and associated fees that the wallet can support for the given DEX configuration. The provided FeeSuggestion is used directly, and should be an estimate based on current network conditions. For quote assets, the caller will have to calculate lotSize based on a rate conversion from the base asset's lot size. lotSize must not be zero and will cause a panic if so.

func (*ExchangeWallet) NewAddress added in v0.4.1

func (dcr *ExchangeWallet) NewAddress() (string, error)

NewAddress returns a new address from the wallet. This satisfies the NewAddresser interface.

func (*ExchangeWallet) OwnsDepositAddress added in v0.5.0

func (dcr *ExchangeWallet) OwnsDepositAddress(address string) (bool, error)

OwnsDepositAddress indicates if the provided address can be used to deposit funds into the wallet.

func (*ExchangeWallet) PreRedeem added in v0.2.0

func (dcr *ExchangeWallet) PreRedeem(req *asset.PreRedeemForm) (*asset.PreRedeem, error)

PreRedeem generates an estimate of the range of redemption fees that could be assessed.

func (*ExchangeWallet) PreSwap added in v0.2.0

func (dcr *ExchangeWallet) PreSwap(req *asset.PreSwapForm) (*asset.PreSwap, error)

PreSwap get order estimates based on the available funds and the wallet configuration.

func (*ExchangeWallet) Reconfigure added in v0.5.3

func (dcr *ExchangeWallet) Reconfigure(ctx context.Context, cfg *asset.WalletConfig, currentAddress string) (restart bool, err error)

Reconfigure attempts to reconfigure the wallet.

func (*ExchangeWallet) Redeem

func (dcr *ExchangeWallet) Redeem(form *asset.RedeemForm) ([]dex.Bytes, asset.Coin, uint64, error)

Redeem sends the redemption transaction, which may contain more than one redemption. FeeSuggestion is just a fallback if an internal estimate using the wallet's redeem confirm block target setting is not available.

func (*ExchangeWallet) RedemptionAddress added in v0.5.0

func (dcr *ExchangeWallet) RedemptionAddress() (string, error)

RedemptionAddress gets an address for use in redeeming the counterparty's swap. This would be included in their swap initialization.

func (*ExchangeWallet) Refund

func (dcr *ExchangeWallet) Refund(coinID, contract dex.Bytes, feeRate uint64) (dex.Bytes, error)

Refund refunds a contract. This can only be used after the time lock has expired. This MUST return an asset.CoinNotFoundError error if the coin is spent. If the provided fee rate is zero, an internal estimate will be used, otherwise it will be used directly, but this behavior may change. NOTE: The contract cannot be retrieved from the unspent coin info as the wallet does not store it, even though it was known when the init transaction was created. The client should store this information for persistence across sessions.

func (*ExchangeWallet) RegFeeConfirmations added in v0.4.0

func (dcr *ExchangeWallet) RegFeeConfirmations(ctx context.Context, coinID dex.Bytes) (confs uint32, err error)

RegFeeConfirmations gets the number of confirmations for the specified output.

func (*ExchangeWallet) ReturnCoins

func (dcr *ExchangeWallet) ReturnCoins(unspents asset.Coins) error

ReturnCoins unlocks coins. This would be necessary in the case of a canceled order. Coins belonging to the tradingAcct, if configured, are transferred to the unmixed account with the exception of unspent split tx outputs which are kept in the tradingAcct and may later be used to fund future orders.

func (*ExchangeWallet) Send added in v0.5.0

func (dcr *ExchangeWallet) Send(address string, value, feeRate uint64) (asset.Coin, error)

Send sends the exact value to the specified address. This is different from Withdraw, which subtracts the tx fees from the amount sent. feeRate is in units of atoms/byte.

func (*ExchangeWallet) SignMessage

func (dcr *ExchangeWallet) SignMessage(coin asset.Coin, msg dex.Bytes) (pubkeys, sigs []dex.Bytes, err error)

SignMessage signs the message with the private key associated with the specified funding Coin. A slice of pubkeys required to spend the Coin and a signature for each pubkey are returned.

func (*ExchangeWallet) Swap

func (dcr *ExchangeWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uint64, error)

Swap sends the swaps in a single transaction. The Receipts returned can be used to refund a failed transaction. The Input coins are manually unlocked because they're not auto-unlocked by the wallet and therefore inaccurately included as part of the locked balance despite being spent.

func (*ExchangeWallet) SwapConfirmations added in v0.4.0

func (dcr *ExchangeWallet) SwapConfirmations(ctx context.Context, coinID, contract dex.Bytes, matchTime time.Time) (confs uint32, spent bool, err error)

SwapConfirmations gets the number of confirmations and the spend status for the specified swap. The contract and matchTime are provided so that wallets may search for the coin using light filters.

For a non-SPV wallet, if the swap appears spent but it cannot be located in a block with a cfilters scan, this will return asset.CoinNotFoundError. For SPV wallets, it is not an error if the transaction cannot be located SPV wallets cannot see non-wallet transactions until they are mined.

If the coin is located, but recognized as spent, no error is returned.

func (*ExchangeWallet) SyncStatus

func (dcr *ExchangeWallet) SyncStatus() (bool, float32, error)

SyncStatus is information about the blockchain sync status.

func (*ExchangeWallet) Unlock

func (dcr *ExchangeWallet) Unlock(pw []byte) error

Unlock unlocks the exchange wallet.

func (*ExchangeWallet) ValidateSecret

func (dcr *ExchangeWallet) ValidateSecret(secret, secretHash []byte) bool

ValidateSecret checks that the secret satisfies the contract.

func (*ExchangeWallet) Withdraw

func (dcr *ExchangeWallet) Withdraw(address string, value, feeRate uint64) (asset.Coin, error)

Withdraw withdraws funds to the specified address. Fees are subtracted from the value. feeRate is in units of atoms/byte. Withdraw satisfies asset.Withdrawer.

type FeeRateEstimator added in v0.5.0

type FeeRateEstimator interface {
	// EstimateSmartFeeRate returns a smart feerate estimate.
	EstimateSmartFeeRate(ctx context.Context, confTarget int64, mode chainjson.EstimateSmartFeeMode) (float64, error)
}

FeeRateEstimator is satisfied by a Wallet that can provide fee rate estimates.

type Mempooler added in v0.5.0

type Mempooler interface {
	// GetRawMempool returns hashes for all txs in a node's mempool.
	GetRawMempool(ctx context.Context) ([]*chainhash.Hash, error)
}

Mempooler is satisfied by a Wallet that can provide mempool info.

type RecoveryCfg added in v0.5.0

type RecoveryCfg struct {
	NumExternalAddresses uint32 `ini:"numexternaladdr"`
	NumInternalAddresses uint32 `ini:"numinternaladdr"`
}

RecoveryCfg is the information that is transferred from the old wallet to the new one when the wallet is recovered.

type TipChangeCallback added in v0.4.0

type TipChangeCallback func(context.Context, *chainhash.Hash, int64, error)

type TxOutput added in v0.4.0

type TxOutput struct {
	*wire.TxOut
	Tree          int8
	Addresses     []string
	Confirmations uint32
}

TxOutput defines properties of a transaction output, including the details of the block containing the tx, if mined.

type Wallet added in v0.4.0

type Wallet interface {
	// Connect establishes a connection to the wallet.
	Connect(ctx context.Context) error
	//  Disconnect shuts down access to the wallet.
	Disconnect()
	// SpvMode returns true if the wallet is connected to the Decred
	// network via SPV peers.
	SpvMode() bool
	// NotifyOnTipChange registers a callback function that the should be
	// invoked when the wallet sees new mainchain blocks. The return value
	// indicates if this notification can be provided. Where this tip change
	// notification is unimplemented, monitorBlocks should be used to track
	// tip changes.
	NotifyOnTipChange(ctx context.Context, cb TipChangeCallback) bool
	// AddressInfo returns information for the provided address. It is an error
	// if the address is not owned by the wallet.
	AddressInfo(ctx context.Context, address string) (*AddressInfo, error)
	// AccountOwnsAddress checks if the provided address belongs to the
	// specified account.
	AccountOwnsAddress(ctx context.Context, addr stdaddr.Address, acctName string) (bool, error)
	// AccountBalance returns the balance breakdown for the specified account.
	AccountBalance(ctx context.Context, confirms int32, acctName string) (*walletjson.GetAccountBalanceResult, error)
	// LockedOutputs fetches locked outputs for the Wallet.
	LockedOutputs(ctx context.Context, acctName string) ([]chainjson.TransactionInput, error)
	// Unspents fetches unspent outputs for the Wallet.
	Unspents(ctx context.Context, acctName string) ([]*walletjson.ListUnspentResult, error)
	// LockUnspent locks or unlocks the specified outpoint.
	LockUnspent(ctx context.Context, unlock bool, ops []*wire.OutPoint) error
	// UnspentOutput returns information about an unspent tx output, if found
	// and unspent. Use wire.TxTreeUnknown if the output tree is unknown, the
	// correct tree will be returned if the unspent output is found.
	// This method is only guaranteed to return results for outputs that pay to
	// the wallet, although wallets connected to a full node may return results
	// for non-wallet outputs. Returns asset.CoinNotFoundError if the unspent
	// output cannot be located.
	UnspentOutput(ctx context.Context, txHash *chainhash.Hash, index uint32, tree int8) (*TxOutput, error)
	// ExternalAddress returns a new external address.
	ExternalAddress(ctx context.Context, acctName string) (stdaddr.Address, error)
	// InternalAddress returns a change address from the Wallet.
	InternalAddress(ctx context.Context, acctName string) (stdaddr.Address, error)
	// SignRawTransaction signs the provided transaction. SignRawTransaction
	// is not used for redemptions, so previous outpoints and scripts should
	// be known by the wallet.
	// SignRawTransaction should not mutate the input transaction.
	SignRawTransaction(context.Context, *wire.MsgTx) (*wire.MsgTx, error)
	// SendRawTransaction broadcasts the provided transaction to the Decred
	// network.
	SendRawTransaction(ctx context.Context, tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error)
	// GetBlockHeader generates a *BlockHeader for the specified block hash. The
	// returned block header is a wire.BlockHeader with the addition of the
	// block's median time.
	GetBlockHeader(ctx context.Context, blockHash *chainhash.Hash) (*BlockHeader, error)
	// GetBlock returns the *wire.MsgBlock.
	GetBlock(ctx context.Context, blockHash *chainhash.Hash) (*wire.MsgBlock, error)
	// GetTransaction returns the details of a wallet tx, if the wallet contains a
	// tx with the provided hash. Returns asset.CoinNotFoundError if the tx is not
	// found in the wallet.
	GetTransaction(ctx context.Context, txHash *chainhash.Hash) (*WalletTransaction, error)
	// GetRawTransaction returns details of the tx with the provided hash.
	// Returns asset.CoinNotFoundError if the tx is not found.
	GetRawTransaction(ctx context.Context, txHash *chainhash.Hash) (*wire.MsgTx, error)
	// GetBestBlock returns the hash and height of the wallet's best block.
	GetBestBlock(ctx context.Context) (*chainhash.Hash, int64, error)
	// GetBlockHash returns the hash of the mainchain block at the specified height.
	GetBlockHash(ctx context.Context, blockHeight int64) (*chainhash.Hash, error)
	// MatchAnyScript looks for any of the provided scripts in the block specified.
	MatchAnyScript(ctx context.Context, blockHash *chainhash.Hash, scripts [][]byte) (bool, error)
	// AccountUnlocked returns true if the account is unlocked.
	AccountUnlocked(ctx context.Context, acctName string) (bool, error)
	// LockAccount locks the account.
	LockAccount(ctx context.Context, acctName string) error
	// UnlockAccount unlocks the account.
	UnlockAccount(ctx context.Context, passphrase []byte, acctName string) error
	// SyncStatus returns the wallet's sync status.
	SyncStatus(ctx context.Context) (bool, float32, error)
	// PeerCount returns the number of network peers to which the wallet or its
	// backing node are connected.
	PeerCount(ctx context.Context) (uint32, error)
	// AddressPrivKey fetches the privkey for the specified address.
	AddressPrivKey(ctx context.Context, address stdaddr.Address) (*secp256k1.PrivateKey, error)

	Reconfigure(ctx context.Context, cfg *asset.WalletConfig, net dex.Network, currentAddress, depositAccount string) (restart bool, err error)
}

Wallet defines methods that the ExchangeWallet uses for communicating with a Decred wallet and blockchain.

type WalletConstructor added in v0.4.0

type WalletConstructor func(settings map[string]string, chainParams *chaincfg.Params, logger dex.Logger) (Wallet, error)

WalletConstructor defines a function that can be invoked to create a custom implementation of the Wallet interface.

type WalletTransaction added in v0.5.0

type WalletTransaction struct {
	Confirmations int64
	BlockHash     string
	Details       []walletjson.GetTransactionDetailsResult
	Hex           string
}

WalletTransaction is a pared down version of walletjson.GetTransactionResult.

Jump to

Keyboard shortcuts

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