-
Notifications
You must be signed in to change notification settings - Fork 3
/
BitFlipWalletWithOrdering.test.ts
312 lines (267 loc) · 8.76 KB
/
BitFlipWalletWithOrdering.test.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import "mocha";
import * as chai from "chai";
import {
solidity,
deployContract,
createMockProvider,
getWallets,
loadFixture
} from "ethereum-waffle";
import * as Broadcaster from "../../build/BroadcasterWithOrdering.json";
import { BigNumber, arrayify, defaultAbiCoder, keccak256 } from "ethers/utils";
import { Provider, JsonRpcProvider } from "ethers/providers";
import { Wallet, Contract } from "ethers";
const expect = chai.expect;
chai.use(solidity);
describe("BitFlip with optional ordering MetaTransaction", () => {
const provider = createMockProvider();
const [wallet] = getWallets(provider);
async function deployBroadcaster(provider: Provider, [signer]: Wallet[]) {
const broadcaster = await deployContract(
wallet, // a wallet to sign transactions
Broadcaster
);
return {
provider: provider as JsonRpcProvider,
signer,
broadcaster
}; // an ethers 'Contract' class instance
}
async function flip5And10And225(provider: Provider, [signer]: Wallet[]) {
const { broadcaster } = await deployBroadcaster(provider, [signer]);
// Fresh nonce
const nonce = new BigNumber("1");
const toFlip = [
new BigNumber("5"),
new BigNumber("10"),
new BigNumber("255")
];
let bitmap = new BigNumber("0");
for (let i = 0; i < toFlip.length; i++) {
const flipped = await setupFlip(
nonce,
bitmap,
toFlip[i],
broadcaster,
signer
);
bitmap = flipped;
}
return {
provider: provider as JsonRpcProvider,
signer,
broadcaster
};
}
async function setupFlip(
nonce: BigNumber,
bitmap: BigNumber,
index: BigNumber,
broadcaster: Contract,
signer: Wallet
): Promise<BigNumber> {
const flipped = flipBit(bitmap, index);
// Signer issues a command for the 0th index of the nonce
const encoded = defaultAbiCoder.encode(
["address", "bytes32", "uint", "uint"],
[
broadcaster.address,
"0x0000000000000000000000000000000000000000000000000000000000000000",
nonce,
flipped
]
);
const h = keccak256(encoded);
const sig = await signer.signMessage(arrayify(h));
await broadcaster.isMetaTransactionApproved(
"0x0000000000000000000000000000000000000000000000000000000000000000",
signer.address,
nonce,
flipped,
sig,
{ gasLimit: 2000000 }
);
return flipped;
}
it("transaction reverts as the signer did not try to flip a bit ", async () => {
const { signer, broadcaster } = await loadFixture(deployBroadcaster);
// Flip the 0th index.
const nonce = new BigNumber("0");
const bitmap = new BigNumber("0");
// Signer issues a command for the 0th index of the nonce
const encoded = defaultAbiCoder.encode(
["address", "bytes32", "uint", "uint"],
[
broadcaster.address,
"0x0000000000000000000000000000000000000000000000000000000000000000",
nonce,
bitmap
]
);
const h = keccak256(encoded);
const sig = await signer.signMessage(arrayify(h));
await expect(
broadcaster.isMetaTransactionApproved(
"0x0000000000000000000000000000000000000000000000000000000000000000",
signer.address,
nonce,
bitmap,
sig
)
).to.be.reverted;
}).timeout(5000);
it("sets the flipbit correctly. pass (hardcoded) ", async () => {
const { signer, broadcaster } = await loadFixture(deployBroadcaster);
// Flip the 0th index.
const nonce = new BigNumber("0");
const bitmap = new BigNumber("0");
const indexToFlip = new BigNumber("0");
const flipped = await setupFlip(
nonce,
bitmap,
indexToFlip,
broadcaster,
signer
);
expect(await broadcaster.isBitmapSet(signer.address, nonce, flipped)).to.be
.true;
}).timeout(5000);
it("fails to flip the same bit twice.", async () => {
const { signer, broadcaster } = await loadFixture(deployBroadcaster);
// Fresh nonce
let nonce = new BigNumber("0");
// Flip the 0th index.
let update = flipBit(new BigNumber("0"), new BigNumber("1"));
// Signer issues a command for the 0th index of the nonce
let encoded = defaultAbiCoder.encode(
["address", "bytes32", "uint", "uint"],
[
broadcaster.address,
"0x0000000000000000000000000000000000000000000000000000000000000000",
nonce,
update
]
);
let h = keccak256(encoded);
const sig = await signer.signMessage(arrayify(h));
await broadcaster.isMetaTransactionApproved(
"0x0000000000000000000000000000000000000000000000000000000000000000",
signer.address,
nonce,
update,
sig
);
// Issue the command like a boyo.
expect(await broadcaster.isBitmapSet(signer.address, nonce, update)).to.be
.true;
// Should fail as bitmap is already set
await expect(
broadcaster.isMetaTransactionApproved(
"0x0000000000000000000000000000000000000000000000000000000000000000",
signer.address,
nonce,
update,
sig
)
).to.be.reverted;
}).timeout(5000);
it("all bits already set", async () => {
const { signer, broadcaster } = await loadFixture(flip5And10And225);
// Flip the 0th index.
const nonce = new BigNumber("1");
const bitmap = new BigNumber("0");
const toFlip = [5, 10, 255];
// Check individual bits
for (let i = 0; i < toFlip.length; i++) {
let indexToFlip = new BigNumber(toFlip[i]);
const flipped = flipBit(bitmap, indexToFlip);
// Issue the command like a boyo.
expect(await broadcaster.isBitmapSet(signer.address, nonce, flipped)).to
.be.true;
}
// Check combined bits
let bits = bitmap;
for (let i = 0; i < toFlip.length; i++) {
let indexToFlip = new BigNumber(toFlip[i]);
bits = flipBit(bits, indexToFlip);
}
// Issue the command like a boyo.
expect(await broadcaster.isBitmapSet(signer.address, nonce, bits)).to.be
.true;
}).timeout(5000);
it("some bits preset, but can still detect new flips", async () => {
const { signer, broadcaster } = await loadFixture(flip5And10And225);
// Flip the 0th index.
const nonce = new BigNumber("0");
let bitmap = new BigNumber("0");
const toFlip = [5, 11, 252];
for (let i = 0; i < toFlip.length; i++) {
let indexToFlip = new BigNumber(toFlip[i]);
bitmap = flipBit(bitmap, indexToFlip);
}
// Issue the command like a boyo.
expect(await broadcaster.isBitmapSet(signer.address, nonce, bitmap)).to.be
.false;
}).timeout(5000);
it("meta-transaction failed as nonce is too far in future (nonce=3)", async () => {
const { signer, broadcaster } = await loadFixture(flip5And10And225);
// Flip the 0th index.
const nonce = new BigNumber("3");
const bitmap = new BigNumber("0");
const indexToFlip = new BigNumber("44");
const flipped = flipBit(bitmap, indexToFlip);
// Signer issues a command for the 0th index of the nonce
const encoded = defaultAbiCoder.encode(
["address", "bytes32", "uint", "uint"],
[
broadcaster.address,
"0x0000000000000000000000000000000000000000000000000000000000000000",
nonce,
flipped
]
);
const h = keccak256(encoded);
const sig = await signer.signMessage(arrayify(h));
await expect(
broadcaster.isMetaTransactionApproved(
"0x0000000000000000000000000000000000000000000000000000000000000000",
signer.address,
nonce,
flipped,
sig
)
).to.be.reverted;
}).timeout(5000);
it("new nonce (nonce=2) accepted, and bitmap for nonce=1 wiped. ", async () => {
const { signer, broadcaster } = await loadFixture(flip5And10And225);
// Flip the 0th index.
const nonce = new BigNumber("2");
const bitmap = new BigNumber("0");
const indexToFlip = new BigNumber("44");
const flipped = await setupFlip(
nonce,
bitmap,
indexToFlip,
broadcaster,
signer
);
expect(await broadcaster.isBitmapSet(signer.address, nonce, flipped)).to.be
.true;
// Lets check if '5' for nonce 1 is still flipped
const oldFlip = flipBit(new BigNumber("1"), new BigNumber("5"));
expect(
await broadcaster.isBitmapSet(signer.address, new BigNumber("1"), oldFlip)
).to.be.false;
}).timeout(5000);
it("new nonce (nonce=2) accepted, and bitmap for nonce=1 wiped. ", async () => {
const { signer, broadcaster } = await loadFixture(flip5And10And225);
}).timeout(5000);
/**
* Flip a bit!
* @param bits 256 bits
* @param toFlip index to flip (0,...,255)
*/
function flipBit(bits: BigNumber, indexToFlip: BigNumber): BigNumber {
return new BigNumber(bits).add(new BigNumber(2).pow(indexToFlip));
}
});