Inspiration

Lightning micropayments offer the ability to create interesting new gameplay and monetization strategies, whilst I think mainstream use of lightning and bitcoin in gaming will not come soon I believe that creating new uses cases and gameplay is a great way to help develop the ecosystem whilst at the same time allowing people to become educated and gain experience with regards to using bitcoin and lightning.

For this hackathon I have developed a trilogy of games to help show case what is possible now with lightning and mobile "gaming oriented" lightning wallets

What it does

Game 1: Blockchain Fighters Video Demo HERE

The first game in the trilogy is basically a Street fighter style game with a complimentary mobile app to allow streaming of payments. The game and app together allow players to battle each other and stream lightning micro payments between them as they hit each other. Player 1 hits Player 2 and gets a micropayment from them and vice versa. The Video demonstrates this well

Game 2: Quick Draw Video Demo HERE

The second game I feel is the most novel and complex, with the introduction of HOLD invoices into the LND code base it became possible to use a secret pre image to create a trustless betting game. Most trustless betting games in the bitcoin space are quite bland and are casino style games therefore I wanted to create something a little more fun. My idea was to create a western quick draw app where 2 players have a mobile and bet/escrow some bitcoin over lightning. The players then shoot each other with the quickest receiving the bitcoin they escrowed before the game started. The great thing is that with HOLD invoices it is possible for the users to trustlessly escrow this bitcoin between them with no 3rd party on the lightning network

You can see a video demo and read up on the development in the complimenting medium article HERE

Game 3: Asgard Video Demo HERE Video Trailer HERE

Asgard is a pokemon go style geocaching app but instead of catching pokemon you catch twitter monsters and receive lightning tips. The game uses the twitter API to load recent tweets from famous bitcoin twitter profiles. These profiles are then populated as monsters on a 3d map built based on the users local environment. A user must them battle one of these twitter monsters and if they win they can receive a lightning tip direct to their mobile lightning wallet.

How I built it

Game 1: Blockchain Fighters This game is built up of 3 components, Game, Mobile Wallet, Server.

The server is a NodeJS hosted websocket server who's function is to allow the game and mobile wallets of the players to connect to each other and send messages/payments back and forth.

Each player has a websocket enabled mobile lightning wallet which they link to the game. The game presents a QRCode which the mobile wallet scans in order to link with the game.

This QRCode contains the following information

  1. Games name
  2. Games icon
  3. Websocket server url
  4. Maximum charge amount in satoshis

This information allows the wallet to show the games name and icon to the player as well as asking permission to let the game charge the wallet up to the maximum charge amount stated. This is important as it lets the game stream payments without the user having to confirm each transaction which would slow down game play. For example the mobile wallet would present

"Allow Blockchain Fighters to charge up to $1 without confirmation?"

Once the user agrees to this the wallet connects to the game server via the provided websocket message and sends a "ready" message to the game, once the 2nd player has done this the game commences.

During battle, as Player 1 hits Player 2 the game sends a request to Player 1 to create an invoice for 1 satoshi, it then forwards this invoice to Player 2 to pay. Conversely if Player 2 hits Player 1 the game requests Player 2 to create an invoice and forwards it to Player 1 to pay. These invoices and requests are all quickly sent between the players via the websocket server.

The wallet is a fork of Pebble Wallet with a special gaming mode added, it can be found on the repo HERE

The only addition needed apart from UI to handle the linking and gameplay was to add a websocket library for iOS and Java.

Game 2: Quick Draw This was complex hence a full explanation can be found in the medium article HERE But it basically involved forking LND to add bindings for HOLD invoices when compiling for mobile wallet build.

Game 3: Asgard I used the Mapbox SDK in Unity3D to create a pokemon go style app, I then used the twitter API to load a list of prominent bitcoin twitterers and fed this as JSON data into the app. The app then uses some monster 3d prefabs with the twitter data to spawn a twitterer monster. I use a custom shader and the twitterers profile image to skin the monster with the twitterers avatar, finally the monster presents the recent tweets of the twitterer.

Battling a monster is very similar to pokemon go, however instead of throwing a pokeball your fling Thor's hammer at the monster. Once a monster is hit it ask the users for an invoice for a certain amount which the game will pay in order to send the user a tip.

In order to achieve this I created a nodeJS server which GRPCs into an instance of LND. The app then sends the users invoice to this server which then pays the invoice via the LND send payment command. the server code can be found HERE

A user can paste an invoice generated by their lightning wallet into the game to receive the tip however I felt this was a bit cumbersome so I added an intent into the game to let it link with pebble wallet and allow the app to request pebble wallet to create an invoice and return it to the player, hence a user can get a tip with a click of a button via linking with pebble wallet. The unity app uses the following URI in its intent to achieve this.

lightning:addinvoice?amt=1

Challenges I ran into

Game 1: Blockchain Fighters There were not too many challenges regarding lightning with this project, it uses the basic addinvoice and sendpayment rpc commands. Pebble wallet which I developed allows these calls to be easily handled. The main challenge was implementing the trafficing of messages between the mobile wallet and game via websockets.

