@meshconnect/uwc-injected-connector 0.27.1 → 0.27.3

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,216 @@
1
+ // @vitest-environment node
2
+ // Regression guard for the flows that worked before lookup tables were applied
3
+ // and the size check was added: every transaction here must reach the wallet
4
+ // byte-for-byte as plain @solana/web3.js builds it.
5
+ import { describe, it, expect, vi } from 'vitest'
6
+ import {
7
+ Keypair,
8
+ PublicKey,
9
+ SystemProgram,
10
+ Transaction,
11
+ TransactionInstruction,
12
+ TransactionMessage,
13
+ VersionedTransaction
14
+ } from '@solana/web3.js'
15
+ import {
16
+ TOKEN_PROGRAM_ID,
17
+ createAssociatedTokenAccountIdempotentInstruction,
18
+ createTransferCheckedInstruction,
19
+ getAssociatedTokenAddress
20
+ } from '@solana/spl-token'
21
+ import type {
22
+ SolanaGenericTransferRequest,
23
+ SolanaNativeTransferRequest,
24
+ SolanaTokenTransferRequest
25
+ } from '@meshconnect/uwc-types'
26
+ import type { ExtendedStandardWalletAdapter } from '../../wallet-standard-discovery'
27
+ import { SolanaTransactionService } from './solana-transaction-service'
28
+ import { bridgedWallet, directWallet } from './__fixtures__/wallets'
29
+
30
+ const NETWORK = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
31
+ const PAYER = Keypair.generate().publicKey
32
+ const RECIPIENT = Keypair.generate().publicKey
33
+ const BLOCKHASH = Keypair.generate().publicKey.toBase58()
34
+ const USDC = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v')
35
+ const MEMO = new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr')
36
+
37
+ const legacyBytes = (instructions: TransactionInstruction[]): Uint8Array => {
38
+ const tx = new Transaction()
39
+ tx.instructions = instructions
40
+ tx.recentBlockhash = BLOCKHASH
41
+ tx.feePayer = PAYER
42
+ return tx.serialize({ requireAllSignatures: false, verifySignatures: false })
43
+ }
44
+
45
+ const v0Bytes = (instructions: TransactionInstruction[]): Uint8Array =>
46
+ new VersionedTransaction(
47
+ new TransactionMessage({
48
+ payerKey: PAYER,
49
+ recentBlockhash: BLOCKHASH,
50
+ instructions
51
+ }).compileToV0Message([])
52
+ ).serialize()
53
+
54
+ const service = new SolanaTransactionService({})
55
+
56
+ const nativeRequest: SolanaNativeTransferRequest = {
57
+ from: PAYER.toBase58(),
58
+ to: RECIPIENT.toBase58(),
59
+ amount: 1_000_000n,
60
+ blockhash: BLOCKHASH
61
+ }
62
+ const nativeInstruction = SystemProgram.transfer({
63
+ fromPubkey: PAYER,
64
+ toPubkey: RECIPIENT,
65
+ lamports: 1_000_000n
66
+ })
67
+
68
+ describe('existing Solana flows are unchanged', () => {
69
+ describe('native SOL transfer', () => {
70
+ it('legacy (default): the wallet gets the same bytes as before', async () => {
71
+ const { adapter, sent } = bridgedWallet(PAYER.toBase58())
72
+
73
+ await service.sendTransaction(nativeRequest, adapter, NETWORK)
74
+
75
+ expect(sent[0]).toEqual(legacyBytes([nativeInstruction]))
76
+ })
77
+
78
+ it('V0 (opted in, wallet supports it): the wallet gets the same bytes as before', async () => {
79
+ const { adapter, sent } = bridgedWallet(PAYER.toBase58())
80
+
81
+ await service.sendTransaction(
82
+ { ...nativeRequest, preferVersionedTransaction: true },
83
+ adapter,
84
+ NETWORK
85
+ )
86
+
87
+ expect(sent[0]).toEqual(v0Bytes([nativeInstruction]))
88
+ })
89
+
90
+ it('direct wallet: still handed the transaction object', async () => {
91
+ const { adapter, sendTransaction } = directWallet(PAYER.toBase58())
92
+
93
+ await service.sendTransaction(nativeRequest, adapter, NETWORK)
94
+
95
+ expect(sendTransaction.mock.calls[0]![0]).toBeInstanceOf(Transaction)
96
+ })
97
+ })
98
+
99
+ describe('SPL token transfer', () => {
100
+ it('legacy: the wallet gets the same bytes as before', async () => {
101
+ const request: SolanaTokenTransferRequest = {
102
+ from: PAYER.toBase58(),
103
+ to: RECIPIENT.toBase58(),
104
+ amount: 1_000_000n,
105
+ blockhash: BLOCKHASH,
106
+ tokenMint: USDC.toBase58(),
107
+ tokenDecimals: 6
108
+ } as SolanaTokenTransferRequest
109
+ const { adapter, sent } = bridgedWallet(PAYER.toBase58())
110
+
111
+ await service.sendTransaction(request, adapter, NETWORK)
112
+
113
+ const source = await getAssociatedTokenAddress(USDC, PAYER)
114
+ const destination = await getAssociatedTokenAddress(USDC, RECIPIENT)
115
+ const expected = legacyBytes([
116
+ createAssociatedTokenAccountIdempotentInstruction(
117
+ PAYER,
118
+ destination,
119
+ RECIPIENT,
120
+ USDC,
121
+ TOKEN_PROGRAM_ID
122
+ ),
123
+ createTransferCheckedInstruction(
124
+ source,
125
+ USDC,
126
+ destination,
127
+ PAYER,
128
+ 1_000_000n,
129
+ 6,
130
+ [],
131
+ TOKEN_PROGRAM_ID
132
+ )
133
+ ])
134
+ expect(sent[0]).toEqual(expected)
135
+ })
136
+ })
137
+
138
+ describe('generic instructions (swaps and bridges) that fit without tables', () => {
139
+ const memoInstruction = new TransactionInstruction({
140
+ programId: MEMO,
141
+ keys: [{ pubkey: PAYER, isSigner: true, isWritable: true }],
142
+ data: Buffer.from('relay-request-id')
143
+ })
144
+ const genericRequest = (
145
+ over: Partial<SolanaGenericTransferRequest> = {}
146
+ ): SolanaGenericTransferRequest => ({
147
+ feePayer: PAYER.toBase58(),
148
+ blockhash: BLOCKHASH,
149
+ instructions: [
150
+ {
151
+ programId: MEMO.toBase58(),
152
+ accounts: [
153
+ {
154
+ shouldFillPubkey: false,
155
+ pubKey: PAYER.toBase58(),
156
+ isSigner: true,
157
+ isWritable: true
158
+ }
159
+ ],
160
+ data: Buffer.from('relay-request-id').toString('base64')
161
+ }
162
+ ],
163
+ ...over
164
+ })
165
+
166
+ it.each([
167
+ ['no states (what Link sends today)', genericRequest()],
168
+ ['an empty states list', genericRequest({ states: [] })]
169
+ ])('%s: the wallet gets the same bytes as before', async (_, request) => {
170
+ const { adapter, sent } = bridgedWallet(PAYER.toBase58())
171
+
172
+ await service.sendTransaction(request, adapter, NETWORK)
173
+
174
+ expect(sent[0]).toEqual(v0Bytes([memoInstruction]))
175
+ })
176
+
177
+ it('direct wallet: still handed a V0 transaction with every account inline', async () => {
178
+ const { adapter, sendTransaction } = directWallet(PAYER.toBase58())
179
+
180
+ await service.sendTransaction(genericRequest(), adapter, NETWORK)
181
+
182
+ const tx = sendTransaction.mock.calls[0]![0] as VersionedTransaction
183
+ expect(tx).toBeInstanceOf(VersionedTransaction)
184
+ expect(tx.message.addressTableLookups).toEqual([])
185
+ expect(tx.serialize()).toEqual(v0Bytes([memoInstruction]))
186
+ })
187
+
188
+ it('the existing checks still run first: a fee payer that is not the wallet is refused', async () => {
189
+ const { adapter } = bridgedWallet(PAYER.toBase58())
190
+
191
+ await expect(
192
+ service.sendTransaction(
193
+ genericRequest({ feePayer: RECIPIENT.toBase58() }),
194
+ adapter,
195
+ NETWORK
196
+ )
197
+ ).rejects.toThrow('From address does not match connected wallet')
198
+ expect(adapter.sendSerializedTransaction).not.toHaveBeenCalled()
199
+ })
200
+
201
+ it('a wallet rejection is still reported as a rejection', async () => {
202
+ const adapter = {
203
+ publicKey: { toBase58: vi.fn().mockResolvedValue(PAYER.toBase58()) },
204
+ sendSerializedTransaction: vi.fn().mockRejectedValue(
205
+ Object.assign(new Error('User rejected the request.'), {
206
+ code: 4001
207
+ })
208
+ )
209
+ } as unknown as ExtendedStandardWalletAdapter
210
+
211
+ await expect(
212
+ service.sendTransaction(genericRequest(), adapter, NETWORK)
213
+ ).rejects.toMatchObject({ type: 'rejected' })
214
+ })
215
+ })
216
+ })
@@ -0,0 +1,232 @@
1
+ // @vitest-environment node
2
+ // Replays the real Relay quotes that failed in production (ONC-2129) through
3
+ // the real send path (service → builder → @solana/web3.js, nothing mocked but
4
+ // the wallet) and checks what the wallet would be asked to sign.
5
+ import { describe, it, expect } from 'vitest'
6
+ import {
7
+ AddressLookupTableAccount,
8
+ PublicKey,
9
+ TransactionMessage,
10
+ VersionedTransaction
11
+ } from '@solana/web3.js'
12
+ import type {
13
+ SolanaAddressLookupTable,
14
+ SolanaGenericTransferRequest,
15
+ SolanaTransactionInstruction
16
+ } from '@meshconnect/uwc-types'
17
+ import {
18
+ SOLANA_TRANSACTION_SIZE_LIMIT,
19
+ WalletConnectorError
20
+ } from '@meshconnect/uwc-types'
21
+ import { SolanaTransactionService } from './solana-transaction-service'
22
+ import quotes from './__fixtures__/relay-solana-quotes.json'
23
+ import { bridgedWallet, directWallet } from './__fixtures__/wallets'
24
+
25
+ // Real Relay quotes for multi-hop Jupiter swaps, logged by Mesh's backend on
26
+ // 2026-10-21, in the request shape Link sends UWC (the backend rewrites hex
27
+ // `data` to base64 and `keys[].pubkey` to `accounts[].pubKey`).
28
+ // `lookupTableKeys` are the quote's `addressLookupTableAddresses`. Without
29
+ // their tables each one failed in production in one of two ways:
30
+ // - cashToSolEncodingOverrun (36 accounts), eurcToSolEncodingOverrun (32): the
31
+ // message alone is over the limit, so web3.js throws `encoding overruns
32
+ // Uint8Array` before the wallet opens.
33
+ // - eurcToSolReachedWallet (31): serializes to 1,289 bytes, reached Trust
34
+ // Wallet, and the RPC rejected it ("VersionedTransaction too large: 1720
35
+ // bytes", the base64 length).
36
+ interface RelaySolanaQuote {
37
+ requestId: string
38
+ pair: string
39
+ feePayer: string
40
+ lookupTableKeys: string[]
41
+ instructions: SolanaTransactionInstruction[]
42
+ }
43
+ const RELAY_SOLANA_QUOTES: Record<keyof typeof quotes, RelaySolanaQuote> =
44
+ quotes
45
+
46
+ const NETWORK = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
47
+ const BLOCKHASH = '4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZAMdL4VZHirAn'
48
+ const ACTIVE = BigInt('18446744073709551615')
49
+
50
+ const QUOTES = Object.entries(RELAY_SOLANA_QUOTES)
51
+
52
+ // Relay's tables hold a route's pool and token accounts. Their on-chain
53
+ // contents are not in the quote, so spread the route's non-signer accounts
54
+ // across the quote's own table keys.
55
+ const tablesFor = (quote: RelaySolanaQuote): SolanaAddressLookupTable[] => {
56
+ const accounts = [
57
+ ...new Set(
58
+ quote.instructions.flatMap(ix =>
59
+ ix.accounts.filter(a => !a.isSigner).map(a => a.pubKey as string)
60
+ )
61
+ )
62
+ ]
63
+ return quote.lookupTableKeys.map((key, index) => ({
64
+ key,
65
+ addresses: accounts.filter(
66
+ (_, i) => i % quote.lookupTableKeys.length === index
67
+ ),
68
+ // As a string, the only JSON form that keeps u64::MAX exact.
69
+ deactivationSlot: ACTIVE.toString() as unknown as bigint,
70
+ lastExtendedSlot: 280_000_000,
71
+ lastExtendedStartIndex: 0
72
+ }))
73
+ }
74
+
75
+ const requestFor = (
76
+ quote: RelaySolanaQuote,
77
+ states?: SolanaAddressLookupTable[]
78
+ ): SolanaGenericTransferRequest => ({
79
+ feePayer: quote.feePayer,
80
+ blockhash: BLOCKHASH,
81
+ instructions: quote.instructions,
82
+ ...(states && { states })
83
+ })
84
+
85
+ const service = new SolanaTransactionService({})
86
+
87
+ describe('real Relay Solana swap quotes (ONC-2129)', () => {
88
+ describe('without lookup tables (what reached UWC in production)', () => {
89
+ it('the 31-account quote still serializes, to the 1,289 bytes the RPC rejected', () => {
90
+ const quote = RELAY_SOLANA_QUOTES.eurcToSolReachedWallet
91
+ const message = new TransactionMessage({
92
+ payerKey: new PublicKey(quote.feePayer),
93
+ recentBlockhash: BLOCKHASH,
94
+ instructions: quote.instructions.map(ix => ({
95
+ programId: new PublicKey(ix.programId),
96
+ keys: ix.accounts.map(a => ({
97
+ pubkey: new PublicKey(a.pubKey as string),
98
+ isSigner: a.isSigner,
99
+ isWritable: a.isWritable
100
+ })),
101
+ data: Buffer.from(ix.data, 'base64')
102
+ }))
103
+ }).compileToV0Message()
104
+ const bytes = new VersionedTransaction(message).serialize()
105
+
106
+ expect(bytes.length).toBe(1289)
107
+ // The RPC's "1720 bytes" is the base64 length of the same transaction.
108
+ expect(Buffer.from(bytes).toString('base64')).toHaveLength(1720)
109
+ })
110
+
111
+ it.each(QUOTES)(
112
+ '%s: refused with a clear error before any wallet is asked',
113
+ async (_, quote) => {
114
+ const bridged = bridgedWallet(quote.feePayer)
115
+ const direct = directWallet(quote.feePayer)
116
+
117
+ for (const adapter of [bridged.adapter, direct.adapter]) {
118
+ const sent = service.sendTransaction(
119
+ requestFor(quote),
120
+ adapter,
121
+ NETWORK
122
+ )
123
+ await expect(sent).rejects.toBeInstanceOf(WalletConnectorError)
124
+ await expect(sent).rejects.toThrow(
125
+ /Solana transaction is too large to send/
126
+ )
127
+ }
128
+ expect(bridged.adapter.sendSerializedTransaction).not.toHaveBeenCalled()
129
+ expect(direct.sendTransaction).not.toHaveBeenCalled()
130
+ }
131
+ )
132
+ })
133
+
134
+ describe('with the lookup tables', () => {
135
+ it.each(QUOTES)(
136
+ '%s: reaches a bridged wallet in one packet, through the tables',
137
+ async (_, quote) => {
138
+ const { adapter, sent } = bridgedWallet(quote.feePayer)
139
+
140
+ await expect(
141
+ service.sendTransaction(
142
+ requestFor(quote, tablesFor(quote)),
143
+ adapter,
144
+ NETWORK
145
+ )
146
+ ).resolves.toBe('sig-bridge')
147
+
148
+ expect(sent).toHaveLength(1)
149
+ expect(sent[0]!.length).toBeLessThanOrEqual(
150
+ SOLANA_TRANSACTION_SIZE_LIMIT
151
+ )
152
+ const { message } = VersionedTransaction.deserialize(sent[0]!)
153
+ expect(message.version).toBe(0)
154
+ expect(message.staticAccountKeys[0]!.toBase58()).toBe(quote.feePayer)
155
+ expect(
156
+ message.addressTableLookups.map(l => l.accountKey.toBase58())
157
+ ).toEqual(quote.lookupTableKeys)
158
+ }
159
+ )
160
+
161
+ it.each(QUOTES)(
162
+ '%s: reaches a direct wallet as the same transaction',
163
+ async (_, quote) => {
164
+ const { adapter, sendTransaction } = directWallet(quote.feePayer)
165
+
166
+ await service.sendTransaction(
167
+ requestFor(quote, tablesFor(quote)),
168
+ adapter,
169
+ NETWORK
170
+ )
171
+
172
+ const tx = sendTransaction.mock.calls[0]![0] as VersionedTransaction
173
+ expect(tx).toBeInstanceOf(VersionedTransaction)
174
+ expect(tx.serialize().length).toBeLessThanOrEqual(
175
+ SOLANA_TRANSACTION_SIZE_LIMIT
176
+ )
177
+ expect(tx.message.addressTableLookups).toHaveLength(
178
+ quote.lookupTableKeys.length
179
+ )
180
+ }
181
+ )
182
+
183
+ // Moving accounts into tables must not change what the swap does.
184
+ it.each(QUOTES)(
185
+ '%s: decompiles back to exactly the quote’s instructions',
186
+ async (_, quote) => {
187
+ const { adapter, sent } = bridgedWallet(quote.feePayer)
188
+ const states = tablesFor(quote)
189
+
190
+ await service.sendTransaction(
191
+ requestFor(quote, states),
192
+ adapter,
193
+ NETWORK
194
+ )
195
+
196
+ const restored = TransactionMessage.decompile(
197
+ VersionedTransaction.deserialize(sent[0]!).message,
198
+ {
199
+ addressLookupTableAccounts: states.map(
200
+ state =>
201
+ new AddressLookupTableAccount({
202
+ key: new PublicKey(state.key),
203
+ state: {
204
+ deactivationSlot: ACTIVE,
205
+ lastExtendedSlot: state.lastExtendedSlot,
206
+ lastExtendedSlotStartIndex: state.lastExtendedStartIndex,
207
+ addresses: state.addresses.map(a => new PublicKey(a))
208
+ }
209
+ })
210
+ )
211
+ }
212
+ )
213
+
214
+ expect(restored.payerKey.toBase58()).toBe(quote.feePayer)
215
+ expect(restored.recentBlockhash).toBe(BLOCKHASH)
216
+ expect(
217
+ restored.instructions.map(ix => ({
218
+ programId: ix.programId.toBase58(),
219
+ data: Buffer.from(ix.data).toString('base64'),
220
+ keys: ix.keys.map(k => k.pubkey.toBase58())
221
+ }))
222
+ ).toEqual(
223
+ quote.instructions.map(ix => ({
224
+ programId: ix.programId,
225
+ data: ix.data,
226
+ keys: ix.accounts.map(a => a.pubKey)
227
+ }))
228
+ )
229
+ }
230
+ )
231
+ })
232
+ })
@@ -14,6 +14,7 @@ import type {
14
14
  SolanaTransactionRequest
15
15
  } from '@meshconnect/uwc-types'
