Class Wallet
-
- All Implemented Interfaces:
-
java.lang.AutoCloseable
,org.bitcoindevkit.Disposable
,org.bitcoindevkit.WalletInterface
public class Wallet implements Disposable, AutoCloseable, WalletInterface
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public class
Wallet.Companion
-
Field Summary
Fields Modifier and Type Field Description public final static Wallet.Companion
Companion
-
Constructor Summary
Constructors Constructor Description Wallet(Pointer pointer)
Wallet(NoPointer noPointer)
This constructor can be used to instantiate a fake object. Wallet(Descriptor descriptor, Descriptor changeDescriptor, Network network, Connection connection)
-
Method Summary
Modifier and Type Method Description Unit
destroy()
Unit
close()
final Pointer
uniffiClonePointer()
Unit
applyUnconfirmedTxs(List<UnconfirmedTx> unconfirmedTxs)
Apply relevant unconfirmed transactions to the wallet. Unit
applyUpdate(Update update)
Applies an update to the wallet and stages the changes (but does not persist them). Balance
balance()
Return the balance, separated into available, trusted-pending, untrusted-pending and immature values. Amount
calculateFee(Transaction tx)
Calculates the fee of a given transaction. FeeRate
calculateFeeRate(Transaction tx)
Calculate the `FeeRate` for a given transaction. Unit
cancelTx(Transaction tx)
Informs the wallet that you no longer intend to broadcast a tx that was built from it. UInt
derivationIndex(KeychainKind keychain)
The derivation index of this wallet. KeychainAndIndex
derivationOfSpk(Script spk)
Finds how the wallet derived the script pubkey spk
.String
descriptorChecksum(KeychainKind keychain)
Return the checksum of the public descriptor associated to keychain
Internally calls `Self::public_descriptor` to fetch the right descriptorBoolean
finalizePsbt(Psbt psbt, SignOptions signOptions)
Finalize a PSBT, i.e. CanonicalTx
getTx(String txid)
Get a single transaction from the wallet as a `WalletTx` (if the transaction exists). LocalOutput
getUtxo(OutPoint op)
Returns the utxo owned by this wallet corresponding to outpoint
if it exists in the wallet's database.Boolean
isMine(Script script)
Return whether or not a script
is part of this wallet (either internal or external)List<LocalOutput>
listOutput()
List all relevant outputs (includes both spent and unspent, confirmed and unconfirmed). List<LocalOutput>
listUnspent()
Return the list of unspent outputs of this wallet List<AddressInfo>
listUnusedAddresses(KeychainKind keychain)
List addresses that are revealed but unused. Boolean
markUsed(KeychainKind keychain, UInt index)
Marks an address used of the given keychain
atindex
.Network
network()
Get the Bitcoin network the wallet is using. UInt
nextDerivationIndex(KeychainKind keychain)
The index of the next address that you would get if you were to ask the wallet for a new address AddressInfo
nextUnusedAddress(KeychainKind keychain)
Get the next unused address for the given keychain
, i.e.AddressInfo
peekAddress(KeychainKind keychain, UInt index)
Peek an address of the given keychain
atindex
without revealing it.Boolean
persist(Connection connection)
Policy
policies(KeychainKind keychain)
List<AddressInfo>
revealAddressesTo(KeychainKind keychain, UInt index)
Reveal addresses up to and including the target index
and return an iterator of newly revealed addresses.AddressInfo
revealNextAddress(KeychainKind keychain)
Attempt to reveal the next address of the given keychain
.SentAndReceivedValues
sentAndReceived(Transaction tx)
Compute the tx
's sent and received `Amount`s.Boolean
sign(Psbt psbt, SignOptions signOptions)
Sign a transaction with all the wallet's signers, in the order specified by every signer's `SignerOrdering`. FullScanRequestBuilder
startFullScan()
Create a `FullScanRequest for this wallet. SyncRequestBuilder
startSyncWithRevealedSpks()
Create a partial `SyncRequest` for this wallet for all revealed spks. List<CanonicalTx>
transactions()
Iterate over the transactions in the wallet.
-
-
-
Constructor Detail
-
Wallet
Wallet(Pointer pointer)
-
Wallet
Wallet(NoPointer noPointer)
This constructor can be used to instantiate a fake object.
-
Wallet
Wallet(Descriptor descriptor, Descriptor changeDescriptor, Network network, Connection connection)
-
-
Method Detail
-
uniffiClonePointer
final Pointer uniffiClonePointer()
-
applyUnconfirmedTxs
Unit applyUnconfirmedTxs(List<UnconfirmedTx> unconfirmedTxs)
Apply relevant unconfirmed transactions to the wallet. Transactions that are not relevant are filtered out.
-
applyUpdate
Unit applyUpdate(Update update)
Applies an update to the wallet and stages the changes (but does not persist them).
Usually you create an
update
by interacting with some blockchain data source and inserting transactions related to your wallet into it.After applying updates you should persist the staged wallet changes. For an example of how to persist staged wallet changes see `Wallet::reveal_next_address`.
-
balance
Balance balance()
Return the balance, separated into available, trusted-pending, untrusted-pending and immature values.
-
calculateFee
Amount calculateFee(Transaction tx)
Calculates the fee of a given transaction. Returns `Amount::ZERO` if
tx
is a coinbase transaction.To calculate the fee for a `Transaction` with inputs not owned by this wallet you must manually insert the TxOut(s) into the tx graph using the `insert_txout` function.
Note
tx
does not have to be in the graph for this to work.
-
calculateFeeRate
FeeRate calculateFeeRate(Transaction tx)
Calculate the `FeeRate` for a given transaction.
To calculate the fee rate for a `Transaction` with inputs not owned by this wallet you must manually insert the TxOut(s) into the tx graph using the `insert_txout` function.
Note
tx
does not have to be in the graph for this to work.
-
cancelTx
Unit cancelTx(Transaction tx)
Informs the wallet that you no longer intend to broadcast a tx that was built from it.
This frees up the change address used when creating the tx for use in future transactions.
-
derivationIndex
UInt derivationIndex(KeychainKind keychain)
The derivation index of this wallet. It will return
None
if it has not derived any addresses. Otherwise, it will return the index of the highest address it has derived.
-
derivationOfSpk
KeychainAndIndex derivationOfSpk(Script spk)
Finds how the wallet derived the script pubkey
spk
.Will only return
Some(_)
if the wallet has given out the spk.
-
descriptorChecksum
String descriptorChecksum(KeychainKind keychain)
Return the checksum of the public descriptor associated to
keychain
Internally calls `Self::public_descriptor` to fetch the right descriptor
-
finalizePsbt
Boolean finalizePsbt(Psbt psbt, SignOptions signOptions)
Finalize a PSBT, i.e., for each input determine if sufficient data is available to pass validation and construct the respective
scriptSig
orscriptWitness
. Please refer to BIP174, and BIP371 for further information.Returns
true
if the PSBT could be finalized, andfalse
otherwise.The `SignOptions` can be used to tweak the behavior of the finalizer.
-
getTx
CanonicalTx getTx(String txid)
Get a single transaction from the wallet as a `WalletTx` (if the transaction exists).
WalletTx
contains the full transaction alongside meta-data such as:Blocks that the transaction is `Anchor`ed in. These may or may not be blocks that exist in the best chain.
The `ChainPosition` of the transaction in the best chain - whether the transaction is confirmed or unconfirmed. If the transaction is confirmed, the anchor which proves the confirmation is provided. If the transaction is unconfirmed, the unix timestamp of when the transaction was last seen in the mempool is provided.
-
getUtxo
LocalOutput getUtxo(OutPoint op)
Returns the utxo owned by this wallet corresponding to
outpoint
if it exists in the wallet's database.
-
isMine
Boolean isMine(Script script)
Return whether or not a
script
is part of this wallet (either internal or external)
-
listOutput
List<LocalOutput> listOutput()
List all relevant outputs (includes both spent and unspent, confirmed and unconfirmed).
To list only unspent outputs (UTXOs), use `Wallet::list_unspent` instead.
-
listUnspent
List<LocalOutput> listUnspent()
Return the list of unspent outputs of this wallet
-
listUnusedAddresses
List<AddressInfo> listUnusedAddresses(KeychainKind keychain)
List addresses that are revealed but unused.
Note if the returned iterator is empty you can reveal more addresses by using Self::reveal_next_address or Self::reveal_addresses_to.
-
markUsed
Boolean markUsed(KeychainKind keychain, UInt index)
Marks an address used of the given
keychain
atindex
.Returns whether the given index was present and then removed from the unused set.
-
nextDerivationIndex
UInt nextDerivationIndex(KeychainKind keychain)
The index of the next address that you would get if you were to ask the wallet for a new address
-
nextUnusedAddress
AddressInfo nextUnusedAddress(KeychainKind keychain)
Get the next unused address for the given
keychain
, i.e. the address with the lowest derivation index that hasn't been used in a transaction.This will attempt to reveal a new address if all previously revealed addresses have been used, in which case the returned address will be the same as calling `Wallet::reveal_next_address`.
WARNING: To avoid address reuse you must persist the changes resulting from one or more calls to this method before closing the wallet. See `Wallet::reveal_next_address`.
-
peekAddress
AddressInfo peekAddress(KeychainKind keychain, UInt index)
Peek an address of the given
keychain
atindex
without revealing it.For non-wildcard descriptors this returns the same address at every provided index.
This panics when the caller requests for an address of derivation index greater than the BIP32 max index.
-
persist
Boolean persist(Connection connection)
-
policies
Policy policies(KeychainKind keychain)
-
revealAddressesTo
List<AddressInfo> revealAddressesTo(KeychainKind keychain, UInt index)
Reveal addresses up to and including the target
index
and return an iterator of newly revealed addresses.If the target
index
is unreachable, we make a best effort to reveal up to the last possible index. If all addresses up to the givenindex
are already revealed, then no new addresses are returned.WARNING: To avoid address reuse you must persist the changes resulting from one or more calls to this method before closing the wallet. See `Wallet::reveal_next_address`.
-
revealNextAddress
AddressInfo revealNextAddress(KeychainKind keychain)
Attempt to reveal the next address of the given
keychain
.This will increment the keychain's derivation index. If the keychain's descriptor doesn't contain a wildcard or every address is already revealed up to the maximum derivation index defined in BIP32, then the last revealed address will be returned.
-
sentAndReceived
SentAndReceivedValues sentAndReceived(Transaction tx)
Compute the
tx
's sent and received `Amount`s.This method returns a tuple
(sent, received)
. Sent is the sum of the txin amounts that spend from previous txouts tracked by this wallet. Received is the summation of this tx's outputs that send to script pubkeys tracked by this wallet.
-
sign
Boolean sign(Psbt psbt, SignOptions signOptions)
Sign a transaction with all the wallet's signers, in the order specified by every signer's `SignerOrdering`. This function returns the
Result
type with an encapsulatedbool
that has the value true if the PSBT was finalized, or false otherwise.The `SignOptions` can be used to tweak the behavior of the software signers, and the way the transaction is finalized at the end. Note that it can't be guaranteed that every signers will follow the options, but the "software signers" (WIF keys and
xprv
) defined in this library will.
-
startFullScan
FullScanRequestBuilder startFullScan()
Create a `FullScanRequest for this wallet.
This is the first step when performing a spk-based wallet full scan, the returned `FullScanRequest collects iterators for the wallet's keychain script pub keys needed to start a blockchain full scan with a spk based blockchain client.
This operation is generally only used when importing or restoring a previously used wallet in which the list of used scripts is not known.
-
startSyncWithRevealedSpks
SyncRequestBuilder startSyncWithRevealedSpks()
Create a partial `SyncRequest` for this wallet for all revealed spks.
This is the first step when performing a spk-based wallet partial sync, the returned `SyncRequest` collects all revealed script pubkeys from the wallet keychain needed to start a blockchain sync with a spk based blockchain client.
-
transactions
List<CanonicalTx> transactions()
Iterate over the transactions in the wallet.
-
-