Also it is not ideal to use the addinvoice/sendpayment model as each time a player wants to send a micropayment to them it becomes a 2 step process, i.e. ask them to create an invoice send it over to you and then pay it.

I would like to continue and adapt this project in the future to use "Invoiceless" lightning payments with Sphinx. Sphinx payments were not ready to use at the time of this hackathon but they allow the sending of a payment directly to a user's node's pubkey without them having to create an invoice and would thus simplify the process and should increase the speed of transactions.

Game 2: Quick Draw This was the most difficult of the games because of the lack of support of HOLD invoices in the LND code base, I had to fork LND and create the mobile bindings to let pebble mobile wallet use the HOLD invoices. I then had to create native Java GRPC protobufs for HOLD invoices as well. Another challenge was the stability of the local mobile LND i.e. the version of LND running locally on the user's phone in the pebble app. I tried to open channels between the 2 players wallets to allow the payments to be sent, however as the mobile wallet was constantly going off and online due to it being mobile the channels would often become inactive or were not usable immediately after the app loaded and you would need to wait for the wallet to "catch up"/sync the graph etc hence It worked better when pebble was connected to remotely running always on nodes.

Game 3: Asgard The main challenge with this game is detecting bots and cheaters, as the game sends the user bitcoin it is subject to being attack/abused by users to try and claim tips they did not earn. This is a difficult problem to solve but it can be mitigated by encrypting all traffic between the app and the server. This is done by encoding a private key in the app in a way that cannot easily be decompiled and found. To do this the key is stored as a byte array not a string which ensures it is stored somewhat randomly in memory where as a string would be stored inline and hence could be picked out by a hex editor. To further mitigate this the private key is then manipulated by an algorithm at runtime to change it and then combined with a second key in which part of it is received from the server. Finally once the app is built it can be obfuscated and have symbols stripped, effectively it makes it virtually impossible for a non dedicated hacker to crack the app and find the key. Now if a hacker wanted to try and trick the server to receive a tip they could not produce a valid request that the server could decrypt. In addition to this basic IP detection on the server can help deduce if the user is a bot or abusing the request limit. Finally each request for a tip from the server is stored so that an attacker cannot simply replay a previous valid request.

Accomplishments that I'm proud of

Game 1: Blockchain Fighters I am proud of combining the idea of using web sockets with lightning micropayments and developing a mobile wallet to allow this to be done. Without a mobile wallet it would become more difficult to enjoy such a game as players would need to bring a personal computer with LND running with them to each event in order to play the game. With a mobile wallet users can attend a party or event which has the game and both link their wallets to the game to start playing quickly.

Game 2: Quick Draw I am proud to think of a use case of HOLD invoices for a non merchant situation, mainly using the pre image and hash nature of the HOLD invoices to create a betting game allowing trustless escrow of funds. I am also proud I managed to fork LND enable HOLD invoices and create a working version for mobile.

Game 3: Asgard I am proud of the game concept, pokemon go is a popular and well known game so a lightning version is a great and easy way for players to get introduced to and start enjoying lightning

What I learned

Game 1: Blockchain Fighters I learned how to connect mobile lightning wallets via websockets to allow the streaming of payments

Game 2: Quick Draw I learned how to use the new HOLD invoice rpc api.

Game 3: Asgard I learned how to use the unity mapbox sdk and also how to link games with mobile wallets via intents to request invoices to send tips to.

What's next for Lightning micropayment gaming trilogy

Game 1: Blockchain Fighters I would like to implement invoiceless payments with sphinx as well as further develop the game to add more characters and a smoother UI. Currently the game is quite simple and charges 1 satoshi to the other player per hit, however I would like to, depending on the attack, change the price. For example a simple jab would cost 1 satoshi but a special move could be 100 satoshis or more. Finally the players HP could be based on their satoshi balance, for example each player starts with 100 satoshis and when that hits 0 the player KOs and the other player wins. This could be expanded to allow the audience to send HP to their favourite player to help them. Once these features are implemented I would bundle the game into a program that anybody could download and enjoy at their meet up/event/party. This game also currently requires a mobile wallet with support for web sockets and the protocol hence I will continue work on pebble and encourage other wallets to also adopt the protocol.

Game 2: Quick Draw To increase the stability of LND running locally in a mobile wallet, currently it is hard for people to enjoy this game as they need to have LND built with a special invoices flag. Hence a downloadable mobile wallet such as pebble would make it more accessible.

Game 3: Asgard I want to add a funding model to the game, currently the game just tips users however there needs to be a source of revenue for the tips. One idea is to add in app payments to the game, players can purchase more weapons skins features and a percentage of that can be sent back to the players as tips. Another idea is to charge for the featuring of twitterers in the game. For example twitterers can pay to the game in order to increase the presence/spawn rate in the game, effectively advertising their twitter profile and getting them more followers. Twitterers who pay more appear more as monsters in the game etc.

Share this project:

Updates