-
Notifications
You must be signed in to change notification settings - Fork 2
/
tower.ts
209 lines (179 loc) · 5.63 KB
/
tower.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
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 "vx-verifier";
import { assert } from "tsafe";
import { bytesToNumberBE } from "@noble/curves/abstract/utils";
async function main() {
console.log("Running vx demo... (tower)");
// Let's generate a random seed
const GS_SEED = randomBytes(32); // very secret!
const COMMITMENT = sha256(GS_SEED);
const levels = 9;
const choicesPerLevel = 4;
const minesPerLevel = 1;
const houseEdge = 0.01;
const commitContext: CommitmentContext = { sha256Commitment: {} };
const VX_PUBKEY = await vx.make_commitment(COMMITMENT, commitContext);
console.log(
`Hey Player! We're ready to go with the following values:
COMMITMENT := ${bytesToHex(COMMITMENT)}
VX_PUBKEY := ${bytesToHex(VX_PUBKEY)}
with ${levels} levels with ${minesPerLevel} mines out of ${choicesPerLevel} options`
);
const url = `https://actuallyfair.com/apps/demo/vx/summary/${bytesToHex(
COMMITMENT
)}`;
console.log(
`Normally we'd show it to a player as single value (by either hashing them together, or concat'ing them) to make it easier to copy&paste, but you seem pretty technical`
);
console.log(url);
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: ");
console.log("Client seed is: ", playerSeed);
let balance = 0;
let nonce = 0;
while (true) {
const res = await rl.question(
`[${nonce}] 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: 0, value: 1 };
const wager: MessageContext = {
tower: {
start: {
houseEdge,
amount,
levels,
choicesPerLevel,
minesPerLevel,
},
},
};
const gsContribution = hmac(
sha256,
GS_SEED,
utf8ToBytes(`${playerSeed}:${nonce}`) // This is inside hmac, so we're not worried about anything like length extension attacks
);
const vxSignature = await vx.make_message(
COMMITMENT,
gsContribution,
nonce,
0,
wager
);
const verified = bls.verify(vxSignature, gsContribution, VX_PUBKEY);
assert(verified);
balance -= amount.value;
let picked = 0;
while (true) {
const res = await rl.question(
`[${nonce}] Pick a number between 0 and ${
choicesPerLevel - 1
} or Cashout (C)?: `
);
if (res.toLowerCase() === "c") {
const messageContext: MessageContext = {
mines: {
cashout: true,
},
};
const gsContribution = hmac(
sha256,
GS_SEED,
utf8ToBytes(`${playerSeed}:${nonce}:cashout`) // This is inside hmac, so we're not worried about anything like length extension attacks
);
const vxSignature = await vx.make_message(
COMMITMENT,
gsContribution,
nonce,
0,
messageContext
);
const verified = bls.verify(vxSignature, gsContribution, VX_PUBKEY);
assert(verified);
console.log("You have been cashed out! ");
// TODO: ..some deterministic algo to fill the rest of the board
// TODO: figure out their winnings
} else {
const choice = Number.parseInt(res, 10);
if (
!Number.isSafeInteger(choice) ||
choice < 0 ||
choice >= choicesPerLevel
) {
console.log("Unknown command or invalid");
continue; // try again...
}
const messageContext: MessageContext = {
tower: {
move: {
choice,
},
},
};
const gsContribution = hmac(
sha256,
GS_SEED,
utf8ToBytes(`${playerSeed}:${nonce}:${choice}`) // This is inside hmac, so we're not worried about anything like length extension attacks
);
const vxSignature = await vx.make_message(
COMMITMENT,
gsContribution,
nonce,
0,
messageContext
);
const verified = bls.verify(vxSignature, gsContribution, VX_PUBKEY);
assert(verified);
const normalized = Number(
bytesToNumberBE(vxSignature) % BigInt(choice - picked)
);
if (normalized < minesPerLevel) {
console.log("You hit a mine! Sorry!", normalized, picked);
break;
}
picked++;
}
}
nonce++;
}
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),
GS_SEED_HASH: bytesToHex(COMMITMENT),
VX_PUBKEY: bytesToHex(VX_PUBKEY),
GAMES_PLAYED: nonce,
}
);
console.log(
`https://www.actuallyfair.com/apps/demo/vx/summary/${bytesToHex(
COMMITMENT
)}`
);
// request input from console input
}
main();