O'Reilly logo

Mastering Bitcoin by Andreas M. Antonopoulos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix D. Available Commands with sx Tools

The sx commands are:

DEPRECATED
  ELECTRUM STYLE 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.

EXPERIMENTAL
  APPS
   wallet   Experimental command-line wallet.

OFFLINE BLOCKCHAIN
  HEADERS
   showblkhead  Show the details of a block header.

OFFLINE KEYS AND ADDRESSES
  BASIC
   addr          See Bitcoin address of a public or private key.
   embed-addr    Generate an address used for embedding record of data into the
                 blockchain
   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.
   validaddr     Validate an address.
  BRAIN STORAGE
   brainwallet   Make 256 bit bitcoin private key from an arbitrary passphrase.
   mnemonic      Make 12 word mnemonic out of 128 bit electrum or bip32 seed.
  HD / BIP32
   hd-priv       Create a private HD key from another HD private key.
   hd-pub        Create an HD public key from another HD private or public key.
   hd-seed       Create a random new HD key.
   hd-to-address Convert an HD public or private key to a Bitcoin address.
   hd-to-wif     Convert an HD private key to a WIF private key.
  MULTISIG ADDRESSES
   scripthash    Create BIP 16 script hash address from raw script hex.
  STEALTH
   stealth-addr            See a stealth address from given input.
   stealth-initiate        Initiate a new stealth payment.
   stealth-newkey          Generate new stealth keys and an address.
   stealth-show-addr       Show details for a stealth address.
   stealth-uncover         Uncover a stealth address.
   stealth-uncover-secret  Uncover a stealth secret.

OFFLINE TRANSACTIONS
  SCRIPTING
   mktx          Create an unsigned tx.
   rawscript     Create the raw hex representation from a script.
   set-input     Set a transaction input.
   showscript    Show the details of a raw script.
   showtx        Show the details of a transaction.
   sign-input    Sign a transaction input.
   unwrap        Validates checksum and recovers version byte and original data
                 from hexstring.
   validsig      Validate a transaction input's signature.
   wrap          Adds version byte and checksum to hexstring.

ONLINE (BITCOIN P2P)
  BLOCKCHAIN UPDATES
   sendtx-node     Send transaction to a single node.
   sendtx-p2p      Send tx to bitcoin network.

ONLINE (BLOCKCHAIN.INFO)
  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
  BLOCKCHAIN UPDATES
   sendtx-bci               Send tx to blockchain.info/pushtx.

ONLINE (BLOCKEXPLORER.COM)
  BLOCKCHAIN QUERIES (blockexplorer.com)
   blke-fetch-transaction    Fetches a transaction from blockexplorer.com

ONLINE (OBELISK)
  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-stealth        Fetch a stealth information using a network connection
                        to make requests against the obelisk load balancer backend.
   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-obelisk      Send tx to obelisk server.
  BLOCKCHAIN WATCHING
   monitor        Monitor an address.
   watchtx        Watch transactions from the network searching for a certain
                  hash.
  OBELISK ADMIN
   initchain      Initialize a new blockchain.

UTILITY
  EC MATH
   ec-add-modp      Calculate the result of INTEGER + INTEGER.
   ec-multiply      Multiply an integer and a point together.
   ec-tweak-add     Calculate the result of POINT + INTEGER * G.
  FORMAT (BASE 58)
   base58-decode         Convert from base58 to hex.
   base58-encode         Convert from hex to base58.
  FORMAT (BASE58CHECK)
   base58check-decode    Convert from base58check to hex.
   base58check-encode    Convert from hex to base58check.
   decode-addr      Decode a address from base58check form to internal RIPEMD
                    representation.
   encode-addr      Encode an address from internal RIPEMD representation to
                    base58check form.
  FORMAT (WIF)
   secret-to-wif    Convert a secret exponent value to Wallet Import Format.
   wif-to-secret    Convert a Wallet Import Format to secret exponent value.
  HASHES
   ripemd-hash      RIPEMD hash data from STDIN.
   sha256           Perform SHA256 hash of data.
  MISC
   qrcode           Generate Bitcoin QR codes offline.
  SATOSHI MATH
   btc              Convert Satoshis into Bitcoins.
   satoshi          Convert Bitcoins into Satoshis.

See 'sx help COMMAND' for more information on a specific command.

Next, we look at some examples of using sx tools to experiment with keys and addresses.

Generate a new private key with the operating system’s random number generator by using 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 reformat the public_key as an address using the addr command. We pass the public_key into standard input:

$ sx addr < public_key
17re1S4Q8ZHyCP8Kw7xQad1Lr6XUzWUnkG

The keys generated are so called type-0 nondeterministic 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 the mnemonic 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 using the mnemonic command again:

$ 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 regenerate thousands of keys, all derived from a single seed in a deterministic chain. This technique is used in many wallet applications to generate keys that can be backed up and restored with a simple multiword mnemonic. This is easier than having to back up the wallet with all its randomly generated keys every time a new key is created.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required