Wallet

abstract class Wallet(val chainSelector: ChainSelector)

This class defines a wallet interface. A wallet is a set of payment destinations that are being tracked on a particular blockchain.

Inheritors

Constructors

Link copied to clipboard
constructor(chainSelector: ChainSelector)

Types

Link copied to clipboard
object Companion
Link copied to clipboard
data class WalletStatistics(val numUnusedAddrs: Int, val numUsedAddrs: Int, val numUnspentTxos: Int, val totalTxos: Int, val numTransactions: Int, val firstReceiveHeight: Long, val lastReceiveHeight: Long, val firstSendHeight: Long, val lastSendHeight: Long, val txCacheSize: Int, val txoCacheSize: Int)

Provide various wallet statistics. Receive this object by calling the statistics member function.

Properties

Link copied to clipboard

Control whether saving the wallet to underlying storage is automatic (true, default), or initiated by API calls. Wallet performance could improve if automaticFlush is false and the wallet is saved rarely. However state may be lost!

Link copied to clipboard
abstract val balance: Long
Link copied to clipboard
abstract val balanceConfirmed: Long
Link copied to clipboard
abstract val balanceUnconfirmed: Long
Link copied to clipboard
abstract val blockchain: Blockchain

This wallet finds its coins on this blockchain

Link copied to clipboard
Link copied to clipboard
var historicalPrice: (String, Long) -> BigDecimal?

Give this wallet access to the historical price (epoch seconds) of its token in the provided fiat currency code.

Link copied to clipboard
abstract val isDeleted: Boolean

True if this wallet is deleted

Link copied to clipboard
abstract val name: String

Any name you may want to call this wallet

Link copied to clipboard
abstract var pause: Boolean

Pause blockchain processing if true

Link copied to clipboard
var spotPrice: (String) -> BigDecimal?

Give this wallet access to the spot price of its token in the provided fiat currency code. You should supply a function that takes a fiat (or digital) currency code, and returns the current price in that currency.

Link copied to clipboard
abstract val syncedDate: Long

Returns the current position of this wallet in the blockchain by block time. Since a fork may have occurred, this information does not unambiguously locate the wallet, but it is valuable for UI.

Link copied to clipboard
abstract val syncedHash: ByteArray

Returns the current position of this wallet in the blockchain by block hash. Since a fork may have occurred, this information does not unambiguously locate the wallet, but it is valuable for UI.

Link copied to clipboard
abstract val syncedHeight: Long

Returns the current position of this wallet in the blockchain by block height. Since a fork may have occurred, this information does not unambiguously locate the wallet, but it is valuable for UI.

Functions

Link copied to clipboard
abstract fun abortTransaction(tx: iTransaction, returnUnusedAddresses: List<PayAddress>? = null)

Abort this transaction, whose inputs were reserved by "prepareSend". Update the wallet to release all inputs reserved by this transaction

Link copied to clipboard

Return all configured identity domains

Link copied to clipboard
fun Wallet.chunkInto(payAddress: PayAddress, numUtxos: Int, amt: Long, delta: Long = 100000): List<iTransaction>

This function will split the wallet's native coin so that there are numUtxos of amt satoshis. If the wallet already has some UTXOs that fit the requirements, they count. So its possible that no transactions will be created

Link copied to clipboard
fun Wallet.chunkTokenInto(gid: GroupId, payAddress: PayAddress, numUtxos: Int, tokenAmt: Long = 1): List<iTransaction>

This function will split the wallet's Tokens so that there are numUtxos of amt satoshis. If the wallet already has some UTXOs that fit the requirements, they count. So its possible that no transactions will be created

Link copied to clipboard
abstract fun cleanReserved(before: Long = Long.MAX_VALUE)

Free up any UTXOs that have been allocated for pending transactions to be used again. This means that they might be double spent if that old reservation is still in progress.

Link copied to clipboard
abstract fun cleanUnconfirmed(before: Long = Long.MAX_VALUE)

