As part of the bchd project we maintain a wallet library and application called bchwallet that is capable of either connecting to your bchd full node or running in SPV mode using neutrino . Our goal with bchwallet is to provide a feature rich wallet backend that developers can use to power their applications.

A feature that is currently in progress and will hopefully be complete early next year is bi-directional payment channels. We’ll save the blog post about this feature until it’s close to being ready but in this post we want to talk about the technology that powers it.

In order to make payment channels work two bchwallets need to be able to communicate directly with each other to open channels, make micropayements, and close channels. This requires some kind of protocol for communication. In Bitcoin’s lightning network they rolled their own custom peer-to-peer network for this purpose. For a variety of reasons this is not really the best approach for our payment channel implementation. Instead we created a generic overlay network based on IPFS’s libp2p. The implementation can be found here.

This overlay network is a peer-to-peer network of connected applications. We say it’s “generic” because the goal is not just to connect bchwallets together for the purpose of payment channels, but to be used by any Bitcoin Cash application that needs access to a high performance, feature rich peer-to-peer network.

Out of the box you instantly get:

Extensible peer identities

Encrypted and authenticated connections

Protocol multiplexing

Stream multiplexing

Multi-transport

Tor integration

DHT — Key/Value store

DHT — Peer routing

DHT — Content routing

Pubsub

Using the overlay network library is really dirt simple:

privKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)



cfg := overlaynetwork.NodeConfig{

PrivateKey: privKey,

Params: &chaincfg.MainnetParams,

Port: uint16(4007),

DataDir: path.Join(os.TempDir(), "overlaynetwork"),

}



node, _ := overlaynetwork.NewOverlayNode(&cfg)

node.StartOnlineServices()

You can take a look at the examples to see how to register a custom protocol, store and retrieve data from the DHT, and publish data over pubsub.

There is also a compatible javascript version of libp2p that can run entirely in the browser. Making a javascript version of the overlay network should be really easy as the entirety of the library is only 614 lines of code.

So what can you build with the overlay network?

Payment channels obviously. But you could also use it for coin mixing protocols, p2p order books, p2p gambling apps, atomic swap apps, etc.

At the end of the day there’s really nothing stopping all Bitcoin Cash apps from being interconnected and having the ability to communicate with each other.

If this interests you and you have any questions feel free to reach out to us!