perawallet / pera-wallet

Source code for Pera Wallet, simply the best Algorand wallet.
https://perawallet.app
Other
178 stars 62 forks source link

WalletConnect tx signing problem on iOS or Android #84

Closed nikitaeverywhere closed 2 years ago

nikitaeverywhere commented 2 years ago

Hello!

You have iOS doing not quite good as Android when it comes to signing transactions. Our DApp integrated with Pera Android throws an error on iOS since it doesn't follow the same protocol and returns strings instead of an encoded object. Please fix!

Below some context from our engineers.

image

Reference code of the client application:

const group = txns.map(({txn, signers}) => (signers.length > 0) ? {txn} : {txn, signers: []})
const result = await wallet.sendCustomRequest({
    id: Date.now() + Math.floor(Math.random() * 1000),
    jsonrpc: "2.0",
    method: "algo_signTxn",
    params: [group],
});

// This errors on iOS: // const signed = result.filter(txn => txn != undefined).map(txn => btoa(String.fromCharCode.apply(null, txn)))
// Since Pera wallet on iOS sends txn as string:
const signed = result
    .filter(txn => txn != undefined)
    .map(txn => typeof txn === "string" ? txn : btoa(String.fromCharCode.apply(null, txn)));

This should be enough for engineers to test and fix the issue.

mucahit commented 2 years ago

Hey @ZitRos,

Our iOS app returns the signed txns in base64 format(which is expected), so you need to handle that on your own. I'd suggest you switch to @perawallet/connect package as soon as possible, so you don't have to deal with the signing infra on your side.

Anyways, here is an example to fix your implementation

const signed = result
    .filter(txn => txn != undefined)
    .map(txn => typeof txn === "string" ? base64ToUint8Array(txn) : btoa(String.fromCharCode.apply(null, txn)));
function base64ToUint8Array(data: string) {
  return Uint8Array.from(window.atob(data), (value) => value.charCodeAt(0));
}

I hope that helps.

nikitaeverywhere commented 2 years ago

@mohammad-beheshti worked on this so he might provide further details.

Let's please identify whether:

taylanpince commented 2 years ago

@ZitRos @mohammad-beheshti We strongly recommend that you switch to Pera's official WalletConnect library (https://github.com/perawallet/connect). It fixes several issues that we've encountered with other libraries and will bring new features in the upcoming months.

We will nevertheless investigate any discrepancies between iOS and Android apps. However as long as you use the official Pera Connect library you should be OK.

nikitaeverywhere commented 2 years ago

Thank you for details. Noted.