Bitcoin addresses – Ledger
5 stars based on
As I have explained in great details in a previous postthe balance of a Bitcoin wallet differs from the balance that can be computed by looking at the blockchain. The reason is that practically every time you make a transaction, the Bitcoin software sends a fraction of the funds back to you, to a new address.
Such addresses are invisible in the user interface, and for all practical purposes can be ignored by end users. These addresses are pre-allocated and constitute the key pool. In this article I explain how you can compute the balance of a BitcoinQt wallet. I will show how you can extract the public addresses contained in the wallet, whether it is encrypted or not, and demonstrate how to use the blockchain. The public key is what is turned bitcoin private key database a public address through hashing detailed below and the private key is the one bitcoin private key database to sign the transactions.
In order to compute the balance of a wallet, we need to extract all the public keys from the key bitcoin private key database. This is an old NoSQL embedded database from long before the term was coined. The keys from the key pool are stored directly into the database as individual records. Bitcoin private key database database keys are split into two parts. The first part of the DB key is some kind bitcoin private key database identifier, that describes the type of record.
Non encrypted Bitcoin keys are identified with key while encrypted keys are identified with ckey. The second part of the DB key is the Bitcoin public key itself, preceded by a single byte that indicates the length in bytes of the public key. The database record associated to the DB key is the private Bitcoin key, which may or may not be encrypted. This splitting of the DB key into two bitcoin private key database may sound odd, you have to remember that the DB keys must be unique to identify a record.
You can see details about the key loarding algorithm in walletdb. Look at the CWalletDB:: I had no luck using node-bdb for reading the wallet with node. Instead, I used a crude algorithm looking for the ckey and key strings within the wallet.
Once you have obtained the Bitcoin public keys from the pool, you can turn them into a Bitcoin addresses through a complicated process bitcoin private key database hashing, which is well documented.
Bitcoin private key database, I found a discrepancy with the documentation. The length of the keys I read from my wallet were all 33 bytes instead of the expected 65 bytes. Both public and private keys are points on a specific elliptic curve Secpk1 whose equation is. The keys are the and coordinates stored as 32 bit integers preceded by one extra header byte that OpenSSL uses to store format information.
Since and satisfy the known equation of the curve, you can store a single coordinate and a sign to compute the other coordinate with. This is what is called a compressed keyand it is 33 bytes long.
I initially thought that I would have to decompress the key, and convert it to the 65 bytes format in order to compute the address. I suspect that means you could have multiple public addresses associated to the same public key: You can simply hash the public keys with with the ripemd and sha algorithms as documented. The details of the hashing are strangely complicated, but easy to follow, and you end up with a 25 bytes binary address. As explained in the source code and the docs, this encoding is used instead of the easier to calculate and more base64 encoding, to avoid confusion between similar characters like 0OIl.
These APIs are public and can be used without authentication, but they are rate limited. This can be a problem if you use naively the single address API. I wrote a quick and dirty sample using node.
The Problem As I have explained in great details in a previous postthe balance of a Bitcoin wallet differs from the balance that can be computed by looking at the blockchain. Computing Bitcoin public addresses Once you have obtained the Bitcoin public keys from the pool, you can turn bitcoin private key database into a Bitcoin addresses through a complicated process of hashing, which is well documented.
Source code I wrote a quick and dirty sample using node.