Forget about all unconfirmed transactions in the wallet. It the transactions are in the network and are confirmed they will be added to the wallet at that point. This API is used to clear out tx that will never confirm for some reason. This API causes the wallet to forget about the inputs that weren't confirmed. "Rediscover" can get those back

Forget about certain transactions in the wallet. If the transactions are in the network and are later confirmed they will be added to the wallet then. This API is used to clear out tx that will never confirm for some reason. This API causes the wallet to forget about the inputs that weren't confirmed. "Rediscover" can get those back

Link copied to clipboard
abstract fun createConsolidationTx(maxAmount: Long? = null): Pair<iTransaction, ByteArray>

Consolidate utxos. This allows you to spend larger quantities of coin because a transaction can only have 256 inputs.

Link copied to clipboard
abstract fun deleteTxo(vararg txos: iTxOutpoint)

delete a TXO from the wallet's history. This should be used ONLY if the txo does not exist on the main chain, not when the txo is spent.

Link copied to clipboard
abstract fun destinationFor(seed: String): PayDestination

Get a repeatable destination, generally used for identity purposes. Given 2 different seeds, this API should at a minimum be statistically unlikely to produce the same address The seed may be public data (a domain name, for example) so wallet security must not depend on it being a secret.

Link copied to clipboard
abstract fun encrypt(passphrase: String)

switch this wallet from unencrypted to encrypted, or from one encryption passkey to another

Link copied to clipboard
abstract fun fastForward(syncedHeight: Long, syncedDate: Long, syncedHash: Hash256, txhist: List<TransactionHistory>)

Low level API, to do a full fastforward see Bip44Wallet.fastForward. If you provide the complete transaction history prior to the passed point, OR you provide the additional transactions from where this wallet currently is to the passed point, then you can call this function to jump the wallet forward. If you do not provide the complete transaction history, the wallet may attempt to spend already spent coins (if you miss a spend) or not have the complete balance (if you miss a receive). In the receive case, these funds are not "lost" since they are on the blockchain. They are simply inaccessible by this instance of the wallet. If you did a correct recovery, they would appear.

Link copied to clipboard
abstract fun forEachTx(doit: (TransactionHistory) -> Boolean)

Wallet history iterator

Link copied to clipboard

Wallet history iterator, for just one address

Link copied to clipboard
abstract fun forEachTxByDate(doit: (TransactionHistory) -> Boolean)

Wallet history iterator, sorted by newest first.

abstract fun forEachTxByDate(startingDate: Long, count: Long = Long.MAX_VALUE, doit: (TransactionHistory) -> Boolean)

Wallet history iterator, sorted by newest first, starting at @startingDate, and moving backwards. This is the most efficient chunked history access. Approximately count records are made available; however every record of a given date is provided meaning that the actual number of available records can exceed the passed count. This ensures that no records are missed if you chunk requests, process all the records in each chunk, and ask for the date of the last record - 1. Providing starting dates is much more efficient than an index because the system does not know the index of a record, so must process every prior record to count them.

Link copied to clipboard
abstract fun forEachTxByOldestDate(startingDate: Long, count: Long = Long.MAX_VALUE, doit: (TransactionHistory) -> Boolean)

Wallet history iterator that is sorted by oldest first, starts at startingDate, and moves forward. This is the most efficient chunked history access. Approximately count records are made available; however every record of a given date is provided meaning that the actual number of available records can exceed the passed count. This ensures that no records are missed if you chunk requests, process all the records in each chunk, and ask for the date of the last record + 1. Providing starting dates is much more efficient than an index because the system does not know the index of a record, so must process every prior record to count them.

Link copied to clipboard
abstract fun forEachTxo(doit: (Spendable) -> Boolean)

Wallet TXO iterator. Your callback should return true to exit early from the loop.

Link copied to clipboard
abstract fun forEachUtxo(doit: (Spendable) -> Boolean)

Wallet UTXO iterator.

Link copied to clipboard

Low level API to generate a new address to receive funds, called by newDestination. Use "newDestination()" in almost all cases. This API does not pre-generate (so may be slow), and does not install the destination into bloom filters, etc, before returning. This means that there may be a race condition between the use of the destination returned here and the wallet's monitoring of that destination which could cause funds to be received but not noticed by this wallet.

