GigaDAO / wasi-sol

💳 A Solana Wallet adapter for WASM frameworks (WIP).
https://wasi-sol.netlify.app/
MIT License
16 stars 4 forks source link

`TypeError` when attempting to sign a transaction using Rust/Wasm integration. #9

Closed wiseaidev closed 1 month ago

wiseaidev commented 2 months ago

Update

Most likely there is a bug in the JS API/WASM Transaction client signature that is not compatible with the JS API. I tried pretty much all possible solutions, from bs58 encoding the transaction object, serializing it and then call the JS function. I have also tried convert it into a bytes array, also converting it to string. None of the solutions worked. Will investigate it and fix it in future releases if possible, but for now the error message is the following:

Screenshot from 2024-06-27 18-49-19

It seems like it is a common bug in the JS API of the wallet, similar issues I found on Github; https://github.com/cryptocoinjs/base-x/issues/63 https://github.com/radixdlt/radixdlt-js-server-example/issues/3

Description:

Sending a transaction using this crate requires a wallet secret key, currently handled through a form, which is not ideal for security. We aim to utilize the signTransaction method from the Phantom JavaScript client to sign transactions and send them using the Rust WebAssembly client. However, attempts to implement this using the sign_transaction method result in a TypeError when signing a transaction, specifically JsValue(TypeError: r.serialize is not a function.

Steps to Reproduce:

Implement the sign_transaction method in the core/wallet module of the codebase:

   async fn sign_transaction(
       &mut self,
       transaction: Transaction,
   ) -> Result<Signature, WalletError> {
       info!("Signing transaction...");

       if self.public_key.is_none() {
           self.emit_error(WalletError::WalletNotConnectedError);
           return Err(WalletError::WalletNotConnectedError);
       }

       let options = js_sys::Object::new();
       js_sys::Reflect::set(
           &options,
           &serde_wasm_bindgen::to_value("transaction").unwrap(),
           &serde_wasm_bindgen::to_value(&transaction).unwrap(),
       )
       .unwrap();

       let promise: Promise = SOLANA.sign_transaction(&options);
       let result = JsFuture::from(promise).await;

       match result {
           Ok(sig) => {
               info!("Transaction signed: {:?}", sig);
           }
           Err(err) => {
               log::error!("Failed to sign transaction: {:?}", err);
           }
       }
       Ok(Signature::default())
   }

This will result in the following error being displayed in the console:

Error Screenshot

Alternatively, trying to serialize the transaction object directly using serde_json:

   async fn sign_transaction(
       &mut self,
       transaction: Transaction,
   ) -> Result<Signature, WalletError> {
       info!("Signing transaction...");

       if self.public_key.is_none() {
           self.emit_error(WalletError::WalletNotConnectedError);
           return Err(WalletError::WalletNotConnectedError);
       }

       let transaction_json = serde_json::to_string(&transaction)
           .map_err(|e| WalletError::WalletSignTransactionError)?;

       let transaction_js = JsValue::from_str(&transaction_json);

       let promise: Promise = SOLANA.sign_transaction(&transaction_js);
       let result = JsFuture::from(promise).await;

       match result {
           Ok(sig) => {
               info!("Transaction signed: {:?}", sig);
           }
           Err(err) => {
               log::error!("Failed to sign transaction: {:?}", err);
           }
       }
       Ok(Signature::default())
   }

This will result in the following error being displayed in the console:

Error Screenshot

Expected Behavior:

The transaction should be successfully signed without encountering any errors related to serialization or type issues.

Actual Behavior:

Encountering a TypeError when attempting to sign a transaction using the signTransaction method from the Phantom JavaScript client. The error messages indicate issues with serialization or object format mismatch.

Additional Context:

wiseaidev commented 1 month ago

I am hacking the mainframe atm, managed to trigger sign_transaction: image

PR soon!