@meshconnect/uwc-types 0.33.0 → 0.33.1
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/solana-transaction.d.ts +40 -0
- package/dist/solana-transaction.d.ts.map +1 -0
- package/dist/solana-transaction.js +80 -0
- package/dist/solana-transaction.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/solana-transaction.test.ts +166 -0
- package/src/solana-transaction.ts +108 -0
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from './signature';
|
|
|
13
13
|
export * from './transactions';
|
|
14
14
|
export * from './errors';
|
|
15
15
|
export * from './solana-message';
|
|
16
|
+
export * from './solana-transaction';
|
|
16
17
|
export * from './observability';
|
|
17
18
|
export * from './bounded-dedup';
|
|
18
19
|
export * from './safe-error-message';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export * from './signature';
|
|
|
13
13
|
export * from './transactions';
|
|
14
14
|
export * from './errors';
|
|
15
15
|
export * from './solana-message';
|
|
16
|
+
export * from './solana-transaction';
|
|
16
17
|
export * from './observability';
|
|
17
18
|
export * from './bounded-dedup';
|
|
18
19
|
export * from './safe-error-message';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { SolanaAddressLookupTable } from './transactions';
|
|
2
|
+
/**
|
|
3
|
+
* Solana transaction helpers shared by the injected and WalletConnect
|
|
4
|
+
* connectors. No @solana/web3.js: callers pass in what needs it.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Solana's maximum serialized transaction size (web3.js `PACKET_DATA_SIZE`).
|
|
8
|
+
* Signature slots are already allocated in an unsigned transaction, so signing
|
|
9
|
+
* does not change its length.
|
|
10
|
+
*/
|
|
11
|
+
export declare const SOLANA_TRANSACTION_SIZE_LIMIT = 1232;
|
|
12
|
+
/**
|
|
13
|
+
* Serialize a transaction, refusing one the network would reject for its size
|
|
14
|
+
* before any wallet is asked to sign it. web3.js only throws (`encoding
|
|
15
|
+
* overruns Uint8Array`) when the message alone is over the limit; a transaction
|
|
16
|
+
* a few bytes over still serializes and would reach the wallet.
|
|
17
|
+
*/
|
|
18
|
+
export declare function serializeWithinSizeLimit(transaction: {
|
|
19
|
+
serialize(): Uint8Array;
|
|
20
|
+
}): Uint8Array;
|
|
21
|
+
/** A lookup table in web3.js's `AddressLookupTableAccount` constructor shape. */
|
|
22
|
+
export interface ParsedLookupTable<Key> {
|
|
23
|
+
key: Key;
|
|
24
|
+
state: {
|
|
25
|
+
deactivationSlot: bigint;
|
|
26
|
+
lastExtendedSlot: number;
|
|
27
|
+
lastExtendedSlotStartIndex: number;
|
|
28
|
+
addresses: Key[];
|
|
29
|
+
authority?: Key;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Rebuild the caller's address lookup tables, addresses included, so
|
|
34
|
+
* `compileToV0Message` can swap inlined 32-byte account keys for one-byte
|
|
35
|
+
* table indexes. Without the addresses every account is inlined and a large
|
|
36
|
+
* route (a multi-hop swap) goes over the size limit. Invalid data throws an
|
|
37
|
+
* error naming the table and field.
|
|
38
|
+
*/
|
|
39
|
+
export declare function parseLookupTables<Key>(states: SolanaAddressLookupTable[], toKey: (value: string) => Key): ParsedLookupTable<Key>[];
|
|
40
|
+
//# sourceMappingURL=solana-transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"solana-transaction.d.ts","sourceRoot":"","sources":["../src/solana-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AAE9D;;;GAGG;AAEH;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,OAAO,CAAA;AAQjD;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE;IACpD,SAAS,IAAI,UAAU,CAAA;CACxB,GAAG,UAAU,CAiBb;AAED,iFAAiF;AACjF,MAAM,WAAW,iBAAiB,CAAC,GAAG;IACpC,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE;QACL,gBAAgB,EAAE,MAAM,CAAA;QACxB,gBAAgB,EAAE,MAAM,CAAA;QACxB,0BAA0B,EAAE,MAAM,CAAA;QAClC,SAAS,EAAE,GAAG,EAAE,CAAA;QAChB,SAAS,CAAC,EAAE,GAAG,CAAA;KAChB,CAAA;CACF;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EACnC,MAAM,EAAE,wBAAwB,EAAE,EAClC,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,GAAG,GAC5B,iBAAiB,CAAC,GAAG,CAAC,EAAE,CA2B1B"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solana transaction helpers shared by the injected and WalletConnect
|
|
3
|
+
* connectors. No @solana/web3.js: callers pass in what needs it.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Solana's maximum serialized transaction size (web3.js `PACKET_DATA_SIZE`).
|
|
7
|
+
* Signature slots are already allocated in an unsigned transaction, so signing
|
|
8
|
+
* does not change its length.
|
|
9
|
+
*/
|
|
10
|
+
export const SOLANA_TRANSACTION_SIZE_LIMIT = 1232;
|
|
11
|
+
const tooLarge = (size) => new Error(`Solana transaction is too large to send: ${size}, over the ${SOLANA_TRANSACTION_SIZE_LIMIT}-byte limit. ` +
|
|
12
|
+
'A large route needs its address lookup tables passed in `states`.');
|
|
13
|
+
/**
|
|
14
|
+
* Serialize a transaction, refusing one the network would reject for its size
|
|
15
|
+
* before any wallet is asked to sign it. web3.js only throws (`encoding
|
|
16
|
+
* overruns Uint8Array`) when the message alone is over the limit; a transaction
|
|
17
|
+
* a few bytes over still serializes and would reach the wallet.
|
|
18
|
+
*/
|
|
19
|
+
export function serializeWithinSizeLimit(transaction) {
|
|
20
|
+
let bytes;
|
|
21
|
+
try {
|
|
22
|
+
bytes = transaction.serialize();
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof RangeError &&
|
|
26
|
+
/encoding overruns/.test(error.message)) {
|
|
27
|
+
throw tooLarge('the message alone does not fit');
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
if (bytes.length > SOLANA_TRANSACTION_SIZE_LIMIT) {
|
|
32
|
+
throw tooLarge(`${bytes.length} bytes`);
|
|
33
|
+
}
|
|
34
|
+
return bytes;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Rebuild the caller's address lookup tables, addresses included, so
|
|
38
|
+
* `compileToV0Message` can swap inlined 32-byte account keys for one-byte
|
|
39
|
+
* table indexes. Without the addresses every account is inlined and a large
|
|
40
|
+
* route (a multi-hop swap) goes over the size limit. Invalid data throws an
|
|
41
|
+
* error naming the table and field.
|
|
42
|
+
*/
|
|
43
|
+
export function parseLookupTables(states, toKey) {
|
|
44
|
+
return states.map((state, tableIndex) => {
|
|
45
|
+
const label = `address lookup table ${tableIndex}`;
|
|
46
|
+
const key = (value, field) => {
|
|
47
|
+
try {
|
|
48
|
+
return toKey(value);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new Error(`Invalid ${label} ${field}: ${String(value)}`);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
if (!Array.isArray(state.addresses)) {
|
|
55
|
+
throw new Error(`Invalid ${label}: addresses must be an array`);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
key: key(state.key, 'key'),
|
|
59
|
+
state: {
|
|
60
|
+
deactivationSlot: toSlot(state.deactivationSlot, label),
|
|
61
|
+
lastExtendedSlot: state.lastExtendedSlot,
|
|
62
|
+
lastExtendedSlotStartIndex: state.lastExtendedStartIndex,
|
|
63
|
+
addresses: state.addresses.map((address, index) => key(address, `address ${index}`)),
|
|
64
|
+
...(state.authority && { authority: key(state.authority, 'authority') })
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// The slot crosses JSON as a number or string even though the type says
|
|
70
|
+
// bigint, so normalise it to the bigint web3.js expects. Only the string form
|
|
71
|
+
// keeps u64::MAX exact: as a JSON number it arrives as 2^64.
|
|
72
|
+
const toSlot = (value, label) => {
|
|
73
|
+
try {
|
|
74
|
+
return BigInt(value);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
throw new Error(`Invalid ${label} deactivation slot: ${String(value)}`);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=solana-transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"solana-transaction.js","sourceRoot":"","sources":["../src/solana-transaction.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,CAAA;AAEjD,MAAM,QAAQ,GAAG,CAAC,IAAY,EAAS,EAAE,CACvC,IAAI,KAAK,CACP,4CAA4C,IAAI,cAAc,6BAA6B,eAAe;IACxG,mEAAmE,CACtE,CAAA;AAEH;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAAC,WAExC;IACC,IAAI,KAAiB,CAAA;IACrB,IAAI,CAAC;QACH,KAAK,GAAG,WAAW,CAAC,SAAS,EAAE,CAAA;IACjC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IACE,KAAK,YAAY,UAAU;YAC3B,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EACvC,CAAC;YACD,MAAM,QAAQ,CAAC,gCAAgC,CAAC,CAAA;QAClD,CAAC;QACD,MAAM,KAAK,CAAA;IACb,CAAC;IACD,IAAI,KAAK,CAAC,MAAM,GAAG,6BAA6B,EAAE,CAAC;QACjD,MAAM,QAAQ,CAAC,GAAG,KAAK,CAAC,MAAM,QAAQ,CAAC,CAAA;IACzC,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAcD;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAAkC,EAClC,KAA6B;IAE7B,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;QACtC,MAAM,KAAK,GAAG,wBAAwB,UAAU,EAAE,CAAA;QAClD,MAAM,GAAG,GAAG,CAAC,KAAa,EAAE,KAAa,EAAO,EAAE;YAChD,IAAI,CAAC;gBACH,OAAO,KAAK,CAAC,KAAK,CAAC,CAAA;YACrB,CAAC;YAAC,MAAM,CAAC;gBACP,MAAM,IAAI,KAAK,CAAC,WAAW,KAAK,IAAI,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;YAChE,CAAC;QACH,CAAC,CAAA;QACD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;YACpC,MAAM,IAAI,KAAK,CAAC,WAAW,KAAK,8BAA8B,CAAC,CAAA;QACjE,CAAC;QAED,OAAO;YACL,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC;YAC1B,KAAK,EAAE;gBACL,gBAAgB,EAAE,MAAM,CAAC,KAAK,CAAC,gBAAgB,EAAE,KAAK,CAAC;gBACvD,gBAAgB,EAAE,KAAK,CAAC,gBAAgB;gBACxC,0BAA0B,EAAE,KAAK,CAAC,sBAAsB;gBACxD,SAAS,EAAE,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAChD,GAAG,CAAC,OAAO,EAAE,WAAW,KAAK,EAAE,CAAC,CACjC;gBACD,GAAG,CAAC,KAAK,CAAC,SAAS,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,KAAK,CAAC,SAAS,EAAE,WAAW,CAAC,EAAE,CAAC;aACzE;SACF,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,wEAAwE;AACxE,8EAA8E;AAC9E,6DAA6D;AAC7D,MAAM,MAAM,GAAG,CAAC,KAAa,EAAE,KAAa,EAAU,EAAE;IACtD,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;IACtB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,WAAW,KAAK,uBAAuB,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IACzE,CAAC;AACH,CAAC,CAAA"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from './signature'
|
|
|
13
13
|
export * from './transactions'
|
|
14
14
|
export * from './errors'
|
|
15
15
|
export * from './solana-message'
|
|
16
|
+
export * from './solana-transaction'
|
|
16
17
|
export * from './observability'
|
|
17
18
|
export * from './bounded-dedup'
|
|
18
19
|
export * from './safe-error-message'
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import type { SolanaAddressLookupTable } from './transactions'
|
|
3
|
+
import {
|
|
4
|
+
SOLANA_TRANSACTION_SIZE_LIMIT,
|
|
5
|
+
parseLookupTables,
|
|
6
|
+
serializeWithinSizeLimit
|
|
7
|
+
} from './solana-transaction'
|
|
8
|
+
|
|
9
|
+
const ACTIVE = BigInt('18446744073709551615')
|
|
10
|
+
|
|
11
|
+
// Stands in for `new PublicKey(value)`: tags valid keys, throws like it does.
|
|
12
|
+
const toKey = (value: string) => {
|
|
13
|
+
if (!/^key-\d+$/.test(value)) throw new Error('Invalid public key input')
|
|
14
|
+
return { key: value }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const table = (
|
|
18
|
+
over: Partial<SolanaAddressLookupTable> = {}
|
|
19
|
+
): SolanaAddressLookupTable => ({
|
|
20
|
+
key: 'key-0',
|
|
21
|
+
addresses: ['key-1', 'key-2'],
|
|
22
|
+
deactivationSlot: ACTIVE,
|
|
23
|
+
lastExtendedSlot: 280_000_000,
|
|
24
|
+
lastExtendedStartIndex: 3,
|
|
25
|
+
...over
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe('parseLookupTables', () => {
|
|
29
|
+
it('keeps every address, in order, in web3.js AddressLookupTableAccount shape', () => {
|
|
30
|
+
expect(parseLookupTables([table()], toKey)).toEqual([
|
|
31
|
+
{
|
|
32
|
+
key: { key: 'key-0' },
|
|
33
|
+
state: {
|
|
34
|
+
deactivationSlot: ACTIVE,
|
|
35
|
+
lastExtendedSlot: 280_000_000,
|
|
36
|
+
lastExtendedSlotStartIndex: 3,
|
|
37
|
+
addresses: [{ key: 'key-1' }, { key: 'key-2' }]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('parses several tables', () => {
|
|
44
|
+
const parsed = parseLookupTables(
|
|
45
|
+
[table(), table({ key: 'key-9', addresses: ['key-8'] })],
|
|
46
|
+
toKey
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
expect(parsed.map(t => t.key.key)).toEqual(['key-0', 'key-9'])
|
|
50
|
+
expect(parsed[1]!.state.addresses).toEqual([{ key: 'key-8' }])
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('returns no tables for no states', () => {
|
|
54
|
+
expect(parseLookupTables([], toKey)).toEqual([])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it.each([
|
|
58
|
+
['a bigint', ACTIVE, ACTIVE],
|
|
59
|
+
['a number', 280_000_000, 280_000_000n],
|
|
60
|
+
['a string (keeps u64::MAX exact)', '18446744073709551615', ACTIVE],
|
|
61
|
+
// JSON.parse turns u64::MAX into 2^64. web3.js does not read the slot
|
|
62
|
+
// when compiling, so the table still applies.
|
|
63
|
+
[
|
|
64
|
+
'a JSON number (u64::MAX rounds up)',
|
|
65
|
+
JSON.parse('18446744073709551615'),
|
|
66
|
+
2n ** 64n
|
|
67
|
+
]
|
|
68
|
+
])('normalises a deactivation slot sent as %s', (_, slot, expected) => {
|
|
69
|
+
const [parsed] = parseLookupTables(
|
|
70
|
+
[table({ deactivationSlot: slot as unknown as bigint })],
|
|
71
|
+
toKey
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
expect(parsed!.state.deactivationSlot).toBe(expected)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('keeps a valid authority', () => {
|
|
78
|
+
const [parsed] = parseLookupTables([table({ authority: 'key-7' })], toKey)
|
|
79
|
+
|
|
80
|
+
expect(parsed!.state.authority).toEqual({ key: 'key-7' })
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it.each([
|
|
84
|
+
['omitted', undefined],
|
|
85
|
+
['empty', '']
|
|
86
|
+
])('leaves out an %s authority', (_, authority) => {
|
|
87
|
+
const [parsed] = parseLookupTables([table({ authority })], toKey)
|
|
88
|
+
|
|
89
|
+
expect(parsed!.state).not.toHaveProperty('authority')
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it.each([
|
|
93
|
+
[
|
|
94
|
+
'a bad table key',
|
|
95
|
+
[table(), table({ key: 'nope' })],
|
|
96
|
+
'Invalid address lookup table 1 key: nope'
|
|
97
|
+
],
|
|
98
|
+
[
|
|
99
|
+
'a bad address, naming its index',
|
|
100
|
+
[table({ addresses: ['key-1', 'bad'] })],
|
|
101
|
+
'Invalid address lookup table 0 address 1: bad'
|
|
102
|
+
],
|
|
103
|
+
[
|
|
104
|
+
'a bad authority',
|
|
105
|
+
[table({ authority: '0x123' })],
|
|
106
|
+
'Invalid address lookup table 0 authority: 0x123'
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
'addresses that are not an array',
|
|
110
|
+
[table({ addresses: 'nope' as unknown as string[] })],
|
|
111
|
+
'Invalid address lookup table 0: addresses must be an array'
|
|
112
|
+
],
|
|
113
|
+
[
|
|
114
|
+
'a deactivation slot that is not an integer',
|
|
115
|
+
[table({ deactivationSlot: 'soon' as unknown as bigint })],
|
|
116
|
+
'Invalid address lookup table 0 deactivation slot: soon'
|
|
117
|
+
]
|
|
118
|
+
])('throws a clear error for %s', (_, states, message) => {
|
|
119
|
+
expect(() => parseLookupTables(states, toKey)).toThrow(message)
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('serializeWithinSizeLimit', () => {
|
|
124
|
+
const ofLength = (length: number) => ({
|
|
125
|
+
serialize: () => new Uint8Array(length)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('allows up to the limit', () => {
|
|
129
|
+
expect(SOLANA_TRANSACTION_SIZE_LIMIT).toBe(1232)
|
|
130
|
+
expect(serializeWithinSizeLimit(ofLength(1232))).toHaveLength(1232)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('refuses anything over the limit, saying how big it is', () => {
|
|
134
|
+
expect(() => serializeWithinSizeLimit(ofLength(1233))).toThrow(
|
|
135
|
+
'Solana transaction is too large to send: 1233 bytes, over the 1232-byte limit. A large route needs its address lookup tables passed in `states`.'
|
|
136
|
+
)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it("turns web3.js's encoding overrun into the same clear error", () => {
|
|
140
|
+
const overrun = {
|
|
141
|
+
serialize: () => {
|
|
142
|
+
throw new RangeError('encoding overruns Uint8Array')
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
expect(() => serializeWithinSizeLimit(overrun)).toThrow(
|
|
147
|
+
'Solana transaction is too large to send: the message alone does not fit, over the 1232-byte limit.'
|
|
148
|
+
)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it.each([
|
|
152
|
+
['an unrelated error', new Error('boom')],
|
|
153
|
+
[
|
|
154
|
+
'a RangeError about something else',
|
|
155
|
+
new RangeError('offset is out of range')
|
|
156
|
+
]
|
|
157
|
+
])('rethrows %s unchanged', (_, error) => {
|
|
158
|
+
const failing = {
|
|
159
|
+
serialize: () => {
|
|
160
|
+
throw error
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
expect(() => serializeWithinSizeLimit(failing)).toThrow(error)
|
|
165
|
+
})
|
|
166
|
+
})
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { SolanaAddressLookupTable } from './transactions'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Solana transaction helpers shared by the injected and WalletConnect
|
|
5
|
+
* connectors. No @solana/web3.js: callers pass in what needs it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Solana's maximum serialized transaction size (web3.js `PACKET_DATA_SIZE`).
|
|
10
|
+
* Signature slots are already allocated in an unsigned transaction, so signing
|
|
11
|
+
* does not change its length.
|
|
12
|
+
*/
|
|
13
|
+
export const SOLANA_TRANSACTION_SIZE_LIMIT = 1232
|
|
14
|
+
|
|
15
|
+
const tooLarge = (size: string): Error =>
|
|
16
|
+
new Error(
|
|
17
|
+
`Solana transaction is too large to send: ${size}, over the ${SOLANA_TRANSACTION_SIZE_LIMIT}-byte limit. ` +
|
|
18
|
+
'A large route needs its address lookup tables passed in `states`.'
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Serialize a transaction, refusing one the network would reject for its size
|
|
23
|
+
* before any wallet is asked to sign it. web3.js only throws (`encoding
|
|
24
|
+
* overruns Uint8Array`) when the message alone is over the limit; a transaction
|
|
25
|
+
* a few bytes over still serializes and would reach the wallet.
|
|
26
|
+
*/
|
|
27
|
+
export function serializeWithinSizeLimit(transaction: {
|
|
28
|
+
serialize(): Uint8Array
|
|
29
|
+
}): Uint8Array {
|
|
30
|
+
let bytes: Uint8Array
|
|
31
|
+
try {
|
|
32
|
+
bytes = transaction.serialize()
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (
|
|
35
|
+
error instanceof RangeError &&
|
|
36
|
+
/encoding overruns/.test(error.message)
|
|
37
|
+
) {
|
|
38
|
+
throw tooLarge('the message alone does not fit')
|
|
39
|
+
}
|
|
40
|
+
throw error
|
|
41
|
+
}
|
|
42
|
+
if (bytes.length > SOLANA_TRANSACTION_SIZE_LIMIT) {
|
|
43
|
+
throw tooLarge(`${bytes.length} bytes`)
|
|
44
|
+
}
|
|
45
|
+
return bytes
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A lookup table in web3.js's `AddressLookupTableAccount` constructor shape. */
|
|
49
|
+
export interface ParsedLookupTable<Key> {
|
|
50
|
+
key: Key
|
|
51
|
+
state: {
|
|
52
|
+
deactivationSlot: bigint
|
|
53
|
+
lastExtendedSlot: number
|
|
54
|
+
lastExtendedSlotStartIndex: number
|
|
55
|
+
addresses: Key[]
|
|
56
|
+
authority?: Key
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Rebuild the caller's address lookup tables, addresses included, so
|
|
62
|
+
* `compileToV0Message` can swap inlined 32-byte account keys for one-byte
|
|
63
|
+
* table indexes. Without the addresses every account is inlined and a large
|
|
64
|
+
* route (a multi-hop swap) goes over the size limit. Invalid data throws an
|
|
65
|
+
* error naming the table and field.
|
|
66
|
+
*/
|
|
67
|
+
export function parseLookupTables<Key>(
|
|
68
|
+
states: SolanaAddressLookupTable[],
|
|
69
|
+
toKey: (value: string) => Key
|
|
70
|
+
): ParsedLookupTable<Key>[] {
|
|
71
|
+
return states.map((state, tableIndex) => {
|
|
72
|
+
const label = `address lookup table ${tableIndex}`
|
|
73
|
+
const key = (value: string, field: string): Key => {
|
|
74
|
+
try {
|
|
75
|
+
return toKey(value)
|
|
76
|
+
} catch {
|
|
77
|
+
throw new Error(`Invalid ${label} ${field}: ${String(value)}`)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (!Array.isArray(state.addresses)) {
|
|
81
|
+
throw new Error(`Invalid ${label}: addresses must be an array`)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
key: key(state.key, 'key'),
|
|
86
|
+
state: {
|
|
87
|
+
deactivationSlot: toSlot(state.deactivationSlot, label),
|
|
88
|
+
lastExtendedSlot: state.lastExtendedSlot,
|
|
89
|
+
lastExtendedSlotStartIndex: state.lastExtendedStartIndex,
|
|
90
|
+
addresses: state.addresses.map((address, index) =>
|
|
91
|
+
key(address, `address ${index}`)
|
|
92
|
+
),
|
|
93
|
+
...(state.authority && { authority: key(state.authority, 'authority') })
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The slot crosses JSON as a number or string even though the type says
|
|
100
|
+
// bigint, so normalise it to the bigint web3.js expects. Only the string form
|
|
101
|
+
// keeps u64::MAX exact: as a JSON number it arrives as 2^64.
|
|
102
|
+
const toSlot = (value: bigint, label: string): bigint => {
|
|
103
|
+
try {
|
|
104
|
+
return BigInt(value)
|
|
105
|
+
} catch {
|
|
106
|
+
throw new Error(`Invalid ${label} deactivation slot: ${String(value)}`)
|
|
107
|
+
}
|
|
108
|
+
}
|