@meshconnect/web-link-sdk 3.2.15 → 3.3.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.
Files changed (101) hide show
  1. package/Link.d.ts +2 -0
  2. package/Link.js +535 -0
  3. package/index.d.ts +3 -0
  4. package/index.js +3 -0
  5. package/package.json +9 -20
  6. package/utils/connectors/evm/chainConfigs.d.ts +2 -0
  7. package/utils/connectors/evm/chainConfigs.js +115 -0
  8. package/utils/connectors/evm/chainSwitching.d.ts +15 -0
  9. package/utils/connectors/evm/chainSwitching.js +242 -0
  10. package/utils/connectors/evm/index.d.ts +8 -0
  11. package/utils/connectors/evm/index.js +8 -0
  12. package/utils/connectors/evm/provider.d.ts +6 -0
  13. package/utils/connectors/evm/provider.js +13 -0
  14. package/utils/connectors/evm/signing.d.ts +1 -0
  15. package/utils/connectors/evm/signing.js +78 -0
  16. package/utils/connectors/evm/transactions.d.ts +28 -0
  17. package/utils/connectors/evm/transactions.js +381 -0
  18. package/utils/connectors/evm/types.d.ts +57 -0
  19. package/utils/connectors/evm/types.js +1 -0
  20. package/utils/connectors/evm/walletConnection.d.ts +20 -0
  21. package/utils/connectors/evm/walletConnection.js +160 -0
  22. package/utils/connectors/evm/walletDiscovery.d.ts +10 -0
  23. package/utils/connectors/evm/walletDiscovery.js +55 -0
  24. package/utils/connectors/solana/connection.d.ts +4 -0
  25. package/utils/connectors/solana/connection.js +108 -0
  26. package/utils/connectors/solana/index.d.ts +5 -0
  27. package/utils/connectors/solana/index.js +5 -0
  28. package/utils/connectors/solana/providerDiscovery.d.ts +3 -0
  29. package/utils/connectors/solana/providerDiscovery.js +127 -0
  30. package/utils/connectors/solana/signing.d.ts +1 -0
  31. package/utils/connectors/solana/signing.js +59 -0
  32. package/utils/connectors/solana/transaction.d.ts +17 -0
  33. package/utils/connectors/solana/transaction.js +362 -0
  34. package/utils/connectors/solana/types.d.ts +71 -0
  35. package/utils/connectors/solana/types.js +8 -0
  36. package/utils/event-types.d.ts +233 -0
  37. package/{src/utils/event-types.test.ts → utils/event-types.js} +5 -15
  38. package/utils/popup.d.ts +3 -0
  39. package/utils/popup.js +36 -0
  40. package/utils/sdk-specs.d.ts +5 -0
  41. package/utils/sdk-specs.js +6 -0
  42. package/utils/style.d.ts +3 -0
  43. package/utils/style.js +13 -0
  44. package/utils/types.d.ts +234 -0
  45. package/utils/types.js +1 -0
  46. package/utils/version.d.ts +1 -0
  47. package/utils/version.js +1 -0
  48. package/utils/wallet/EVMWalletStrategy.d.ts +31 -0
  49. package/utils/wallet/EVMWalletStrategy.js +265 -0
  50. package/utils/wallet/SolanaWalletStrategy.d.ts +33 -0
  51. package/utils/wallet/SolanaWalletStrategy.js +300 -0
  52. package/utils/wallet/WalletStrategy.d.ts +61 -0
  53. package/utils/wallet/WalletStrategy.js +25 -0
  54. package/utils/wallet/WalletStrategyFactory.d.ts +15 -0
  55. package/utils/wallet/WalletStrategyFactory.js +31 -0
  56. package/utils/wallet/index.d.ts +4 -0
  57. package/utils/wallet/index.js +4 -0
  58. package/utils/wallet-browser-event-types.d.ts +116 -0
  59. package/utils/wallet-browser-event-types.js +17 -0
  60. package/jest.setup.ts +0 -4
  61. package/src/Link.test.ts +0 -434
  62. package/src/Link.ts +0 -491
  63. package/src/index.ts +0 -3
  64. package/src/utils/__snapshots__/popup.test.ts.snap +0 -89
  65. package/src/utils/connectors/evm/chainConfigs.ts +0 -120
  66. package/src/utils/connectors/evm/chainSwitching.ts +0 -165
  67. package/src/utils/connectors/evm/index.ts +0 -8
  68. package/src/utils/connectors/evm/provider.ts +0 -22
  69. package/src/utils/connectors/evm/signing.ts +0 -39
  70. package/src/utils/connectors/evm/transactions.ts +0 -356
  71. package/src/utils/connectors/evm/types.ts +0 -63
  72. package/src/utils/connectors/evm/walletConnection.ts +0 -140
  73. package/src/utils/connectors/evm/walletDiscovery.ts +0 -67
  74. package/src/utils/connectors/solana/connection.ts +0 -69
  75. package/src/utils/connectors/solana/index.ts +0 -5
  76. package/src/utils/connectors/solana/providerDiscovery.ts +0 -153
  77. package/src/utils/connectors/solana/signing.ts +0 -18
  78. package/src/utils/connectors/solana/transaction.ts +0 -382
  79. package/src/utils/connectors/solana/types.ts +0 -66
  80. package/src/utils/event-types.ts +0 -350
  81. package/src/utils/popup.test.ts +0 -50
  82. package/src/utils/popup.ts +0 -123
  83. package/src/utils/sdk-specs.test.ts +0 -18
  84. package/src/utils/sdk-specs.ts +0 -7
  85. package/src/utils/style.test.ts +0 -33
  86. package/src/utils/style.ts +0 -15
  87. package/src/utils/types.ts +0 -270
  88. package/src/utils/version.ts +0 -1
  89. package/src/utils/wallet/EVMWalletStrategy.ts +0 -176
  90. package/src/utils/wallet/SolanaWalletStrategy.ts +0 -207
  91. package/src/utils/wallet/WalletStrategy.ts +0 -99
  92. package/src/utils/wallet/WalletStrategyFactory.ts +0 -46
  93. package/src/utils/wallet/__tests__/EVMWalletStrategy.test.ts +0 -233
  94. package/src/utils/wallet/__tests__/SolanaWalletStrategy.test.ts +0 -253
  95. package/src/utils/wallet/__tests__/WalletStrategy.test.ts +0 -77
  96. package/src/utils/wallet/__tests__/WalletStrategyFactory.test.ts +0 -65
  97. package/src/utils/wallet/index.ts +0 -4
  98. package/src/utils/wallet-browser-event-types.ts +0 -190
  99. package/tools/copy.js +0 -26
  100. package/tools/update-version.js +0 -10
  101. package/tsconfig.json +0 -14
