network-bitcoin-1.9.1: An interface to bitcoind.

Safe HaskellNone
LanguageHaskell98

Network.Bitcoin

Contents

Description

A Haskell binding to the bitcoind server.

Synopsis

Common Types

type Client = ByteString -> IO ByteString Source #

Client describes authentication credentials and host info for making API requests to the Bitcoin daemon.

getClient :: String -> ByteString -> ByteString -> IO Client Source #

getClient takes a url, rpc username, and rpc password and returns a Client that can be used to make API calls. Each Client encloses a Manager (from http-client) that re-uses connections for requests, so long as the same Client is is used for each call.

data BitcoinException Source #

A BitcoinException is thrown when 'callApi encounters an error. The API error code is represented as an Int, the message as a String.

It may also be thrown when the value returned by the bitcoin API wasn't what we expected.

WARNING: Any of the functions in this module's public API may throw this exception. You should plan on handling it.

Constructors

BitcoinApiError Int Text

A BitcoinApiError has an error code error message, as returned by bitcoind's JSON-RPC response.

BitcoinResultTypeError ByteString

The raw JSON returned, if we can't figure out what actually went wrong.

type HexString = Text Source #

A string returned by the bitcoind API, representing data as hex.

What that data represents depends on the API call, but should be dcumented accordingly.

type TransactionID = HexString Source #

A hexadecimal string representation of a 256-bit unsigned integer.

This integer is a unique transaction identifier.

data Satoshi Source #

A satoshi is the smallest subdivision of bitcoins. For the resolution, use resolution from Fixed.

Constructors

Satoshi 
Instances
HasResolution Satoshi Source # 
Instance details

Defined in Network.Bitcoin.Types

Methods

resolution :: p Satoshi -> Integer #

type BTC = Fixed Satoshi Source #

The type of bitcoin money, represented with a fixed-point number.

type Account = Text Source #

An account on the wallet is just a label to easily specify private keys.

The default account is an empty string.

type Address = HexString Source #

An address for sending or receiving money.

Block Chain Operations

getBlockCount :: Client -> IO BlockHeight Source #

Returns the number of blocks in the longest block chain.

getDifficulty :: Client -> IO Integer Source #

Returns the proof-of-work difficulty as a multiple of the minimum difficulty.

setTransactionFee :: Client -> BTC -> IO () Source #

Sets the transaction fee will will pay to the network. Values of 0 are rejected.

getRawMemoryPool :: Client -> IO (Vector TransactionID) Source #

Returns all transaction identifiers in the memory pool.

type BlockHash = HexString Source #

The hash of a given block.

getBlockHash Source #

Arguments

:: Client 
-> BlockHeight

Block index.

-> IO BlockHash 

Returns the hash of the block in best-block-chain at the given index.

data Block Source #

Information about a given block in the block chain.

Constructors

Block 

Fields

Instances
Eq Block Source # 
Instance details

Defined in Network.Bitcoin.BlockChain

Methods

(==) :: Block -> Block -> Bool #

(/=) :: Block -> Block -> Bool #

Ord Block Source # 
Instance details

Defined in Network.Bitcoin.BlockChain

Methods

compare :: Block -> Block -> Ordering #

(<) :: Block -> Block -> Bool #

(<=) :: Block -> Block -> Bool #

(>) :: Block -> Block -> Bool #

(>=) :: Block -> Block -> Bool #

max :: Block -> Block -> Block #

min :: Block -> Block -> Block #

Read Block Source # 
Instance details

Defined in Network.Bitcoin.BlockChain

Show Block Source # 
Instance details

Defined in Network.Bitcoin.BlockChain

Methods

showsPrec :: Int -> Block -> ShowS #

show :: Block -> String #

showList :: [Block] -> ShowS #

FromJSON Block Source # 
Instance details

Defined in Network.Bitcoin.BlockChain

getBlock :: Client -> BlockHash -> IO Block Source #

Returns details of a block with given block-hash.

data OutputSetInfo Source #

Information on the unspent transaction in the output set.

Constructors

OutputSetInfo 

Fields

getOutputSetInfo :: Client -> IO OutputSetInfo Source #

Returns statistics about the unspent transaction output set.

data OutputInfo Source #

Details about an unspent transaction output.

Constructors

OutputInfo 

Fields

getOutputInfo Source #

Arguments

:: Client 
-> TransactionID 
-> Integer

The index we're looking at.

-> IO (Maybe OutputInfo) 

Returns details about an unspent transaction output.

Private Key Operations

importPrivateKey Source #

Arguments

:: Client 
-> PrivateKey 
-> Maybe Account