16
16
  import { parseError } from '../../utils/error-utils'
17
+ import { serializeWithinSizeLimit } from '@meshconnect/uwc-types'
17
18
  import { SolanaTransactionBuilder } from './solana-transaction-builder'
18
19
  import type {
19
20
  ExtendedStandardWalletAdapter,
@@ -260,7 +261,7 @@ export class SolanaTransactionService {
260
261
  )
261
262
 
262
263
  const rpcUrl = this.getRpcUrl(networkId)
263
- const serializedTx = transaction.serialize()
264
+ const serializedTx = serializeWithinSizeLimit(transaction)
264
265
 
265
266
  return await this.executeTransaction(
266
267
  adapter,
@@ -0,0 +1,91 @@
1
+ // @vitest-environment node
2
+ // Real @solana/web3.js transactions against the shared size check: the sizes
3
+ // asserted here are the bytes a wallet receives.
4
+ import { describe, it, expect } from 'vitest'
5
+ import {
6
+ Keypair,
7
+ PACKET_DATA_SIZE,
8
+ PublicKey,
9
+ TransactionInstruction,
10
+ TransactionMessage,
11
+ VersionedTransaction
12
+ } from '@solana/web3.js'
13
+ import {
14
+ SOLANA_TRANSACTION_SIZE_LIMIT,
15
+ serializeWithinSizeLimit
16
+ } from '@meshconnect/uwc-types'
17
+
18
+ const PAYER = Keypair.generate().publicKey
19
+ const PROGRAM = new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr')
20
+ const BLOCKHASH = Keypair.generate().publicKey.toBase58()
21
+
22
+ // Above 127 bytes so the data's length prefix stays 2 bytes at every size
23
+ // below, which keeps one data byte equal to one transaction byte.
24
+ const BASE_DATA = 200
25
+
26
+ const transactionWithData = (dataLength: number): VersionedTransaction =>
27
+ new VersionedTransaction(
28
+ new TransactionMessage({
29
+ payerKey: PAYER,
30
+ recentBlockhash: BLOCKHASH,
31
+ instructions: [
32
+ new TransactionInstruction({
33
+ programId: PROGRAM,
34
+ keys: [],
35
+ data: Buffer.alloc(dataLength, 1)
36
+ })
37
+ ]
38
+ }).compileToV0Message()
39
+ )
40
+
41
+ const BASE_SIZE = transactionWithData(BASE_DATA).serialize().length
42
+ const transactionOfSize = (size: number): VersionedTransaction =>
43
+ transactionWithData(BASE_DATA + size - BASE_SIZE)
44
+
45
+ describe('serializeWithinSizeLimit', () => {
46
+ it('uses the same limit as web3.js', () => {
47
+ expect(SOLANA_TRANSACTION_SIZE_LIMIT).toBe(PACKET_DATA_SIZE)
48
+ })
49
+
50
+ it('returns the serialized bytes for a transaction exactly at the limit', () => {
51
+ const tx = transactionOfSize(1232)
52
+
53
+ const bytes = serializeWithinSizeLimit(tx)
54
+
55
+ expect(bytes.length).toBe(1232)
56
+ expect(bytes).toEqual(tx.serialize())
57
+ })
58
+
59
+ it('returns the serialized bytes for a small transaction', () => {
60
+ expect(serializeWithinSizeLimit(transactionOfSize(300)).length).toBe(300)
61
+ })
62
+
63
+ // web3.js serializes these without complaint, so without the check they
64
+ // reach the wallet and the RPC rejects them after the user approves.
65
+ it.each([1233, 1289])(
66
+ 'refuses a transaction that serializes to %i bytes',
67
+ size => {
68
+ const tx = transactionOfSize(size)
69
+ expect(tx.serialize().length).toBe(size)
70
+
71
+ expect(() => serializeWithinSizeLimit(tx)).toThrow(
72
+ `Solana transaction is too large to send: ${size} bytes, over the 1232-byte limit.`
73
+ )
74
+ }
75
+ )
76
+
77
+ it('names the fix (lookup tables) in the error', () => {
78
+ expect(() => serializeWithinSizeLimit(transactionOfSize(1233))).toThrow(
79
+ /address lookup tables passed in `states`/
80
+ )
81
+ })
82
+
83
+ it("turns web3.js's encoding overrun into the same clear error", () => {
84
+ const tx = transactionWithData(1300)
85
+ expect(() => tx.serialize()).toThrow(/encoding overruns/)
86
+
87
+ expect(() => serializeWithinSizeLimit(tx)).toThrow(
88
+ 'Solana transaction is too large to send: the message alone does not fit, over the 1232-byte limit.'
89
+ )
90
+ })
91
+ })