Skip to content

Commit

Permalink
fix(wc): handle untyped byte arrays returned by sign request (#218)
Browse files Browse the repository at this point in the history
* fix(wc): handle untyped byte arrays returned by sign request

Closes #217

* test(wc): test converting untyped byte array to Uint8Array
  • Loading branch information
drichar authored Aug 19, 2024
1 parent b76e294 commit 9a39164
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,15 @@ describe('WalletConnect', () => {

expect(result).toEqual([txn1.toByte()])
})

it('should return encoded signed transactions if the wallet returns untyped byte arrays', async () => {
const signedTxn = Array.from(txn1.toByte())
mockSignClient.request.mockResolvedValueOnce([signedTxn])

const result = await wallet.signTransactions([txn1])

expect(result).toEqual([txn1.toByte()])
})
})

describe('transactionSigner', () => {
Expand Down
17 changes: 14 additions & 3 deletions packages/use-wallet/src/wallets/walletconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type WalletConnectModalOptions = Pick<

export type WalletConnectOptions = SignClientOptions & WalletConnectModalOptions

export type SignTxnsResponse = Array<Uint8Array | string | null | undefined>
export type SignTxnsResponse = Array<Uint8Array | number[] | string | null | undefined>

export class SessionError extends Error {
constructor(message: string) {
Expand Down Expand Up @@ -518,10 +518,21 @@ export class WalletConnect extends BaseWallet {
request
})

// Filter out unsigned transactions, convert base64 strings to Uint8Array
// Filter out unsigned transactions, convert signed transactions to Uint8Array
const signedTxns = signTxnsResult.reduce<Uint8Array[]>((acc, value) => {
if (value) {
const signedTxn = typeof value === 'string' ? base64ToByteArray(value) : value
let signedTxn: Uint8Array
if (typeof value === 'string') {
signedTxn = base64ToByteArray(value)
} else if (value instanceof Uint8Array) {
signedTxn = value
} else if (Array.isArray(value)) {
signedTxn = new Uint8Array(value)
} else {
// Log unexpected types for debugging
console.warn(`[${this.metadata.name}] Unexpected type in signTxnsResult`, value)
signedTxn = new Uint8Array()
}
acc.push(signedTxn)
}
return acc
Expand Down

0 comments on commit 9a39164

Please sign in to comment.