An optional label for the key.

-> IO () 

Adds a private key (as returned by dumpprivkey) to your wallet.

dumpPrivateKey :: Client -> Address -> IO PrivateKey Source #

Reveals the private key corresponding to the given address.

Mining Operations

generate Source #

Arguments

:: Client 
-> Int

The number of blocks to generate. The RPC call will not return until all blocks have been generated or the maxium number of iterations has been reached

-> Maybe Int

The maximum number of iterations that are tried to create the requested number of blocks. Default is 1000000

-> IO [HexString]

An array containing the block header hashes of the generated blocks (may be empty if used with generate 0)

The generate RPC nearly instantly generates blocks. See https://bitcoin.org/en/developer-reference#generate for more details.

generateToAddress Source #

Arguments

:: Client 
-> Int

The number of blocks to generate. The RPC call will not return until all blocks have been generated or the maxium number of iterations has been reached

-> Address

The address to send the newly generated Bitcoin to

-> Maybe Int

The maximum number of iterations that are tried to create the requested number of blocks. Default is 1000000

-> IO [HexString] 

The generatetoaddress RPC mines blocks immediately to a specified address. See https://bitcoin.org/en/developer-reference#generatetoaddress for more details.

getGenerate Source #

Arguments

:: Client

bitcoind RPC client

-> IO Bool 

Returns whether or not bitcoind is generating bitcoins.

setGenerate Source #

Arguments

:: Client

bitcoind RPC client

-> Bool

Turn it on, or turn it off?

-> Maybe Int

Generation is limited to this number of processors. Set it to Nothing to keep the value at what it was before, Just -1 to use all available cores, and any other value to limit it. If bitcoind runs in regtest mode instead of the number of processors, this specifies the number of hashes to generate.

-> IO (Maybe [HexString]) 

Controls whether or not bitcoind is generating bitcoins. If bitcoind runs in regtest mode the number of generated hashes is returned. See https://bitcoin.org/en/developer-reference#setgenerate for more details.

getHashesPerSec :: Client -> IO Integer Source #

Returns a recent hashes per second performance measurement while generating.

data MiningInfo Source #

Information related to the current bitcoind mining operation.

If a field is undocumented here, it's because I don't know what it means. If you DO know what it means, I'd love it if you would submit a patch to help complete this documentation.

Constructors

MiningInfo 

Fields

getMiningInfo :: Client -> IO MiningInfo Source #

Returns an object containing mining-related information.

data HashData Source #

The hash data returned from getWork.

Constructors

HashData 

Fields

getWork :: Client -> IO HashData Source #

Returns formatted hash data to work on.

solveBlock :: Client -> HexString -> IO Bool Source #

Tries to solve the given block, and returns true if it was successful.

data BlockTemplate Source #

A template for constructing a block to work on.

See https://en.bitcoin.it/wiki/BIP_0022 for the full specification.

Constructors

BlockTemplate 

Fields

getBlockTemplate :: Client -> IO BlockTemplate Source #

Returns data needed to construct a block to work on.

submitBlock Source #

Arguments

:: Client 
-> HexString

The block to submit.

-> IO Bool

Was the block accepted by the network?

Attempts to submit a new block to the network.

Network Operations

getConnectionCount :: Client -> IO Integer Source #

Returns the number of connections to other nodes.

data PeerInfo Source #

Information about a peer node of the Bitcoin network.

The documentation for this data structure is incomplete, as I honestly don't know what some of these fields are for. Patches are welcome!

Constructors

PeerInfo 

Fields

getPeerInfo :: Client -> IO [PeerInfo] Source #

Returns data about all connected peer nodes.

Raw Transaction Operations

type RawTransaction = HexString Source #

Just like most binary data retrieved from bitcoind, a raw transaction is represented by a hexstring.

This is a serialized, hex-encoded transaction.

getRawTransaction :: Client -> TransactionID -> IO RawTransaction Source #

Get a raw transaction from its unique ID.

data TxIn Source #

A transaction into an account. This can either be a coinbase transaction, or a standard transaction with another account.

Constructors

TxCoinbase 
TxIn 

Fields

Instances
Eq TxIn Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Methods

(==) :: TxIn -> TxIn -> Bool #

(/=) :: TxIn -> TxIn -> Bool #

Ord TxIn Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Methods

compare :: TxIn -> TxIn -> Ordering #

(<) :: TxIn -> TxIn -> Bool #

(<=) :: TxIn -> TxIn -> Bool #

(>) :: TxIn -> TxIn -> Bool #

(>=) :: TxIn -> TxIn -> Bool #

