@sign-global/tokentable 0.4.2-beta.2 → 0.4.2-beta.4

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.
Files changed (119) hide show
  1. package/dist/cjs/airdrop/common/index.d.ts +1 -1
  2. package/dist/cjs/airdrop/constants/chain-config.d.ts +1 -1
  3. package/dist/cjs/airdrop/constants/chain-config.js +1 -1
  4. package/dist/cjs/airdrop/constants/chain-config.js.map +1 -1
  5. package/dist/esm/airdrop/common/index.d.ts +1 -1
  6. package/dist/esm/airdrop/constants/chain-config.d.ts +1 -1
  7. package/dist/esm/airdrop/constants/chain-config.js +1 -1
  8. package/dist/esm/airdrop/constants/chain-config.js.map +1 -1
  9. package/package.json +2 -9
  10. package/src/airdrop/AirdropClient.ts +0 -307
  11. package/src/airdrop/SignatureAirdropClient.ts +0 -378
  12. package/src/airdrop/common/index.ts +0 -54
  13. package/src/airdrop/common/projectDataForTon.ts +0 -79
  14. package/src/airdrop/constants/chain-config.ts +0 -107
  15. package/src/airdrop/constants/index.ts +0 -9
  16. package/src/airdrop/constants/network.ts +0 -78
  17. package/src/airdrop/contracts/evm/AirdropService.ts +0 -181
  18. package/src/airdrop/contracts/evm/SignatureAirdropService.ts +0 -114
  19. package/src/airdrop/contracts/evm/contracts/AirdropSignatureClient.ts +0 -63
  20. package/src/airdrop/contracts/evm/contracts/AirdropSignatureFeeClient.ts +0 -61
  21. package/src/airdrop/contracts/evm/contracts/AirdropV2Client.ts +0 -37
  22. package/src/airdrop/contracts/evm/contracts/AirdropV3Client.ts +0 -49
  23. package/src/airdrop/contracts/evm/contracts/AirdropV3FeeClient.ts +0 -16
  24. package/src/airdrop/contracts/evm/contracts/AirdropV4Client.ts +0 -36
  25. package/src/airdrop/contracts/evm/contracts/ContractBaseClient.ts +0 -85
  26. package/src/airdrop/contracts/evm/contracts/DeployClient.ts +0 -35
  27. package/src/airdrop/contracts/evm/contracts/FeeCollectorClient.ts +0 -21
  28. package/src/airdrop/contracts/evm/contracts/KycClient.ts +0 -21
  29. package/src/airdrop/contracts/evm/contracts/abis/Airdrop.json +0 -674
  30. package/src/airdrop/contracts/evm/contracts/abis/AirdropV2.json +0 -676
  31. package/src/airdrop/contracts/evm/contracts/abis/AirdropV3.json +0 -740
  32. package/src/airdrop/contracts/evm/contracts/abis/AirdropV3_Fee.json +0 -678
  33. package/src/airdrop/contracts/evm/contracts/abis/CustomFeeNativeAirdropV4.json +0 -701
  34. package/src/airdrop/contracts/evm/contracts/abis/Deployer.json +0 -271
  35. package/src/airdrop/contracts/evm/contracts/abis/ERC721.json +0 -737
  36. package/src/airdrop/contracts/evm/contracts/abis/FeeCollector.json +0 -322
  37. package/src/airdrop/contracts/evm/contracts/abis/FungibleTokenECDSADistributor.json +0 -564
  38. package/src/airdrop/contracts/evm/contracts/abis/FungibleTokenWithFeesECDSADistributor.json +0 -260
  39. package/src/airdrop/contracts/evm/contracts/abis/KycHook.json +0 -312
  40. package/src/airdrop/contracts/index.ts +0 -4
  41. package/src/airdrop/contracts/solana/AirdropSignatureClient.ts +0 -242
  42. package/src/airdrop/contracts/solana/FeeCollectorClient.ts +0 -43
  43. package/src/airdrop/contracts/solana/SignatureAirdropService.ts +0 -51
  44. package/src/airdrop/contracts/solana/SolanaContractClient.ts +0 -126
  45. package/src/airdrop/contracts/solana/fee_collector.json +0 -1063
  46. package/src/airdrop/contracts/solana/fee_collector.ts +0 -1069
  47. package/src/airdrop/contracts/solana/fungible_token_distributor_solana.json +0 -1573
  48. package/src/airdrop/contracts/solana/fungible_token_distributor_solana.ts +0 -1579
  49. package/src/airdrop/contracts/ton/AirdropClientV1.ts +0 -122
  50. package/src/airdrop/contracts/ton/AirdropClientV2.ts +0 -118
  51. package/src/airdrop/contracts/ton/AirdropClientV21.ts +0 -131
  52. package/src/airdrop/contracts/ton/AirdropClientV3.ts +0 -127
  53. package/src/airdrop/contracts/ton/AirdropService.ts +0 -150
  54. package/src/airdrop/contracts/ton/TonContractBaseClient.ts +0 -236
  55. package/src/airdrop/contracts/ton/v1/build/Leaf.compiled.json +0 -1
  56. package/src/airdrop/contracts/ton/v1/build/MerkleTokenDistributor.compiled.json +0 -1
  57. package/src/airdrop/contracts/ton/v1/utils/constants.ts +0 -42
  58. package/src/airdrop/contracts/ton/v1/utils/date.utils.ts +0 -11
  59. package/src/airdrop/contracts/ton/v1/utils/index.ts +0 -3
  60. package/src/airdrop/contracts/ton/v1/utils/ton.utils.ts +0 -84
  61. package/src/airdrop/contracts/ton/v1/wrappers/JettonMinter.ts +0 -81
  62. package/src/airdrop/contracts/ton/v1/wrappers/JettonWallet.ts +0 -73
  63. package/src/airdrop/contracts/ton/v1/wrappers/Leaf.ts +0 -88
  64. package/src/airdrop/contracts/ton/v1/wrappers/MerkleTokenDistributor.ts +0 -317
  65. package/src/airdrop/contracts/ton/v1/wrappers/index.ts +0 -2
  66. package/src/airdrop/contracts/ton/v2/build/Leaf.compiled.json +0 -1
  67. package/src/airdrop/contracts/ton/v2/build/MerkleTokenDistributor.compiled.json +0 -1
  68. package/src/airdrop/contracts/ton/v2/utils/constants.ts +0 -43
  69. package/src/airdrop/contracts/ton/v2/utils/date.utils.ts +0 -11
  70. package/src/airdrop/contracts/ton/v2/utils/index.ts +0 -3
  71. package/src/airdrop/contracts/ton/v2/utils/ton.utils.ts +0 -102
  72. package/src/airdrop/contracts/ton/v2/wrappers/Leaf.ts +0 -69
  73. package/src/airdrop/contracts/ton/v2/wrappers/MerkleTokenDistributor.ts +0 -340
  74. package/src/airdrop/contracts/ton/v2/wrappers/index.ts +0 -2
  75. package/src/airdrop/contracts/ton/v21/build/Leaf.compiled.json +0 -1
  76. package/src/airdrop/contracts/ton/v21/build/MerkleTokenDistributor.compiled.json +0 -1
  77. package/src/airdrop/contracts/ton/v21/utils/constants.ts +0 -48
  78. package/src/airdrop/contracts/ton/v21/utils/date.utils.ts +0 -11
  79. package/src/airdrop/contracts/ton/v21/utils/index.ts +0 -3
  80. package/src/airdrop/contracts/ton/v21/utils/ton.utils.ts +0 -84
  81. package/src/airdrop/contracts/ton/v21/wrappers/Leaf.ts +0 -69
  82. package/src/airdrop/contracts/ton/v21/wrappers/MerkleTokenDistributor.ts +0 -346
  83. package/src/airdrop/contracts/ton/v21/wrappers/index.ts +0 -2
  84. package/src/airdrop/contracts/ton/v3/build/Leaf.compiled.json +0 -1
  85. package/src/airdrop/contracts/ton/v3/build/MerkleTokenDistributor.compiled.json +0 -1
  86. package/src/airdrop/contracts/ton/v3/utils/constants.ts +0 -43
  87. package/src/airdrop/contracts/ton/v3/utils/date.utils.ts +0 -11
  88. package/src/airdrop/contracts/ton/v3/utils/index.ts +0 -3
  89. package/src/airdrop/contracts/ton/v3/utils/ton.utils.ts +0 -84
  90. package/src/airdrop/contracts/ton/v3/wrappers/Leaf.ts +0 -68
  91. package/src/airdrop/contracts/ton/v3/wrappers/MerkleTokenDistributor.ts +0 -316
  92. package/src/airdrop/contracts/ton/v3/wrappers/index.ts +0 -2
  93. package/src/airdrop/index.ts +0 -10
  94. package/src/airdrop/services/index.ts +0 -166
  95. package/src/airdrop/services/wallet-service/ton/WalletService.ts +0 -226
  96. package/src/airdrop/services/wallet-service/ton/index.ts +0 -1
  97. package/src/airdrop/strategies/AirdropStrategyFactory.ts +0 -31
  98. package/src/airdrop/strategies/EvmAirdropStrategy.ts +0 -81
  99. package/src/airdrop/strategies/IAirdropStrategy.ts +0 -26
  100. package/src/airdrop/strategies/StrategyCache.ts +0 -34
  101. package/src/airdrop/strategies/TonAirdropStrategy.ts +0 -77
  102. package/src/airdrop/tests/index.test.ts +0 -387
  103. package/src/airdrop/types/index.ts +0 -200
  104. package/src/index.ts +0 -3
  105. package/src/utils/api-client.ts +0 -69
  106. package/src/utils/index.ts +0 -3
  107. package/src/utils/utils.ts +0 -45
  108. package/src/utils/web3.ts +0 -172
  109. package/src/wallets/WalletBase.ts +0 -92
  110. package/src/wallets/WalletFactory.tsx +0 -35
  111. package/src/wallets/index.ts +0 -3
  112. package/src/wallets/providers/evm/EvmWallet.ts +0 -101
  113. package/src/wallets/providers/evm/index.tsx +0 -56
  114. package/src/wallets/providers/index.ts +0 -4
  115. package/src/wallets/providers/solana/index.tsx +0 -133
  116. package/src/wallets/providers/ton/TonWallet.ts +0 -153
  117. package/src/wallets/providers/ton/index.tsx +0 -41
  118. package/src/wallets/types.ts +0 -45
  119. package/src/wallets/utils/index.ts +0 -61