Link copied to clipboard
abstract fun getBalanceIn(dest: PayAddress, unspent: Boolean = true): Long

abstract fun getBalanceIn(groupId: GroupId, dest: PayAddress? = null, minConfirms: Int = 0, unspent: Boolean = true): Long

Get the current balance of a token

Link copied to clipboard

Get the current address to receive funds. Different wallets may support different payment destination types. This API returns whatever is the "default" type for this wallet. This allows generic algorithms to be created that can be applied to many different wallet types. When this API returns, the destination is ready for use (monitoring is installed in remote nodes, IF any remote nodes exist). This API may pre-generate destinations. This address will be returned until activity is detected on-chain, or until markAddressUsed is called

Link copied to clipboard

Gets a new address: convenience function that is functionally similar to @newDestination but mirrors the bitcoin-cli command.

Link copied to clipboard

Gets a new address: convenience function that is functionally similar to @newDestination but mirrors the bitcoin-cli command.

Link copied to clipboard

Get a new address to receive funds. This will return and "retire" the current address. Different wallets may support different payment destination types. This API returns whatever is the "default" type for this wallet. This allows generic algorithms to be created that can be applied to many different wallet types. When this API returns, the destination is ready for use (monitoring is installed in remote nodes, IF any remote nodes exist). This API may pre-generate destinations.

Link copied to clipboard

Wallet implementations may allow access to addresses generated from specific private keys or nonstandard HD derivation paths. The wallet will never offer these destinations as current payment targets. It will only spend them (and show their balance).

Link copied to clipboard
abstract fun getTx(txIdem: Hash256): TransactionHistory?

wallet history by transaction idem

Link copied to clipboard
abstract fun getTxo(txo: iTxOutpoint): Spendable?

wallet output by outpoint

Link copied to clipboard
Link copied to clipboard
abstract fun isWalletAddress(dest: PayAddress): Boolean
Link copied to clipboard
abstract fun lock()

relock an unlocked wallet

Link copied to clipboard
abstract fun lockedState(): Boolean?
Link copied to clipboard

Return identity domain data if this domain has previously been used

Link copied to clipboard
Link copied to clipboard
abstract fun markAddressUsed(addr: PayAddress)

Mark an address as 'used' so it will not be provided any longer. Note that if an address is 'seen' on the blockchain, this happens automatically.

Link copied to clipboard
abstract fun mintTokens(address: PayAddress, gid: GroupId, amountTokens: Long): iTransaction

Mint tokens.

Link copied to clipboard
abstract suspend fun newDestination(): PayDestination

Get a new address to receive funds. Different wallets may support different payment destination types. This API returns whatever is the "default" type for this wallet. This allows generic algorithms to be created that can be applied to many different wallet types. When this API returns, the destination is ready for use (monitoring is installed in remote nodes, IF any remote nodes exist). This API may pre-generate destinations.

Link copied to clipboard
abstract fun newToken(authAddress: PayAddress, opRetTokenDesc: SatoshiScript?, groupFlags: Int, authorityFlags: Long = GroupAuthorityFlags.ALL_AUTHORITIES.toLong()): Pair<iTransaction, GroupId>

Create a new group token

Link copied to clipboard
abstract fun numTx(): Int
Link copied to clipboard
abstract fun numTxos(): Int

Return the total number of ledger entries (TXOs) this wallet has ever used

Link copied to clipboard
abstract fun numUtxos(): Int

Return the number of currently active ledger entries (UTXOs) in this wallet

Link copied to clipboard
abstract fun prepareDestinations(minAmt: Int = MIN_UNUSED_ADDRESSES, chunk: Int = DEFAULT_GEN_ADDRESS_CHUNK_SIZE)

Tell the wallet that now is a good time to top-up a cache of unused destinations

Link copied to clipboard
abstract fun prepareSend(outputs: MutableList<iTxOutput>, minConfirms: Int = 0, deductFeeFromAmount: Boolean = false): iTransaction

