11import { encryptTokenTransfer , decryptBalance } from './encryption' ;
2+ import { ethers } from 'ethers' ;
23
34const connectBtn = document . getElementById ( 'connectBtn' ) ;
45const 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