-
Notifications
You must be signed in to change notification settings - Fork 0
/
re-initialization.ts
91 lines (77 loc) · 2.84 KB
/
re-initialization.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
import * as anchor from "@coral-xyz/anchor";
import { web3 } from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { ReInitialization } from "../../target/types/re_initialization";
describe("re-initialization", () => {
let provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.ReInitialization as Program<ReInitialization>;
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("Insecure Initialize - 1!", async () => {
const [metadata, mBump] = web3.PublicKey.findProgramAddressSync([Buffer.from("metadata")], program.programId);
const createData = {
name: "a",
symbol: "a",
uri: "a",
yearOfCreation: new anchor.BN(2)
}
await program.methods.insecureInitializev1(createData).accounts({
creator: creator.publicKey,
metadata: metadata,
systemProgram: web3.SystemProgram.programId,
}).signers([creator]).rpc({ commitment: "confirmed" });
});
it("Insecure Initialize - 2!", async () => {
const [metadata, mBump] = web3.PublicKey.findProgramAddressSync([Buffer.from("metadata")], program.programId);
const createData = {
name: "a",
symbol: "a",
uri: "a",
yearOfCreation: new anchor.BN(2)
}
await program.methods.insecureInitializev2(createData).accounts({
creator: creator.publicKey,
metadata: metadata,
systemProgram: web3.SystemProgram.programId,
}).signers([creator]).rpc({ commitment: "confirmed" });
});
it("Secure Initialize!", async () => {
const [metadata, mBump] = web3.PublicKey.findProgramAddressSync([Buffer.from("metadata")], program.programId);
const createData = {
name: "a",
symbol: "a",
uri: "a",
yearOfCreation: new anchor.BN(2)
}
await program.methods.secureInitialize(createData).accounts({
creator: creator.publicKey,
metadata: metadata,
systemProgram: web3.SystemProgram.programId,
}).signers([creator]).rpc({ commitment: "confirmed" });
});
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// | | | | | | | | | | | | | | | | | | | | |
// 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
});
export async function airdrop(
connection: any,
address: any,
amount = 500_000_000_000
) {
await connection.confirmTransaction(
await connection.requestAirdrop(address, amount),
'confirmed'
);
}