Building peer-to-peer games on IPFS — a look at Interplanetary Tag
To showcase a few ways that game developers can leverage IPFS using Textile, we recently built, Interplanetary Tag. The game is simple, it’s a real-life game of tag with where the game state is tracked and reported in a mobile app running on every player’s phone, tags happen using a decentralized database, and every event player who sends updates to the database is cryptographically verified. We recently did our first test run of Interplanetary Tag at the IPFS Camp in Barcelona. Because a lot of people asked how it worked, I wanted to share some of the basic Textile features used by Interplanetary Tag and how other developers can use them in future games.
Public profiles and user discovery
Most games today require some form of a profile, a display name and an avatar perhaps. Textile provides a couple of straightforward APIs that allow apps to create cryptographic identities (key pairs) for each user and attach player metadata to those identities. Cryptographic identities make it so each user in our game (or any app) will sign their transactions, messages, or updates in a verifiable way so all other users can be sure of an update’s origin.
We use identities in Interplanetary Tag to link simple display names with verifiable events. We also use the same system in Textile Photos to allow users to create profiles for sharing an connecting. You can learn how to create user profiles in the Tour of Textile, but let’s just say, it’s very very easy.
Game state, sharing, connectivity, and recovery
Every game needs to track the state of the game. To help handle this, Textile comes with an eventually-consistent database called, threads. Threads are a bit more than databases, threads facilitate p2p transfer of updates, can leverage remote cafes for short or long term storage, provide end-to-end encrypted chat and sharing, multi-device synchronization, recovery, and much more. Importantly for our game, threads allow every user’s device to track the latest state of the game.
In Interplanetary Tag, game updates are simultaneously written to the local database, transmitted to other players over p2p, and streamed to dedicated cafe nodes to maintain persistent replicas of each database update. As the game progresses, changes sent to the cafe can help new users who join the game to rapidly find the most up-to-date state of the game.
In-app rules and validation
A game developer is going to need to think a lot about how to encode and enforce rules in their game. Using the cryptographic identities described above, we can can enforce roles (who can do what), transaction order (e.g. possession order), and other common game rules in a straight forward way.
The first part is the cryptographic signatures that are part of Textile identities, where every user in the game can verify the originator of a message or an update, no spoofing is possible. Every user in your game will have a unique peer ID contained in their profile. When developing an app on Textile, you don’t need to think much about the cryptographic identities. However, behind the scenes, Textile will use them to validate ever message that is transmitted. That can be seen in the log overview below, where both updates that don’t match the game state and updates that aren’t signed by the expected users are rejected by all other user’s nodes.
We use a very simple approach to validation and verification of game events in Interplanetary Tag:
- The second event (above) originates from the device of the user being tagged, User C in this case.
- That event is cryptographically signed by User C, meaning all other users now believe that User Cis it.
- If we wanted another layer of security, User A could now counter-sign that event.
With the countersignature, we could easily flip the model by having the event originate from User A and get counter-signed by User C before anyone trusts the event. This basic verification system can be extended to enable or support many different rule-systems in your game.
Read more about how peer IDs and their cryptographic identity is created in our previous blog post, How IPFS Peer Nodes Identify Each Other.
Multi-player chat is ubiquitous in games today. Textile makes it simple to deploy chat (or multiple chat rooms) into any app you create, including games. Every thread database you create has a light-weight, dedicated, and encrypted side-channel for members to exchanges messages. We use chat in both Interplanetary Tag and in Textile Photos to let subgroups within the app have private messaging among members.
Interplanetary Tag runs on Textile, meaning that every user running the app is running a local IPFS node right on their device! We built it using Textile’s React Native Boilerplate as a quick way to start the app from an already working example. The boilerplate exposes the Textile APIs right in React Native and builds off both our iOS and Android native libraries.
We build Interplanetary Tag using our React Native Boilerplate and you can find the complete game code on the game’s branch. The bulk of interesting code can be found in a handful of Sagas that handle game database setup, event creation, tags, invites, etc.
We taught a live workshop at IPFS Camp on the basics of Interplanetary Tag with Textile. We’ll share the video of that workshop as soon as it’s available, but in the meantime, you can find the material online, here.
If you have plans to build a game on IPFS, we’d love to hear about it. Join us on our Slack channel or shoot us a message over on Twitter. If you want to read more about building games with Textile, head on over to our documentation. Let’s see what you can create!
If you’ll be at DWeb Camp this weekend, we hope to play a few more games of Tag.