Skip to content

Commit bcd1bb8

Browse files
committed
add confidential-token-demo
1 parent 980784a commit bcd1bb8

2 files changed

Lines changed: 100 additions & 7 deletions

File tree

examples/confidential-token-demo/index.html

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ <h1>Confidential ERC-20 Transfer</h1>
3232
<input type="text" id="providerUrl" value="http://127.0.0.1:1234">
3333
</div>
3434

35-
<div class="form-group">
36-
<label for="chainID">Chain ID (Hex)</label>
37-
<input type="text" id="chainID" value="0x2f8d4b0d3abc9">
38-
</div>
39-
4035
<div class="form-group">
4136
<label for="tokenAddress">Token Address</label>
4237
<input type="text" id="tokenAddress" placeholder="0x...">
@@ -70,6 +65,29 @@ <h2>Decrypt Balance</h2>
7065
<button id="decryptBtn">Decrypt Balance</button>
7166
<div id="decryptResult" style="margin-top: 10px; font-weight: bold;"></div>
7267
</div>
68+
69+
<div class="card" style="margin-top: 20px;">
70+
<h2>User Management</h2>
71+
<button id="generateUserBtn">Generate New User</button>
72+
<div id="generatedUserInfo" style="display:none; background:#f9f9f9; padding:10px; margin-top:10px; word-break: break-all;">
73+
<p style="margin: 5px 0;"><strong>Private Key:</strong> <span id="genPrivKey" style="font-family: monospace;"></span></p>
74+
<p style="margin: 5px 0;"><strong>Public Key:</strong> <span id="genPubKey" style="font-family: monospace;"></span></p>
75+
<p style="margin: 5px 0;"><strong>Address:</strong> <span id="genAddress" style="font-family: monospace;"></span></p>
76+
</div>
77+
78+
<hr style="margin: 20px 0; border: 0; border-top: 1px solid #ddd;">
79+
80+
<h3>Register New User</h3>
81+
<div class="form-group">
82+
<label for="regPublicKey">Public Key</label>
83+
<textarea id="regPublicKey" rows="3" placeholder="0x..." style="width: 100%; border: 1px solid #ccc; border-radius: 4px; padding: 8px;"></textarea>
84+
</div>
85+
<div class="form-group">
86+
<label for="regAmount">CTX Deposit</label>
87+
<input type="text" id="regAmount" placeholder="0.0">
88+
</div>
89+
<button id="registerUserBtn">Register New User</button>
90+
</div>
7391

7492
<script type="module" src="/src/main.js"></script>
7593
</body>

examples/confidential-token-demo/src/main.js

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { encryptTokenTransfer, decryptBalance } from './encryption';
2+
import { ethers } from 'ethers';
23

34
const connectBtn = document.getElementById('connectBtn');
45
const sendBtn = document.getElementById('sendBtn');
@@ -35,7 +36,6 @@ async function handleSend() {
3536
sendBtn.disabled = true;
3637

3738
const providerUrl = document.getElementById('providerUrl').value;
38-
const chainID = document.getElementById('chainID').value;
3939
const tokenAddress = document.getElementById('tokenAddress').value;
4040
const recipient = document.getElementById('recipient').value;
4141
const amount = document.getElementById('amount').value;
@@ -58,7 +58,7 @@ async function handleSend() {
5858
to: result.to,
5959
data: result.data,
6060
value: '0x0', // 0 ETH
61-
gas: '0x30d40', // 200,000 gas
61+
gas: '0xf4240', // 200,000 gas
6262
};
6363

6464
const txHash = await window.ethereum.request({
@@ -121,3 +121,78 @@ if (decryptBtn) {
121121
decryptBtn.addEventListener('click', handleDecrypt);
122122
}
123123

124+
/* User Management */
125+
126+
const generateUserBtn = document.getElementById('generateUserBtn');
127+
const generatedUserInfo = document.getElementById('generatedUserInfo');
128+
const genPrivKey = document.getElementById('genPrivKey');
129+
const genPubKey = document.getElementById('genPubKey');
130+
const genAddress = document.getElementById('genAddress');
131+
132+
if (generateUserBtn) {
133+
generateUserBtn.addEventListener('click', () => {
134+
const wallet = ethers.Wallet.createRandom();
135+
genPrivKey.textContent = wallet.privateKey;
136+
genPubKey.textContent = wallet.signingKey.publicKey;
137+
genAddress.textContent = wallet.address;
138+
generatedUserInfo.style.display = 'block';
139+
});
140+
}
141+
142+
const registerUserBtn = document.getElementById('registerUserBtn');
143+
const regPublicKeyInput = document.getElementById('regPublicKey');
144+
const regAmountInput = document.getElementById('regAmount');
145+
146+
if (registerUserBtn) {
147+
registerUserBtn.addEventListener('click', async () => {
148+
try {
149+
const pubKeyHex = regPublicKeyInput.value.trim();
150+
const amount = regAmountInput.value.trim();
151+
const tokenAddr = document.getElementById('tokenAddress').value;
152+
153+
if (!pubKeyHex || !amount || !tokenAddr) {
154+
setStatus("Please fill all fields (Token Addr, Public Key, Amount)", "error");
155+
return;
156+
}
157+
158+
// Parse Public Key
159+
let cleanKey = pubKeyHex;
160+
if (cleanKey.startsWith('0x')) cleanKey = cleanKey.slice(2);
161+
// Remove 04 prefix if present and length is 130
162+
if (cleanKey.length === 130 && cleanKey.startsWith('04')) {
163+
cleanKey = cleanKey.slice(2);
164+
}
165+
166+
if (cleanKey.length !== 128) {
167+
setStatus("Invalid Public Key length. Expected uncompressed key (start with 04).", "error");
168+
return;
169+
}
170+
171+
const x = "0x" + cleanKey.slice(0, 64);
172+
const y = "0x" + cleanKey.slice(64);
173+
174+
setStatus("Registering user...", "info");
175+
176+
// ABI for registerPublicKey
177+
const abi = [
178+
"function registerPublicKey((bytes32,bytes32) publicKey) payable"
179+
];
180+
181+
// Use browser provider
182+
const provider = new ethers.BrowserProvider(window.ethereum);
183+
const signer = await provider.getSigner();
184+
const contract = new ethers.Contract(tokenAddr, abi, signer);
185+
186+
const tx = await contract.registerPublicKey([x, y], {
187+
value: ethers.parseEther(amount),
188+
gasLimit: 500000
189+
});
190+
191+
setStatus(`Registration sent! Hash: ${tx.hash}`, 'success');
192+
193+
} catch (e) {
194+
console.error(e);
195+
setStatus("Error: " + e.message, "error");
196+
}
197+
});
198+
}

0 commit comments

Comments
 (0)