Creates an unsigned transaction that sends to a list of outputs. This function will select input coins from the wallet to fill the passed quantity and sign the transaction, but will not relay the transaction to the network.

Link copied to clipboard
abstract fun pubkeyToSecret(pubkey: ByteArray): Secret?

Find and return the secret corresponding to the passed pubkey.

Link copied to clipboard
abstract fun rediscover(forgetAddresses: Boolean = false, noPrehistory: Boolean = false, forgetHistory: Boolean = false)

Forget all transaction and blockchain state, and asynchronously redo the search for wallet transactions. This is intended for testing and debug

Link copied to clipboard
abstract fun removeIdentityDomain(name: String)

Remove identity domain data

Link copied to clipboard
abstract fun removeIdentityInfo(id: PayAddress)

Remove identity information

Link copied to clipboard
abstract fun removeOnWalletChange(key: Int)

Remove a callback handler for whenever this wallet changes. You must pass the handler you set to ensure you have the wallet change callback.

Link copied to clipboard
abstract fun save(force: Boolean = false)

Write this wallet's changes to underlying storage. Called internally if @automaticSave is true, and/or explicitly called. Synchronous. An inability to save the wallet is a severe problem so exceptions are thrown.

Link copied to clipboard
abstract fun <T> searchTx(doit: (TransactionHistory) -> T?): T?

Wallet history iterator.

Link copied to clipboard
abstract fun send(tx: iTransaction, sync: Boolean = false, note: String? = null)

Post this transaction and update the wallet based on any inputs spent. Typically the provided tx came from calling prepareSend

abstract fun send(addrAmt: List<Pair<PayAddress, Long>>, sync: Boolean = false, note: String? = null, minConfirms: Int = 0): iTransaction

Send funds to multiple destinations. This function will select input coins from the wallet to fill the passed quantity

fun send(vararg outputs: iTxOutput, sync: Boolean = false, flags: Int = TxCompletionFlags.BIND_OUTPUT_PARAMETERS or TxCompletionFlags.FUND_GROUPS or TxCompletionFlags.FUND_NATIVE or TxCompletionFlags.USE_GROUP_AUTHORITIES or TxCompletionFlags.SIGN, note: String? = null, minConfirms: Int = 0): iTransaction

fun send(amountTokens: Long, destAddress: PayAddress, groupId: GroupId, sync: Boolean = false, note: String? = null): iTransaction

Send tokens to a destination. This is a convenience function to send to one destination. Use send(vararg outputs:iTxOutput...) to send multiple token types at once.

abstract fun send(amountSatoshis: Long, destAddress: String, deductFeeFromAmount: Boolean = false, sync: Boolean = false, note: String? = null, minConfirms: Int = 0): iTransaction
abstract fun send(amountSatoshis: Long, destAddress: PayAddress, deductFeeFromAmount: Boolean = false, sync: Boolean = false, note: String? = null, minConfirms: Int = 0): iTransaction
abstract fun send(amountSatoshis: Long, destScript: SatoshiScript, deductFeeFromAmount: Boolean = false, sync: Boolean = false, note: String? = null, minConfirms: Int = 0): iTransaction

Send funds to this destination. This function will select input coins from the wallet to fill the passed quantity

Link copied to clipboard
fun sendNative(vararg outputs: iTxOutput, sync: Boolean = false, note: String? = null): iTransaction

Send funds to multiple destinations (native coin only). This function will select input coins from the wallet to fill the passed quantity

Link copied to clipboard
abstract fun setOnWalletChange(callback: (Wallet, List<TransactionHistory>?) -> Unit): Int

Install a callback handler for whenever this wallet changes

Link copied to clipboard
abstract fun signData(message: ByteArray, addr: PayAddress? = null): ByteArray

Sign (using blockchain compatible SHA256 and Schnorr) the provided data with the provided address (this wallet must have the private key for that address), or pass null to use the common identity.

Link copied to clipboard
abstract fun signHash(hash: ByteArray, addr: PayAddress? = null): ByteArray
abstract fun signHash(hash: Hash256, addr: PayAddress? = null): ByteArray

