-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
171 lines (131 loc) · 5.54 KB
/
Copy pathapp.ts
File metadata and controls
171 lines (131 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { setup } from "./mud/setup.js"
import { zkFunctions } from "./zero-knowledge.js"
import { writeFileSync } from "node:fs"
import { execSync } from "child_process"
const mudSetup = await setup()
function assert(condition: boolean, message: string) {
if (!condition) {
throw new Error(message || "Assertion failed");
}
}
type Game = {
map: boolean[][]
mapWithBorders: boolean[][]
hash: string
}
let gamesInProgress: Record<string, Game> = {}
// We can't do anything until we have the configuration
mudSetup.network.components.Configuration.update$.subscribe(async update => {
const height = update.value[0]?.height
const width = update.value[0]?.width
const mineNumber = update.value[0]?.numberOfBombs
// If any variable isn't defined, we don't have the configuration yet
if (width === undefined || height == undefined || mineNumber === undefined)
return ;
console.log("Configuration retrieved")
const {calculateMapHash, zkDig, verifyDig, solidityVerifier} =
await zkFunctions(width, height)
console.log("Zero-knowledge created")
assert((width+2)*(height+2) < 512, "map can't exceed 512 bits after adding borders")
assert(width*height > mineNumber, "can't have more mines than the map size")
try {
writeFileSync("../contracts/src/verifier.sol", solidityVerifier)
const verifierAddress = execSync("./deployVerifier.sh").toString().slice(0,-1)
console.log(`Verifier address: ${verifierAddress}`)
const tx = await mudSetup.network.worldContract.write.
app__setVerifier([verifierAddress])
await mudSetup.network.waitForTransaction(tx);
} catch (err) {
console.error(err)
process.exit(-1)
}
// Handle requests for new games
mudSetup.network.components.PendingGame.update$.subscribe(async (update) => {
// Only create a new game if wanted
if (!Object.values(update.value)[0]?.wantsGame)
return
const gameHash = newGame()
const newGameAddress = '0x' + update.entity.slice(-40)
console.log(`New game ${gameHash} for ${newGameAddress}`)
const tx = await mudSetup.network.worldContract.write.app__newGameResponse(
[newGameAddress, gameHash])
await mudSetup.network.waitForTransaction(tx);
})
mudSetup.network.components.PendingDig.update$.subscribe(async (update) => {
console.log(`Dig in game ${update?.entity} ` +
`at (${Object.values(update.value)[0]?.x},${Object.values(update.value)[0]?.y})`)
if (!gamesInProgress[update?.entity]) {
console.log("ERROR: Trying a non-existent game")
console.log(`\tGame identity: ${update?.entity}`)
console.log(`\tGames in progress: ${Object.keys(gamesInProgress)}`)
return
}
const digResult = zkDig(gamesInProgress[update?.entity].mapWithBorders,
Object.values(update.value)[0]?.x, Object.values(update.value)[0]?.y)
if (update?.entity != digResult.inputs[2]) {
console.log("ERROR: Hash does not match game")
console.log(`\tMy gameID: ${update?.entity}`)
console.log(`\tHash from zero knowledge: ${digResult.inputs[2]}`)
return
}
const tx = await mudSetup.network.worldContract.write.app__digResponse(
[digResult.inputs[2], digResult.inputs[0], digResult.inputs[1],
digResult.inputs[3],
[
digResult.proof.a[0], digResult.proof.a[1],
digResult.proof.b[0][0], digResult.proof.b[0][1],
digResult.proof.b[1][0], digResult.proof.b[1][1],
digResult.proof.c[0], digResult.proof.c[1],
]
]);
await mudSetup.network.waitForTransaction(tx);
})
const newGame = function() : string {
const newGame: Game = createGame()
gamesInProgress[newGame.hash.toString()] = newGame
return newGame.hash.toString()
}
const createGame = function() : Game
{
let map: boolean[][]
map = new Array(width)
for(let x=0; x<width; x++) {
map[x] = new Array(height)
for(let y=0; y<height; y++)
map[x][y] = false
}
for (var i=0; i<mineNumber; i++) {
let x: number = Math.floor(Math.random()*width)
let y: number = Math.floor(Math.random()*height)
while (map[x][y]) {
x = Math.floor(Math.random()*width)
y = Math.floor(Math.random()*height)
}
map[x][y] = true
}
const mapWithBorders : boolean[][] = makeMapBorders(map);
return {
map,
mapWithBorders,
hash: calculateMapHash(mapWithBorders)
}
}
// Because of the way Zokrates works, we need to give the map empty borders
const makeMapBorders = function(map: boolean[][]) : boolean[][] {
let mapWithBorders = new Array(width+2)
for(let x=0; x<width+2; x++) {
mapWithBorders[x] = new Array(height+2)
}
for(let x=0; x<width; x++) {
mapWithBorders[x+1][0] = false
mapWithBorders[x+1][height+1] = false
for(let y=0; y<height; y++)
mapWithBorders[x+1][y+1] = map[x][y]
}
for(let y=0; y<=height+1; y++) {
mapWithBorders[0][y] = false
mapWithBorders[width+1][y] = false
}
return mapWithBorders
}
})