max :: TxIn -> TxIn -> TxIn #

min :: TxIn -> TxIn -> TxIn #

Read TxIn Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Show TxIn Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Methods

showsPrec :: Int -> TxIn -> ShowS #

show :: TxIn -> String #

showList :: [TxIn] -> ShowS #

FromJSON TxIn Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

data TxnOutputType Source #

The type of a transaction out.

More documentation is needed here. Submit a patch if you know what this is about!

Constructors

TxnPubKey

JSON of "pubkey" received.

TxnPubKeyHash

JSON of "pubkeyhash" received.

TxnScriptHash

JSON of "scripthash" received.

TxnMultisig

JSON of "multisig" received.

data ScriptPubKey Source #

A public key of someone we sent money to.

Constructors

NonStandardScriptPubKey 

Fields

StandardScriptPubKey 

Fields

data TxOut Source #

A transaction out of an account.

Constructors

TxOut 

Fields

Instances
Eq TxOut Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Methods

(==) :: TxOut -> TxOut -> Bool #

(/=) :: TxOut -> TxOut -> Bool #

Ord TxOut Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Methods

compare :: TxOut -> TxOut -> Ordering #

(<) :: TxOut -> TxOut -> Bool #

(<=) :: TxOut -> TxOut -> Bool #

(>) :: TxOut -> TxOut -> Bool #

(>=) :: TxOut -> TxOut -> Bool #

max :: TxOut -> TxOut -> TxOut #

min :: TxOut -> TxOut -> TxOut #

Read TxOut Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Show TxOut Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

Methods

showsPrec :: Int -> TxOut -> ShowS #

show :: TxOut -> String #

showList :: [TxOut] -> ShowS #

FromJSON TxOut Source # 
Instance details

Defined in Network.Bitcoin.RawTransaction

data BlockInfo Source #

Information on a single block.

Constructors

ConfirmedBlock 

Fields

UnconfirmedBlock

An unconfirmed block is boring, but a possibility.

data RawTransactionInfo Source #

The raw transaction info for a given transaction ID.

Constructors

RawTransactionInfo 

Fields

getRawTransactionInfo :: Client -> TransactionID -> IO RawTransactionInfo Source #

Get raw transaction info for a given transaction ID. The data structure returned is quite sprawling and undocumented, so any patches to help simplify things would be greatly appreciated.

listUnspent Source #

Arguments

:: Client 
-> Maybe Int

minconf. Defaults to 1 if Nothing.

-> Maybe Int

maxconf. Defaults to 9999999 if Nothing.

-> Vector Address

Use empty for no filtering.

-> IO (Vector UnspentTransaction) 

Returns an array of unspent transaction outputs with between minconf and maxconf (inclusive) confirmations. If addresses are given, the result will be filtered to include only those addresses.

createRawTransaction Source #

Arguments

:: Client 
-> Vector UnspentTransaction

The unspent transactions we'll be using as our output.

-> Vector (Address, BTC)

The addresses we're sending money to, along with how much each of them gets.

-> IO HexString 

Create a transaction spending given inputs, sending to given addresses.

Note that the transaction's inputs are not signed, and it is not stored in the wallet or transmitted to the network.

Also, there is no checking to see if it's possible to send that much to the targets specified. In the future, such a scenario might throw an exception.

data DecodedRawTransaction Source #

A successfully decoded raw transaction, from a given serialized, hex-encoded transaction.

Constructors

DecodedRawTransaction 

Fields

decodeRawTransaction :: Client -> RawTransaction -> IO DecodedRawTransaction Source #

Decodes a raw transaction into a more accessible data structure.

data WhoCanPay Source #

Who can pay for a given transaction.

data RawSignedTransaction Source #

A raw signed transaction contains the raw, signed hexstring and whether or not this transaction has a complete signature set.

signRawTransaction Source #

Arguments

:: Client 
-> RawTransaction

The raw transaction whose inputs we're signing.

-> Maybe (Vector UnspentTransaction)

An optional list of previous transaction outputs that this transaction depends on but may not yet be in the block chain.

-> Maybe (Vector HexString)

An array of base58-encoded private keys that, if given, will be the only keys used to sign the transaction.

-> Maybe WhoCanPay

Who can pay for this transaction? All by default.

-> IO RawSignedTransaction

Returns Nothing if the transaction has a complete set of signatures, and the raw signed transa

Sign inputs for a raw transaction.

Wallet Operations

data BitcoindInfo Source #

A plethora of information about a bitcoind instance.

Constructors

BitcoindInfo 

Fields

