@etherplay/connect 0.17.2 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -3
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +254 -17
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/src/index.ts +308 -23
package/src/index.ts
CHANGED
|
@@ -11,6 +11,9 @@ import type {
|
|
|
11
11
|
import {walletPrompts} from '@etherplay/wallet-connector';
|
|
12
12
|
import {EthereumWalletConnector, type UnderlyingEthereumProvider} from '@etherplay/wallet-connector-ethereum';
|
|
13
13
|
import {writable} from 'sveltore';
|
|
14
|
+
// Straight from `delegated-identity` rather than through connect-core: this is the check the
|
|
15
|
+
// services that receive the publication signature run, so keeping only what it accepts is the point.
|
|
16
|
+
import {recoverAccount} from 'delegated-identity';
|
|
14
17
|
import {createPopupLauncher, type PopupPromise} from './popup.js';
|
|
15
18
|
import {withTimeout} from './utils.js';
|
|
16
19
|
import {
|
|
@@ -879,6 +882,21 @@ export type ConnectionStore<
|
|
|
879
882
|
// to upgrade from it. Pass `options.doNotStoreLocally` to keep the choice out of
|
|
880
883
|
// persisted storage (no auto-connect restore on reload).
|
|
881
884
|
selectWallet: (name?: string, options?: {doNotStoreLocally?: boolean}) => Promise<void>;
|
|
885
|
+
/**
|
|
886
|
+
* The account's authorisation of this origin's session public key:
|
|
887
|
+
* `originPublicKeyPublicationMessage(origin, account.signer.publicKey)` signed by the account.
|
|
888
|
+
*
|
|
889
|
+
* A wallet is asked AT MOST ONCE per account and origin key. The answer is kept on the account as
|
|
890
|
+
* `savedPublicKeyPublicationSignature`, in the store (subscribers see the update) and in the
|
|
891
|
+
* persisted account, so later calls and later page loads return it without a prompt, and
|
|
892
|
+
* concurrent calls share one prompt. Only a signature that recovers to `account.address` is kept,
|
|
893
|
+
* and a kept one is checked again before it is returned, so it is never handed out for another
|
|
894
|
+
* origin, key or account. A wallet answer that does not recover (an ERC-1271 smart-contract
|
|
895
|
+
* wallet) is returned but not kept, so such a wallet is asked each time.
|
|
896
|
+
*
|
|
897
|
+
* A hosted account returns the signature it made at sign-in, and throws if it made none.
|
|
898
|
+
* Throws `Not signed in` unless `step === 'SignedIn'`.
|
|
899
|
+
*/
|
|
882
900
|
getSignatureForPublicKeyPublication: () => Promise<`0x${string}`>;
|
|
883
901
|
getDelegation: (target: {chainId: number; contract: `0x${string}`; deadline?: number}) => Promise<SavedDelegation>;
|
|
884
902
|
switchWalletChain: (chainInfo?: BasicChainInfo) => Promise<void>;
|
|
@@ -1251,6 +1269,65 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1251
1269
|
);
|
|
1252
1270
|
}
|
|
1253
1271
|
|
|
1272
|
+
// THE RESTORE IN FLIGHT, which an attempt asking for nothing new JOINS rather than overtakes.
|
|
1273
|
+
//
|
|
1274
|
+
// `flowTransitions` makes the restore yield to anything newer, and that is right for an attempt
|
|
1275
|
+
// that names something DIFFERENT: another wallet, another account, a `selectWallet`, a
|
|
1276
|
+
// `disconnect`. It was wrong for one that names nothing the restore is not already doing, and that
|
|
1277
|
+
// is the commonest call there is: every game action calls `ensureConnected()`, so a click made while
|
|
1278
|
+
// the page was still loading superseded the restore of a perfectly good stored session and asked
|
|
1279
|
+
// the user to sign in again (with `requestSignatureAutomaticallyIfPossible`, a fresh signature).
|
|
1280
|
+
// Measured downstream on 0.17.1 (template-commit-reveal's `table.e2e.ts`: sign in, reload, click).
|
|
1281
|
+
// So such an attempt waits for the restore to settle and carries on from wherever it landed. See
|
|
1282
|
+
// `restoreToJoin` for what counts as naming nothing new, and `test/joining-the-restore.test.ts`.
|
|
1283
|
+
type RestoreOutcome = 'landed' | 'failed' | 'overtaken';
|
|
1284
|
+
type RestoreInFlight = {
|
|
1285
|
+
walletName: string;
|
|
1286
|
+
/** The account it restores, lowercased; `undefined` for a wallet choice, which reaches none. */
|
|
1287
|
+
address: `0x${string}` | undefined;
|
|
1288
|
+
settled: Promise<RestoreOutcome>;
|
|
1289
|
+
};
|
|
1290
|
+
let restoreInFlight: RestoreInFlight | undefined;
|
|
1291
|
+
/**
|
|
1292
|
+
* The restore an attempt for `mechanism` should JOIN, or `undefined` if it should run (and win).
|
|
1293
|
+
*
|
|
1294
|
+
* It joins when it names nothing the restore is not already doing: no mechanism, or a wallet
|
|
1295
|
+
* mechanism whose name (if any) is the restore's wallet and whose address (if any) is the
|
|
1296
|
+
* restore's account. The STEP asked for does not enter into it: a step the restore reaches or
|
|
1297
|
+
* passes resolves when it lands, and one it falls short of is driven on from where it landed,
|
|
1298
|
+
* which beats superseding it and starting over. A hosted mechanism, another wallet or another
|
|
1299
|
+
* account names something different and wins, exactly as before.
|
|
1300
|
+
*
|
|
1301
|
+
* A restore already overtaken is not joined even if it has not noticed yet (it notices only when
|
|
1302
|
+
* the wallet answers): it will publish nothing, so there is nothing to wait for.
|
|
1303
|
+
*/
|
|
1304
|
+
function restoreToJoin(mechanism: Mechanism | undefined): Promise<RestoreOutcome> | undefined {
|
|
1305
|
+
const restore = restoreInFlight;
|
|
1306
|
+
if (!restore || autoConnectOvertaken()) {
|
|
1307
|
+
return undefined;
|
|
1308
|
+
}
|
|
1309
|
+
if (mechanism) {
|
|
1310
|
+
if (mechanism.type !== 'wallet') {
|
|
1311
|
+
return undefined;
|
|
1312
|
+
}
|
|
1313
|
+
if (mechanism.name && !namesSameWallet(mechanism.name, restore.walletName)) {
|
|
1314
|
+
return undefined;
|
|
1315
|
+
}
|
|
1316
|
+
if (mechanism.address && mechanism.address.toLowerCase() !== restore.address) {
|
|
1317
|
+
return undefined;
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
return restore.settled;
|
|
1321
|
+
}
|
|
1322
|
+
/** A name may be an EIP-6963 uuid, which is what `connect` looks wallets up by too. */
|
|
1323
|
+
function namesSameWallet(asked: string, restoring: string): boolean {
|
|
1324
|
+
if (asked === restoring) {
|
|
1325
|
+
return true;
|
|
1326
|
+
}
|
|
1327
|
+
const handle = $connection.wallets.find((v) => v.info.name === asked || v.info.uuid === asked);
|
|
1328
|
+
return !!handle && (handle.info.name === restoring || handle.info.uuid === restoring);
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1254
1331
|
let $connection: Connection<WalletProviderType> = {
|
|
1255
1332
|
step: 'Idle',
|
|
1256
1333
|
loading: true,
|
|
@@ -1732,6 +1809,28 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1732
1809
|
// synchronously, before anything else can run, so it has nothing to yield to.
|
|
1733
1810
|
const autoConnectStartedAt = flowTransitions;
|
|
1734
1811
|
const autoConnectOvertaken = () => flowTransitions !== autoConnectStartedAt;
|
|
1812
|
+
/**
|
|
1813
|
+
* Record a wallet restore as IN FLIGHT, so an attempt that asks for nothing it is not already
|
|
1814
|
+
* doing can join it rather than overtake it: see `restoreToJoin`. Cleared when it settles, whatever
|
|
1815
|
+
* the outcome.
|
|
1816
|
+
*/
|
|
1817
|
+
const trackRestore = (walletName: string, address: `0x${string}` | undefined, restore: Promise<RestoreOutcome>) => {
|
|
1818
|
+
const tracked: RestoreInFlight = {
|
|
1819
|
+
walletName,
|
|
1820
|
+
address: address?.toLowerCase() as `0x${string}` | undefined,
|
|
1821
|
+
settled: restore.then(
|
|
1822
|
+
(outcome) => outcome,
|
|
1823
|
+
// Unreachable (every branch catches), but a joiner must never be left hanging on it.
|
|
1824
|
+
() => (autoConnectOvertaken() ? 'overtaken' : 'failed') as RestoreOutcome,
|
|
1825
|
+
),
|
|
1826
|
+
};
|
|
1827
|
+
restoreInFlight = tracked;
|
|
1828
|
+
tracked.settled.then(() => {
|
|
1829
|
+
if (restoreInFlight === tracked) {
|
|
1830
|
+
restoreInFlight = undefined;
|
|
1831
|
+
}
|
|
1832
|
+
});
|
|
1833
|
+
};
|
|
1735
1834
|
if (autoConnect) {
|
|
1736
1835
|
if (typeof window !== 'undefined') {
|
|
1737
1836
|
try {
|
|
@@ -1745,12 +1844,12 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1745
1844
|
| WalletMechanism<string, `0x${string}`>;
|
|
1746
1845
|
if (mechanismUsed.type == 'wallet') {
|
|
1747
1846
|
const walletMechanism = mechanismUsed as WalletMechanism<string, `0x${string}`>;
|
|
1748
|
-
waitForWallet(walletMechanism.name)
|
|
1749
|
-
.then(async (walletDetails: WalletHandle<WalletProviderType>) => {
|
|
1847
|
+
const restore = waitForWallet(walletMechanism.name)
|
|
1848
|
+
.then(async (walletDetails: WalletHandle<WalletProviderType>): Promise<RestoreOutcome> => {
|
|
1750
1849
|
const walletProvider = walletDetails.walletProvider;
|
|
1751
1850
|
const chainIdAsHex = await withTimeout(walletProvider.getChainId());
|
|
1752
1851
|
if (autoConnectOvertaken()) {
|
|
1753
|
-
return;
|
|
1852
|
+
return 'overtaken';
|
|
1754
1853
|
}
|
|
1755
1854
|
const chainId = Number(chainIdAsHex).toString();
|
|
1756
1855
|
_wallet = {provider: walletProvider, chainId, info: walletDetails.info};
|
|
@@ -1759,12 +1858,19 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1759
1858
|
let accounts: `0x${string}`[] = [];
|
|
1760
1859
|
accounts = await withTimeout(walletProvider.getAccounts());
|
|
1761
1860
|
if (autoConnectOvertaken()) {
|
|
1762
|
-
return;
|
|
1861
|
+
return 'overtaken';
|
|
1763
1862
|
}
|
|
1764
1863
|
accounts = accounts.map((v) => v.toLowerCase() as `0x${string}`);
|
|
1864
|
+
// A kept publication signature is SHOWN only if it is this account's for this
|
|
1865
|
+
// origin and key, so an app can read the field to renew without a prompt. See
|
|
1866
|
+
// `getSignatureForPublicKeyPublication`.
|
|
1867
|
+
const account = await withoutForeignPublicationSignature(existingAccount);
|
|
1868
|
+
if (autoConnectOvertaken()) {
|
|
1869
|
+
return 'overtaken';
|
|
1870
|
+
}
|
|
1765
1871
|
set({
|
|
1766
1872
|
step: 'SignedIn',
|
|
1767
|
-
account
|
|
1873
|
+
account,
|
|
1768
1874
|
mechanism: walletMechanism,
|
|
1769
1875
|
wallets: $connection.wallets,
|
|
1770
1876
|
wallet: {
|
|
@@ -1780,15 +1886,18 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1780
1886
|
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
1781
1887
|
onAccountChanged(accounts);
|
|
1782
1888
|
watchForAccountChange(walletProvider);
|
|
1889
|
+
return 'landed';
|
|
1783
1890
|
})
|
|
1784
|
-
.catch((err) => {
|
|
1891
|
+
.catch((err): RestoreOutcome => {
|
|
1785
1892
|
if (autoConnectOvertaken()) {
|
|
1786
|
-
return;
|
|
1893
|
+
return 'overtaken';
|
|
1787
1894
|
}
|
|
1788
1895
|
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
1789
1896
|
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
1790
1897
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1898
|
+
return 'failed';
|
|
1791
1899
|
});
|
|
1900
|
+
trackRestore(walletMechanism.name, existingAccount.address, restore);
|
|
1792
1901
|
} else {
|
|
1793
1902
|
set({
|
|
1794
1903
|
step: 'SignedIn',
|
|
@@ -1809,12 +1918,12 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1809
1918
|
autoConnectHandled = true;
|
|
1810
1919
|
const lastWallet = getLastWallet();
|
|
1811
1920
|
if (lastWallet) {
|
|
1812
|
-
waitForWallet(lastWallet.name)
|
|
1813
|
-
.then(async (walletDetails: WalletHandle<WalletProviderType>) => {
|
|
1921
|
+
const restore = waitForWallet(lastWallet.name)
|
|
1922
|
+
.then(async (walletDetails: WalletHandle<WalletProviderType>): Promise<RestoreOutcome> => {
|
|
1814
1923
|
const walletProvider = walletDetails.walletProvider;
|
|
1815
1924
|
const chainIdAsHex = await withTimeout(walletProvider.getChainId());
|
|
1816
1925
|
if (autoConnectOvertaken()) {
|
|
1817
|
-
return;
|
|
1926
|
+
return 'overtaken';
|
|
1818
1927
|
}
|
|
1819
1928
|
const chainId = Number(chainIdAsHex).toString();
|
|
1820
1929
|
_wallet = {provider: walletProvider, chainId, info: walletDetails.info};
|
|
@@ -1836,15 +1945,20 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1836
1945
|
switchingChain: false,
|
|
1837
1946
|
},
|
|
1838
1947
|
});
|
|
1948
|
+
return 'landed';
|
|
1839
1949
|
})
|
|
1840
|
-
.catch((err) => {
|
|
1950
|
+
.catch((err): RestoreOutcome => {
|
|
1841
1951
|
if (autoConnectOvertaken()) {
|
|
1842
|
-
return;
|
|
1952
|
+
return 'overtaken';
|
|
1843
1953
|
}
|
|
1844
1954
|
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
1845
1955
|
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
1846
1956
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1957
|
+
return 'failed';
|
|
1847
1958
|
});
|
|
1959
|
+
// No address: choosing a wallet never asks it for accounts, so an attempt naming one
|
|
1960
|
+
// names something this restore will not reach.
|
|
1961
|
+
trackRestore(lastWallet.name, undefined, restore);
|
|
1848
1962
|
} else {
|
|
1849
1963
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1850
1964
|
}
|
|
@@ -1857,12 +1971,12 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1857
1971
|
// The WalletConnected/SignedIn auto-connect needs an address to restore the
|
|
1858
1972
|
// account; without one, fall back to Idle rather than constructing an invalid state.
|
|
1859
1973
|
if (lastWallet && lastWallet.address) {
|
|
1860
|
-
waitForWallet(lastWallet.name)
|
|
1861
|
-
.then(async (walletDetails: WalletHandle<WalletProviderType>) => {
|
|
1974
|
+
const restore = waitForWallet(lastWallet.name)
|
|
1975
|
+
.then(async (walletDetails: WalletHandle<WalletProviderType>): Promise<RestoreOutcome> => {
|
|
1862
1976
|
const walletProvider = walletDetails.walletProvider;
|
|
1863
1977
|
const chainIdAsHex = await withTimeout(walletProvider.getChainId());
|
|
1864
1978
|
if (autoConnectOvertaken()) {
|
|
1865
|
-
return;
|
|
1979
|
+
return 'overtaken';
|
|
1866
1980
|
}
|
|
1867
1981
|
const chainId = Number(chainIdAsHex).toString();
|
|
1868
1982
|
_wallet = {provider: walletProvider, chainId, info: walletDetails.info};
|
|
@@ -1872,7 +1986,7 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1872
1986
|
let accounts: `0x${string}`[] = [];
|
|
1873
1987
|
accounts = await withTimeout(walletProvider.getAccounts());
|
|
1874
1988
|
if (autoConnectOvertaken()) {
|
|
1875
|
-
return;
|
|
1989
|
+
return 'overtaken';
|
|
1876
1990
|
}
|
|
1877
1991
|
accounts = accounts.map((v) => v.toLowerCase() as `0x${string}`);
|
|
1878
1992
|
set({
|
|
@@ -1893,15 +2007,18 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
1893
2007
|
alwaysOnProviderWrapper.setWalletStatus('connected');
|
|
1894
2008
|
onAccountChanged(accounts);
|
|
1895
2009
|
watchForAccountChange(walletProvider);
|
|
2010
|
+
return 'landed';
|
|
1896
2011
|
})
|
|
1897
|
-
.catch((err) => {
|
|
2012
|
+
.catch((err): RestoreOutcome => {
|
|
1898
2013
|
if (autoConnectOvertaken()) {
|
|
1899
|
-
return;
|
|
2014
|
+
return 'overtaken';
|
|
1900
2015
|
}
|
|
1901
2016
|
// The wallet may have been registered on the wrapper before the failure (e.g.
|
|
1902
2017
|
// getAccounts threw). `set` tears it down: Idle carries no wallet.
|
|
1903
2018
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
2019
|
+
return 'failed';
|
|
1904
2020
|
});
|
|
2021
|
+
trackRestore(lastWallet.name, lastWallet.address, restore);
|
|
1905
2022
|
} else {
|
|
1906
2023
|
set({step: 'Idle', loading: false, wallet: undefined, wallets: $connection.wallets});
|
|
1907
2024
|
}
|
|
@@ -2088,6 +2205,9 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2088
2205
|
},
|
|
2089
2206
|
metadata: {},
|
|
2090
2207
|
mechanismUsed: $connection.mechanism,
|
|
2208
|
+
// Empty until `getSignatureForPublicKeyPublication` is first called, which asks the wallet and
|
|
2209
|
+
// keeps the answer here. Not asked for now: that would put a second prompt on every sign-in,
|
|
2210
|
+
// including in the apps that never publish the key.
|
|
2091
2211
|
savedPublicKeyPublicationSignature: undefined,
|
|
2092
2212
|
// Nothing pre-generated on the wallet path, and nothing missing either: the owner is a
|
|
2093
2213
|
// live wallet that can be asked to sign at the moment a credential is needed, which is
|
|
@@ -2473,6 +2593,40 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
2473
2593
|
if (!mechanism && (targetStep === 'WalletConnected' || walletOnly)) {
|
|
2474
2594
|
mechanism = {type: 'wallet'};
|
|
2475
2595
|
}
|
|
2596
|
+
// A connect that names nothing the restore in flight is not already doing JOINS it, for the
|
|
2597
|
+
// same reason `ensureConnected` does: starting would overtake the restore and ask the user to
|
|
2598
|
+
// sign in again over a stored session. See `restoreToJoin`. What it does once the restore settles
|
|
2599
|
+
// depends on how it ended:
|
|
2600
|
+
//
|
|
2601
|
+
// - FAILED: nothing was restored, so it runs as asked.
|
|
2602
|
+
// - OVERTAKEN: something started after this call (a `disconnect`, a connect to another account)
|
|
2603
|
+
// and the newer thing wins, as it would have had this call started at once.
|
|
2604
|
+
// - LANDED: it finishes what the restore left short, and only that. `SignedIn` is all a connect
|
|
2605
|
+
// reaches, so nothing is left. `WalletConnected` is what a connect lands on too, and it asks
|
|
2606
|
+
// for the signature from there under the same condition a connect does (the last-wallet
|
|
2607
|
+
// fallback under `targetStep: 'SignedIn'`). `WalletChosen` asked the wallet for no accounts,
|
|
2608
|
+
// which is the very thing a connect is for, so it runs and upgrades the chosen wallet.
|
|
2609
|
+
const joined = restoreToJoin(mechanism);
|
|
2610
|
+
if (joined) {
|
|
2611
|
+
const outcome = await joined;
|
|
2612
|
+
if (outcome === 'overtaken') {
|
|
2613
|
+
return;
|
|
2614
|
+
}
|
|
2615
|
+
if (outcome === 'landed') {
|
|
2616
|
+
if (
|
|
2617
|
+
$connection.step === 'WalletConnected' &&
|
|
2618
|
+
$connection.wallet.status === 'connected' &&
|
|
2619
|
+
(requestSignatureAutomaticallyIfPossible || options?.requestSignatureRightAway) &&
|
|
2620
|
+
!options?.requireUserConfirmationBeforeSignatureRequest
|
|
2621
|
+
) {
|
|
2622
|
+
await requestSignature();
|
|
2623
|
+
return;
|
|
2624
|
+
}
|
|
2625
|
+
if ($connection.step !== 'WalletChosen') {
|
|
2626
|
+
return;
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
}
|
|
2476
2630
|
remember = !(options?.doNotStoreLocally || false);
|
|
2477
2631
|
if (mechanism) {
|
|
2478
2632
|
if (mechanism.type === 'wallet') {
|
|
@@ -3090,6 +3244,18 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3090
3244
|
mechanism = {type: 'wallet'};
|
|
3091
3245
|
}
|
|
3092
3246
|
|
|
3247
|
+
// A call that names nothing the restore in flight is not already doing JOINS it: it waits for
|
|
3248
|
+
// the restore to settle, then decides from wherever that left the flow, exactly as if it had been
|
|
3249
|
+
// made a moment later. Landed SignedIn as the stored account, it resolves at once, with no
|
|
3250
|
+
// attempt and so no signature request; landed short of the step, it drives on from there; failed
|
|
3251
|
+
// or overtaken, it does what it does from the state it finds. Without this, the attempt it
|
|
3252
|
+
// starts would overtake the restore (see `flowTransitions`) and ask the user to sign in again
|
|
3253
|
+
// over a stored session. See `restoreToJoin`.
|
|
3254
|
+
const joined = restoreToJoin(mechanism);
|
|
3255
|
+
if (joined) {
|
|
3256
|
+
await joined;
|
|
3257
|
+
}
|
|
3258
|
+
|
|
3093
3259
|
const promise = new Promise<
|
|
3094
3260
|
WalletChosen<WalletProviderType> | WalletConnected<WalletProviderType> | SignedIn<WalletProviderType>
|
|
3095
3261
|
>((resolve, reject) => {
|
|
@@ -3971,6 +4137,22 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3971
4137
|
// app gets an uncaught exception instead of the reason. `getDelegation` beside it is async and
|
|
3972
4138
|
// rejects, and two siblings on the same object failing in two different ways is a difference
|
|
3973
4139
|
// nothing in either signature would warn a caller about.
|
|
4140
|
+
//
|
|
4141
|
+
// ASKED AT MOST ONCE per (origin, public key, account) on a wallet account, and the answer is kept
|
|
4142
|
+
// on the account as `savedPublicKeyPublicationSignature`, which is where a hosted account has
|
|
4143
|
+
// always carried its own. The same bytes are verified by several services (push registration,
|
|
4144
|
+
// trigger subscriptions renewed on every page load, missiv), so asking each time meant one wallet
|
|
4145
|
+
// prompt per service per reload. Concurrent calls join the request in flight rather than prompting
|
|
4146
|
+
// twice, the same "join, do not supersede" rule `ensureConnected` follows for the restore.
|
|
4147
|
+
//
|
|
4148
|
+
// Only a signature that RECOVERS to the account is kept, and a kept one is checked again before it
|
|
4149
|
+
// is returned. That single check is the binding: the message names the origin and the key, the
|
|
4150
|
+
// recovered address names the account, so a kept value for anything else (another origin, key or
|
|
4151
|
+
// account, or whatever an older persisted shape left in the field) recovers to somebody else and
|
|
4152
|
+
// is ignored. A wallet answer that does not recover (an ERC-1271 smart-contract wallet, a wallet
|
|
4153
|
+
// that signed with another account than the one it was asked for) is returned as before but not
|
|
4154
|
+
// kept: making it permanent would turn one bad answer into a failure no reload could clear, and
|
|
4155
|
+
// `delegated-identity`'s `recoverAccount`, which the services verify with, refuses it anyway.
|
|
3974
4156
|
async function getSignatureForPublicKeyPublication(): Promise<`0x${string}`> {
|
|
3975
4157
|
if ($connection.step !== 'SignedIn') {
|
|
3976
4158
|
throw new Error('Not signed in');
|
|
@@ -3980,11 +4162,39 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3980
4162
|
if (!_wallet) {
|
|
3981
4163
|
throw new Error(`no provider`);
|
|
3982
4164
|
}
|
|
3983
|
-
const
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
}
|
|
4165
|
+
const binding: PublicationBinding = {
|
|
4166
|
+
origin: originToSignWith(),
|
|
4167
|
+
publicKey: account.signer.publicKey,
|
|
4168
|
+
address: account.address.toLowerCase() as `0x${string}`,
|
|
4169
|
+
};
|
|
4170
|
+
const key = `${binding.origin}\n${binding.publicKey}\n${binding.address}`;
|
|
4171
|
+
if (publicationInFlight?.key === key) {
|
|
4172
|
+
return publicationInFlight.promise;
|
|
4173
|
+
}
|
|
4174
|
+
const kept = account.savedPublicKeyPublicationSignature;
|
|
4175
|
+
const promise = (async () => {
|
|
4176
|
+
if (kept && (await recoversTo(binding, kept))) {
|
|
4177
|
+
return kept;
|
|
4178
|
+
}
|
|
4179
|
+
const message = originPublicKeyPublicationMessage(binding.origin, binding.publicKey);
|
|
4180
|
+
// Announced, for the same reason as `getDelegation` above.
|
|
4181
|
+
const signature = await alwaysOnProviderWrapper.signMessage(message, account.address, {
|
|
4182
|
+
purpose: 'public-key-publication',
|
|
4183
|
+
});
|
|
4184
|
+
if (await recoversTo(binding, signature)) {
|
|
4185
|
+
keepPublicationSignature(binding, signature);
|
|
4186
|
+
}
|
|
4187
|
+
return signature;
|
|
4188
|
+
})();
|
|
4189
|
+
const inFlight = {key, promise};
|
|
4190
|
+
publicationInFlight = inFlight;
|
|
4191
|
+
const clear = () => {
|
|
4192
|
+
if (publicationInFlight === inFlight) {
|
|
4193
|
+
publicationInFlight = undefined;
|
|
4194
|
+
}
|
|
4195
|
+
};
|
|
4196
|
+
promise.then(clear, clear);
|
|
4197
|
+
return promise;
|
|
3988
4198
|
}
|
|
3989
4199
|
|
|
3990
4200
|
if (account.savedPublicKeyPublicationSignature) {
|
|
@@ -3996,6 +4206,81 @@ export function createConnection<WalletProviderType = UnderlyingEthereumProvider
|
|
|
3996
4206
|
throw new Error(`no saved public key publication signature for ${account.address}`);
|
|
3997
4207
|
}
|
|
3998
4208
|
|
|
4209
|
+
type PublicationBinding = {origin: string; publicKey: `0x${string}`; address: `0x${string}`};
|
|
4210
|
+
// The publication prompt the wallet is showing, if any. Deliberately NOT cleared by `disconnect`
|
|
4211
|
+
// or a switch: neither withdraws a prompt already on the user's screen (see `disconnect`), so a
|
|
4212
|
+
// call for the same (origin, key, account) made after signing back in joins the prompt that is
|
|
4213
|
+
// still open rather than stacking a second one on it. Its answer is only written to the account
|
|
4214
|
+
// if that account is the one signed in when it arrives; see `keepPublicationSignature`.
|
|
4215
|
+
let publicationInFlight: {key: string; promise: Promise<`0x${string}`>} | undefined;
|
|
4216
|
+
|
|
4217
|
+
async function recoversTo(binding: PublicationBinding, signature: `0x${string}`): Promise<boolean> {
|
|
4218
|
+
try {
|
|
4219
|
+
const recovered = await recoverAccount({
|
|
4220
|
+
appOrigin: binding.origin,
|
|
4221
|
+
publicKey: binding.publicKey,
|
|
4222
|
+
ownershipSignature: signature,
|
|
4223
|
+
});
|
|
4224
|
+
return recovered.toLowerCase() === binding.address;
|
|
4225
|
+
} catch {
|
|
4226
|
+
// Malformed: not 64 or 65 bytes, an invalid recovery bit, not a point. Nothing to keep.
|
|
4227
|
+
return false;
|
|
4228
|
+
}
|
|
4229
|
+
}
|
|
4230
|
+
|
|
4231
|
+
/** The account without a kept publication signature that is not its own for this origin and key. */
|
|
4232
|
+
async function withoutForeignPublicationSignature(account: OriginAccount): Promise<OriginAccount> {
|
|
4233
|
+
const kept = account.savedPublicKeyPublicationSignature;
|
|
4234
|
+
if (!kept) {
|
|
4235
|
+
return account;
|
|
4236
|
+
}
|
|
4237
|
+
const binding: PublicationBinding = {
|
|
4238
|
+
origin: originToSignWith(),
|
|
4239
|
+
publicKey: account.signer.publicKey,
|
|
4240
|
+
address: account.address.toLowerCase() as `0x${string}`,
|
|
4241
|
+
};
|
|
4242
|
+
if (await recoversTo(binding, kept)) {
|
|
4243
|
+
return account;
|
|
4244
|
+
}
|
|
4245
|
+
const {savedPublicKeyPublicationSignature, ...rest} = account;
|
|
4246
|
+
return rest;
|
|
4247
|
+
}
|
|
4248
|
+
|
|
4249
|
+
/**
|
|
4250
|
+
* Write a publication signature onto the account it is FOR, in the store and wherever that account
|
|
4251
|
+
* is persisted. Nothing if the connection has moved on to another account or key while the wallet
|
|
4252
|
+
* was being asked: the answer is still returned to whoever asked, but it is not this account's.
|
|
4253
|
+
*/
|
|
4254
|
+
function keepPublicationSignature(binding: PublicationBinding, signature: `0x${string}`) {
|
|
4255
|
+
const isTheAccount = (account: OriginAccount | undefined) =>
|
|
4256
|
+
!!account && account.address.toLowerCase() === binding.address && account.signer?.publicKey === binding.publicKey;
|
|
4257
|
+
if (
|
|
4258
|
+
$connection.step === 'SignedIn' &&
|
|
4259
|
+
$connection.mechanism.type === 'wallet' &&
|
|
4260
|
+
originToSignWith() === binding.origin &&
|
|
4261
|
+
isTheAccount($connection.account)
|
|
4262
|
+
) {
|
|
4263
|
+
set({...$connection, account: {...$connection.account, savedPublicKeyPublicationSignature: signature}});
|
|
4264
|
+
}
|
|
4265
|
+
// Per storage and in place, never through `saveOriginAccount`: an account the user asked not to
|
|
4266
|
+
// store (`doNotStoreLocally`) stays unstored, and one that has left a storage is not resurrected
|
|
4267
|
+
// into it. See `stripStoredEntropyKey` for the same rule.
|
|
4268
|
+
for (const storage of [localStorage, sessionStorage]) {
|
|
4269
|
+
try {
|
|
4270
|
+
const raw = storage.getItem(storageKeyAccount);
|
|
4271
|
+
const stored = raw ? (JSON.parse(raw) as OriginAccount) : undefined;
|
|
4272
|
+
if (stored && isTheAccount(stored)) {
|
|
4273
|
+
storage.setItem(
|
|
4274
|
+
storageKeyAccount,
|
|
4275
|
+
JSON.stringify(withoutEntropyKey({...stored, savedPublicKeyPublicationSignature: signature})),
|
|
4276
|
+
);
|
|
4277
|
+
}
|
|
4278
|
+
} catch {
|
|
4279
|
+
// Unreadable or unwritable storage: the store still has it for this page.
|
|
4280
|
+
}
|
|
4281
|
+
}
|
|
4282
|
+
}
|
|
4283
|
+
|
|
3999
4284
|
async function unlock() {
|
|
4000
4285
|
const wallet = $connection.wallet;
|
|
4001
4286
|
if (!wallet || wallet.status !== 'locked') {
|