Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example to calculate address balance #36

Open
KryptoNova opened this issue Jan 13, 2018 · 5 comments
Open

Example to calculate address balance #36

KryptoNova opened this issue Jan 13, 2018 · 5 comments
Labels

Comments

@KryptoNova
Copy link

KryptoNova commented Jan 13, 2018

Hello,

I am working on a project that has many private keys that have balances in them. I do not want to have to go out to a public API to query for security concerns.

I see there is a way to get the unspent amounts for an address in the example. However, I am not seeing a way to connect the private key to it's address. I believe this is what I would have to do to determine how much BTC the private key is linked to.

Is the data structure in indexd able to perform this type of query against the blockchain?

Thanks in advance!

@dcousens dcousens self-assigned this Jan 13, 2018
@KryptoNova
Copy link
Author

I'm still learning about the nuts and bolts of the blockchain and have refined my understanding of the question I first posed. From what I understand, there is a public key that is stored in the blockchain along with the transaction. I understand that each private key can have two public addresses, compressed and uncompressed. The private key can be used to calculate it's associated public keys, but the public keys cannot be reversed to find the private key. Please correct me if I am wrong as I am still learning.

If this is the case, then as long as the public key is a searchable field in the database that is being parsed, then it should be possible to determine the unspents of a private key. Is the data being parsed able to be queried based on the public keys associated with the transactions?

@dcousens
Copy link
Contributor

dcousens commented Jan 16, 2018

@KryptoNova the blockchain contains blocks, which each have transactions, which each have inputs and outputs.

Each output has a script (the output script) which is typically (but not always) of some form akin to:

  • P2PKH OP_DUP OP_HASH160 {hash160(pubKey)} OP_EQUALVERIFY OP_CHECKSIG
  • P2SH OP_HASH160 {hash160(scriptPubKey2)} OP_EQUAL
  • P2WPKH 0 {hash160(pubKey)}
  • P2WSH 0 {sha256(scriptPubKey2)}

By looking at the output scripts, you can build an index (aka database) of unspents outputs for each unique script, or each unique public key hash (hash160(pubKey)), or some combination.
Rarely will you see the raw public key on its own.

@KryptoNova
Copy link
Author

I think I understand what needs to be done to make this happen. Thank you for the enlightenment!

@dcousens
Copy link
Contributor

dcousens commented Jan 17, 2018

@KryptoNova apologies, I thought this was question was for another project.

Is the data structure in indexd able to perform this type of query against the blockchain?

indexd has a script index. A script can be derived from an address. If you have an address, or many addresses, you can determine the balance using indexd.

I'll add an example here at some point.

@dcousens dcousens reopened this Jan 17, 2018
@dcousens dcousens changed the title Calculate Balance Example to calculate address balance Jan 17, 2018
@dcousens dcousens removed their assignment Dec 27, 2018
@bchociej
Copy link
Contributor

bchociej commented Mar 5, 2021

@KryptoNova and others who might be interested: bitcoinjs/regtest-server implements most of this functionality and may serve as your example: https://github.com/bitcoinjs/regtest-server/blob/b699cfad29e21272c7c644d9d66c5ad089d56be8/routes/1.js#L107-L112

  router.get('/a/:address/unspents', addressWare, (req, res) => {
    let { scId } = req.params
    indexd().utxosByScriptRange({
      scId, heightRange: [0, 0xffffffff], mempool: true
    }, DBLIMIT, res.easy)
  })

The result is an array of UTXO objects; you may sum the value field of each to find the unspent balance of the address in satoshis.

Note the addressWare middleware function for converting the address to a script ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants