Skip to content

Commit

Permalink
chore: deprecation warnings, closes #4928
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Feb 13, 2024
1 parent 67809a2 commit 81fea0d
Showing 1 changed file with 56 additions and 20 deletions.
76 changes: 56 additions & 20 deletions src/inpage/inpage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,31 +252,67 @@ const provider: HiroWalletProviderOverrides = {
},
};

function consoleDeprecationNotice(text: string) {
// eslint-disable-next-line no-console
console.warn(`Deprecation warning: ${text}`);
}

function warnAboutDeprecatedProvider(legacyProvider: object) {
return Object.fromEntries(
Object.entries(legacyProvider).map(([key, value]) => {
if (typeof value === 'function') {
return [
key,
(...args: any[]) => {
switch (key) {
case 'authenticationRequest':
consoleDeprecationNotice(
`Use LeatherProvider.request('getAddresses') instead, see docs https://leather.gitbook.io/developers/bitcoin/connect-users/get-addresses`
);
break;
case 'psbtRequest':
consoleDeprecationNotice(
`Use LeatherProvider.request('signPsbt') instead, see docs https://leather.gitbook.io/developers/bitcoin/sign-transactions/partially-signed-bitcoin-transactions-psbts`
);
break;
case 'structuredDataSignatureRequest':
case 'signatureRequest':
consoleDeprecationNotice(`Use LeatherProvider.request('stx_signMessage') instead`);
break;
default:
consoleDeprecationNotice(
'The provider object is deprecated. Use `LeatherProvider` instead'
);
}

return value(...args);
},
];
}
return [key, value];
})
);
}

try {
// Make properties immutable to contend with other wallets that use agressive
// "prioritisation" default settings. As wallet use this approach, Leather has
// to use it too, resulting in browsers' own internal logic being used to
// determine content script exeuction order. A more fair way to contend over
// shared provider space.
Object.defineProperty(window, 'StacksProvider', { get: () => provider, set: () => {} });
Object.defineProperty(window, 'LeatherProvider', { get: () => provider, set: () => {} });
// Makes properties immutable to contend with other wallets that use agressive
// "prioritisation" default settings. As other wallet's use this approach,
// Leather has to use it too, so that the browsers' own internal logic being
// used to determine content script exeuction order. A more fair way to
// contend over shared provider space. `StacksProvider` should be considered
// deprecated and each wallet use their own provider namespace.
Object.defineProperty(window, 'StacksProvider', {
get: () => warnAboutDeprecatedProvider(provider),
set: () => {},
});
Object.defineProperty(window, 'HiroWalletProvider', {
get: () => provider,
get: () => warnAboutDeprecatedProvider(provider),
set: () => {},
});
Object.defineProperty(window, 'LeatherProvider', { get: () => provider, set: () => {} });
} catch (e) {}

// Legacy product provider objects
if (typeof window.btc === 'undefined') {
(window as any).btc = {
request: (window as any).StacksProvider?.request,
listen(event: 'accountChange', callback: (arg: any) => void) {
function handler(e: MessageEvent) {
if (!e.data) return;
if ((e as any).event !== event) return;
callback((e as any).event);
}
window.addEventListener('message', handler);
return () => window.removeEventListener('message', handler);
},
};
(window as any).btc = warnAboutDeprecatedProvider(provider);
}

0 comments on commit 81fea0d

Please sign in to comment.