@@ -1,382 +0,0 @@
1
- import {
2
- PublicKey,
3
- SystemProgram,
4
- TransactionMessage,
5
- VersionedTransaction,
6
- TransactionInstruction
7
- } from '@meshconnect/solana-web3.js'
8
- import { getSolanaProvider } from './providerDiscovery'
9
- import { TransactionConfig, SolanaProvider } from './types'
10
- import {
11
- SolanaAccountMeta,
12
- SolanaTransferWithInstructionsPayload,
13
- TransactionInstructionDto
14
- } from '../../../utils/types'
15
-
16
- const TOKEN_PROGRAM_ID = new PublicKey(
17
- 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
18
- )
19
- const TOKEN_2022_PROGRAM_ID = new PublicKey(
20
- 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb'
21
- )
22
- const ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey(
23
- 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'
24
- )
25
-
26
- const isUserRejection = (error: unknown): boolean => {
27
- if (!error || typeof error !== 'object') return false
28
- const err = error as Record<string, any>
29
- const message = (err.message || '').toLowerCase()
30
- return (
31
- message.includes('user rejected') ||
32
- message.includes('declined') ||
33
- message.includes('cancelled') ||
34
- message.includes('denied') ||
35
- err.code === 4001
36
- )
37
- }
38
-
39
- export async function getAssociatedTokenAddress(
40
- mint: PublicKey,
41
- owner: PublicKey,
42
- programId = TOKEN_PROGRAM_ID.toBase58()
43
- ): Promise<PublicKey> {
44
- const [address] = await PublicKey.findProgramAddress(
45
- [owner.toBuffer(), new PublicKey(programId).toBuffer(), mint.toBuffer()],
46
- ASSOCIATED_TOKEN_PROGRAM_ID
47
- )
48
- return address
49
- }
50
-
51
- function createTokenAccountInstruction(
52
- payer: PublicKey,
53
- associatedToken: PublicKey,
54
- owner: PublicKey,
55
- mint: PublicKey,
56
- programId = TOKEN_PROGRAM_ID
57
- ): TransactionInstruction {
58
- const keys = [
59
- { pubkey: payer, isSigner: true, isWritable: true },
60
- { pubkey: associatedToken, isSigner: false, isWritable: true },
61
- { pubkey: owner, isSigner: false, isWritable: false },
62
- { pubkey: mint, isSigner: false, isWritable: false },
63
- { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
64
- { pubkey: programId, isSigner: false, isWritable: false }
65
- ]
66
-
67
- return new TransactionInstruction({
68
- keys,
69
- programId: ASSOCIATED_TOKEN_PROGRAM_ID,
70
- data: Buffer.alloc(0)
71
- })
72
- }
73
-
74
- export function createTransferCheckedInstruction(
75
- fromTokenAccount: PublicKey,
76
- toTokenAccount: PublicKey,
77
- owner: PublicKey,
78
- amount: bigint,
79
- decimals = 9,
80
- tokenMint: PublicKey
81
- ): TransactionInstruction {
82
- const data = Buffer.alloc(10)
83
- data[0] = 12 // TransferChecked instruction enum
84
- data.writeBigUInt64LE(amount, 1) // 8-byte amount
85
- data[9] = decimals // single-byte decimal
86
-
87
- const programId = TOKEN_2022_PROGRAM_ID
88
-
89
- return new TransactionInstruction({
90
- keys: [
91
- { pubkey: fromTokenAccount, isSigner: false, isWritable: true },
92
- { pubkey: tokenMint, isSigner: false, isWritable: false },
93
- { pubkey: toTokenAccount, isSigner: false, isWritable: true },
94
- { pubkey: owner, isSigner: true, isWritable: false }
95
- ],
96
- programId,
97
- data
98
- })
99
- }
100
-
101
- export function createSPLTransferInstruction({
102
- fromTokenAccount,
103
- toTokenAccount,
104
- owner,
105
- amount
106
- }: {
107
- fromTokenAccount: PublicKey
108
- toTokenAccount: PublicKey
109
- owner: PublicKey
110
- amount: bigint
111
- }): TransactionInstruction {
112
- const data = Buffer.alloc(9)
113
- data[0] = 3 // Transfer instruction
114
- data.writeBigUInt64LE(amount, 1)
115
-
116
- return new TransactionInstruction({
117
- keys: [
118
- { pubkey: fromTokenAccount, isSigner: false, isWritable: true },
119
- { pubkey: toTokenAccount, isSigner: false, isWritable: true },
120
- { pubkey: owner, isSigner: true, isWritable: false }
121
- ],
122
- programId: TOKEN_PROGRAM_ID,
123
- data
124
- })
125
- }
126
-
127
- export async function createTransferTransaction(
128
- config: TransactionConfig
129
- ): Promise<VersionedTransaction> {
130
- const instructions = await createTransferInstructions(config)
131
-
132
- const fromPubkey = new PublicKey(config.fromAddress)
133
-
134
- const messageV0 = new TransactionMessage({
135
- payerKey: fromPubkey,
136
- recentBlockhash: config.blockhash,
137
- instructions
138
- }).compileToV0Message()
139
-
140
- return new VersionedTransaction(messageV0)
141
- }
142
-
143
- async function createTransferInstructions(config: TransactionConfig) {
144
- const fromPubkey = new PublicKey(config.fromAddress)
145
- const toPubkey = new PublicKey(config.toAddress)
146
- const instructions: TransactionInstruction[] = []
147
-
148
- if (!config.tokenMint) {
149
- instructions.push(
150
- SystemProgram.transfer({
151
- fromPubkey,
152
- toPubkey,
153
- lamports: Number(config.amount)
154
- })
155
- )
156
- } else {
157
- const tokenMintPubkey = new PublicKey(config.tokenMint)
158
-
159
- const fromTokenAccount = await getAssociatedTokenAddress(
160
- tokenMintPubkey,
161
- fromPubkey,
162
- config.tokenProgram
163
- )
164
-
165
- const toTokenAccount = await getAssociatedTokenAddress(
166
- tokenMintPubkey,
167
- toPubkey,
168
- config.tokenProgram
169
- )
170
-
171
- if (config.createATA) {
172
- instructions.push(
173
- createTokenAccountInstruction(
174
- fromPubkey,
175
- toTokenAccount,
176
- toPubkey,
177
- tokenMintPubkey,
178
- config.tokenProgram
179
- ? new PublicKey(config.tokenProgram)
180
- : TOKEN_PROGRAM_ID
181
- )
182
- )
183
- }
184
-
185
- if (config.tokenProgram === TOKEN_2022_PROGRAM_ID.toBase58()) {
186
- instructions.push(
187
- createTransferCheckedInstruction(
188
- fromTokenAccount,
189
- toTokenAccount,
190
- fromPubkey,
191
- BigInt(config.amount),
192
- config.tokenDecimals,
193
- tokenMintPubkey
194
- )
195
- )
196
- } else {
197
- instructions.push(
198
- createSPLTransferInstruction({
199
- fromTokenAccount,
200
- toTokenAccount,
201
- owner: fromPubkey,
202
- amount: BigInt(config.amount)
203
- })
204
- )
205
- }
206
- }
207
- return instructions
208
- }
209
-
210
- export async function handleManualSignAndSend(
211
- transaction: VersionedTransaction,
212
- provider: SolanaProvider
213
- ): Promise<string> {
214
- try {
215
- if (provider.signAndSendTransaction) {
216
- const { signature } = await provider.signAndSendTransaction(transaction)
217
- return signature
218
- } else {
219
- const signedTransaction = await provider.signTransaction(transaction)
220
- if (!provider.sendTransaction) {
221
- throw new Error('Provider does not support sendTransaction')
222
- }
223
- const signature = await provider.sendTransaction(signedTransaction)
224
- return signature
225
- }
226
- } catch (error: unknown) {
227
- console.error('Error in handleManualSignAndSend:', error)
228
- if (error instanceof Error && error.message?.includes('User rejected')) {
229
- throw new Error('Transaction was rejected by user')
230
- }
231
- throw error
232
- }
233
- }
234
-
235
- export async function getTransferInstructions(
236
- instructions: TransactionInstructionDto[]
237
- ): Promise<TransactionInstruction[]> {
238
- const result: TransactionInstruction[] = []
239
-
240
- for (let instrIndex = 0; instrIndex < instructions.length; instrIndex++) {
241
- const ix = instructions[instrIndex]
242
- const programId = new PublicKey(ix.programId)
243
-
244
- const keys = ix.accounts.map(
245
- (meta: SolanaAccountMeta, accountIndex: number) => {
246
- if (!meta.pubKey) {
247
- throw new Error(
248
- `Account at instruction ${instrIndex}, index ${accountIndex} has no pubKey and is not fillable`
249
- )
250
- }
251
-
252
- const resolvedPubkey: PublicKey = new PublicKey(meta.pubKey)
253
- return {
254
- pubkey: resolvedPubkey,
255
- isSigner: meta.isSigner,
256
- isWritable: meta.isWritable
257
- }
258
- }
259
- )
260
-
261
- result.push(
262
- new TransactionInstruction({
263
- keys,
264
- programId,
265
- data: Buffer.from(ix.data, 'base64')
266
- })
267
- )
268
- }
269
-
270
- return result
271
- }
272
-
273
- export const sendSOLTransaction = async (
274
- config: TransactionConfig
275
- ): Promise<string> => {
276
- try {
277
- const provider = getSolanaProvider(config.walletName)
278
- const transaction = await createTransferTransaction(config)
279
-
280
- const isManualWallet =
281
- (provider as any).isTrust ||
282
- (provider as any).isTrustWallet ||
283
- config.walletName.toLowerCase().includes('trust')
284
-
285
- if (isManualWallet) {
286
- return await handleManualSignAndSend(transaction, provider)
287
- }
288
-
289
- if (provider.signAndSendTransaction) {
290
- try {
291
- const { signature }: { signature: string } =
292
- await provider.signAndSendTransaction(transaction)
293
-
294
- // @TODO: validate that signature was a successful tx
295
- return signature
296
- } catch (error) {
297
- if (isUserRejection(error)) {
298
- throw new Error('Transaction was rejected by user')
299
- }
300
- return handleManualSignAndSend(transaction, provider)
301
- }
302
- }
303
-
304
- return handleManualSignAndSend(transaction, provider)
305
- } catch (error) {
306
- if (isUserRejection(error)) {
307
- throw new Error('Transaction was rejected by user')
308
- }
309
- throw error instanceof Error
310
- ? error
311
- : new Error(
312
- `Failed to send SOL transaction with ${config.walletName} wallet`
313
- )
314
- }
315
- }
316
-
317
- export const sendSOLTransactionWithInstructions = async (
318
- payload: SolanaTransferWithInstructionsPayload,
319
- transferConfig: TransactionConfig
320
- ): Promise<string> => {
321
- const walletName = payload.transactionInstructions.walletName || 'Phantom'
322
-
323
- try {
324
- const instructions = await getTransferInstructions(
325
- payload.transactionInstructions.instructions
326
- )
327
-
328
- const fromPubkey = new PublicKey(transferConfig.fromAddress)
329
-
330
- const transferInstructions = await createTransferInstructions(
331
- transferConfig
332
- )
333
-
334
- instructions.push(...transferInstructions)
335
-
336
- const transaction = new VersionedTransaction(
337
- new TransactionMessage({
338
- payerKey: fromPubkey,
339
- recentBlockhash: payload.transactionInstructions.blockhash,
340
- instructions: instructions
341
- }).compileToV0Message()
342
- )
343
-
344
- return await sendSolanaTransfer(walletName, transaction)
345
- } catch (error) {
346
- if (isUserRejection(error)) {
347
- throw new Error('Transaction was rejected by user')
348
- }
349
- throw error instanceof Error
350
- ? error
351
- : new Error(`Failed to send SOL transaction with ${walletName} wallet`)
352
- }
353
- }
354
-
355
- export const sendSolanaTransfer = async (
356
- walletName: string,
357
- transaction: VersionedTransaction
358
- ): Promise<string> => {
359
- const provider = getSolanaProvider(walletName)
360
- const isManualWallet =
361
- (provider as any).isTrust ||
362
- (provider as any).isTrustWallet ||
363
- walletName.includes('trust')
364
-
365
- if (isManualWallet) {
366
- return await handleManualSignAndSend(transaction, provider)
367
- }
368
-
369
- if (provider.signAndSendTransaction) {
370
- try {
371
- const { signature }: { signature: string } =
372
- await provider.signAndSendTransaction(transaction)
373
- return signature
374
- } catch (error) {
375
- if (isUserRejection(error)) {
376
- throw new Error('Transaction was rejected by user')
377
- }
378
- return handleManualSignAndSend(transaction, provider)
379
- }
380
- }
381
- return handleManualSignAndSend(transaction, provider)
382
- }
@@ -1,66 +0,0 @@
1
- import { Transaction, VersionedTransaction } from '@meshconnect/solana-web3.js'
2
-
3
- export enum SolanaWalletType {
4
- PHANTOM = 'phantom',
5
- SOLFLARE = 'solflare',
6
- TRUST = 'trustwallet',
7
- EXODUS = 'exodus',
8
- UNKNOWN = 'unknown'
9
- }
10
-
11
- export interface SolanaConnectResult {
12
- accounts: string[]
13
- chainId: string
14
- isConnected: boolean
15
- }
16
-
17
- export interface SolanaProvider {
18
- connect(options?: {
19
- onlyIfTrusted?: boolean
20
- }): Promise<{ publicKey: { toString(): string; toBase58(): string } }>
21
- disconnect(): Promise<void>
22
- walletType?: SolanaWalletType
23
- isConnected?: boolean
24
- publicKey?: { toString(): string; toBase58(): string }
25
- on(
26
- event: 'connect' | 'disconnect' | 'accountChanged',
27
- callback: (publicKey?: any) => void
28
- ): void
29
- signMessage(message: Uint8Array): Promise<{ signature: Uint8Array }>
30
- signTransaction(
31
- transaction: Transaction | VersionedTransaction
32
- ): Promise<Transaction | VersionedTransaction>
33
- signAndSendTransaction?(
34
- transaction: Transaction | VersionedTransaction
35
- ): Promise<{ signature: string }>
36
- sendTransaction?(
37
- transaction: Transaction | VersionedTransaction
38
- ): Promise<string>
39
- // Wallet checks
40
- isPhantom?: boolean
41
- isSolflare?: boolean
42
- isTrust?: boolean
43
- isTrustWallet?: boolean
44
- isExodus?: boolean
45
- }
46
-
47
- export interface WindowWithSolanaProviders extends Window {
48
- solana?: SolanaProvider
49
- phantom?: { solana?: SolanaProvider }
50
- exodus?: { solana?: SolanaProvider }
51
- trustwallet?: { solana?: SolanaProvider }
52
- solflare?: SolanaProvider
53
- [key: string]: { solana?: SolanaProvider } | SolanaProvider | undefined | any
54
- }
55
-
56
- export interface TransactionConfig {
57
- toAddress: string
58
- amount: bigint
59
- fromAddress: string
60
- blockhash: string
61
- walletName: string
62
- tokenMint?: string
63
- createATA?: boolean
64
- tokenProgram?: string
65
- tokenDecimals?: number
66
- }