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

Dash InstantSend on xPub API #983

Open
pshenmic opened this issue Oct 25, 2023 · 7 comments
Open

Dash InstantSend on xPub API #983

pshenmic opened this issue Oct 25, 2023 · 7 comments

Comments

@pshenmic
Copy link

Hey guys, I’m trying to do something and I need some help. I’m not quite sure how to approach this.

What I’m trying to do is to enable InstantSend in the TrustWallet. This is quite old and established Dash feature that enables near instant transactions in the blockchain that takes only 1-2s, but it still yet not implemented in all wallets. My intention is to complete it

InstantSend works by settling pending mempool transactions forcefully in the chain with the help of the masternode quorums. That means, that once transaction gives InstantSend status, it cannot be removed or rejected from the chain. Few years ago, there were a major network upgrade - all transactions are now InstantSend and it works several times faster.

I found out that Trust Wallet seems be using blockbook api for querying data in the app, and it’s using xPub blockbook api to show total confirmed balance, but it does not include InstantSend transactions.

Indeed, there is a tx-specific blockbook api, that shows the status of the InstantSend, but it shows that only per transaction, and not for xpub.

Blockbook xpub api call does have a list of txids (even unconfirmed), that you could possible look at with tx-specific, but you can’t match the correct output, because you don’t know what address exactly belongs to given xpub, so you can’t sum it. Alternatively, there is a tokens option in the api, that list all address transfers on this xpub, but unfortunately it shows it only for confirmed transactions.

So, to wrap everything up, I would like to get the total balance for the given xPub that also includes InstantSend transactions too. How can I make this possible? I’m eager to suggest all necessary changes via PR.

@greatwolf
Copy link

I did run into a somewhat similar issue #578 of getting Dash specific info. The solution I'm using at the moment is to make two api calls, one to endpoint /api/v2/utxo/<xpub_addr>. The json response includes unconfirmed transactions and also the associated xpub address for that transaction in the address field.

Afterwards I make a second call to /api/v2/tx-specific/<txid>. The response will include an instantsend and chainlock field.

It's not really efficient and would be nice to be able to just do this with one call but I don't know how accommodating the upstream maintainers are to accepting PR for improving this situation. Would be wonderful if they did.

@pshenmic
Copy link
Author

pshenmic commented Nov 7, 2023

@greatwolf
How do you exactly match your unconfirmed transactions outputs with the given xPub? I don't think it works correct all the way, because you have to know which addresses are belongs to this xPub to correctly sum all outputs. As an example, often transactions has at least two outputs: one for the target and second for the change. Which one will you choose in this case?

/api/v2/utxo/<xpub_addr> does have an optional field called tokens, which lists all of the addresses of the given xPub that has transactions to it, but again it works only with the confirmed transactions.

@greatwolf
Copy link

greatwolf commented Nov 8, 2023

I might be misunderstanding the problem but when you make the request you provide the xpub as a parameter that forms the url. So that means all the transaction outputs in the json response returned by /api/v2/utxo/<xpub_addr> is by definition belongs to that xpub.

As far as telling apart which is the recipient and which is the change addr you just have to combine the txid with vout field which together forms the outpoint. Assuming the recipient address is from another xpub, eg. you're not sending funds to yourself, then the txid + vout listed in the response must belong to your change address.

Also, there's a path field as well which tells you exactly what the HD derivation path is for a given address. Assuming we're just following normal convention, change addresses use the path m/44'/5'/0'/1/*, so that would be another way to tell them apart.

@pshenmic
Copy link
Author

pshenmic commented Nov 10, 2023

Let's say user is receiving a mining rewards on his xPub, as mining pools usually send payments to multiple recipients in one tx (1 input - many outputs).

In that case, when you fetch an unconfirmed transaction, what vout would you choose to calculate overall xpub balance?

The path field only exists inside tokens, which lists all the addresses happened on your xPub, but it only shows it for the confirmed transactions.

image

@greatwolf
Copy link

Are you sure you're not confusing /api/v2/xpub endpoint with /api/v2/utxo? It looks utxo endpoint doesn't have a token parameter, https://github.com/trezor/blockbook/blob/master/docs/api.md#get-utxo. The response from your screenshot looks like it's from the former.

@greatwolf
Copy link

greatwolf commented Nov 10, 2023

Forgot to note, there is a subtle detail. Make sure you use /api/v2/utxo and not /api/utxo because that will default to version 1 of the api response. version 1 won't have the path or address fields in the response.

@greatwolf
Copy link

As far as summing the balances, you might not need to do it yourself. The /api/v2/xpub endpoint does have a balance and unconfirmedBalance field in satoshi's/duffs.

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

No branches or pull requests

2 participants