package/src/utils/web3.ts DELETED
@@ -1,172 +0,0 @@
1
- import TonWeb from 'tonweb';
2
- import {
3
- ASSOCIATED_TOKEN_PROGRAM_ID,
4
- getAssociatedTokenAddress,
5
- getAccount,
6
- createAssociatedTokenAccountInstruction,
7
- TokenAccountNotFoundError,
8
- TokenInvalidAccountOwnerError,
9
- TokenInvalidMintError,
10
- TokenInvalidOwnerError,
11
- TOKEN_PROGRAM_ID,
12
- TOKEN_2022_PROGRAM_ID
13
- } from '@solana/spl-token';
14
- import {
15
- Cluster,
16
- clusterApiUrl,
17
- Commitment,
18
- Connection,
19
- ParsedAccountData,
20
- PublicKey,
21
- Transaction
22
- } from '@solana/web3.js';
23
-
24
- export function flipAddressType(address: string) {
25
- if (!address) return '';
26
- return address.includes(':')
27
- ? new TonWeb.utils.Address(address).toString(true, true, true)
28
- : new TonWeb.utils.Address(address).toString(false, false, true);
29
- }
30
-
31
- export function hexStrToU8Array(hexStr: string): number[] {
32
- // 移除 "0x" 前缀(如果存在)
33
- hexStr = hexStr.startsWith('0x') ? hexStr.slice(2) : hexStr;
34
-
35
- // 确保 hexStr 长度正确(64 个字符,即 32 字节)
36
- // if (hexStr.length !== 64) {
37
- // throw new Error('Invalid hex string length. Expected 64 characters (32 bytes).');
38
- // }
39
-
40
- // 将 16 进制字符串转换为 Uint8Array
41
- const bytes = [];
42
- for (let i = 0; i < hexStr.length / 2; i++) {
43
- bytes[i] = parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
44
- }
45
-
46
- return bytes;
47
- }
48
-
49
- export async function getOrCreateAssociatedTokenAccount({
50
- connection,
51
- payer,
52
- mint,
53
- owner,
54
- signTransaction,
55
- allowOwnerOffCurve = false,
56
- commitment,
57
- programId = TOKEN_PROGRAM_ID,
58
- associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID
59
- }: {
60
- connection: Connection;
61
- payer: PublicKey;
62
- mint: PublicKey;
63
- owner: PublicKey;
64
- allowOwnerOffCurve?: boolean;
65
- commitment?: Commitment;
66
- programId?: PublicKey;
67
- associatedTokenProgramId?: PublicKey;
68
- signTransaction: (signature: Transaction) => Promise<Transaction>;
69
- }) {
70
- const associatedToken = await getAssociatedTokenAddress(
71
- mint,
72
- owner,
73
- allowOwnerOffCurve,
74
- programId,
75
- associatedTokenProgramId
76
- );
77
- // This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
78
- // Sadly we can't do this atomically.
79
- let account;
80
- try {
81
- account = await getAccount(connection, associatedToken, commitment, programId);
82
- } catch (error) {
83
- // TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
84
- // becoming a system account. Assuming program derived addressing is safe, this is the only case for the
85
- // TokenInvalidAccountOwnerError in this code path.
86
- if (error instanceof TokenAccountNotFoundError || error instanceof TokenInvalidAccountOwnerError) {
87
- // As this isn't atomic, it's possible others can create associated accounts meanwhile.
88
- try {
89
- const transaction = new Transaction().add(
90
- createAssociatedTokenAccountInstruction(
91
- payer,
92
- associatedToken,
93
- owner,
94
- mint,
95
- programId,
96
- associatedTokenProgramId
97
- )
98
- );
99
- const blockHash = await connection.getRecentBlockhash();
100
- transaction.feePayer = await payer;
101
- transaction.recentBlockhash = await blockHash.blockhash;
102
- const signed = await signTransaction(transaction);
103
-
104
- const signature = await connection.sendRawTransaction(signed.serialize());
105
-
106
- await connection.confirmTransaction(signature);
107
- } catch (error) {
108
- console.log(error);
109
- // Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
110
- // instruction error if the associated account exists already.
111
- }
112
- // Now this should always succeed
113
- account = await getAccount(connection, associatedToken, commitment, programId);
114
- } else {
115
- throw error;
116
- }
117
- }
118
- if (!account.mint.equals(mint)) throw new TokenInvalidMintError();
119
- if (!account.owner.equals(owner)) throw new TokenInvalidOwnerError();
120
- return account;
121
- }
122
-
123
- export enum SplTokenVersion {
124
- SplToken = 'spl-token',
125
- SplToken2022 = 'spl-token-2022'
126
- }
127
-
128
- export type FetchTokenVersionOptions = {
129
- clusterOrEndpoint?: Cluster | string;
130
- };
131
-
132
- export type FetchTokenMetadataOptions = FetchTokenVersionOptions & {
133
- fetchOffchainMetadata?: boolean;
134
- };
135
-
136
- async function _getTokenProgramAndParsedAccountData(conn: Connection, tokenAddress: string) {
137
- const tokenMintAddress = new PublicKey(tokenAddress);
138
- const parsedMintAccountInfo = await conn.getParsedAccountInfo(tokenMintAddress);
139
- const parsedMintAccountData = parsedMintAccountInfo.value?.data as ParsedAccountData;
140
- if (parsedMintAccountData?.program === SplTokenVersion.SplToken) {
141
- return {
142
- name: SplTokenVersion.SplToken,
143
- programId: TOKEN_PROGRAM_ID,
144
- parsedMintAccountData
145
- };
146
- } else if (parsedMintAccountData?.program === SplTokenVersion.SplToken2022) {
147
- return {
148
- name: SplTokenVersion.SplToken2022,
149
- programId: TOKEN_2022_PROGRAM_ID,
150
- parsedMintAccountData
151
- };
152
- }
153
- return null;
154
- }
155
-
156
- function _getConnection(clusterOrEndpoint: Cluster | string) {
157
- let endpoint: string;
158
- try {
159
- endpoint = clusterApiUrl((clusterOrEndpoint as Cluster) ?? 'mainnet-beta');
160
- } catch {
161
- endpoint = clusterOrEndpoint as string;
162
- }
163
-
164
- const conn = new Connection(endpoint);
165
- return conn;
166
- }
167
-
168
- export async function fetchTokenVersion(tokenAddress: string, options: FetchTokenMetadataOptions) {
169
- const conn = _getConnection(options.clusterOrEndpoint!);
170
- const data = await _getTokenProgramAndParsedAccountData(conn, tokenAddress);
171
- return data ? { name: data.name, programId: data.programId } : null;
172
- }
@@ -1,92 +0,0 @@
1
- import { ISignResult } from './types';
2
- import { prepareSignMessage } from './utils';
3
-
4
- export abstract class WalletBase<S> {
5
- type: string;
6
- provider: any;
7
- address?: string;
8
- publicKey?: string;
9
- chainId?: number | string;
10
- protocolTag?: string;
11
- authType: string;
12
- userAddress?: string;
13
- network?: string;
14
-
15
- constructor(type: string, userAddress?: string) {
16
- this.type = type;
17
- this.provider = null;
18
- this.address = '';
19
- this.chainId = 1;
20
- this.authType = '';
21
- this.userAddress = userAddress || '';
22
- // this.init();
23
- }
24
-
25
- // abstract init(): void;
26
-
27
- // abstract init(): void;
28
- protected get isConnected(): boolean {
29
- throw new Error('Not implemented');
30
- }
31
-
32
- abstract connect(_?: string): void;
33
-
34
- // common sign
35
- abstract sign(_: string, _1?: string): Promise<ISignResult>;
36
-
37
- // checkAddressValidity(): boolean {
38
- // console.log(this.userAddress, this.address);
39
- // if (!this.address) {
40
- // // 兼容ton的情况,ton只有sign之后才有地址
41
- // return true;
42
- // }
43
- // if (this.userAddress && this.address !== this.userAddress) {
44
- // console.error('Wallet address and user address do not match.');
45
- // globalVm.toggleAccountChangedModal(true);
46
- // return false;
47
- // }
48
- // return true;
49
- // }
50
-
51
- async safeSign(_: string, _1?: string): Promise<ISignResult> {
52
- // if (!this.checkAddressValidity()) {
53
- // throw new Error('Wallet address and user address do not match.');
54
- // }
55
- return this.sign(_, _1);
56
- }
57
-
58
- async signin(statement?: string): Promise<ISignResult> {
59
- const msgRes = prepareSignMessage({ statement, chainId: this.chainId!, address: this.address! });
60
- return this.sign(JSON.stringify(msgRes, null, ' '));
61
- }
62
-
63
- // for metamask sign of EIP-712
64
- signByEIP712(_: string): Promise<ISignResult> {
65
- throw new Error(`received message:${_},Method not implemented.`);
66
- }
67
-
68
- getSignEIP712Message(_: { chainId: number | string; info: string; hash: string }): string {
69
- throw new Error(`received message:${JSON.stringify(_)},Method not implemented.`);
70
- }
71
-
72
- getWallet(): {
73
- address?: string;
74
- isConnected: boolean;
75
- publicKey: string | undefined;
76
- type: string;
77
- network: string | undefined;
78
- } {
79
- // 获取钱包信息
80
- return {
81
- type: this.type,
82
- isConnected: this.isConnected,
83
- address: this.address,
84
- publicKey: this.publicKey,
85
- network: this.network
86
- };
87
- }
88
-
89
- abstract getStore(): S;
90
-
91
- // abstract decrypt(message: string, address: string): Promise<string>;
92
- }
@@ -1,35 +0,0 @@
1
- import { EvmWallet } from './providers/evm';
2
- import { ChainType } from './types';
3
- import { TonWallet } from './providers/ton';
4
- import { SolWallet } from './providers/solana';
5
-
6
- export abstract class WalletFactory {
7
- private static instances: Record<string, EvmWallet | TonWallet | SolWallet> = {};
8
- static getWallet(type: ChainType, userAddress?: string): EvmWallet | TonWallet | SolWallet {
9
- if (!this.instances[type]) {
10
- let WalletClass;
11
- switch (type) {
12
- case ChainType.Evm:
13
- WalletClass = EvmWallet;
14
- break;
15
- case ChainType.Ton:
16
- WalletClass = TonWallet;
17
- break;
18
- // case ChainType.Starknet:
19
- // WalletClass = StarknetWallet;
20
- // break;
21
- case ChainType.Solana:
22
- WalletClass = SolWallet;
23
- break;
24
- default:
25
- throw new Error('Unsupported wallet type');
26
- }
27
-
28
- if (!WalletClass) {
29
- throw new Error('Unsupported wallet type');
30
- }
31
- this.instances[type] = new WalletClass(type, userAddress);
32
- }
33
- return this.instances[type];
34
- }
35
- }
@@ -1,3 +0,0 @@
1
- export { WalletFactory } from './WalletFactory';
2
-
3
- export * from './providers';
@@ -1,101 +0,0 @@
1
- import { useConnectModal } from '@rainbow-me/rainbowkit';
2
- import { ResolvedRegister, useAccount, useWalletClient } from 'wagmi';
3
- import { WalletBase } from '../../WalletBase';
4
- import { disconnect, getAccount, signMessage } from 'wagmi/actions';
5
- import { EventEmitter } from 'events';
6
- import { ISignResult } from '../../types';
7
- import { get4361Message, prepareSignMessage } from '../../utils';
8
-
9
- export const eventBus = new EventEmitter();
10
-
11
- const EVM_EVENT_KEY = 'evm_signin';
12
-
13
- export interface EVMStoreType {
14
- account: ReturnType<typeof useAccount> | null;
15
- connectModal: ReturnType<typeof useConnectModal> | undefined;
16
- walletClient: ReturnType<typeof useWalletClient> | undefined;
17
- config: ResolvedRegister['config'] | undefined;
18
- counter: number;
19
- }
20
-
21
- export const evmStore: EVMStoreType = {
22
- account: null,
23
- connectModal: undefined,
24
- walletClient: undefined,
25
- config: undefined,
26
- counter: 0
27
- };
28
-
29
- export const getEventKey = () => {
30
- return `${EVM_EVENT_KEY}_${evmStore.counter}`;
31
- };
32
-
33
- export class EvmWallet extends WalletBase<EVMStoreType> {
34
- get isConnected() {
35
- return !!(evmStore.account?.address && evmStore.account?.isConnected);
36
- }
37
-
38
- connect() {
39
- evmStore?.connectModal?.openConnectModal?.();
40
- }
41
-
42
- async disconnect() {
43
- await disconnect(evmStore.config!);
44
- }
45
-
46
- async sign(_message: string) {
47
- try {
48
- const sign = await signMessage(evmStore.config!, {
49
- message: _message
50
- });
51
- return {
52
- message: _message,
53
- signature: sign
54
- };
55
- } catch (error) {
56
- throw error;
57
- }
58
- }
59
-
60
- override async signin(statement: string, prepare = true): Promise<ISignResult> {
61
- return new Promise((resolve, reject) => {
62
- const signCallback = async (data: ReturnType<typeof useAccount>) => {
63
- try {
64
- this.address = data.address;
65
- this.chainId = data.chainId;
66
- let fullMessage = statement;
67
- if (prepare) {
68
- const msg = prepareSignMessage({ statement, chainId: data.chainId!, address: data.address! });
69
- fullMessage = get4361Message(msg);
70
- }
71
-
72
- const res = await this.sign(fullMessage);
73
-
74
- resolve(res);
75
- } catch (error) {
76
- reject(error);
77
- }
78
- };
79
-
80
- const account = getAccount(evmStore.config!);
81
- this.provider = evmStore.walletClient;
82
-
83
- if (this.isConnected) {
84
- signCallback(account);
85
- return;
86
- }
87
-
88
- evmStore.counter++;
89
- this.connect();
90
- const eventKey = getEventKey();
91
- eventBus.once(eventKey, (data) => {
92
- console.log('listen event key success, event key = %s, data = %j', eventKey, data);
93
- signCallback(data);
94
- });
95
- });
96
- }
97
-
98
- getStore(): EVMStoreType {
99
- return evmStore;
100
- }
101
- }
@@ -1,56 +0,0 @@
1
- import '@rainbow-me/rainbowkit/styles.css';
2
- import { RainbowKitProvider, darkTheme, useConnectModal } from '@rainbow-me/rainbowkit';
3
- import { ResolvedRegister, WagmiProvider, useAccount, useWalletClient } from 'wagmi';
4
- import { ReactNode, useEffect } from 'react';
5
-
6
- import { evmStore, getEventKey, eventBus } from './EvmWallet';
7
-
8
- export { EvmWallet } from './EvmWallet';
9
-
10
- export const EVMConnector = () => {
11
- const connectModal = useConnectModal();
12
- const walletClient = useWalletClient();
13
- const account = useAccount();
14
-
15
- evmStore.connectModal = connectModal;
16
- evmStore.walletClient = walletClient;
17
- evmStore.account = account;
18
-
19
- useEffect(() => {
20
- if (account.isConnected) {
21
- const eventKey = getEventKey();
22
- eventBus.emit(eventKey, account);
23
- }
24
- }, [account]);
25
-
26
- return null;
27
- };
28
-
29
- export const EvmProvider = ({
30
- children,
31
- config,
32
- theme
33
- }: {
34
- children: ReactNode;
35
- config: ResolvedRegister['config'];
36
- theme?: any;
37
- }) => {
38
- evmStore.config = config;
39
- return (
40
- <WagmiProvider config={config}>
41
- <RainbowKitProvider
42
- locale="en-US"
43
- theme={
44
- theme ||
45
- darkTheme({
46
- accentColor: 'var(--t-primary)',
47
- accentColorForeground: 'var(--t-button-primary-foreground)'
48
- })
49
- }
50
- >
51
- {children}
52
- <EVMConnector />
53
- </RainbowKitProvider>
54
- </WagmiProvider>
55
- );
56
- };
@@ -1,4 +0,0 @@
1
- export { EvmWallet, EvmProvider } from './evm';
2
- export { SolWallet, SolanaProvider } from './solana';
3
- // export { StarknetWallet } from './starknet';
4
- export { TonWallet, TonProvider } from './ton';
@@ -1,133 +0,0 @@
1
- import { ReactNode, useCallback, useEffect, useMemo } from 'react';
2
- import { WalletBase } from '../../WalletBase';
3
- import { useWalletModal, WalletModalProvider } from '@solana/wallet-adapter-react-ui';
4
- import '@solana/wallet-adapter-react-ui/styles.css';
5
- import { ISignResult } from '../../types';
6
- import { ConnectionProvider, useWallet, WalletProvider } from '@solana/wallet-adapter-react';
7
- import { PhantomWalletAdapter, TrustWalletAdapter } from '@solana/wallet-adapter-wallets';
8
- import { Cluster, clusterApiUrl } from '@solana/web3.js';
9
- import bs58 from 'bs58';
10
- import { get4361Message, prepareSignMessage } from '../../utils';
11
- import EventEmitter from 'events';
12
-
13
- interface SolStoreType {
14
- connectModal: ReturnType<typeof useWalletModal> | null;
15
- account: ReturnType<typeof useWallet> | null;
16
- counter: number;
17
- }
18
-
19
- const solStore: SolStoreType = {
20
- connectModal: null,
21
- account: null,
22
- counter: 0
23
- };
24
-
25
- export const eventBus = new EventEmitter();
26
-
27
- const SOL_EVENT_KEY = 'sol_signin';
28
-
29
- export const getEventKey = () => {
30
- return `${SOL_EVENT_KEY}_${solStore.counter}`;
31
- };
32
-
33
- export class SolWallet extends WalletBase<SolStoreType> {
34
- getStore(): SolStoreType {
35
- return solStore;
36
- }
37
- get isConnected() {
38
- return !!solStore.account?.connected;
39
- }
40
- connect(): void {
41
- solStore.connectModal?.setVisible(true);
42
- }
43
-
44
- disconnect() {
45
- solStore.account?.disconnect();
46
- }
47
-
48
- async sign(message: string): Promise<ISignResult> {
49
- try {
50
- const provider = solStore.account!;
51
- const encodedMessage = new TextEncoder().encode(message);
52
- const signedMessage = await provider.signMessage?.(encodedMessage);
53
-
54
- return {
55
- message,
56
- signature: bs58.encode(signedMessage!)
57
- };
58
- } catch (error) {
59
- console.error(error);
60
- throw error;
61
- }
62
- }
63
-
64
- signin(statement: string, prepare = true): Promise<ISignResult> {
65
- return new Promise((resolve) => {
66
- const signCallback = (data: ReturnType<typeof useWallet>) => {
67
- this.publicKey = data.publicKey?.toString();
68
- this.address = this.publicKey;
69
- this.chainId = 1;
70
- let fullMessage = statement;
71
- if (prepare) {
72
- const msg = prepareSignMessage({ statement, chainId: '1', address: this.address! });
73
- fullMessage = get4361Message(msg);
74
- }
75
-
76
- const res = this.sign(fullMessage);
77
-
78
- resolve(res);
79
- };
80
-
81
- if (this.isConnected) {
82
- signCallback(solStore.account!);
83
- return;
84
- }
85
- solStore.counter++;
86
- this.connect();
87
- const eventKey = getEventKey();
88
- eventBus.once(eventKey, (data) => {
89
- console.log('listen event key success, event key = %s, data = %j', eventKey, data);
90
- signCallback(data);
91
- });
92
- });
93
- }
94
- }
95
-
96
- //Solana connection process: Open the modal first, then manually call wallet connect
97
- export const SolConnector = () => {
98
- const walletModal = useWalletModal();
99
- const wallet = useWallet();
100
-
101
- useEffect(() => {
102
- if (wallet.wallet) {
103
- if (wallet.connected) {
104
- const eventKey = getEventKey();
105
- eventBus.emit(eventKey, wallet);
106
- }
107
- }
108
- }, [wallet]);
109
-
110
- solStore.connectModal = walletModal;
111
- solStore.account = wallet;
112
- return null;
113
- };
114
-
115
- export const SolanaProvider = ({ children, cluster }: { children: ReactNode; cluster?: Cluster }) => {
116
- const endpoint = clusterApiUrl(cluster ? cluster : 'mainnet-beta');
117
-
118
- const wallets = useMemo(() => [new PhantomWalletAdapter(), new TrustWalletAdapter()], []);
119
-
120
- const onError = useCallback((error: any) => {
121
- console.error(error);
122
- }, []);
123
- return (
124
- <ConnectionProvider endpoint={endpoint}>
125
- <WalletProvider wallets={wallets} onError={onError} autoConnect>
126
- <WalletModalProvider>
127
- <SolConnector />
128
- {children}
129
- </WalletModalProvider>
130
- </WalletProvider>
131
- </ConnectionProvider>
132
- );
133
- };