@sign-global/tokentable-wallets 1.3.0 → 1.5.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.
@@ -0,0 +1,139 @@
1
+ import '@mysten/dapp-kit/dist/index.css';
2
+ import EventEmitter from 'events';
3
+ import { ReactNode, useEffect, useState } from 'react';
4
+ import { WalletBase } from '../../WalletBase';
5
+ import { ISignResult } from '../../types';
6
+ import { get4361Message, prepareSignMessage } from '../../utils';
7
+ import {
8
+ createNetworkConfig,
9
+ SuiClientProvider,
10
+ WalletProvider,
11
+ ConnectModal,
12
+ useCurrentAccount,
13
+ useCurrentWallet
14
+ } from '@mysten/dapp-kit';
15
+ import { getFullnodeUrl } from '@mysten/sui/client';
16
+
17
+ interface SuiStoreType {
18
+ connectModal: {
19
+ open: boolean;
20
+ setOpen: (open: boolean) => void;
21
+ } | null;
22
+ account: ReturnType<typeof useCurrentAccount> | null;
23
+ wallet: ReturnType<typeof useCurrentWallet> | null;
24
+ counter: number;
25
+ }
26
+
27
+ const suiStore: SuiStoreType = {
28
+ connectModal: null,
29
+ account: null,
30
+ wallet: null,
31
+ counter: 0
32
+ };
33
+
34
+ export const eventBus = new EventEmitter();
35
+
36
+ const Sui_EVENT_KEY = 'sui_signin';
37
+
38
+ export const getEventKey = () => {
39
+ return `${Sui_EVENT_KEY}_${suiStore.counter}`;
40
+ };
41
+
42
+ export class SuiWallet extends WalletBase<SuiStoreType> {
43
+ getStore(): SuiStoreType {
44
+ return suiStore;
45
+ }
46
+ get isConnected(): boolean {
47
+ return !!this.getStore().wallet?.isConnected;
48
+ }
49
+
50
+ connect() {
51
+ suiStore.connectModal?.setOpen(true);
52
+ }
53
+
54
+ disconnect() {
55
+ suiStore.wallet?.currentWallet?.features['standard:disconnect']?.disconnect();
56
+ }
57
+ async sign(message: string): Promise<ISignResult> {
58
+ const signPersonalFeature = suiStore.wallet?.currentWallet?.features['sui:signPersonalMessage'];
59
+ const res = await signPersonalFeature?.signPersonalMessage({
60
+ message: new Uint8Array(Buffer.from(message)),
61
+ account: suiStore.account!
62
+ });
63
+
64
+ return {
65
+ message: message,
66
+ signature: res!.signature
67
+ };
68
+ }
69
+
70
+ signin(statement: string, prepare = true): Promise<ISignResult> {
71
+ return new Promise((resolve) => {
72
+ const signCallback = (data: ReturnType<typeof useCurrentAccount>) => {
73
+ this.publicKey = Buffer.from(data!.publicKey).toString('hex');
74
+ this.address = data?.address;
75
+ this.chainId = 1;
76
+ let fullMessage = statement;
77
+ if (prepare) {
78
+ const msg = prepareSignMessage({ statement, chainId: this.chainId, address: this.address! });
79
+ fullMessage = get4361Message(msg);
80
+ }
81
+
82
+ const res = this.sign(JSON.stringify(fullMessage, null, ' '));
83
+
84
+ resolve(res);
85
+ };
86
+
87
+ if (this.isConnected && suiStore.account) {
88
+ signCallback(suiStore.account!);
89
+ return;
90
+ }
91
+ suiStore.counter++;
92
+ this.connect();
93
+ const eventKey = getEventKey();
94
+ eventBus.once(eventKey, (data) => {
95
+ console.log('listen event key success, event key = %s, data = %j', eventKey, data);
96
+ signCallback(data);
97
+ });
98
+ });
99
+ }
100
+ }
101
+
102
+ export const SuiConnector = () => {
103
+ const [showModal, setShowModal] = useState(false);
104
+ const account = useCurrentAccount();
105
+ const wallet = useCurrentWallet();
106
+
107
+ useEffect(() => {
108
+ if (wallet) {
109
+ if (wallet.isConnected && account) {
110
+ const eventKey = getEventKey();
111
+ eventBus.emit(eventKey, account);
112
+ }
113
+ }
114
+ }, [wallet, account]);
115
+
116
+ suiStore.connectModal = {
117
+ open: showModal,
118
+ setOpen: setShowModal
119
+ };
120
+ suiStore.account = account;
121
+ suiStore.wallet = wallet;
122
+
123
+ return <ConnectModal open={showModal} onOpenChange={(open) => setShowModal(open)} trigger={<div></div>} />;
124
+ };
125
+
126
+ export const SuiProvider = ({ children, network }: { children: ReactNode; network: 'testnet' | 'mainnet' }) => {
127
+ const { networkConfig } = createNetworkConfig({
128
+ testnet: { url: getFullnodeUrl('testnet') },
129
+ mainnet: { url: getFullnodeUrl('mainnet') }
130
+ });
131
+ return (
132
+ <SuiClientProvider networks={networkConfig} network={network}>
133
+ <WalletProvider autoConnect>
134
+ <SuiConnector />
135
+ {children}
136
+ </WalletProvider>
137
+ </SuiClientProvider>
138
+ );
139
+ };
package/src/types.ts CHANGED
@@ -1,24 +1,3 @@
1
- export enum WalletType {
2
- EVM = 'evm',
3
- BITCOIN = 'bitcoin',
4
- SOLANA = 'solana',
5
- TON = 'ton',
6
- APTOS = 'aptos',
7
- PARTICLE = 'particle-evm',
8
- OKX = 'okx',
9
- Cyber = 'cyber',
10
- StarkNet = 'starknet',
11
- WALLETCONNECT = 'walletconnect',
12
- Cardano = 'cardano'
13
- }
14
-
15
- export enum ExtendedWalletType {
16
- UNISAT = 'unisat',
17
- OKXBTC = 'okx_btc'
18
- }
19
-
20
- export type ProviderType = WalletType | ExtendedWalletType;
21
-
22
1
  export type ISignResult =
23
2
  | {
24
3
  message: string;