ch3 sx tools

pull/2/head
Andreas M. Antonopoulos 10 years ago
parent d97cbcc6a1
commit e5003bbe0f

@ -248,33 +248,6 @@ rpcuser=bitcoinrpc
rpcpassword=7p687uGU8wMyBprB2aQrnt72r9Lh6jZy
----
Now, run the bitcoin client. The first time you run it, it will rebuild the bitcoin blockchain. This is a multi-gigabyte file and will take on average 2 days to download in full. You can shorten the blockchain initialization time by downloading a partial copy of the blockchain using bittorrent from +http://sourceforge.net/projects/bitcoin/files/Bitcoin/blockchain/+.
Run bitcoind in the background with the option +-daemon+:
----
$ bitcoind -daemon
$
Bitcoin version v0.9.0rc1-beta (2014-01-31 09:30:15 +0100)
Using OpenSSL version OpenSSL 1.0.1c 10 May 2012
Default data directory /home/bitcoin/.bitcoin
Using data directory /bitcoin/
Using at most 4 connections (1024 file descriptors available)
init message: Verifying wallet...
dbenv.open LogDir=/bitcoin/database ErrorFile=/bitcoin/db.log
Bound to [::]:8333
Bound to 0.0.0.0:8333
init message: Loading block index...
Opening LevelDB in /bitcoin/blocks/index
Opened LevelDB successfully
Opening LevelDB in /bitcoin/chainstate
Opened LevelDB successfully
[... more startup messages ...]
----
=== Using bitcoind from the command line
The reference client bitcoind offers a number of commands that can be run from the command line. These are the same commands as those offered via the JSON-RPC API, so the command line allows us to experiment interactively with the capabilities that are also available programmatically. To start, we can invoke the +help+ command to see a list of the available bitcoin commands:
@ -354,7 +327,36 @@ verifychain ( checklevel numblocks )
verifymessage "bitcoinaddress" "signature" "message"
----
==== Command: bitcoind getinfo
==== Running bitcoind
Commands: -daemon, getinfo
Now, run the bitcoin client. The first time you run it, it will rebuild the bitcoin blockchain. This is a multi-gigabyte file and will take on average 2 days to download in full. You can shorten the blockchain initialization time by downloading a partial copy of the blockchain using bittorrent from +http://sourceforge.net/projects/bitcoin/files/Bitcoin/blockchain/+.
Run bitcoind in the background with the option +-daemon+:
----
$ bitcoind -daemon
$
Bitcoin version v0.9.0rc1-beta (2014-01-31 09:30:15 +0100)
Using OpenSSL version OpenSSL 1.0.1c 10 May 2012
Default data directory /home/bitcoin/.bitcoin
Using data directory /bitcoin/
Using at most 4 connections (1024 file descriptors available)
init message: Verifying wallet...
dbenv.open LogDir=/bitcoin/database ErrorFile=/bitcoin/db.log
Bound to [::]:8333
Bound to 0.0.0.0:8333
init message: Loading block index...
Opening LevelDB in /bitcoin/blocks/index
Opened LevelDB successfully
Opening LevelDB in /bitcoin/chainstate
Opened LevelDB successfully
[... more startup messages ...]
----
Bitcoin's +getinfo+ command shows us basic information about the status of the bitcoin network node, the wallet and the blockchain database:
@ -488,7 +490,7 @@ $ bitcoind getreceivedbyaddress 1hvzSofGwT8cjb8JU7nBsCSfEVQX5u9CL 0
0.05000000
----
If we ommit the zero from the end of this command, we will only see the amounts that have at least +minconf+ confirmations, where +minconf+ is the setting for the minimum number of confirmations before a transaction is ilsted in the balance. The +minconf+ setting is specified in the bitcoind configuration file. Since the transaction sending this bitcoin was only sent in the last few seconds, it has still not confirmed and therefore we will see it list a zero balance:
If we ommit the zero from the end of this command, we will only see the amounts that have at least +minconf+ confirmations, where +minconf+ is the setting for the minimum number of confirmations before a transaction is listed in the balance. The +minconf+ setting is specified in the bitcoind configuration file. Since the transaction sending this bitcoin was only sent in the last few seconds, it has still not confirmed and therefore we will see it list a zero balance:
----
$ bitcoind getreceivedbyaddress 1hvzSofGwT8cjb8JU7nBsCSfEVQX5u9CL
@ -988,6 +990,211 @@ $ bitcoind gettransaction ae74538baa914f3799081ba78429d5d84f36a0127438e9f721dff5
As before, we can also examine this in more detail using the +getrawtransaction+ and +decodetransaction+ commands. These commands will return the exact same hex string that we produced and decoded previously just before we sent it on the network.
=== Alternative clients, libraries and toolkits
Beyond the reference client, bitcoind, there are other clients and libraries that can be used to interact with the bitcoin network and data structures. These are implemented in a variety of programming languages, offering programmers native interfaces in their own language.
Alternative implementations include:
* libbitcoin and sx tools, a C++ multi-threaded full node client and library with command-line tools (https://libbitcoin.dyne.org/)
* bitcoinj, a Java full node client library (https://code.google.com/p/bitcoinj/)
* btcd, a Go language full node bitcoin client (https://opensource.conformal.com/wiki/btcd)
* Bits of Proof (BOP), a Java enterprise-class implementation of bitcoin (https://bitsofproof.com)
* picocoin, a C implementation of a light-weight client library for bitcoin (https://github.com/jgarzik/picocoin)
Many more libraries exist in a variety of other programming languages and more are created all the time.
==== Libbitcoin and sx tools
The libbitcoin library is a C++ scalable multi-threaded and modular implemntation that supports a full-node client and a command-line toolset named "sx", which offers many of the same capabilities as the bitcoind client commands we illustrated in this chapter. The sx tools also offer some key management and manipulation tools that are not offered by bitcoind, including type-2 deterministic keys and key mnemonics.
===== Installing sx
To install sx and the supporting library libbitcoin, download and run the online installer on a Linux system:
----
$ wget http://sx.dyne.org/install-sx.sh
$ sudo bash ./install-sx.sh
----
You should now have the sx tools installed. Type +sx+ with no parameters to display the help text, which lists all the available commands:
----
Usage: sx COMMAND [ARGS]...
-c, --config Specify a config file
The sx commands are:
DETERMINISTIC KEYS AND ADDRESSES
genaddr Generate a Bitcoin address deterministically from a wallet
seed or master public key.
genpriv Generate a private key deterministically from a seed.
genpub Generate a public key deterministically from a wallet
seed or master public key.
mpk Extract a master public key from a deterministic wallet seed.
newseed Create a new deterministic wallet seed.
TRANSACTION PARSING
showscript Show the details of a raw script.
showtx Show the details of a transaction.
BLOCKCHAIN QUERIES (blockexplorer.com)
blke-fetch-transaction Fetches a transaction from blockexplorer.com
FORMAT
base58-decode Convert from base58 to hex
base58-encode Convert from hex to base58
base58check-decode Convert from base58check to hex
base58check-encode Convert from hex to base58check
decode-addr Decode an address to its internal RIPEMD representation.
embed-addr Generate an address used for embedding record of data into the blockchain.
encode-addr Encode an address to base58check form.
ripemd-hash RIPEMD hash data from STDIN.
unwrap Validates checksum and recovers version byte and original data from hexstring.
validaddr Validate an address.
wrap Adds version byte and checksum to hexstring.
BRAINWALLET
brainwallet Make a private key from a brainwallet
mnemonic Work with Electrum compatible mnemonics (12 words wallet seed).
BLOCKCHAIN WATCHING
monitor Monitor an address.
watchtx Watch transactions from the network searching for a certain hash.
BLOCKCHAIN QUERIES (blockchain.info)
bci-fetch-last-height Fetch the last block height using blockchain.info.
bci-history Get list of output points, values, and their spends
from blockchain.info
MISC
btc Convert Satoshis into Bitcoins.
initchain Initialize a new blockchain.
qrcode Generate Bitcoin QR codes offline.
satoshi Convert Bitcoins into Satoshis.
showblkhead Show the details of a block header.
wallet Experimental command line wallet.
MULTISIG ADDRESSES
scripthash Create BIP 16 script hash address from raw script hex.
LOOSE KEYS AND ADDRESSES
addr See Bitcoin address of a public or private key.
get-pubkey Get the pubkey of an address if available
newkey Create a new private key.
pubkey See the public part of a private key.
STEALTH
secret-to-wif Convert a secret exponent value to Wallet. Import. Format.
stealth-new Generate a new master stealth secret.
stealth-recv Regenerate the secret from your master secret and provided nonce.
stealth-send Generate a new sending address and a stealth nonce.
CREATE TRANSACTIONS
mktx Create an unsigned tx.
rawscript Create the raw hex representation from a script.
set-input Set a transaction input.
sign-input Sign a transaction input.
VALIDATE
validsig Validate a transaction input's signature.
BLOCKCHAIN QUERIES
balance Show balance of a Bitcoin address in satoshis.
fetch-block-header Fetch raw block header.
fetch-last-height Fetch the last block height.
fetch-transaction Fetch a raw transaction using a network connection to make requests against the obelisk load balancer backend.
fetch-transaction-index Fetch block height and index in block of transaction.
get-utxo Get enough unspent transaction outputs from a given set of
addresses to pay a given number of satoshis
history Get list of output points, values, and their spends for an
address. grep can filter for just unspent outputs which can
be fed into mktx.
validtx Validate a transaction.
BLOCKCHAIN UPDATES
sendtx-bci Send tx to blockchain.info/pushtx.
sendtx-node Send transaction to a single node.
sendtx-obelisk Send tx to obelisk server.
sendtx-p2p Send tx to bitcoin network.
See 'sx help COMMAND' for more information on a specific command.
SpesmiloXchange home page: <http://sx.dyne.org/>
----
===== Generating and manipulating keys with sx
Generate a new private key, using the operating system's random number generator, with the +newkey+ command. We save the standard output into the file +private_key+:
----
$ sx newkey > private_key
$ cat private_key
5Jgx3UAaXw8AcCQCi1j7uaTaqpz2fqNR9K3r4apxdYn6rTzR1PL
----
Now, generate the public key from that private key, using the +pubkey+ command. Pass the +private_key+ file into the standard input and save the standard output of the command into a new file +public_key+:
----
$ sx pubkey < private_key > public_key
$ cat public_key
02fca46a6006a62dfdd2dbb2149359d0d97a04f430f12a7626dd409256c12be500
----
We can re-format the public_key as an address, using the +addr+ command. We pass the +public_key+ into standard input:
----
$ sx addr < public_key
17re1S4Q8ZHyCP8Kw7xQad1Lr6XUzWUnkG
----
===== Deterministic keys with sx
The keys generated above are so called type-1 non-deterministic keys. That means that each one is generated from a random number generator. The sx tools also support type-2 deterministic keys, where a "master" key is created and then extended to produce a chain or tree of subkeys.
First, we generate a "seed" that will be used as the basis to derive a chain of keys, compatible with the Electrum wallet and other similar implementations. We use the +newseed+ command to produce a seed value:
----
$ sx newseed > seed
$ cat seed
eb68ee9f3df6bd4441a9feadec179ff1
----
The seed value can also be exported as a word mnemonic that is human readable and easier to store and type than a hexadecimal string, using sx tools +mnemonic+ sub-command:
----
$ sx mnemonic < seed > words
$ cat words
adore repeat vision worst especially veil inch woman cast recall dwell appreciate
----
The mnemonic words can be used to reproduce the seed. Notice that we use the +sx-mnemonic+ command (with a dash), not the sx command with the mnemonic sub-command:
----
$ sx-mnemonic < words
eb68ee9f3df6bd4441a9feadec179ff1
----
With the seed, we can now generate a sequence of private and public keys, a key chain. We use the +genpriv+ command to generate a sequence of private keys from a seed and the +addr+ command to generate the corresponding public key.
----
$ sx genpriv 0 < seed
5JzY2cPZGViPGgXZ4Syb9Y4eUGjJpVt6sR8noxrpEcqgyj7LK7i
$ sx genpriv 0 < seed | sx addr
1esVQV2vR9JZPhFeRaeWkAhzmWq7Fi7t7
$ sx genpriv 1 < seed
5JdtL7ckAn3iFBFyVG1Bs3A5TqziFTaB9f8NeyNo8crnE2Sw5Mz
$ sx genpriv 1 < seed | sx addr
1G1oTeXitk76c2fvQWny4pryTdH1RTqSPW
----
With deterministic keys we can generate and re-generate a large number of keys, all derived from a single seed in a deterministic chain. This technique is used in many wallets to generate keys that can be backed up and restored with a single simple word mnemonic. This makes it easier than requiring that the wallet be backed up with all its randomly generated keys every time a key is added.

Loading…
Cancel
Save