SPV in Cordova app


Thank you for providing the bitcore js library. I am working on a point of sales app, which should support verification of payments without requiring a server or similiar. The app should soley use the p2p network to verify. As I understand from the satoshi paper and from other sources, this is called simple payment verification (SPV).

I haven’t found a formal description of the concrete way SPV is working. So I am not very sure what is necessary to implement this. Basically, I have two main questions:

  • Is SPV already available within the bitcore library?
  • If not, what is missing to implement SPV with bitcore?

In my case I am developing a Cordova app, so the limitations, which are set by the browser (e.g. p2p connections) are not given directly.

BIP37 https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki is supported in bitcore-p2p and the MerkleBlock type is also available it bitcore-lib. There have been a few projects started from contributors, however I’m not sure of the status.

ok. thank you for your answer! I have looked into the proposed solutions, unfortunately i couldn’t make too much use of them. Maybe i have to study the code in more detail. Maybe I also have yet too less understanding of how SPV really works. Still, I have the feeling it is not precisely described anywhere.

I’d take a look at the Database service: https://github.com/bitpay/bitcore-node/blob/master/lib/services/db.js

From my understanding SPV is implemented by creating a chain of headers only and verifying proof-of-work, and then receiving MerkleBlock messages from peers instead of the standard Block message that includes all transactions. The MerkleBlock will only include transactions that match the filter that has been loaded for a peer.

There are potential downsides with SPV though, from sybil attacks, transactions being omitted, and not being able to verify soft forks when they happen, as such was the case with BIP066: https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki Adding a trusted peer, I think would resolve those issues. Also, enabling pruning of the block chain may be a better alternative to reduce the necessity to store the entire block chain: https://en.bitcoin.it/wiki/Running_Bitcoin#Command-line_arguments Though that may not be an option within a Cordova app.

Also, if you haven’t already, bitcoinj implements SPV, and would likely be the most complete implementation to reference: https://bitcoinj.github.io/

Did you have any luck getting an SPV client to work in cordova @Stefan_Huber? I’m looking to do something similar and would be very happy to hear about your progress.


  • I researched the code of bitcoinj about SPV and gained a good understanding of how SPV is done in this library.
  • Most features to implement SPV are already inside bitcore: e.g. bloom filters
  • My current main concern is:
    • bitcore-p2p works well in a node environment, as the network / socket api is available.
    • inside a cordova app (let alone inside a browser) the node net package is not available, thus the lib doesn’t work out of the box for this environment
    • i couldn’t yet figure out a way to get it working. however i found cordova plugins which allow direct network usage like sockets, which is required by bitcore-p2p

I will require a solution for that anyway. In my situation however, i only have to very incoming payments, as i have a POS scenario. As a fallback one could go the other “SPV-way” like electrum is doing with a dedicated server component. This however would, at least for me, be only in a worst case scenario…

I could figure out a person (Ryan X. Charles) who is also working on such a feature (https://www.youtube.com/watch?v=Qmo3zdYSqxM). I must admit, i haven’t looked into his work yet.

If you want we could join forces and come up with a solution together…

Got to about the same point as you, although the work of Ryan X. Charles was new to me. Seems he is using PeerJS to connect to the bitcoin nodes. Haven’t looked into or this could fully replace the nodejs net module though.

Myself would like to use this to build an SPV wallet on cordova/ionic. For security reasons using dedicated (trusted) servers is far less preferable in my case too. So guess we should definitely keep each other informed on any progress!

It seems bitcore-p2p is most likely the easiest way to get started. However the sockets will have to be created with something else than Net in nodejs. I have tried to use both ngWebsockets and the cordova plugin sockets-for-cordova but unto now to no avail. Does anyone know if it is possible to connect to a full node using websockets?

I am quite sure, that the only way to go is via tcp sockets. Looking into the code of bitcore-p2p reveals, that only in very view places the net package is used. So one could create a substitute module for net, which works with cordova.

  • the Peer (peer.js) is using a client socket, which sockets-for-cordova (as you mentioned) provides. So, using bitcore-p2p with a known set of peers (IP and ports) should be possible “easily”.
  • the Pool (pool.js) would require a socket server, for bootstrapping and further stuff… There is also a module for socket servers: here.

In order to use bitcore-p2p with cordova, one needs to browserify the module, to get rid of all the require, export stuff.

I propose and will also try the following:

  • create an api identical to the parts of net (from nodejs) used inside bitcore-p2p, based on the mentioned cordova plugins.
  • within the browserify build step, substitute the net module (somehow)

If anybody has time/interest, would be great to have some input/help…

:slight_smile: ok somebody has done this already: cordova-bitcore

Haha guess you found my repo :stuck_out_tongue: can’t seem to get the correct verack response (or any response) from the full nodes. Although it seems the tcp socket is correctly connected.

If somebody could help test the repo and help me with finding the bug I would greatly appreciate it.

(I’ll add a notice to the readme file that cordova-bitcore it is still in development)

Hey all. I did SPV implementation for Chromawallet and for ngcccbase.

Python implementation available here: ngcccbase/blockchain.py (probably it wouldn’t be easy for understanding without ngcccbase context)
Implement it was really easy, because we don’t have any limitations as in browser, so I just need to write small additional service chromanode.py that send chunks (2016 headers – two weeks network work) and can calculate proof for transaction. bitcoin proof js package

Later I write electrumjs-server (now deprecated) and chromanode, both use PostgreSQL and know about headers chunks and transaction proof. Chromanode also has colored coin scanner (epobc only).

JS implementation wasn’t easy, because the purpose was work without WebSQL (LocalStorage has only 2.5MB!). Firstly implementation was able in cc-wallet-core, but later I moved it to blockchainjs. blockchainjs has two modes: full and compact. Full mode store all headers in storage like WebSQL, SQLite, Memory. Compact mode store all headers of latest not fully chunk and sha256 hashes of previous chunks, so you just need ~248KB for chain that has 600k headers if you store in hex ((600000 mod 2016 + 600000 div 2016) * 160). If you need check transaction proof from fully chunk you should download all chunk and verify him through sha256 hash. Currently supports next storages: IndexedDB, LocalStorage, Memory, SQLite, WebSQL. But works only with chromanode… which required a lot of time for syncronization. SPV code isn’t huge, less than 500 lines on es7 with two modes.
I think blockchainjs with WebSQL in Cordova will work fine.

I hope links would helpful : )

1 Like