getBitcoindInfo :: Client -> IO BitcoindInfo Source #

Returns an object containing various state info.

Availability: < 0.16

getNewAddress :: Client -> Maybe Account -> IO Address Source #

Returns a new bitcoin address for receiving payments.

If an account is specified (recommended), the new address is added to the address book so payments received with the address will be credited to the given account.

If no account is specified, the address will be credited to the account whose name is the empty string. i.e. the default account.

getAccountAddress :: Client -> Account -> IO Address Source #

Returns the current Bitcoin address for receiving payments to the given account.

getAccount :: Client -> Address -> IO Account Source #

Returns the account associated with the given address.

setAccount :: Client -> Address -> Account -> IO () Source #

Sets the account associated with the given address.

getAddressesByAccount :: Client -> Account -> IO (Vector Address) Source #

Returns the list of addresses for the given address.

sendToAddress Source #

Arguments

:: Client 
-> Address

Who we're sending to.

-> BTC

The amount to send.

-> Maybe Text

An optional comment for the transaction.

-> Maybe Text

An optional comment-to (who did we sent this to?) for the transaction.

-> IO TransactionID 

Sends some bitcoins to an address.

data AddressInfo Source #

Information on a given address.

Constructors

AddressInfo 

Fields

listAddressGroupings :: Client -> IO (Vector (Vector AddressInfo)) Source #

Lists groups of addresses which have had their common ownership made public by common use as inputs or as the resulting change in past transactions.

type Signature = HexString Source #

A signature is a base-64 encoded string.

signMessage Source #

Arguments

:: Client 
-> Address

The address whose private key we'll use.

-> Text

The message to sign.

-> IO Signature 

Sign a message with the private key of an address.

verifyMessage Source #

Arguments

:: Client 
-> Address

The address of the original signer.

-> Signature

The message's signature.

-> Text

The message.

-> IO Bool

Was the signature valid?

Verifies a signed message.

getReceivedByAddress :: Client -> Address -> IO BTC Source #

Returns the total amount received by the given address with at least one confirmation.

getReceivedByAddress' Source #

Arguments

:: Client 
-> Address 
-> Int

The minimum number of confirmations needed for a transaction to to count towards the total.

-> IO BTC 

Returns the total amount received by the given address, with at least the give number of confirmations.

getReceivedByAccount :: Client -> Account -> IO BTC Source #

Returns the total amount received by address with the given account.

getReceivedByAccount' Source #

Arguments

:: Client 
-> Account

The account in question.

-> Int

The minimum number of confirmations needed for a transaction to count towards the total.

-> IO BTC 

Returns the total amount received by addresses with the given account, counting only transactions with the given minimum number of confirmations.

getBalance :: Client -> IO BTC Source #

Returns the server's total available balance.

getBalance' :: Client -> Account -> IO BTC Source #

Returns the balance in the given account, counting only transactions with at least one confirmation.

getBalance'' Source #

Arguments

:: Client 
-> Account 
-> Int

The minimum number of confirmations needed for a transaction to count towards the total.

-> IO BTC 

Returns the balance in the given account, counting only transactions with at least the given number of confirmations.

moveBitcoins Source #

Arguments

:: Client 
-> Account

From.

-> Account

To.

-> BTC

The amount to transfer.

-> Text

A comment to record for the transaction.

-> IO () 

Move bitcoins from one account in your wallet to another.

If you want to send bitcoins to an address not in your wallet, use sendFromAccount.

sendFromAccount Source #

Arguments

:: Client 
-> Account

The account to send from.

-> Address

The address to send to.

-> BTC

The amount to send.

-> Maybe Text

An optional transaction comment.

-> Maybe Text

An optional comment on who the money is going to.

-> IO TransactionID 

Sends bitcoins from a given account in our wallet to a given address.

A transaction and sender comment may be optionally provided.

sendMany Source #

Arguments

:: Client 
-> Account

The account to send from.

-> Vector (Address, BTC)

The address, and how much to send to each one.

-> Maybe Text

An optional transaction comment.

-> IO TransactionID 

Send to a whole bunch of address at once.

estimateSmartFee :: Client -> Word32 -> Maybe EstimationMode -> IO Double Source #

Estimate the fee per kb to send a transaction

data ReceivedByAddress Source #

Information on how much was received by a given address.

Constructors

ReceivedByAddress 

Fields

listReceivedByAddress :: Client -> IO (Vector ReceivedByAddress) Source #

Lists the amount received by each address which has received money at some point, counting only transactions with at least one confirmation.

listReceivedByAddress' Source #

Arguments

:: Client 
-> Int

