|
1 |
| -import { |
2 |
| - AccountUpdate, |
3 |
| - Field, |
4 |
| - MerkleTree, |
5 |
| - Mina, |
6 |
| - PrivateKey, |
7 |
| - UInt64 |
8 |
| -} from "o1js"; |
| 1 | +import { Field, MerkleTree, Mina, PrivateKey, PublicKey, UInt64 } from "o1js"; |
9 | 2 | import { HumanIDWitness } from "../humanIDv1";
|
10 | 3 | import { signHumanIDv1, truncateHumanIDv1 } from "../humanIDv1.test";
|
11 | 4 | import { Airdrop } from "./Airdrop";
|
12 | 5 |
|
13 |
| -describe('Example Airdrop zkApp', () => { |
14 |
| - const deployerKey = PrivateKey.random(); |
15 |
| - const deployer = deployerKey.toPublicKey(); |
16 |
| - const senderKey = PrivateKey.random(); |
17 |
| - const sender = senderKey.toPublicKey(); |
18 |
| - const appKey = PrivateKey.random(); |
19 |
| - const appAddr = appKey.toPublicKey(); |
| 6 | +describe("Example Airdrop zkApp", () => { |
20 | 7 | let tree: MerkleTree;
|
| 8 | + let senderKey: PrivateKey; |
| 9 | + let appKey: PrivateKey; |
| 10 | + let sender: PublicKey; |
21 | 11 | let app: Airdrop;
|
22 | 12 |
|
23 | 13 | beforeAll(() => Airdrop.compile());
|
24 | 14 |
|
25 |
| - beforeEach(() => Mina.LocalBlockchain({ proofsEnabled: true }) |
26 |
| - .then((local) => { |
| 15 | + beforeEach(() => |
| 16 | + Mina.LocalBlockchain({ proofsEnabled: true }).then((local) => { |
27 | 17 | tree = new MerkleTree(33);
|
28 | 18 | Mina.setActiveInstance(local);
|
29 |
| - app = new Airdrop(appAddr); |
30 |
| - local.addAccount(deployer, "100000000000"); |
31 |
| - local.addAccount(sender, "100000000000"); |
32 |
| - })); |
| 19 | + senderKey = local.testAccounts[0].key; |
| 20 | + sender = senderKey.toPublicKey(); |
| 21 | + appKey = local.testAccounts[1].key; |
| 22 | + app = new Airdrop(appKey.toPublicKey()); |
| 23 | + |
| 24 | + const deployerKey = local.testAccounts[2].key; |
| 25 | + const deployer = deployerKey.toPublicKey(); |
| 26 | + return Mina.transaction(deployer, () => app.deploy()) |
| 27 | + .prove() |
| 28 | + .sign([appKey, deployerKey]) |
| 29 | + .send(); |
| 30 | + }) |
| 31 | + ); |
33 | 32 |
|
34 | 33 | const getWitnessAndInsert = (humanIDv1Key: bigint) => {
|
35 | 34 | const truncated = truncateHumanIDv1(humanIDv1Key);
|
36 | 35 | const witness = new HumanIDWitness(tree.getWitness(truncated));
|
37 | 36 | tree.setLeaf(truncated, Field(1));
|
38 | 37 | return witness;
|
39 |
| - } |
| 38 | + }; |
40 | 39 |
|
41 |
| - const fundZkApp = () => Mina.transaction(sender, async () => { |
42 |
| - let senderUpdate = AccountUpdate.create(sender); |
43 |
| - senderUpdate.requireSignature(); |
44 |
| - senderUpdate.send({ to: appAddr, amount: 100 * 1e9 }); |
45 |
| - }).then((txn) => txn.prove()) |
46 |
| - .then((txn) => txn.sign([senderKey]).send()); |
| 40 | + it("should deploy the app", () => |
| 41 | + console.log("Deployed HumanIDs contract at", app.address)); |
47 | 42 |
|
48 |
| - const deploy = () => Mina.transaction(deployer, () => { |
49 |
| - AccountUpdate.fundNewAccount(deployer); |
50 |
| - return app.deploy() |
51 |
| - }).then((txn) => txn.prove()) |
52 |
| - .then((txn) => txn.sign([deployerKey, appKey]).send()) |
53 |
| - |
54 |
| - it('should deploy the app and fund it', async () => { |
55 |
| - await deploy(); |
56 |
| - await fundZkApp(); |
57 |
| - console.log('Deployed HumanIDs contract at', app.address); |
58 |
| - }); |
59 |
| - |
60 |
| - it('should let people claimReward()', async () => { |
61 |
| - await deploy(); |
62 |
| - await fundZkApp() |
63 |
| - |
64 |
| - await Mina.transaction(sender, () => { |
65 |
| - return app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n)); |
66 |
| - }).then((txn) => txn.prove()) |
67 |
| - .then((txn) => txn.sign([senderKey]).send()); |
68 |
| - }); |
69 |
| - |
70 |
| - it('should let 2 people claimReward()', async () => { |
71 |
| - await deploy(); |
72 |
| - await fundZkApp(); |
| 43 | + it("should let people claimReward()", () => |
| 44 | + Mina.transaction(sender, () => |
| 45 | + app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n)) |
| 46 | + ) |
| 47 | + .prove() |
| 48 | + .sign([senderKey]) |
| 49 | + .send()); |
73 | 50 |
|
| 51 | + it("should let 2 people claimReward()", async () => { |
74 | 52 | const id1 = 123123123123123123123123123123n;
|
75 |
| - await Mina.transaction( |
76 |
| - sender, |
77 |
| - () => app.claimReward(...signHumanIDv1(id1, sender), getWitnessAndInsert(id1)) |
| 53 | + await Mina.transaction(sender, () => |
| 54 | + app.claimReward(...signHumanIDv1(id1, sender), getWitnessAndInsert(id1)) |
78 | 55 | )
|
79 |
| - .then((txn) => txn.prove()) |
80 |
| - .then((txn) => txn.sign([senderKey]).send()); |
| 56 | + .prove() |
| 57 | + .sign([senderKey]) |
| 58 | + .send(); |
81 | 59 |
|
82 | 60 | const id2 = 123123123123123123123123123124n;
|
83 |
| - await Mina.transaction( |
84 |
| - sender, |
85 |
| - () => app.claimReward(...signHumanIDv1(id2, sender), getWitnessAndInsert(id2)) |
| 61 | + await Mina.transaction(sender, () => |
| 62 | + app.claimReward(...signHumanIDv1(id2, sender), getWitnessAndInsert(id2)) |
86 | 63 | )
|
87 |
| - .then((txn) => txn.prove()) |
88 |
| - .then((txn) => txn.sign([senderKey]).send()); |
| 64 | + .prove() |
| 65 | + .sign([senderKey]) |
| 66 | + .send(); |
89 | 67 | });
|
90 | 68 |
|
91 |
| - it('should reject inconsistent witness', async () => { |
92 |
| - await deploy(); |
93 |
| - await fundZkApp(); |
94 |
| - |
| 69 | + it("should reject inconsistent witness", async () => { |
95 | 70 | const id = 123123123123123123123123123124n;
|
96 |
| - expect(() => Mina.transaction( |
97 |
| - sender, |
98 |
| - () => app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(100n)) |
99 |
| - ) |
100 |
| - .then((txn) => txn.prove()) |
101 |
| - .then((txn) => txn.sign([senderKey]).send())).rejects.toThrow(/does not match/); |
102 |
| - }) |
103 |
| - |
104 |
| - it('should not let double claimReward()', async () => { |
105 |
| - await deploy(); |
106 |
| - await fundZkApp(); |
| 71 | + expect(() => |
| 72 | + Mina.transaction(sender, () => |
| 73 | + app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(100n)) |
| 74 | + ) |
| 75 | + .prove() |
| 76 | + .sign([senderKey]) |
| 77 | + .send() |
| 78 | + ).rejects.toThrow(/does not match/); |
| 79 | + }); |
107 | 80 |
|
| 81 | + it("should not let double claimReward()", async () => { |
108 | 82 | const id = 123123123123123123123123123123n;
|
109 |
| - await Mina.transaction( |
110 |
| - sender, |
111 |
| - () => app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id)) |
112 |
| - ) |
113 |
| - .then((txn) => txn.prove()) |
114 |
| - .then((txn) => txn.sign([senderKey]).send()); |
115 |
| - |
116 |
| - expect(() => Mina.transaction( |
117 |
| - sender, |
118 |
| - () => app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id)) |
| 83 | + await Mina.transaction(sender, () => |
| 84 | + app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id)) |
119 | 85 | )
|
120 |
| - .then((txn) => txn.prove()) |
121 |
| - .then((txn) => txn.sign([senderKey]).send())).rejects.toThrow(/already exists/); |
122 |
| - }) |
123 |
| - |
124 |
| - it('should send the reciepient 10 MINA', async () => { |
125 |
| - await deploy(); |
126 |
| - await fundZkApp(); |
| 86 | + .prove() |
| 87 | + .sign([senderKey]) |
| 88 | + .send(); |
| 89 | + |
| 90 | + expect(() => |
| 91 | + Mina.transaction(sender, () => |
| 92 | + app.claimReward(...signHumanIDv1(id, sender), getWitnessAndInsert(id)) |
| 93 | + ) |
| 94 | + .prove() |
| 95 | + .sign([senderKey]) |
| 96 | + .send() |
| 97 | + ).rejects.toThrow(/already exists/); |
| 98 | + }); |
127 | 99 |
|
| 100 | + it("should send the reciepient 10 MINA", async () => { |
128 | 101 | let firstBalance = Mina.getBalance(sender);
|
129 | 102 |
|
130 |
| - await Mina.transaction( |
131 |
| - sender, |
132 |
| - () => app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n)) |
| 103 | + await Mina.transaction(sender, () => |
| 104 | + app.claimReward(...signHumanIDv1(100n, sender), getWitnessAndInsert(100n)) |
133 | 105 | )
|
134 |
| - .then((txn) => txn.prove()) |
135 |
| - .then((txn) => txn.sign([senderKey]).send()); |
| 106 | + .prove() |
| 107 | + .sign([senderKey]) |
| 108 | + .send(); |
136 | 109 |
|
137 | 110 | let secondBalance = Mina.getBalance(sender);
|
138 |
| - |
139 | 111 | expect(secondBalance.sub(firstBalance)).toEqual(UInt64.from(10 * 1e9));
|
140 |
| - }) |
| 112 | + }); |
141 | 113 | });
|
0 commit comments