forked from actuallyfair/vx-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmines.ts
214 lines (183 loc) · 5.44 KB
/
mines.ts
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { bytesToHex, randomBytes, utf8ToBytes } from "@noble/hashes/utils";
import { sha256 } from "@noble/hashes/sha256";
import { hmac } from "@noble/hashes/hmac";
import { bls12_381 as bls } from "@noble/curves/bls12-381";
import * as vx from "./vx";
import * as readline from "node:readline/promises";
import { stdin, stdout } from "node:process";
import { CommitmentContext, MessageContext, RevealContext } from "verifier";
import { assert } from "tsafe";
import { bytesToNumberBE } from "@noble/curves/abstract/utils";
import { Currency } from "verifier/dist/generated/currency";
import { computeMineLocations } from "verifier/dist/compute-wager";
async function main() {
console.log("Running vx demo... (mines)");
// Let's generate a random seed
const GS_SEED = randomBytes(32); // very secret!
const commitment = sha256(GS_SEED);
const mines = 1;
const cellLineBreak = 2;
const cells = 4;
const commitContext: CommitmentContext = { sha256Commitment: {} };
const VX_PUBKEY = await vx.make_commitment(commitment, commitContext);
console.log(
`We are going to play mines with ${mines} mines in a ${cells} cell grid`
);
console.log("--");
// Allow the user to pick their own player seed by using console input
const rl = readline.createInterface({ input: stdin, output: stdout });
const playerSeed = await rl.question("Enter player Seed: ");
let balance = 0;
let index = 0;
while (true) {
const res = await rl.question(
`[${index}] Do you want to bet (B) or Quit (Q): `
);
if (res.toLowerCase() === "q") {
break;
}
if (res.toLowerCase() !== "b") {
continue; // try again...
}
console.log("---");
// ok we're betting
const amount = { currency: Currency.BTC, value: 100 }; // 100 satoshi
const wager: MessageContext = {
mines: {
start: {
amount,
cells,
cellLineBreak,
mines,
},
},
};
let subIndex = 0;
const message = hmac(
sha256,
GS_SEED,
utf8ToBytes(`${playerSeed}:${index}`) // We don't use the subIndex here, because it's zero
);
subIndex++;
const vxSignature = await vx.make_message(
commitment,
message,
index,
subIndex,
wager
);
const verified = bls.verify(vxSignature, message, VX_PUBKEY);
assert(verified);
balance -= amount.value;
let revealedCells: Set<number> = new Set();
while (true) {
const res = await rl.question(
`[${index}] Pick a number between 0 and ${cells - 1} or Cashout (C)?: `
);
const message = hmac(
sha256,
GS_SEED,
utf8ToBytes(`${playerSeed}:${index}:${subIndex}`)
);
if (res.toLowerCase() === "c") {
const messageContext: MessageContext = {
mines: {
cashout: true,
},
};
const vxSignature = await vx.make_message(
commitment,
message,
index,
subIndex,
messageContext
);
const verified = bls.verify(vxSignature, message, VX_PUBKEY);
assert(verified);
subIndex++;
console.log("You have been cashed out! ");
const mineLocations = computeMineLocations(
vxSignature,
revealedCells,
cells,
mines
);
console.log("The rest of the mines were here: ", mineLocations);
// TODO: ..some deterministic algo to fill the rest of the board
// TODO: figure out their winnings
} else {
const pickedCell = Number.parseInt(res, 10);
if (
!Number.isSafeInteger(pickedCell) ||
pickedCell < 0 ||
pickedCell >= cells
) {
console.log("Unknown command or invalid");
continue; // try again...
}
const messageContext: MessageContext = {
mines: {
move: {
cell: pickedCell,
},
},
};
const gsContribution = hmac(
sha256,
GS_SEED,
utf8ToBytes(`${playerSeed}:${index}:${subIndex}`)
);
const vxSignature = await vx.make_message(
commitment,
gsContribution,
index,
subIndex,
messageContext
);
const verified = bls.verify(vxSignature, gsContribution, VX_PUBKEY);
assert(verified);
subIndex++;
const mineLocations = computeMineLocations(
vxSignature,
revealedCells,
cells,
mines
);
if (mineLocations.has(pickedCell)) {
console.log("oh no, you hit a mine. Sorry!");
console.log("The cells with mines were: ", mineLocations);
break;
} else {
console.log("phew, you are safe. ");
revealedCells.add(pickedCell);
}
}
}
index++;
}
rl.close();
const reveal: RevealContext = {
standardDerivation: {
playerSeed: playerSeed,
},
};
await vx.make_reveal(commitment, GS_SEED, reveal);
console.log(
"Thanks for playing! ",
balance,
"\nJust to recap, the information you'll need to verify your games is: ",
{
GS_SEED: bytesToHex(GS_SEED),
COMMITMENT: bytesToHex(commitment),
VX_PUBKEY: bytesToHex(VX_PUBKEY),
GAMES_PLAYED: index,
}
);
console.log(
`https://www.actuallyfair.com/apps/demo/vx/summary/${bytesToHex(
commitment
)}`
);
// request input from console input
}
main();