The minimum number of confirmations before a transaction counts toward the total amount received.

-> Bool

Should we include addresses with no money received?

-> IO (Vector ReceivedByAddress) 

List the amount received by each of our addresses, counting only transactions with the given minimum number of confirmations.

data ReceivedByAccount Source #

Constructors

ReceivedByAccount 

Fields

listReceivedByAccount :: Client -> IO (Vector ReceivedByAccount) Source #

Lists the amount received by each account which has received money at some point, counting only transactions with at leaset one confirmation.

listReceivedByAccount' Source #

Arguments

:: Client 
-> Int

The minimum number of confirmations before a transaction counts toward the total received.

-> Bool

Should we include the accounts with no money received?

-> IO (Vector ReceivedByAccount) 

List the amount received by each of our accounts, counting only transactions with the given minimum number of confirmations.

listTransactions Source #

Arguments

:: Client 
-> Account

Limits the BlockTransaction returned to those from or to the given Account.

-> Int

Limits the number of BlockTransaction returned.

-> Int

Number of most recent transactions to skip.

-> IO (Vector SimpleTransaction) 

Returns transactions from the blockchain.

listTransactions' Source #

Arguments

:: Client 
-> Maybe Account

Limits the BlockTransaction returned to those from or to the given Account. If Nothing all accounts are included in the query.

-> Maybe Int

Limits the number of BlockTransaction returned. If Nothing all transactions are returned.

-> Maybe Int

Number of most recent transactions to skip.

-> IO (Vector SimpleTransaction) 

Returns transactions from the blockchain.

listAccounts Source #

Arguments

:: Client 
-> Maybe Int

Minimum number of confirmations required before payments are included in the balance.

-> IO (HashMap Account BTC) 

List accounts and their current balance.

importAddress Source #

Arguments

:: Client 
-> Address

Address to import

-> Maybe Account

Optional account, default ""

-> Maybe Bool

Optional rescan the blockchain, default true

-> IO () 

Import an address

data SimpleTransaction Source #

Data type for simple transactions. Rules involving Maybe are indications of the most probable value only when the transaction is obtained from listTransactions or listSinceBlock are their associated methods. They are never enforced on this side.

Constructors

SimpleTransaction 

Fields

data TransactionCategory Source #

listSinceBlock Source #

Arguments

:: Client 
-> BlockHash

The hash of the first block to list.

-> Maybe Int

The minimum number of confirmations before a transaction can be returned as sbLastBlockHash. This does not in any way affect which transactions are returned (see https://github.com/bitcoin/bitcoin/pull/199#issuecomment-1514952)

-> IO SinceBlock 

Gets all transactions in blocks since the given block.

listSinceBlock' Source #

Arguments

:: Client 
-> Maybe BlockHash

The hash of the first block to list.

-> Maybe Int

The minimum number of confirmations before a transaction can be returned as sbLastBlockHash. This does not in any way affect which transactions are returned (see https://github.com/bitcoin/bitcoin/pull/199#issuecomment-1514952)

-> IO SinceBlock 

Gets all transactions in blocks since the given block, or all transactions if ommited.

data DetailedTransaction Source #

Data type for detailed transactions. Rules involving trCategory are indications of the most probable value only when the transaction is obtained from listTransactions or listSinceBlock are their associated methods.

Constructors

DetailedTransaction 

Fields

data DetailedTransactionDetails Source #

Constructors

DetailedTransactionDetails 

Fields

backupWallet :: Client -> FilePath -> IO () Source #

Safely copies wallet.dat to the given destination, which can be either a directory, or a path with filename.

keyPoolRefill :: Client -> IO () Source #

Fills the keypool.

unlockWallet Source #

Arguments

:: Client 
-> Text

The decryption key.

-> Integer

How long to store the key in memory (in seconds).

-> IO () 

Stores the wallet decryption key in memory for the given amount of time.

lockWallet :: Client -> IO () Source #

Removes the wallet encryption key from memory, locking the wallet.

After calling this function, you will need to call unlockWallet again before being able to call methods which require the wallet to be unlocked.

Note: In future releases, we might introduce an "unlocked" monad, so locking and unlocking is automatic.

changePassword Source #

Arguments

:: Client 
-> Text

The old password.

-> Text

The new password.

-> IO () 

Changes the wallet passphrase.

encryptWallet :: Client -> Text -> IO () Source #

Encrypts the wallet with the given passphrase.

WARNING: bitcoind will shut down after calling this method. Don't say I didn't warn you.

isAddressValid :: Client -> Address -> IO Bool Source #

Checks if a given address is a valid one.