Skip to content

Commit

Permalink
fix: wallet overrides (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks authored Mar 6, 2025
1 parent c7818a6 commit a980e7e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-deers-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@stacks/connect': patch
---

Add wallet result overrides for inconsistent wallet results
43 changes: 41 additions & 2 deletions packages/connect/src/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { base64 } from '@scure/base';
import { bytesToHex } from '@stacks/common';
import { bytesToHex, hexToBytes } from '@stacks/common';
import {
getInstalledProviders,
getProvider,
Expand Down Expand Up @@ -173,7 +173,10 @@ export async function request<M extends keyof Methods>(
shallowDefined(options)
);

const req = createRequestWithStorage(opts.enableLocalStorage);
const req = wrapResultOverrides(
opts.enableOverrides,
createRequestWithStorage(opts.enableLocalStorage)
);

// WITHOUT UI
if (opts.provider && !opts.forceWalletSelect) {
Expand Down Expand Up @@ -335,6 +338,42 @@ function shallowDefined<T extends object>(obj: T): Partial<T> {
return result;
}

/** @internal Higher-order function for proxying request results with overrides */
function wrapResultOverrides<
M extends keyof MethodsRaw,
P extends MethodParamsRaw<M>,
R extends MethodResultRaw<M>,
>(
enableOverrides: boolean,
request: (provider: StacksProvider, method: M, params?: P) => Promise<R>
) {
if (!enableOverrides) return request;

return async (provider: StacksProvider, method: M, params?: P): Promise<R> => {
const result = await request(provider, method, params);

const modifiedResult = { ...result };

// Handle txId/txid case variation (Fordefi returns `txId`, others return `txid`)
if (result !== null && 'txId' in result && result.txId && !('txid' in modifiedResult)) {
(modifiedResult as any).txid = result.txId;
}

// Handle Leather returning `hex` instead of `psbt` (base64 encoded)
if (
result !== null &&
'hex' in result &&
result.hex &&
typeof result.hex === 'string' &&
!('psbt' in modifiedResult)
) {
(modifiedResult as any).psbt = base64.encode(hexToBytes(result.hex));
}

return modifiedResult;
};
}

function getMethodOverrides<M extends keyof Methods>(
provider: StacksProvider,
method: M,
Expand Down

0 comments on commit a980e7e

Please sign in to comment.