@@ -519,28 +519,62 @@ async function redeemHongbaoWithWallet(encryptedKey, timestamp, amount, wallet)
519519 const hongbaoVisual = document . getElementById ( "hongbao-visual-redeem" ) ;
520520 const resultElement = document . getElementById ( "redeem-result" ) ;
521521
522- detailsElement . innerHTML = "Checking for decrypted key ...<br>" ;
522+ detailsElement . innerHTML = "Checking for password protection ...<br>" ;
523523 detailsElement . classList . remove ( "hidden" ) ;
524524
525525 let decryptedPrivateKey ;
526526
527- // Check for a stored decrypted key
528- const decryptedKeyField = document . getElementById ( 'decrypted-hongbao-key' ) ;
529- if ( decryptedKeyField && decryptedKeyField . value ) {
530- decryptedPrivateKey = decryptedKeyField . value ;
531- } else if ( window . decryptedHongbaoKey ) {
532- decryptedPrivateKey = window . decryptedHongbaoKey ;
527+ // Step 1: Check if the key is password-protected
528+ const isProtected = new URLSearchParams ( window . location . search ) . get ( "protected" ) === "true" ;
529+ if ( isProtected ) {
530+ const password = document . getElementById ( "redeem-password" ) . value . trim ( ) ;
531+ if ( ! password ) {
532+ alert ( "Password is required to decrypt this Hongbao." ) ;
533+ return ;
534+ }
535+
536+ try {
537+ const encryptedObject = JSON . parse ( encryptedKey ) ;
538+ decryptedPrivateKey = await decryptWithPassword (
539+ encryptedObject . encrypted ,
540+ password ,
541+ encryptedObject . iv
542+ ) ;
543+ } catch ( error ) {
544+ alert ( "Invalid password. Unable to decrypt the Hongbao." ) ;
545+ return ;
546+ }
533547 } else {
534- alert ( "Decrypted key not found. Please decrypt the Hongbao first." ) ;
535- return ;
548+ // If not protected, use the provided encrypted key directly
549+ decryptedPrivateKey = encryptedKey ;
536550 }
537551
538- // Validate the decrypted key
552+ // Step 2: Check if the key is still encrypted with Shutter
553+ if (
554+ decryptedPrivateKey . startsWith ( "0x03" ) && // BLST ciphertext
555+ decryptedPrivateKey . length > 66
556+ ) {
557+ const urlParams = new URLSearchParams ( window . location . hash . split ( "?" ) [ 1 ] ) ;
558+ const identityParam = urlParams . get ( "identity" ) ;
559+ if ( ! identityParam ) {
560+ alert ( "Missing Shutter identity. Cannot complete final decryption." ) ;
561+ return ;
562+ }
563+
564+ const finalKey = await getShutterDecryptionKey ( identityParam ) ;
565+
566+ // Perform Shutter decryption
567+ decryptedPrivateKey = await shutterDecryptPrivateKey ( decryptedPrivateKey , finalKey ) ;
568+
569+ console . log ( "Final Decrypted Private Key:" , decryptedPrivateKey ) ;
570+ }
571+
572+ // Step 3: Validate the final key
539573 if ( ! decryptedPrivateKey . startsWith ( "0x" ) || decryptedPrivateKey . length !== 66 ) {
540574 throw new Error ( "Invalid private key after decryption." ) ;
541575 }
542576
543- // Sweep funds to the wallet
577+ // Step 4: Sweep funds to the wallet
544578 const hongbaoAccount = fallbackWeb3 . eth . accounts . privateKeyToAccount ( decryptedPrivateKey ) ;
545579 fallbackWeb3 . eth . accounts . wallet . add ( hongbaoAccount ) ;
546580
@@ -570,6 +604,11 @@ async function redeemHongbaoWithWallet(encryptedKey, timestamp, amount, wallet)
570604 chainId : parseInt ( GNOSIS_CHAIN_PARAMS . chainId , 16 ) ,
571605 } ;
572606
607+ detailsElement . innerHTML += `
608+ Amount gifted: <strong>${ amount } XDAI</strong><br>
609+ Signing transaction and sending funds...<br>
610+ Pending transaction confirmation...<br>
611+ ` ;
573612 const signedTx = await hongbaoAccount . signTransaction ( tx ) ;
574613 await fallbackWeb3 . eth . sendSignedTransaction ( signedTx . rawTransaction ) ;
575614
0 commit comments