Skip to content

This repository provides a hands-on tutorial on how to develop a battleship game using zero-knowledge principles

License

Notifications You must be signed in to change notification settings

chainstacklabs/zk-battleship-game-hardhat

Repository files navigation

Labs

Chainstack is the leading suite of services connecting developers with Web3 infrastructure

         

HomepageSupported protocolsChainstack blogChainstack docsBlockchain API reference
Start for free

Game of Battleship Solidity and Hardhat

This project is the practical part of a tutorial that outlines how to build a game of Battleship on the Ethereum blockchain. It discusses the challenges of storing private data on a public blockchain and proposes a solution that uses public-private signatures to ensure that the players' ship coordinates remain private while still allowing the game to be played on the blockchain.

Find the article in the Chainstack developer portal

Quick start

Clone the repository:

git clone https://github.com/chainstacklabs/zk-battleship-game-hardhat.git

Install dependencies:

npm install

Run tests:

npx hardhat test

Add environment variables to .env.sample and rename to .env:

Deploy a node with Chainstack:

  1. Sign up with Chainstack.
  2. Deploy a node.
  3. View node access and credentials.

In this specific version we specify the account playing by taking the private key from the environment variable. Develop this further by adding a user interface.

SEPOLIA_ENDPOINT="YOUR_CHAINSTACK_ENDPOINT"
GOERLI_ENDPOINT="YOUR_CHAINSTACK_ENDPOINT"
MAINNET_URL="YOUR_CHAINSTACK_ENDPOINT"
PRIVATE_KEY="PRIVATE_KEY_1"
PRIVATE_KEY_2="PRIVATE_KEY_2"
ETHERSCAN="ETHERSCAN_API_KEY"

Use the Chainstack faucet to get test funds:

Deploy the smart contract:

npx hardhat run scripts/deploy.ts --network sepolia

or

npx hardhat run scripts/deploy.ts --network goerli

Now the contract will be deployed and verified on Etherscan.

Example response

Compiled 3 Solidity files successfully
Deploying contract...
BattleShipGame was deployed at: 0x27F1a73dB2aCD037B9AeAA993F1A2D6c5606456B
Waiting for 3 blocks...
3 blocks have passed!
Verifying contract...
Contract verified!
The contract 0x27F1a73dB2aCD037B9AeAA993F1A2D6c5606456B has already been verified

About

This repository provides a hands-on tutorial on how to develop a battleship game using zero-knowledge principles

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published