Sign (using blockchain compatible Schnorr) the provided data with the provided address (this wallet must have the private key for that address), or pass null to use the common identity.

Link copied to clipboard
abstract fun signMessage(message: ByteArray, addr: PayAddress? = null): ByteArray
fun signMessage(message: String, addr: PayAddress? = null): String

Sign the passed message with the passed address (which must be owned by this wallet) using Bitcoin-style message signing.

Link copied to clipboard

Report various wallet statistics, see WalletStatistics

Link copied to clipboard
abstract fun suggestFee(tx: iTransaction, pad: Int = 0, priority: Int = 0): Long

Calculate a suggested fee for this transaction

Link copied to clipboard
fun sync(maxWait: Long = 10L*86400L*365L, epochTimeinMsOrBlockHeight: Long = -1L): Boolean

Return whether this wallet is synced with its underlying blockchain. If it is not synced, properties like balance and balanceUnconfirmed express some previous state. In the unsynced case, this API will wait for it to do so, but no more than the provided time in milliseconds. If a height is provided, this API returns true if the wallet is synced up to or beyond this height If a height is not provided (or is -1), this API assumes you mean up to "now". This is special cased with an extra check that the blockchain's tip timestamp is within an hour of now. Of course, since blocks can be discovered at any time, and connected nodes can be slow at processing blocks one cannot ever absolutely know whether the wallet is really synced up to "now", so this function is more accurately described as "nearly synced" for any "now" call.

Link copied to clipboard
abstract fun synced(epochTimeinMsOrBlockHeight: Long = -1L): Boolean

Return whether this wallet is synced with its underlying blockchain. If it is not synced, properties like balance and balanceUnconfirmed express some previous state. In the unsynced case, this API will wait for it to do so, but no more than the provided time in milliseconds. If a height is provided, this API returns true if the wallet is synced up to or beyond this height If a height is not provided (or is -1), this API assumes you mean up to "now". This is special cased with an extra check that the blockchain's tip timestamp is within an hour of now. Of course, since blocks can be discovered at any time, and connected nodes can be slow at processing blocks one cannot ever absolutely know whether the wallet is really synced up to "now", so this function is more accurately described as "nearly synced" for any "now" call.

Link copied to clipboard
abstract fun txCompleter(tx: iTransaction, minConfirms: Int, flags: Int, inputAmount: Long? = null, adjustableOutput: Int? = null, destinationAddress: PayAddress? = null, changeAddress: PayAddress? = null, sigHashTypeOverride: ByteArray? = null, contractId: ByteArray? = null)

Modify the passed transaction to complete it to the extent possible by this wallet.

Link copied to clipboard
abstract fun unlock(passphrase: String): Boolean?

unlock this wallet by providing the passphrase.

Link copied to clipboard

Add or update identity domain data

Link copied to clipboard

Add or update identity data

Link copied to clipboard
fun verifyMessage(message: String, addr: PayAddress, sigS: String): Boolean

Verify that the provided message was signed properly (using Bitcoin-style message signing)

Link copied to clipboard
abstract fun verifySigForData(data: ByteArray, sig: ByteArray, addr: PayAddress? = null): Boolean

Verify (using blockchain compatible SHA256 and Schnorr) the provided data (the message) with the provided address (this wallet must have the private key for that address), or pass null to use the common identity. Use libnexa.verifySignedHashSchnorr or libnexa.verifySignedDataSchnorr if you need to verify a non-wallet signature with a public key. Use verifySigForHash you may use if the data has already been run through a cryptgraphic hash function.

Link copied to clipboard
abstract fun verifySigForHash(hash: ByteArray, sig: ByteArray, addr: PayAddress? = null): Boolean

Verify (using blockchain compatible SHA256 and Schnorr) the provided data with the provided address (this wallet must have the private key for that address), or pass null to use the common identity. Use libnexa.verifySignedHashSchnorr or libnexa.verifySignedDataSchnorr if you need to verify a non-wallet signature with a public key. You may call verifySigForData instead as a shortcut if you have the data "preimage" not the hash and want to automatically use the standard cryptographic hash for this blockchain.

Link copied to clipboard