-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
126 lines (98 loc) · 2.77 KB
/
main.js
File metadata and controls
126 lines (98 loc) · 2.77 KB
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
import { encryptValue } from "https://esm.sh/@inco/lightning-sdk";
/* ---------------- CONFIG ---------------- */
const {
Connection,
PublicKey,
Transaction,
TransactionInstruction
} = solanaWeb3;
const {
getAssociatedTokenAddress,
createTransferInstruction
} = splToken;
const connection = new Connection(
"https://api.devnet.solana.com",
"confirmed"
);
const USDC_MINT = new PublicKey(
"4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU"
);
const INCO_PROGRAM_ID = new PublicKey(
"5sjEbPiqgZrYwR31ahR6Uk9wf5awoX61YGg7jExQSwaj"
);
const logEl = document.getElementById("log");
const log = (m) => logEl.textContent += m + "\n";
/* ---------------- WALLET ---------------- */
let wallet = null;
document.getElementById("connectBtn").onclick = async () => {
if (!window.solana) {
alert("Phantom not found");
return;
}
wallet = window.solana;
await wallet.connect();
log("✅ Wallet connected: " + wallet.publicKey.toBase58());
};
/* ---------------- PAYMENT ---------------- */
document.getElementById("sendBtn").onclick = async () => {
try {
if (!wallet) throw new Error("Connect wallet first");
const recipient = new PublicKey(
document.getElementById("to").value.trim()
);
const amountUi = Number(
document.getElementById("amount").value
);
const amount = BigInt(amountUi * 1_000_000);
log("🔐 Encrypting amount with Inco...");
const encryptedAmount = await encryptValue(amount);
const sender = wallet.publicKey;
const senderATA = await getAssociatedTokenAddress(
USDC_MINT,
sender
);
const recipientATA = await getAssociatedTokenAddress(
USDC_MINT,
recipient
);
log("🪙 Building SPL transfer...");
const transferIx = createTransferInstruction(
senderATA,
recipientATA,
sender,
Number(amount)
);
log("⚡ Building Inco encrypted instruction...");
const incoIx = new TransactionInstruction({
programId: INCO_PROGRAM_ID,
keys: [
{ pubkey: sender, isSigner: true, isWritable: false }
],
data: Buffer.from(encryptedAmount)
});
const tx = new Transaction().add(
transferIx,
incoIx
);
tx.feePayer = sender;
tx.recentBlockhash =
(await connection.getLatestBlockhash()).blockhash;
log("✍️ Requesting signature...");
const signed = await wallet.signTransaction(tx);
log("📡 Sending...");
const sig = await connection.sendRawTransaction(
signed.serialize()
);
await connection.confirmTransaction(sig);
log("✅ SUCCESS");
log("Tx: " + sig);
log(
"Explorer: https://explorer.solana.com/tx/" +
sig +
"?cluster=devnet"
);
} catch (err) {
log("❌ ERROR: " + err.message);
console.error(err);
}
};