Version: v0.4.3 Latest Latest

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

Go to latest
Published: Apr 29, 2022 License: BlueOak-1.0.0 Imports: 42 Imported by: 3




View Source
const (

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


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:              walletTypeDcrwRPC,
			Tab:               "External",
			Description:       "Connect to dcrwallet",
			DefaultConfigPath: defaultConfigPath,
			ConfigOpts:        configOpts,


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.


type Config

type Config struct {
	Account          string  `ini:"account"`
	RPCUser          string  `ini:"username"`
	RPCPass          string  `ini:"password"`
	RPCListen        string  `ini:"rpclisten"`
	RPCCert          string  `ini:"rpccert"`
	UseSplitTx       bool    `ini:"txsplit"`
	FallbackFeeRate  float64 `ini:"fallbackfee"`
	FeeRateLimit     float64 `ini:"feeratelimit"`
	RedeemConfTarget uint64  `ini:"redeemconftarget"`
	// Context should be canceled when the application exits. This will cause
	// some cleanup to be performed during shutdown.
	Context context.Context `ini:"-"`

Config holds the parameters needed to initialize an RPC connection to a dcr wallet. Default values are used for RPCListen and/or RPCCert if not set.

type Driver

type Driver struct{}

Driver implements asset.Driver.

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) 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) Address

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

Address returns an address for 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) 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(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(lotSize, feeSuggestion uint64, nfo *dex.Asset) (*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 fees are an estimate based on current network conditions, and will be <= the fees associated with nfo.MaxFeeRate. 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) OwnsAddress

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

OwnsAddress indicates if an address belongs to the wallet.

func (*ExchangeWallet) PayFee

func (dcr *ExchangeWallet) PayFee(address string, regFee, feeRate uint64) (asset.Coin, error)

PayFee sends the dex registration fee. Transaction fees are in addition to the registration fee, and the fee rate is taken from the DEX configuration.

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) 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.

func (*ExchangeWallet) Refund

func (dcr *ExchangeWallet) Refund(coinID, contract dex.Bytes, feeSuggestion 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. 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.

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.

type TipChangeCallback added in v0.4.0

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

type TxOutput added in v0.4.0

type TxOutput struct {
	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.
	// Disconnected returns true if the wallet is not connected.
	Disconnected() bool
	// Network returns the network of the connected wallet.
	Network(ctx context.Context) (wire.CurrencyNet, error)
	// 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
	// AccountOwnsAddress checks if the provided address belongs to the
	// specified account.
	AccountOwnsAddress(ctx context.Context, account, address string) (bool, error)
	// AccountBalance returns the balance breakdown for the specified account.
	AccountBalance(ctx context.Context, account string, confirms int32) (*walletjson.GetAccountBalanceResult, error)
	// LockedOutputs fetches locked outputs for the specified account.
	LockedOutputs(ctx context.Context, account string) ([]chainjson.TransactionInput, error)
	// EstimateSmartFeeRate returns a smart feerate estimate.
	EstimateSmartFeeRate(ctx context.Context, confTarget int64, mode chainjson.EstimateSmartFeeMode) (float64, error)
	// Unspents fetches unspent outputs for the specified account.
	Unspents(ctx context.Context, account string) ([]walletjson.ListUnspentResult, error)
	// GetChangeAddress returns a change address from the specified account.
	GetChangeAddress(ctx context.Context, account string) (stdaddr.Address, 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)
	// GetNewAddressGapPolicy returns an address from the specified account using
	// the specified gap policy.
	GetNewAddressGapPolicy(ctx context.Context, account string, gap dcrwallet.GapPolicy) (stdaddr.Address, error)
	// SignRawTransaction signs the provided transaction.
	SignRawTransaction(ctx context.Context, txHex string) (*walletjson.SignRawTransactionResult, error)
	// SendRawTransaction broadcasts the provided transaction to the Decred
	// network.
	SendRawTransaction(ctx context.Context, tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error)
	// GetBlockHeaderVerbose returns block header info for the specified block hash.
	GetBlockHeaderVerbose(ctx context.Context, blockHash *chainhash.Hash) (*chainjson.GetBlockHeaderVerboseResult, error)
	// GetBlockVerbose returns information about a block, optionally including verbose
	// tx info.
	GetBlockVerbose(ctx context.Context, blockHash *chainhash.Hash, verboseTx bool) (*chainjson.GetBlockVerboseResult, 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) (*walletjson.GetTransactionResult, error)
	// GetRawTransactionVerbose returns details of the tx with the provided hash.
	// Returns asset.CoinNotFoundError if the tx is not found.
	GetRawTransactionVerbose(ctx context.Context, txHash *chainhash.Hash) (*chainjson.TxRawResult, error)
	// GetRawMempool returns hashes for all txs of the specified type in the node's
	// mempool.
	GetRawMempool(ctx context.Context, txType chainjson.GetRawMempoolTxTypeCmd) ([]*chainhash.Hash, 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)
	// BlockCFilter fetches the block filter info for the specified block.
	BlockCFilter(ctx context.Context, blockHash *chainhash.Hash) (filter, key string, err error)
	// LockWallet locks the wallet.
	LockWallet(ctx context.Context) error
	// UnlockWallet unlocks the wallet.
	UnlockWallet(ctx context.Context, passphrase string, timeoutSecs int64) error
	// WalletUnlocked returns true if the wallet is unlocked.
	WalletUnlocked(ctx context.Context) bool
	// AccountUnlocked returns true if the specified account is unlocked.
	AccountUnlocked(ctx context.Context, account string) (*walletjson.AccountUnlockedResult, error)
	// LockAccount locks the specified account.
	LockAccount(ctx context.Context, account string) error
	// UnlockAccount unlocks the specified account.
	UnlockAccount(ctx context.Context, account, passphrase 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) (*dcrutil.WIF, error)

Wallet defines methods that the ExchangeWallet uses for communicating with a Decred wallet and blockchain. TODO: Where possible, replace walletjson and chainjson return types with other types that define fewer fields e.g. *chainjson.TxRawResult with *wire.MsgTx.

type WalletConstructor added in v0.4.0

type WalletConstructor func(cfg *asset.WalletConfig, 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.

Jump to

Keyboard shortcuts

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