@mysten/dapp-kit-core 1.0.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/CHANGELOG.md +27 -0
- package/LICENSE +201 -0
- package/README.md +3 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +793 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types-x68aipir.d.mts +240 -0
- package/dist/types-x68aipir.d.mts.map +1 -0
- package/dist/web/index.d.mts +270 -0
- package/dist/web/index.d.mts.map +1 -0
- package/dist/web/index.mjs +1446 -0
- package/dist/web/index.mjs.map +1 -0
- package/package.json +83 -0
- package/src/core/actions/connect-wallet.ts +117 -0
- package/src/core/actions/disconnect-wallet.ts +43 -0
- package/src/core/actions/sign-and-execute-transaction.ts +119 -0
- package/src/core/actions/sign-personal-message.ts +42 -0
- package/src/core/actions/sign-transaction.ts +83 -0
- package/src/core/actions/switch-account.ts +32 -0
- package/src/core/actions/switch-network.ts +16 -0
- package/src/core/index.ts +119 -0
- package/src/core/initializers/autoconnect-wallet.ts +116 -0
- package/src/core/initializers/manage-connection.ts +50 -0
- package/src/core/initializers/registered-wallets.ts +68 -0
- package/src/core/initializers/sync-state-to-storage.ts +44 -0
- package/src/core/store.ts +130 -0
- package/src/core/types.ts +87 -0
- package/src/index.ts +19 -0
- package/src/types.ts +15 -0
- package/src/utils/errors.ts +29 -0
- package/src/utils/lit.ts +61 -0
- package/src/utils/name.ts +26 -0
- package/src/utils/networks.ts +35 -0
- package/src/utils/signer.ts +75 -0
- package/src/utils/storage.ts +40 -0
- package/src/utils/transaction-result.ts +50 -0
- package/src/utils/wallets.ts +82 -0
- package/src/wallets/index.ts +56 -0
- package/src/wallets/slush-web.ts +27 -0
- package/src/wallets/unsafe-burner.ts +165 -0
- package/src/web/dapp-kit-connect-button.stories.ts +88 -0
- package/src/web/dapp-kit-connect-button.ts +114 -0
- package/src/web/dapp-kit-connect-modal.stories.ts +62 -0
- package/src/web/dapp-kit-connect-modal.styles.ts +48 -0
- package/src/web/dapp-kit-connect-modal.ts +244 -0
- package/src/web/index.ts +12 -0
- package/src/web/internal/base-modal.ts +149 -0
- package/src/web/internal/button.styles.ts +54 -0
- package/src/web/internal/button.ts +49 -0
- package/src/web/internal/connected-account-menu-item.ts +116 -0
- package/src/web/internal/connected-account-menu.styles.ts +217 -0
- package/src/web/internal/connected-account-menu.ts +193 -0
- package/src/web/internal/connection-status.styles.ts +47 -0
- package/src/web/internal/connection-status.ts +31 -0
- package/src/web/internal/icons/arrow-right-up-icon.ts +19 -0
- package/src/web/internal/icons/back-icon.ts +19 -0
- package/src/web/internal/icons/chevron-down-icon.ts +18 -0
- package/src/web/internal/icons/circle-check-icon.ts +19 -0
- package/src/web/internal/icons/close-icon.ts +18 -0
- package/src/web/internal/icons/copy-icon.ts +18 -0
- package/src/web/internal/icons/download-icon.ts +24 -0
- package/src/web/internal/icons/plus-icon.ts +18 -0
- package/src/web/internal/icons/unlink-icon.ts +18 -0
- package/src/web/internal/wallet-list-item.styles.ts +41 -0
- package/src/web/internal/wallet-list-item.ts +45 -0
- package/src/web/internal/wallet-list.styles.ts +49 -0
- package/src/web/internal/wallet-list.ts +46 -0
- package/src/web/styles/icon-button.ts +28 -0
- package/src/web/styles/index.ts +7 -0
- package/src/web/styles/reset.ts +45 -0
- package/src/web/styles/theme.ts +56 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { DAppKitStores } from '../store.js';
|
|
5
|
+
import { SuiSignTransaction, SuiSignTransactionBlock } from '@mysten/wallet-standard';
|
|
6
|
+
import type {
|
|
7
|
+
SuiSignTransactionBlockFeature,
|
|
8
|
+
SuiSignTransactionFeature,
|
|
9
|
+
SuiSignTransactionInput,
|
|
10
|
+
} from '@mysten/wallet-standard';
|
|
11
|
+
import { getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as getWalletAccountForUiWalletAccount } from '@wallet-standard/ui-registry';
|
|
12
|
+
import { FeatureNotSupportedError, WalletNotConnectedError } from '../../utils/errors.js';
|
|
13
|
+
import { getChain } from '../../utils/networks.js';
|
|
14
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
15
|
+
import { tryGetAccountFeature } from '../../utils/wallets.js';
|
|
16
|
+
|
|
17
|
+
export type SignTransactionArgs = {
|
|
18
|
+
transaction: Transaction | string;
|
|
19
|
+
} & Omit<SuiSignTransactionInput, 'account' | 'chain' | 'transaction'>;
|
|
20
|
+
|
|
21
|
+
export function signTransactionCreator({ $connection, $currentClient }: DAppKitStores) {
|
|
22
|
+
/**
|
|
23
|
+
* Prompts the specified wallet account to sign a transaction.
|
|
24
|
+
*/
|
|
25
|
+
return async function signTransaction({ transaction, ...standardArgs }: SignTransactionArgs) {
|
|
26
|
+
const { account, supportedIntents } = $connection.get();
|
|
27
|
+
if (!account) {
|
|
28
|
+
throw new WalletNotConnectedError('No wallet is connected.');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const underlyingAccount = getWalletAccountForUiWalletAccount(account);
|
|
32
|
+
const suiClient = $currentClient.get();
|
|
33
|
+
const chain = getChain(suiClient.network);
|
|
34
|
+
|
|
35
|
+
const transactionWrapper = {
|
|
36
|
+
toJSON: async () => {
|
|
37
|
+
if (typeof transaction === 'string') {
|
|
38
|
+
return transaction;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
transaction.setSenderIfNotSet(account.address);
|
|
42
|
+
return await transaction.toJSON({ client: suiClient, supportedIntents });
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const signTransactionFeature = tryGetAccountFeature({
|
|
47
|
+
account,
|
|
48
|
+
chain,
|
|
49
|
+
featureName: SuiSignTransaction,
|
|
50
|
+
}) as SuiSignTransactionFeature[typeof SuiSignTransaction];
|
|
51
|
+
|
|
52
|
+
if (signTransactionFeature) {
|
|
53
|
+
return await signTransactionFeature.signTransaction({
|
|
54
|
+
...standardArgs,
|
|
55
|
+
transaction: transactionWrapper,
|
|
56
|
+
account: underlyingAccount,
|
|
57
|
+
chain,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const signTransactionBlockFeature = tryGetAccountFeature({
|
|
62
|
+
account,
|
|
63
|
+
chain,
|
|
64
|
+
featureName: SuiSignTransactionBlock,
|
|
65
|
+
}) as SuiSignTransactionBlockFeature[typeof SuiSignTransactionBlock];
|
|
66
|
+
|
|
67
|
+
if (signTransactionBlockFeature) {
|
|
68
|
+
const transaction = Transaction.from(await transactionWrapper.toJSON());
|
|
69
|
+
const { transactionBlockBytes, signature } =
|
|
70
|
+
await signTransactionBlockFeature.signTransactionBlock({
|
|
71
|
+
transactionBlock: transaction,
|
|
72
|
+
account: underlyingAccount,
|
|
73
|
+
chain,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return { bytes: transactionBlockBytes, signature };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
throw new FeatureNotSupportedError(
|
|
80
|
+
`The account ${account.address} does not support signing transactions.`,
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { DAppKitStores } from '../store.js';
|
|
5
|
+
import { uiWalletAccountBelongsToUiWallet } from '@wallet-standard/ui';
|
|
6
|
+
import type { UiWalletAccount } from '@wallet-standard/ui';
|
|
7
|
+
import { WalletNotConnectedError, WalletAccountNotFoundError } from '../../utils/errors.js';
|
|
8
|
+
|
|
9
|
+
export type SwitchAccountArgs = {
|
|
10
|
+
/** The account to switch to. */
|
|
11
|
+
account: UiWalletAccount;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function switchAccountCreator({ $baseConnection, $connection }: DAppKitStores) {
|
|
15
|
+
/**
|
|
16
|
+
* Switches the currently selected account to the specified account.
|
|
17
|
+
*/
|
|
18
|
+
return function switchAccount({ account }: SwitchAccountArgs) {
|
|
19
|
+
const { wallet } = $connection.get();
|
|
20
|
+
if (!wallet) {
|
|
21
|
+
throw new WalletNotConnectedError('No wallet is connected.');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!uiWalletAccountBelongsToUiWallet(account, wallet)) {
|
|
25
|
+
throw new WalletAccountNotFoundError(
|
|
26
|
+
`No account with address ${account.address} is connected to ${wallet.name}.`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
$baseConnection.setKey('currentAccount', account);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { DAppKitStores } from '../store.js';
|
|
5
|
+
import type { Networks } from '../../utils/networks.js';
|
|
6
|
+
|
|
7
|
+
export function switchNetworkCreator<TNetworks extends Networks>({
|
|
8
|
+
$currentNetwork,
|
|
9
|
+
}: DAppKitStores<TNetworks>) {
|
|
10
|
+
/**
|
|
11
|
+
* Switches the currently selected network to the specified network.
|
|
12
|
+
*/
|
|
13
|
+
return function switchNetwork<T extends TNetworks[number]>(network: T | TNetworks[number]) {
|
|
14
|
+
$currentNetwork.set(network);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ReadableAtom } from 'nanostores';
|
|
5
|
+
import { readonlyType } from 'nanostores';
|
|
6
|
+
import type { DAppKitStores } from './store.js';
|
|
7
|
+
import { createStores } from './store.js';
|
|
8
|
+
import { syncRegisteredWallets } from './initializers/registered-wallets.js';
|
|
9
|
+
import { autoConnectWallet } from './initializers/autoconnect-wallet.js';
|
|
10
|
+
import { createInMemoryStorage, DEFAULT_STORAGE_KEY, getDefaultStorage } from '../utils/storage.js';
|
|
11
|
+
import { syncStateToStorage } from './initializers/sync-state-to-storage.js';
|
|
12
|
+
import { manageWalletConnection } from './initializers/manage-connection.js';
|
|
13
|
+
import { createNetworkConfig } from '../utils/networks.js';
|
|
14
|
+
import type { Networks } from '../utils/networks.js';
|
|
15
|
+
import type { CreateDAppKitOptions, DAppKitCompatibleClient } from './types.js';
|
|
16
|
+
import { switchNetworkCreator } from './actions/switch-network.js';
|
|
17
|
+
import type { ConnectWalletArgs } from './actions/connect-wallet.js';
|
|
18
|
+
import { connectWalletCreator } from './actions/connect-wallet.js';
|
|
19
|
+
import { disconnectWalletCreator } from './actions/disconnect-wallet.js';
|
|
20
|
+
import type { SwitchAccountArgs } from './actions/switch-account.js';
|
|
21
|
+
import { switchAccountCreator } from './actions/switch-account.js';
|
|
22
|
+
import type { SignPersonalMessageArgs } from './actions/sign-personal-message.js';
|
|
23
|
+
import { signPersonalMessageCreator } from './actions/sign-personal-message.js';
|
|
24
|
+
import type {
|
|
25
|
+
SignAndExecuteTransactionArgs,
|
|
26
|
+
SignAndExecuteTransactionResult,
|
|
27
|
+
} from './actions/sign-and-execute-transaction.js';
|
|
28
|
+
import { signAndExecuteTransactionCreator } from './actions/sign-and-execute-transaction.js';
|
|
29
|
+
import type { SignTransactionArgs } from './actions/sign-transaction.js';
|
|
30
|
+
import { signTransactionCreator } from './actions/sign-transaction.js';
|
|
31
|
+
import { slushWebWalletInitializer } from '../wallets/slush-web.js';
|
|
32
|
+
import { registerAdditionalWallets } from '../wallets/index.js';
|
|
33
|
+
import { unsafeBurnerWalletInitializer } from '../wallets/unsafe-burner.js';
|
|
34
|
+
import type { SignedPersonalMessage, SignedTransaction } from '@mysten/wallet-standard';
|
|
35
|
+
import type { UiWalletAccount } from '@wallet-standard/ui';
|
|
36
|
+
|
|
37
|
+
export type DAppKit<
|
|
38
|
+
TNetworks extends Networks = [],
|
|
39
|
+
Client extends DAppKitCompatibleClient = DAppKitCompatibleClient,
|
|
40
|
+
> = {
|
|
41
|
+
networks: TNetworks;
|
|
42
|
+
getClient: (network?: TNetworks[number]) => Client;
|
|
43
|
+
signTransaction: (args: SignTransactionArgs) => Promise<SignedTransaction>;
|
|
44
|
+
signAndExecuteTransaction: (
|
|
45
|
+
args: SignAndExecuteTransactionArgs,
|
|
46
|
+
) => Promise<SignAndExecuteTransactionResult>;
|
|
47
|
+
signPersonalMessage: (args: SignPersonalMessageArgs) => Promise<SignedPersonalMessage>;
|
|
48
|
+
connectWallet: (args: ConnectWalletArgs) => Promise<{
|
|
49
|
+
accounts: UiWalletAccount[];
|
|
50
|
+
}>;
|
|
51
|
+
disconnectWallet: () => Promise<void>;
|
|
52
|
+
switchAccount: (args: SwitchAccountArgs) => void;
|
|
53
|
+
switchNetwork: (network: TNetworks[number]) => void;
|
|
54
|
+
stores: {
|
|
55
|
+
$wallets: DAppKitStores['$compatibleWallets'];
|
|
56
|
+
$connection: DAppKitStores['$connection'];
|
|
57
|
+
$currentNetwork: ReadableAtom<TNetworks[number]>;
|
|
58
|
+
$currentClient: ReadableAtom<Client>;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export function createDAppKit<
|
|
63
|
+
TNetworks extends Networks = Networks,
|
|
64
|
+
Client extends DAppKitCompatibleClient = DAppKitCompatibleClient,
|
|
65
|
+
>({
|
|
66
|
+
autoConnect = true,
|
|
67
|
+
networks,
|
|
68
|
+
createClient,
|
|
69
|
+
defaultNetwork = networks[0],
|
|
70
|
+
enableBurnerWallet = false,
|
|
71
|
+
slushWalletConfig,
|
|
72
|
+
storage = getDefaultStorage(),
|
|
73
|
+
storageKey = DEFAULT_STORAGE_KEY,
|
|
74
|
+
walletInitializers = [],
|
|
75
|
+
}: CreateDAppKitOptions<TNetworks, Client>): DAppKit<TNetworks, Client> {
|
|
76
|
+
const networkConfig = createNetworkConfig(networks, createClient);
|
|
77
|
+
const stores = createStores({ defaultNetwork, getClient: networkConfig.getClient });
|
|
78
|
+
|
|
79
|
+
function getClient<T extends TNetworks[number]>(network?: TNetworks[number] | T): Client {
|
|
80
|
+
return network ? networkConfig.getClient(network) : stores.$currentClient.get();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
storage ||= createInMemoryStorage();
|
|
84
|
+
syncStateToStorage({ stores, storageKey, storage });
|
|
85
|
+
|
|
86
|
+
syncRegisteredWallets(stores);
|
|
87
|
+
manageWalletConnection(stores);
|
|
88
|
+
|
|
89
|
+
if (autoConnect) {
|
|
90
|
+
autoConnectWallet({ networks, stores, storageKey, storage });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
registerAdditionalWallets(
|
|
94
|
+
[
|
|
95
|
+
...walletInitializers,
|
|
96
|
+
...(enableBurnerWallet ? [unsafeBurnerWalletInitializer()] : []),
|
|
97
|
+
...(slushWalletConfig !== null ? [slushWebWalletInitializer(slushWalletConfig)] : []),
|
|
98
|
+
],
|
|
99
|
+
{ networks, getClient },
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
networks,
|
|
104
|
+
getClient,
|
|
105
|
+
signTransaction: signTransactionCreator(stores),
|
|
106
|
+
signAndExecuteTransaction: signAndExecuteTransactionCreator(stores),
|
|
107
|
+
signPersonalMessage: signPersonalMessageCreator(stores),
|
|
108
|
+
connectWallet: connectWalletCreator(stores, networks),
|
|
109
|
+
disconnectWallet: disconnectWalletCreator(stores),
|
|
110
|
+
switchAccount: switchAccountCreator(stores),
|
|
111
|
+
switchNetwork: switchNetworkCreator(stores),
|
|
112
|
+
stores: {
|
|
113
|
+
$wallets: stores.$compatibleWallets,
|
|
114
|
+
$connection: stores.$connection,
|
|
115
|
+
$currentNetwork: readonlyType(stores.$currentNetwork),
|
|
116
|
+
$currentClient: stores.$currentClient,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { onMount, task } from 'nanostores';
|
|
5
|
+
import type { DAppKitStores } from '../store.js';
|
|
6
|
+
import type { StateStorage } from '../../utils/storage.js';
|
|
7
|
+
import type { UiWallet } from '@wallet-standard/ui';
|
|
8
|
+
import { getWalletUniqueIdentifier } from '../../utils/wallets.js';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
getSupportedIntentsFromFeature,
|
|
12
|
+
internalConnectWallet,
|
|
13
|
+
} from '../actions/connect-wallet.js';
|
|
14
|
+
import type { Networks } from '../../utils/networks.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Attempts to connect to a previously authorized wallet account on mount and when new wallets are registered.
|
|
18
|
+
*/
|
|
19
|
+
export function autoConnectWallet({
|
|
20
|
+
networks,
|
|
21
|
+
stores: { $baseConnection, $compatibleWallets },
|
|
22
|
+
storage,
|
|
23
|
+
storageKey,
|
|
24
|
+
}: {
|
|
25
|
+
networks: Networks;
|
|
26
|
+
stores: DAppKitStores;
|
|
27
|
+
storage: StateStorage;
|
|
28
|
+
storageKey: string;
|
|
29
|
+
}) {
|
|
30
|
+
onMount($compatibleWallets, () => {
|
|
31
|
+
return $compatibleWallets.listen(
|
|
32
|
+
async (wallets, oldWallets: readonly UiWallet[] | undefined) => {
|
|
33
|
+
if (oldWallets && oldWallets.length > wallets.length) return;
|
|
34
|
+
|
|
35
|
+
const connection = $baseConnection.get();
|
|
36
|
+
if (connection.status !== 'disconnected') return;
|
|
37
|
+
|
|
38
|
+
const savedWalletAccount = await task(() => {
|
|
39
|
+
return getSavedWalletAccount({
|
|
40
|
+
networks,
|
|
41
|
+
storage,
|
|
42
|
+
storageKey,
|
|
43
|
+
wallets,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (savedWalletAccount) {
|
|
48
|
+
$baseConnection.set({
|
|
49
|
+
status: 'connected',
|
|
50
|
+
currentAccount: savedWalletAccount.account,
|
|
51
|
+
supportedIntents: savedWalletAccount.supportedIntents,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function getSavedWalletAccount({
|
|
60
|
+
networks,
|
|
61
|
+
storage,
|
|
62
|
+
storageKey,
|
|
63
|
+
wallets,
|
|
64
|
+
}: {
|
|
65
|
+
networks: Networks;
|
|
66
|
+
storage: StateStorage;
|
|
67
|
+
storageKey: string;
|
|
68
|
+
wallets: readonly UiWallet[];
|
|
69
|
+
}) {
|
|
70
|
+
const savedWalletIdAndAddress = await storage.getItem(storageKey);
|
|
71
|
+
if (!savedWalletIdAndAddress) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const [savedWalletId, savedAccountAddress, savedIntents] = savedWalletIdAndAddress.split(':');
|
|
76
|
+
if (!savedWalletId || !savedAccountAddress) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const targetWallet = wallets.find(
|
|
81
|
+
(wallet) => getWalletUniqueIdentifier(wallet) === savedWalletId,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (!targetWallet) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const existingAccount = targetWallet.accounts.find(
|
|
89
|
+
(account) => account.address === savedAccountAddress,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
if (existingAccount) {
|
|
93
|
+
const supportedIntents = savedIntents ? savedIntents.split(',') : null;
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
account: existingAccount,
|
|
97
|
+
supportedIntents: supportedIntents ?? (await getSupportedIntentsFromFeature(targetWallet)),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// For wallets that don't pre-populate the accounts array on page load,
|
|
102
|
+
// we need to silently request authorization and get the account directly.
|
|
103
|
+
const { accounts: alreadyAuthorizedAccounts, supportedIntents } = await internalConnectWallet(
|
|
104
|
+
targetWallet,
|
|
105
|
+
networks,
|
|
106
|
+
{
|
|
107
|
+
silent: true,
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const account = alreadyAuthorizedAccounts.find(
|
|
112
|
+
(account) => account.address === savedAccountAddress,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return account ? { account, supportedIntents } : null;
|
|
116
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { onMount } from 'nanostores';
|
|
5
|
+
import type { DAppKitStores } from '../store.js';
|
|
6
|
+
|
|
7
|
+
import type { UiWallet, UiWalletAccount } from '@wallet-standard/ui';
|
|
8
|
+
import { uiWalletAccountBelongsToUiWallet, uiWalletAccountsAreSame } from '@wallet-standard/ui';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Handles updating the connection state in response to wallets and their properties changing.
|
|
12
|
+
*/
|
|
13
|
+
export function manageWalletConnection({ $compatibleWallets, $baseConnection }: DAppKitStores) {
|
|
14
|
+
onMount($compatibleWallets, () => {
|
|
15
|
+
return $compatibleWallets.listen(async (wallets) => {
|
|
16
|
+
const connection = $baseConnection.get();
|
|
17
|
+
if (connection.status !== 'connected') return;
|
|
18
|
+
|
|
19
|
+
const resolvedAccount = resolveWalletAccount(connection.currentAccount, wallets);
|
|
20
|
+
if (resolvedAccount) {
|
|
21
|
+
// Update the current account in response to the account properties changing.
|
|
22
|
+
// If the current account was deemed incompatible, we'll default to the
|
|
23
|
+
// first account in the wallet:
|
|
24
|
+
$baseConnection.setKey('currentAccount', resolvedAccount);
|
|
25
|
+
} else {
|
|
26
|
+
// Reset the connection if the underlying wallet was un-registered or deemed incompatible:
|
|
27
|
+
$baseConnection.set({
|
|
28
|
+
status: 'disconnected',
|
|
29
|
+
currentAccount: null,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function resolveWalletAccount(currentAccount: UiWalletAccount, wallets: readonly UiWallet[]) {
|
|
37
|
+
for (const wallet of wallets) {
|
|
38
|
+
for (const walletAccount of wallet.accounts) {
|
|
39
|
+
if (uiWalletAccountsAreSame(currentAccount, walletAccount)) {
|
|
40
|
+
return walletAccount;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (uiWalletAccountBelongsToUiWallet(currentAccount, wallet) && wallet.accounts[0]) {
|
|
45
|
+
return wallet.accounts[0];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { getWallets, StandardEvents } from '@mysten/wallet-standard';
|
|
5
|
+
import { onMount } from 'nanostores';
|
|
6
|
+
import type { DAppKitStores } from '../store.js';
|
|
7
|
+
|
|
8
|
+
import { getOrCreateUiWalletForStandardWallet_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as getOrCreateUiWalletForStandardWallet } from '@wallet-standard/ui-registry';
|
|
9
|
+
import type { StandardEventsFeature, Wallet, WalletWithFeatures } from '@mysten/wallet-standard';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Handles updating the store in response to wallets being added, removed, and their properties changing.
|
|
13
|
+
*/
|
|
14
|
+
export function syncRegisteredWallets({ $registeredWallets }: DAppKitStores) {
|
|
15
|
+
onMount($registeredWallets, () => {
|
|
16
|
+
const walletsApi = getWallets();
|
|
17
|
+
const unsubscribeCallbacksByWallet = new Map<Wallet, () => void>();
|
|
18
|
+
|
|
19
|
+
const onWalletsChanged = () => {
|
|
20
|
+
const wallets = walletsApi.get();
|
|
21
|
+
$registeredWallets.set(wallets.map(getOrCreateUiWalletForStandardWallet));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const subscribeToWalletEvents = (wallet: WalletWithFeatures<StandardEventsFeature>) => {
|
|
25
|
+
const unsubscribeFromChange = wallet.features[StandardEvents].on('change', () => {
|
|
26
|
+
onWalletsChanged();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// NOTE: The underlying wallet entities returned from the Wallet Standard are
|
|
30
|
+
// referentially equal even when the properties of a wallet change. Thus, it is
|
|
31
|
+
// safe to use the wallet object itself as the key for the unsubscribe mapping.
|
|
32
|
+
unsubscribeCallbacksByWallet.set(wallet, unsubscribeFromChange);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const unsubscribeFromRegister = walletsApi.on('register', (...addedWallets) => {
|
|
36
|
+
addedWallets.filter(hasStandardEvents).forEach(subscribeToWalletEvents);
|
|
37
|
+
onWalletsChanged();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const unsubscribeFromUnregister = walletsApi.on('unregister', (...removedWallets) => {
|
|
41
|
+
removedWallets.filter(hasStandardEvents).forEach((wallet) => {
|
|
42
|
+
const unsubscribeFromChange = unsubscribeCallbacksByWallet.get(wallet);
|
|
43
|
+
if (unsubscribeFromChange) {
|
|
44
|
+
unsubscribeCallbacksByWallet.delete(wallet);
|
|
45
|
+
unsubscribeFromChange();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
onWalletsChanged();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const wallets = walletsApi.get();
|
|
53
|
+
wallets.filter(hasStandardEvents).forEach(subscribeToWalletEvents);
|
|
54
|
+
onWalletsChanged();
|
|
55
|
+
|
|
56
|
+
return () => {
|
|
57
|
+
unsubscribeFromRegister();
|
|
58
|
+
unsubscribeFromUnregister();
|
|
59
|
+
|
|
60
|
+
unsubscribeCallbacksByWallet.forEach((unsubscribe) => unsubscribe());
|
|
61
|
+
unsubscribeCallbacksByWallet.clear();
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function hasStandardEvents(wallet: Wallet): wallet is WalletWithFeatures<StandardEventsFeature> {
|
|
67
|
+
return StandardEvents in wallet.features;
|
|
68
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { onMount } from 'nanostores';
|
|
5
|
+
import type { DAppKitStores } from '../store.js';
|
|
6
|
+
import type { StateStorage } from '../../utils/storage.js';
|
|
7
|
+
import type { UiWalletAccount } from '@wallet-standard/ui';
|
|
8
|
+
import { getWalletUniqueIdentifier } from '../../utils/wallets.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Syncs the most recently connected wallet name and address to storage.
|
|
12
|
+
*/
|
|
13
|
+
export function syncStateToStorage({
|
|
14
|
+
stores: { $connection },
|
|
15
|
+
storage,
|
|
16
|
+
storageKey,
|
|
17
|
+
}: {
|
|
18
|
+
stores: DAppKitStores;
|
|
19
|
+
storage: StateStorage;
|
|
20
|
+
storageKey: string;
|
|
21
|
+
}) {
|
|
22
|
+
onMount($connection, () => {
|
|
23
|
+
return $connection.listen((connection, oldConnection) => {
|
|
24
|
+
if (!oldConnection || oldConnection.status === connection.status) return;
|
|
25
|
+
|
|
26
|
+
if (connection.account) {
|
|
27
|
+
storage.setItem(
|
|
28
|
+
storageKey,
|
|
29
|
+
getSavedAccountStorageKey(connection.account, connection.supportedIntents),
|
|
30
|
+
);
|
|
31
|
+
} else {
|
|
32
|
+
storage.removeItem(storageKey);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function getSavedAccountStorageKey(
|
|
39
|
+
account: UiWalletAccount,
|
|
40
|
+
supportedIntents: string[],
|
|
41
|
+
): string {
|
|
42
|
+
const walletIdentifier = getWalletUniqueIdentifier(account);
|
|
43
|
+
return `${walletIdentifier.replace(':', '_')}:${account.address}:${supportedIntents.join(',')}:`;
|
|
44
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { UiWallet, UiWalletAccount } from '@wallet-standard/ui';
|
|
5
|
+
import type { StoreValue } from 'nanostores';
|
|
6
|
+
import { atom, computed, map } from 'nanostores';
|
|
7
|
+
import { getChain } from '../utils/networks.js';
|
|
8
|
+
import type { Networks } from '../utils/networks.js';
|
|
9
|
+
import {
|
|
10
|
+
getAssociatedWalletOrThrow,
|
|
11
|
+
requiredWalletFeatures,
|
|
12
|
+
signingFeatures,
|
|
13
|
+
} from '../utils/wallets.js';
|
|
14
|
+
import type { DAppKitCompatibleClient } from './types.js';
|
|
15
|
+
|
|
16
|
+
type InternalWalletConnection =
|
|
17
|
+
| {
|
|
18
|
+
status: 'disconnected' | 'connecting';
|
|
19
|
+
currentAccount: null;
|
|
20
|
+
}
|
|
21
|
+
| {
|
|
22
|
+
status: 'reconnecting' | 'connected';
|
|
23
|
+
currentAccount: UiWalletAccount;
|
|
24
|
+
supportedIntents: string[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type DAppKitStores<
|
|
28
|
+
TNetworks extends Networks = Networks,
|
|
29
|
+
Client extends DAppKitCompatibleClient = DAppKitCompatibleClient,
|
|
30
|
+
> = ReturnType<typeof createStores<TNetworks, Client>>;
|
|
31
|
+
|
|
32
|
+
export type WalletConnection = StoreValue<DAppKitStores['$connection']>;
|
|
33
|
+
|
|
34
|
+
export function createStores<
|
|
35
|
+
TNetworks extends Networks = [],
|
|
36
|
+
Client extends DAppKitCompatibleClient = DAppKitCompatibleClient,
|
|
37
|
+
>({
|
|
38
|
+
defaultNetwork,
|
|
39
|
+
getClient,
|
|
40
|
+
}: {
|
|
41
|
+
defaultNetwork: TNetworks[number];
|
|
42
|
+
getClient: (network: TNetworks[number]) => Client;
|
|
43
|
+
}) {
|
|
44
|
+
const $baseConnection = map<InternalWalletConnection>({
|
|
45
|
+
status: 'disconnected',
|
|
46
|
+
currentAccount: null,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const $currentNetwork = atom<TNetworks[number]>(defaultNetwork);
|
|
50
|
+
const $registeredWallets = atom<UiWallet[]>([]);
|
|
51
|
+
|
|
52
|
+
const $compatibleWallets = computed(
|
|
53
|
+
[$registeredWallets, $currentNetwork],
|
|
54
|
+
(wallets, currentNetwork) => {
|
|
55
|
+
return wallets.filter((wallet) => {
|
|
56
|
+
const areChainsCompatible = wallet.chains.some(
|
|
57
|
+
(chain) => getChain(currentNetwork) === chain,
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const hasRequiredFeatures = requiredWalletFeatures.every((featureName) =>
|
|
61
|
+
wallet.features.includes(featureName),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const canSignTransactions = signingFeatures.some((featureName) =>
|
|
65
|
+
wallet.features.includes(featureName),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return areChainsCompatible && hasRequiredFeatures && canSignTransactions;
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
$currentNetwork,
|
|
75
|
+
$registeredWallets,
|
|
76
|
+
$compatibleWallets,
|
|
77
|
+
$baseConnection,
|
|
78
|
+
$currentClient: computed($currentNetwork, getClient),
|
|
79
|
+
$connection: computed([$baseConnection, $compatibleWallets], (connection, wallets) => {
|
|
80
|
+
switch (connection.status) {
|
|
81
|
+
case 'connected':
|
|
82
|
+
return {
|
|
83
|
+
wallet: getAssociatedWalletOrThrow(connection.currentAccount, wallets),
|
|
84
|
+
account: connection.currentAccount,
|
|
85
|
+
status: connection.status,
|
|
86
|
+
supportedIntents: connection.supportedIntents,
|
|
87
|
+
isConnected: true,
|
|
88
|
+
isConnecting: false,
|
|
89
|
+
isReconnecting: false,
|
|
90
|
+
isDisconnected: false,
|
|
91
|
+
} as const;
|
|
92
|
+
case 'connecting':
|
|
93
|
+
return {
|
|
94
|
+
wallet: null,
|
|
95
|
+
account: connection.currentAccount,
|
|
96
|
+
status: connection.status,
|
|
97
|
+
supportedIntents: [],
|
|
98
|
+
isConnected: false,
|
|
99
|
+
isConnecting: true,
|
|
100
|
+
isReconnecting: false,
|
|
101
|
+
isDisconnected: false,
|
|
102
|
+
} as const;
|
|
103
|
+
case 'reconnecting':
|
|
104
|
+
return {
|
|
105
|
+
wallet: getAssociatedWalletOrThrow(connection.currentAccount, wallets),
|
|
106
|
+
account: connection.currentAccount,
|
|
107
|
+
status: connection.status,
|
|
108
|
+
supportedIntents: connection.supportedIntents,
|
|
109
|
+
isConnected: false,
|
|
110
|
+
isConnecting: false,
|
|
111
|
+
isReconnecting: true,
|
|
112
|
+
isDisconnected: false,
|
|
113
|
+
} as const;
|
|
114
|
+
case 'disconnected':
|
|
115
|
+
return {
|
|
116
|
+
wallet: null,
|
|
117
|
+
account: connection.currentAccount,
|
|
118
|
+
status: connection.status,
|
|
119
|
+
supportedIntents: [],
|
|
120
|
+
isConnected: false,
|
|
121
|
+
isConnecting: false,
|
|
122
|
+
isReconnecting: false,
|
|
123
|
+
isDisconnected: true,
|
|
124
|
+
} as const;
|
|
125
|
+
default:
|
|
126
|
+
throw new Error(`Encountered unknown connection status: ${connection}`);
|
|
127
|
+
}
|
|
128
|
+
}),
|
|
129
|
+
};
|
|
130
|
+
}
|