@qntx/wallet-chains 0.12.0 → 0.14.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 (62) hide show
  1. package/README.md +13 -10
  2. package/dist/{account-ref-D2rW_qlI.mjs → account-ref-BFTeEyHt.mjs} +2 -2
  3. package/dist/{account-ref-D2rW_qlI.mjs.map → account-ref-BFTeEyHt.mjs.map} +1 -1
  4. package/dist/assemble-8GvNDony.mjs +405 -0
  5. package/dist/assemble-8GvNDony.mjs.map +1 -0
  6. package/dist/assemble-B3sO9Fvc.mjs +629 -0
  7. package/dist/assemble-B3sO9Fvc.mjs.map +1 -0
  8. package/dist/assemble-BdiPo0ZX.mjs +32 -0
  9. package/dist/assemble-BdiPo0ZX.mjs.map +1 -0
  10. package/dist/assets-sg2_Z3nZ.mjs +200 -0
  11. package/dist/assets-sg2_Z3nZ.mjs.map +1 -0
  12. package/dist/bitcoin-sign.d.mts +3 -0
  13. package/dist/bitcoin-sign.mjs +52 -18
  14. package/dist/bitcoin-sign.mjs.map +1 -1
  15. package/dist/bitcoin.d.mts +123 -7
  16. package/dist/bitcoin.mjs +162 -69
  17. package/dist/bitcoin.mjs.map +1 -1
  18. package/dist/evm-sign.d.mts +3 -1
  19. package/dist/evm-sign.mjs +25 -82
  20. package/dist/evm-sign.mjs.map +1 -1
  21. package/dist/evm.d.mts +252 -22
  22. package/dist/evm.mjs +2460 -448
  23. package/dist/evm.mjs.map +1 -1
  24. package/dist/families-BlgplKAJ.mjs +412 -0
  25. package/dist/families-BlgplKAJ.mjs.map +1 -0
  26. package/dist/index-CIRaHKpx.d.mts +941 -0
  27. package/dist/index.d.mts +2 -2
  28. package/dist/index.mjs +1 -1
  29. package/dist/networks.d.mts +29 -0
  30. package/dist/networks.mjs +245 -0
  31. package/dist/networks.mjs.map +1 -0
  32. package/dist/plan-wCnxHqRu.mjs +1311 -0
  33. package/dist/plan-wCnxHqRu.mjs.map +1 -0
  34. package/dist/solana-sign.d.mts +3 -0
  35. package/dist/solana-sign.mjs +23 -10
  36. package/dist/solana-sign.mjs.map +1 -1
  37. package/dist/solana.d.mts +136 -24
  38. package/dist/solana.mjs +1244 -63
  39. package/dist/solana.mjs.map +1 -1
  40. package/dist/src-CHB_z45M.mjs +387 -0
  41. package/dist/src-CHB_z45M.mjs.map +1 -0
  42. package/dist/tasks-7AFv3PQn.mjs +86 -0
  43. package/dist/tasks-7AFv3PQn.mjs.map +1 -0
  44. package/dist/token-call-DH22g8BL.mjs +914 -0
  45. package/dist/token-call-DH22g8BL.mjs.map +1 -0
  46. package/dist/tron-sign.d.mts +10 -0
  47. package/dist/tron-sign.mjs +36 -0
  48. package/dist/tron-sign.mjs.map +1 -0
  49. package/dist/tron.d.mts +328 -0
  50. package/dist/tron.mjs +1484 -0
  51. package/dist/tron.mjs.map +1 -0
  52. package/dist/typed-data-CE9h4U0F.d.mts +51 -0
  53. package/dist/typed-data-DRtKULsH.mjs +466 -0
  54. package/dist/typed-data-DRtKULsH.mjs.map +1 -0
  55. package/package.json +17 -10
  56. package/dist/index-DYwm4-Ec.d.mts +0 -256
  57. package/dist/message-ChhEZ0lx.mjs +0 -188
  58. package/dist/message-ChhEZ0lx.mjs.map +0 -1
  59. package/dist/src-8NZeEnrW.mjs +0 -67
  60. package/dist/src-8NZeEnrW.mjs.map +0 -1
  61. package/dist/typed-data-J5g2WemC.mjs +0 -227
  62. package/dist/typed-data-J5g2WemC.mjs.map +0 -1
package/dist/solana.mjs CHANGED
@@ -1,9 +1,14 @@
1
- import { t as ChainError } from "./src-8NZeEnrW.mjs";
2
- import { n as requestBase, t as isRecord$1 } from "./account-ref-D2rW_qlI.mjs";
1
+ import { t as ChainError } from "./src-CHB_z45M.mjs";
2
+ import { n as requestBase, t as isRecord$1 } from "./account-ref-BFTeEyHt.mjs";
3
+ import { withNativeAmountMeta } from "./networks.mjs";
4
+ import { t as RECOGNISED_FAMILIES } from "./families-BlgplKAJ.mjs";
5
+ import { n as planSolana, t as assembleSolana } from "./assemble-BdiPo0ZX.mjs";
6
+ import { ed25519 } from "@noble/curves/ed25519.js";
3
7
  import { address, getAddressDecoder, getAddressEncoder } from "@solana/addresses";
4
8
  import { getCompiledTransactionMessageDecoder } from "@solana/transaction-messages";
5
- import { getU32Decoder, getU64Decoder } from "@solana/codecs";
6
- import { ed25519 } from "@noble/curves/ed25519.js";
9
+ import { getI64Decoder, getU16Decoder, getU32Decoder, getU64Decoder } from "@solana/codecs";
10
+ import { sha256 } from "@noble/hashes/sha2.js";
11
+ import { getTransactionDecoder } from "@solana/transactions";
7
12
  // src/solana/ids.ts
8
13
  /** Well-known Solana program ids (base58, 32 bytes) — frozen constants. */
9
14
  const PROGRAM_IDS = Object.freeze({
@@ -12,7 +17,10 @@ const PROGRAM_IDS = Object.freeze({
12
17
  associatedToken: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
13
18
  token: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
14
19
  token2022: "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb",
15
- stake: "Stake11111111111111111111111111111111111111"
20
+ stake: "Stake11111111111111111111111111111111111111",
21
+ ed25519: "Ed25519SigVerify111111111111111111111111111",
22
+ secp256k1: "KeccakSecp256k11111111111111111111111111111",
23
+ secp256r1: "Secp256r1SigVerify1111111111111111111111111"
16
24
  });
17
25
  /**
18
26
  * CAIP-2 ids the wallet knows. The wire message itself carries no network marker — `network` is the
@@ -21,33 +29,66 @@ const PROGRAM_IDS = Object.freeze({
21
29
  const SOLANA_NETWORKS = Object.freeze(["solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1"]);
22
30
  const BASE58 = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
23
31
  const isSolanaAddress = (value) => BASE58.test(value);
24
- const decoder$1 = getAddressDecoder();
32
+ const decoder$2 = getAddressDecoder();
25
33
  /** Base58-encoded address for a 32-byte ed25519 public key. */
26
34
  function solanaAddressFromPublicKey(pubkey) {
27
35
  if (pubkey.length !== 32) throw new ChainError("invalid-address", "expected a 32-byte ed25519 public key");
28
- return decoder$1.decode(pubkey);
36
+ return decoder$2.decode(pubkey);
29
37
  }
30
- const decoder = getCompiledTransactionMessageDecoder();
38
+ // src/solana/message.ts
39
+ const LEGACY_TRANSACTION_MAX_BYTES = 1232;
40
+ const V1_TRANSACTION_MAX_BYTES = 4096;
41
+ const decoder$1 = getCompiledTransactionMessageDecoder();
42
+ const utf8 = new TextDecoder("utf-8", { fatal: true });
43
+ /**
44
+ * Whether a cluster could accept `bytes` as the message of a transaction — signing them as a raw
45
+ * message would authorise it.
46
+ */
47
+ const mayBeTransactionMessage = (bytes) => {
48
+ const [first] = bytes;
49
+ if (first === void 0 || first === 255) return false;
50
+ if (first >= 128) try {
51
+ utf8.decode(bytes);
52
+ return false;
53
+ } catch {
54
+ return true;
55
+ }
56
+ try {
57
+ const [, end] = decoder$1.read(bytes, 0);
58
+ return end === bytes.length;
59
+ } catch {
60
+ return false;
61
+ }
62
+ };
31
63
  /**
32
- * Decodes a compiled wire message; rejects trailing bytes and the 1232-byte transaction limit. The
33
- * fee payer is `staticAccounts[0]` — every valid transaction signs it.
64
+ * Decodes a compiled wire message; rejects trailing bytes and the transaction size limit (1232 for
65
+ * legacy/v0, 4096 including signatures for v1). The fee payer is `staticAccounts[0]` — every valid
66
+ * transaction signs it.
34
67
  */
35
68
  const parseCompiledMessage = (bytes) => {
69
+ const [first] = bytes;
70
+ if (first === void 0) throw new ChainError("invalid-request", "compiled message is empty");
71
+ if (first === 129) return parseV1Message(bytes);
72
+ if (first > 129) throw new ChainError("unsupported", `Solana transaction version ${first - 128} is not supported`);
36
73
  if (bytes.length > 1232) throw new ChainError("invalid-request", "compiled message exceeds the 1232-byte limit");
37
- if (bytes.length === 0) throw new ChainError("invalid-request", "compiled message is empty");
38
74
  let decoded;
75
+ let end;
39
76
  try {
40
- decoded = decoder.decode(bytes);
77
+ [decoded, end] = decoder$1.read(bytes, 0);
41
78
  } catch {
42
79
  throw new ChainError("invalid-request", "compiled message failed to decode");
43
80
  }
81
+ if (end !== bytes.length) throw new ChainError("invalid-request", "compiled message has trailing bytes");
44
82
  const message = asCompiledMessage(decoded);
45
83
  const staticAccounts = [...message.staticAccounts];
46
84
  const lookups = message.addressTableLookups;
47
85
  return {
48
86
  version: message.version,
49
87
  numRequiredSignatures: message.numSignerAccounts,
88
+ numReadonlySignerAccounts: message.numReadonlySignerAccounts,
89
+ numReadonlyNonSignerAccounts: message.numReadonlyNonSignerAccounts,
50
90
  staticAccounts,
91
+ lifetimeToken: message.lifetimeToken,
51
92
  instructions: message.instructions,
52
93
  lookups: lookups.map((l) => ({
53
94
  address: l.lookupTableAddress,
@@ -59,8 +100,7 @@ const parseCompiledMessage = (bytes) => {
59
100
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
60
101
  const isStringArray = (value) => Array.isArray(value) && value.every((v) => typeof v === "string");
61
102
  const isNumberArray = (value) => Array.isArray(value) && value.every((v) => typeof v === "number");
62
- const isArrayLike = (value) => isRecord(value) && typeof value["length"] === "number";
63
- const isIx = (value) => isRecord(value) && typeof value["programAddressIndex"] === "number" && (value["accountIndices"] === void 0 || isNumberArray(value["accountIndices"]) || isArrayLike(value["accountIndices"])) && value["data"] instanceof Uint8Array;
103
+ const isIx = (value) => isRecord(value) && typeof value["programAddressIndex"] === "number" && (value["accountIndices"] === void 0 || isNumberArray(value["accountIndices"]) || value["accountIndices"] instanceof Uint8Array) && (value["data"] === void 0 || value["data"] instanceof Uint8Array);
64
104
  const isLookup = (value) => isRecord(value) && typeof value["lookupTableAddress"] === "string" && (isNumberArray(value["readonlyIndexes"]) || value["readonlyIndexes"] instanceof Uint8Array) && (isNumberArray(value["writableIndexes"]) || value["writableIndexes"] instanceof Uint8Array);
65
105
  const asCompiledMessage = (value) => {
66
106
  if (!isRecord(value) || !isRecord(value["header"])) throw new ChainError("invalid-request", "compiled message failed to decode");
@@ -68,15 +108,18 @@ const asCompiledMessage = (value) => {
68
108
  const { staticAccounts } = value;
69
109
  const { instructions } = value;
70
110
  const lookups = value["addressTableLookups"];
71
- if (typeof header["numSignerAccounts"] !== "number" || !isStringArray(staticAccounts) || !Array.isArray(instructions) || !instructions.every(isIx) || lookups !== void 0 && (!Array.isArray(lookups) || !lookups.every(isLookup))) throw new ChainError("invalid-request", "compiled message failed to decode");
72
- const version = value["version"] === 0 ? 0 : "legacy";
111
+ const { lifetimeToken } = value;
112
+ if (typeof header["numSignerAccounts"] !== "number" || typeof header["numReadonlySignerAccounts"] !== "number" || typeof header["numReadonlyNonSignerAccounts"] !== "number" || !isStringArray(staticAccounts) || typeof lifetimeToken !== "string" || !Array.isArray(instructions) || !instructions.every(isIx) || lookups !== void 0 && (!Array.isArray(lookups) || !lookups.every(isLookup))) throw new ChainError("invalid-request", "compiled message failed to decode");
113
+ const versionField = value["version"];
114
+ if (versionField !== "legacy" && versionField !== 0) throw new ChainError("invalid-request", "compiled message failed to decode");
115
+ const version = versionField;
73
116
  const ixs = instructions.map((ix) => {
74
117
  if (!isIx(ix)) throw new ChainError("invalid-request", "compiled message failed to decode");
75
118
  const indices = ix.accountIndices;
76
119
  return {
77
120
  programAddressIndex: ix.programAddressIndex,
78
121
  accountIndices: indices === void 0 ? [] : [...indices],
79
- data: ix.data
122
+ data: ix.data ?? /* @__PURE__ */ new Uint8Array(0)
80
123
  };
81
124
  });
82
125
  const tables = (lookups ?? []).map((t) => {
@@ -90,11 +133,144 @@ const asCompiledMessage = (value) => {
90
133
  return {
91
134
  version,
92
135
  numSignerAccounts: header["numSignerAccounts"],
136
+ numReadonlySignerAccounts: header["numReadonlySignerAccounts"],
137
+ numReadonlyNonSignerAccounts: header["numReadonlyNonSignerAccounts"],
93
138
  staticAccounts,
139
+ lifetimeToken,
94
140
  instructions: ixs,
95
141
  addressTableLookups: tables
96
142
  };
97
143
  };
144
+ const V1_MESSAGE_MIN_BYTES = 42;
145
+ const V1_PRIORITY_FEE_BITS = 3;
146
+ const V1_MAX_SIGNATURES = 12;
147
+ const V1_MAX_ACCOUNTS = 64;
148
+ const V1_MAX_INSTRUCTIONS = 64;
149
+ const V1_HEAP_MIN = 32768;
150
+ const V1_HEAP_MAX = 262144;
151
+ const V1_HEAP_MULTIPLE = 1024;
152
+ const isV1InstructionHeader = (value) => isRecord(value) && typeof value["programAccountIndex"] === "number" && typeof value["numInstructionAccounts"] === "number" && typeof value["numInstructionDataBytes"] === "number";
153
+ const isV1InstructionPayload = (value) => isRecord(value) && (isNumberArray(value["instructionAccountIndices"]) || value["instructionAccountIndices"] instanceof Uint8Array) && value["instructionData"] instanceof Uint8Array;
154
+ const asV1Message = (value) => {
155
+ if (!isRecord(value) || !isRecord(value["header"])) throw new ChainError("invalid-request", "compiled message failed to decode");
156
+ const { header } = value;
157
+ const { staticAccounts } = value;
158
+ const headers = value["instructionHeaders"];
159
+ const payloads = value["instructionPayloads"];
160
+ if (value["version"] !== 1 || typeof header["numSignerAccounts"] !== "number" || typeof header["numReadonlySignerAccounts"] !== "number" || typeof header["numReadonlyNonSignerAccounts"] !== "number" || typeof value["lifetimeToken"] !== "string" || !isStringArray(staticAccounts) || !Array.isArray(headers) || !Array.isArray(payloads)) throw new ChainError("invalid-request", "compiled message failed to decode");
161
+ const instructions = headers.map((h, i) => {
162
+ const payload = payloads[i];
163
+ if (!isV1InstructionHeader(h) || !isV1InstructionPayload(payload)) throw new ChainError("invalid-request", "compiled message failed to decode");
164
+ return {
165
+ programAccountIndex: h.programAccountIndex,
166
+ accountIndices: payload.instructionAccountIndices,
167
+ data: payload.instructionData
168
+ };
169
+ });
170
+ return {
171
+ version: 1,
172
+ header: {
173
+ numSignerAccounts: header["numSignerAccounts"],
174
+ numReadonlySignerAccounts: header["numReadonlySignerAccounts"],
175
+ numReadonlyNonSignerAccounts: header["numReadonlyNonSignerAccounts"]
176
+ },
177
+ instructions,
178
+ lifetimeToken: value["lifetimeToken"],
179
+ staticAccounts
180
+ };
181
+ };
182
+ const v1Config = (mask, bytes, staticAccountCount) => {
183
+ const view = new DataView(bytes.buffer, bytes.byteOffset + 42 + 32 * staticAccountCount);
184
+ let offset = 0;
185
+ const config = {};
186
+ if ((mask & V1_PRIORITY_FEE_BITS) !== 0) {
187
+ config.priorityFeeLamports = view.getBigUint64(offset, true);
188
+ offset += 8;
189
+ }
190
+ if ((mask & 4) !== 0) {
191
+ config.computeUnitLimit = view.getUint32(offset, true);
192
+ offset += 4;
193
+ }
194
+ if ((mask & 8) !== 0) {
195
+ config.loadedAccountsDataSizeLimit = view.getUint32(offset, true);
196
+ offset += 4;
197
+ }
198
+ if ((mask & 16) !== 0) config.heapSize = view.getUint32(offset, true);
199
+ return config;
200
+ };
201
+ const sanitizeV1Message = (message, config) => {
202
+ const { numSignerAccounts: signatures, numReadonlySignerAccounts: readonlySigners, numReadonlyNonSignerAccounts: readonlyNonSigners } = message.header;
203
+ const accounts = message.staticAccounts.length;
204
+ if (signatures < 1 || signatures > V1_MAX_SIGNATURES) throw new ChainError("invalid-request", `v1 message requires ${signatures} signatures (1–12 allowed)`);
205
+ if (readonlySigners >= signatures) throw new ChainError("invalid-request", `v1 message marks ${readonlySigners} of ${signatures} signers read-only`);
206
+ if (accounts > V1_MAX_ACCOUNTS) throw new ChainError("invalid-request", `v1 message declares ${accounts} accounts (64 allowed)`);
207
+ if (accounts < signatures + readonlyNonSigners) throw new ChainError("invalid-request", `v1 message has ${accounts} accounts for ${signatures} signers and ${readonlyNonSigners} read-only non-signers`);
208
+ if (message.instructions.length > V1_MAX_INSTRUCTIONS) throw new ChainError("invalid-request", `v1 message declares ${message.instructions.length} instructions (64 allowed)`);
209
+ const seen = /* @__PURE__ */ new Set();
210
+ for (const account of message.staticAccounts) {
211
+ if (seen.has(account)) throw new ChainError("invalid-request", `v1 message repeats account ${account}`);
212
+ seen.add(account);
213
+ }
214
+ for (const [i, instruction] of message.instructions.entries()) {
215
+ if (instruction.programAccountIndex === 0) throw new ChainError("invalid-request", `v1 instruction ${i} uses the fee payer as its program`);
216
+ if (instruction.programAccountIndex >= accounts) throw new ChainError("invalid-request", `v1 instruction ${i} uses program index ${instruction.programAccountIndex} of ${accounts} accounts`);
217
+ for (const index of instruction.accountIndices) if (index >= accounts) throw new ChainError("invalid-request", `v1 instruction ${i} uses account index ${index} of ${accounts} accounts`);
218
+ }
219
+ const heap = config.heapSize;
220
+ if (heap !== void 0 && (heap % V1_HEAP_MULTIPLE !== 0 || heap < V1_HEAP_MIN || heap > V1_HEAP_MAX)) throw new ChainError("invalid-request", `v1 heap size ${heap} is not a multiple of 1024 within 32768–262144`);
221
+ };
222
+ /**
223
+ * A v1 message: the mask is checked before kit decodes — kit reads only the bits it knows, so an
224
+ * unknown bit or a half priority-fee pair must be caught here. The size bound covers the whole
225
+ * transaction (message plus 64 bytes per required signature), then every Agave sanitize rule runs
226
+ * before the message maps onto the same model as legacy/v0.
227
+ */
228
+ const parseV1Message = (bytes) => {
229
+ if (bytes.length < V1_MESSAGE_MIN_BYTES) throw new ChainError("invalid-request", "compiled message failed to decode");
230
+ const mask = new DataView(bytes.buffer, bytes.byteOffset + 4, 4).getUint32(0, true);
231
+ if ((mask & -32) !== 0) throw new ChainError("unsupported", `Solana v1 transaction config mask 0x${mask.toString(16)} sets bits this wallet does not know`);
232
+ if ((mask & V1_PRIORITY_FEE_BITS) !== 0 && (mask & V1_PRIORITY_FEE_BITS) !== V1_PRIORITY_FEE_BITS) throw new ChainError("invalid-request", "Solana v1 transaction config mask sets only one of the two priority-fee bits");
233
+ if (bytes.length + 64 * (bytes[1] ?? 0) > 4096) throw new ChainError("invalid-request", "v1 transaction exceeds the 4096-byte limit");
234
+ let decoded;
235
+ let end;
236
+ try {
237
+ [decoded, end] = decoder$1.read(bytes, 0);
238
+ } catch {
239
+ throw new ChainError("invalid-request", "compiled message failed to decode");
240
+ }
241
+ if (end !== bytes.length) throw new ChainError("invalid-request", "compiled message has trailing bytes");
242
+ const message = asV1Message(decoded);
243
+ const config = v1Config(mask, bytes, message.staticAccounts.length);
244
+ sanitizeV1Message(message, config);
245
+ return {
246
+ version: 1,
247
+ numRequiredSignatures: message.header.numSignerAccounts,
248
+ numReadonlySignerAccounts: message.header.numReadonlySignerAccounts,
249
+ numReadonlyNonSignerAccounts: message.header.numReadonlyNonSignerAccounts,
250
+ staticAccounts: [...message.staticAccounts],
251
+ lifetimeToken: message.lifetimeToken,
252
+ instructions: message.instructions.map((instruction) => ({
253
+ programAddressIndex: instruction.programAccountIndex,
254
+ accountIndices: [...instruction.accountIndices],
255
+ data: new Uint8Array(instruction.data)
256
+ })),
257
+ lookups: [],
258
+ config
259
+ };
260
+ };
261
+ /**
262
+ * Whether the account `index` is writable in the message: a static signer index is writable below
263
+ * `numRequiredSignatures - numReadonlySignerAccounts`, a static non-signer index below
264
+ * `staticAccounts.length - numReadonlyNonSignerAccounts`, and a loaded index is writable exactly in
265
+ * the leading writable section of the loaded addresses.
266
+ */
267
+ const isWritableIndex = (parsed, index) => {
268
+ const n = parsed.staticAccounts.length;
269
+ if (index < n) return index < parsed.numRequiredSignatures ? index < parsed.numRequiredSignatures - parsed.numReadonlySignerAccounts : index < n - parsed.numReadonlyNonSignerAccounts;
270
+ let writable = 0;
271
+ for (const lookup of parsed.lookups) writable += lookup.writableIndexes.length;
272
+ return index - n < writable;
273
+ };
98
274
  /**
99
275
  * Resolves an account index across static accounts and (request-declared) lookup tables in the
100
276
  * compiled lookup order: writable entries then readable.
@@ -102,20 +278,560 @@ const asCompiledMessage = (value) => {
102
278
  const resolveAccountIndex = (parsed, index, lookups) => {
103
279
  if (index < parsed.staticAccounts.length) return parsed.staticAccounts[index];
104
280
  if (lookups === void 0) return;
105
- let offset = index - parsed.staticAccounts.length;
106
- for (const table of lookups) {
107
- if (offset < table.addresses.length) return table.addresses[offset];
108
- offset -= table.addresses.length;
109
- }
281
+ const declared = new Map(lookups.map((t) => [t.address, t.addresses]));
282
+ return [...parsed.lookups.flatMap((t) => t.writableIndexes.map((i) => declared.get(t.address)?.[i])), ...parsed.lookups.flatMap((t) => t.readonlyIndexes.map((i) => declared.get(t.address)?.[i]))][index - parsed.staticAccounts.length];
283
+ };
284
+ const namedAccounts = (parsed, lookups) => {
285
+ const fromTable = /* @__PURE__ */ new Set();
286
+ return {
287
+ fromTable,
288
+ at: (index) => {
289
+ if (index < parsed.staticAccounts.length) return parsed.staticAccounts[index];
290
+ const resolved = resolveAccountIndex(parsed, index, lookups);
291
+ if (resolved !== void 0) fromTable.add(resolved);
292
+ }
293
+ };
110
294
  };
111
295
  /** All declared lookup-table entries must be valid base58 32-byte addresses. */
112
296
  const validateLookupTables = (tables) => {
113
297
  if (tables === void 0) return;
114
298
  for (const table of tables) if (!isSolanaAddress(table.address) || !table.addresses.every(isSolanaAddress)) throw new ChainError("invalid-address", "lookup table entries must be base58 32-byte addresses");
115
299
  };
300
+ // src/solana/ata.ts
301
+ const PDA_SEED = new TextEncoder().encode("ProgramDerivedAddress");
302
+ const encoder = getAddressEncoder();
303
+ const decoder = getAddressDecoder();
304
+ const onCurve = (candidate) => {
305
+ try {
306
+ ed25519.Point.fromBytes(candidate);
307
+ return true;
308
+ } catch {
309
+ return false;
310
+ }
311
+ };
312
+ /**
313
+ * Sync Associated Token Account derivation for the kernel: `findAssociatedTokenPda` is async, so
314
+ * the decoder derives `sha256(owner ‖ tokenProgram ‖ mint ‖ bump ‖ ata ‖ "ProgramDerivedAddress")`
315
+ * over bump 255→0 and takes the first hash off the ed25519 curve — matching the on-chain rule.
316
+ */
317
+ const associatedTokenAddress = (owner, mint, tokenProgram) => {
318
+ const ownerBytes = encoder.encode(address(owner));
319
+ const programBytes = encoder.encode(address(tokenProgram));
320
+ const mintBytes = encoder.encode(address(mint));
321
+ const ataBytes = encoder.encode(address(PROGRAM_IDS.associatedToken));
322
+ const input = new Uint8Array(ownerBytes.length + programBytes.length + mintBytes.length + 1 + ataBytes.length + PDA_SEED.length);
323
+ let o = 0;
324
+ const put = (bytes) => {
325
+ input.set(bytes, o);
326
+ o += bytes.length;
327
+ };
328
+ put(ownerBytes);
329
+ put(programBytes);
330
+ put(mintBytes);
331
+ const bumpAt = o;
332
+ o += 1;
333
+ put(ataBytes);
334
+ put(PDA_SEED);
335
+ for (let bump = 255; bump >= 0; bump -= 1) {
336
+ input[bumpAt] = bump;
337
+ const candidate = sha256(input);
338
+ if (!onCurve(candidate)) return decoder.decode(candidate);
339
+ }
340
+ throw new ChainError("invalid-request", "no PDA bump for the associated token account");
341
+ };
342
+ /** SPL Token — AMM v4 asserts its token program account equals this id (processor.rs check). */
343
+ const SPL_TOKEN = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
344
+ const SWAP_V2 = Uint8Array.from([
345
+ 43,
346
+ 4,
347
+ 237,
348
+ 11,
349
+ 26,
350
+ 201,
351
+ 30,
352
+ 98
353
+ ]);
354
+ const SWAP_BASE_INPUT = Uint8Array.from([
355
+ 143,
356
+ 190,
357
+ 90,
358
+ 218,
359
+ 196,
360
+ 30,
361
+ 51,
362
+ 222
363
+ ]);
364
+ const DLMM_SWAP = Uint8Array.from([
365
+ 248,
366
+ 198,
367
+ 158,
368
+ 145,
369
+ 225,
370
+ 117,
371
+ 135,
372
+ 200
373
+ ]);
374
+ const DLMM_SWAP2 = Uint8Array.from([
375
+ 65,
376
+ 75,
377
+ 63,
378
+ 76,
379
+ 235,
380
+ 91,
381
+ 91,
382
+ 136
383
+ ]);
384
+ const u32$3 = getU32Decoder();
385
+ const u64$4 = getU64Decoder();
386
+ const startsWith$1 = (data, prefix) => data.length >= prefix.length && prefix.every((b, i) => data[i] === b);
387
+ /**
388
+ * AMM swap instructions whose account layout pins the signer's worst-case in/out. Anything that
389
+ * does not match a documented shape exactly — tag, data length, account count, signer slot, the AMM
390
+ * v4 token program — returns undefined and falls back to the program-authority rule.
391
+ */
392
+ const decodeAmmSwapIx = (ix, named, program) => {
393
+ const { data } = ix;
394
+ const account = (i) => {
395
+ const index = ix.accountIndices[i];
396
+ return index === void 0 ? void 0 : named.at(index);
397
+ };
398
+ if (program === "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8") {
399
+ const tag = data.at(0);
400
+ if (!(tag === 9 || tag === 16) && !(tag === 17) || data.length !== 17) return;
401
+ const n = ix.accountIndices.length;
402
+ const expected = tag === 16 || tag === 17 ? 8 : null;
403
+ if (expected === null ? n !== 17 && n !== 18 : n !== expected) return;
404
+ if (account(0) !== SPL_TOKEN) return;
405
+ const authority = account(n - 1);
406
+ const sourceAccount = account(n - 3);
407
+ const destinationAccount = account(n - 2);
408
+ if (authority === void 0 || sourceAccount === void 0 || destinationAccount === void 0) return;
409
+ return {
410
+ program,
411
+ protocol: "raydium-amm-v4",
412
+ authority,
413
+ sourceAccount,
414
+ destinationAccount,
415
+ maxIn: u64$4.decode(data, 1),
416
+ minOut: u64$4.decode(data, 9)
417
+ };
418
+ }
419
+ if (program === "CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C") {
420
+ if (!startsWith$1(data, SWAP_BASE_INPUT) || data.length !== 24 || ix.accountIndices.length !== 13) return;
421
+ const authority = account(0);
422
+ const sourceAccount = account(4);
423
+ const destinationAccount = account(5);
424
+ const sourceMint = account(10);
425
+ const destinationMint = account(11);
426
+ const destinationTokenProgram = account(9);
427
+ if (authority === void 0 || sourceAccount === void 0 || destinationAccount === void 0 || sourceMint === void 0 || destinationMint === void 0 || destinationTokenProgram === void 0) return;
428
+ return {
429
+ program,
430
+ protocol: "raydium-cpmm",
431
+ authority,
432
+ sourceAccount,
433
+ destinationAccount,
434
+ sourceMint,
435
+ destinationMint,
436
+ destinationTokenProgram,
437
+ maxIn: u64$4.decode(data, 8),
438
+ minOut: u64$4.decode(data, 16)
439
+ };
440
+ }
441
+ if (program === "CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK") {
442
+ if (!startsWith$1(data, SWAP_V2) || data.length !== 41 || ix.accountIndices.length < 13) return;
443
+ const authority = account(0);
444
+ const sourceAccount = account(3);
445
+ const destinationAccount = account(4);
446
+ const sourceMint = account(11);
447
+ const destinationMint = account(12);
448
+ if (authority === void 0 || sourceAccount === void 0 || destinationAccount === void 0 || sourceMint === void 0 || destinationMint === void 0) return;
449
+ if (data.at(40) !== 1) return;
450
+ return {
451
+ program,
452
+ protocol: "raydium-clmm",
453
+ authority,
454
+ sourceAccount,
455
+ destinationAccount,
456
+ sourceMint,
457
+ destinationMint,
458
+ maxIn: u64$4.decode(data, 8),
459
+ minOut: u64$4.decode(data, 16)
460
+ };
461
+ }
462
+ if (program === "whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc") {
463
+ if (!startsWith$1(data, SWAP_V2) || data.length < 43 || ix.accountIndices.length < 15) return;
464
+ const option = data.at(42);
465
+ if (option === 0) {
466
+ if (data.length !== 43) return;
467
+ } else if (option === 1) {
468
+ if (data.length < 47) return;
469
+ const slices = u32$3.decode(data, 43);
470
+ if (data.length !== 47 + slices * 2) return;
471
+ } else return;
472
+ const authority = account(3);
473
+ if (authority === void 0) return;
474
+ const aToB = data.at(41);
475
+ if (aToB !== 0 && aToB !== 1 || data.at(40) !== 1) return;
476
+ const sourceIndex = aToB === 1 ? 7 : 9;
477
+ const destinationIndex = aToB === 1 ? 9 : 7;
478
+ const sourceAccount = account(sourceIndex);
479
+ const destinationAccount = account(destinationIndex);
480
+ const sourceMint = account(aToB === 1 ? 5 : 6);
481
+ const destinationMint = account(aToB === 1 ? 6 : 5);
482
+ const destinationTokenProgram = account(aToB === 1 ? 1 : 0);
483
+ if (sourceAccount === void 0 || destinationAccount === void 0 || sourceMint === void 0 || destinationMint === void 0 || destinationTokenProgram === void 0) return;
484
+ return {
485
+ program,
486
+ protocol: "orca-whirlpool",
487
+ authority,
488
+ sourceAccount,
489
+ destinationAccount,
490
+ sourceMint,
491
+ destinationMint,
492
+ destinationTokenProgram,
493
+ maxIn: u64$4.decode(data, 8),
494
+ minOut: u64$4.decode(data, 16)
495
+ };
496
+ }
497
+ if (program === "LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo") {
498
+ const swap = startsWith$1(data, DLMM_SWAP);
499
+ const swap2 = !swap && startsWith$1(data, DLMM_SWAP2);
500
+ if (!swap && !swap2 || data.length < 24 || ix.accountIndices.length < (swap ? 15 : 16)) return;
501
+ if (swap) {
502
+ if (data.length !== 24) return;
503
+ } else {
504
+ if (data.length < 28) return;
505
+ const slices = u32$3.decode(data, 24);
506
+ let offset = 28;
507
+ for (let i = 0; i < slices; i++) {
508
+ const tag = data.at(offset);
509
+ if (tag === void 0 || tag > 4) return;
510
+ offset += tag === 3 ? 2 : 1;
511
+ if (data.at(offset) === void 0) return;
512
+ offset += 1;
513
+ }
514
+ if (offset !== data.length) return;
515
+ }
516
+ const authority = account(10);
517
+ const sourceAccount = account(4);
518
+ const destinationAccount = account(5);
519
+ const mintX = account(6);
520
+ const mintY = account(7);
521
+ const programX = account(11);
522
+ const programY = account(12);
523
+ if (authority === void 0 || sourceAccount === void 0 || destinationAccount === void 0 || mintX === void 0 || mintY === void 0 || programX === void 0 || programY === void 0) return;
524
+ const ataX = associatedTokenAddress(authority, mintX, programX);
525
+ const ataY = associatedTokenAddress(authority, mintY, programY);
526
+ const sourceMint = sourceAccount === ataX ? mintX : sourceAccount === ataY ? mintY : void 0;
527
+ const destinationMint = destinationAccount === ataX ? mintX : destinationAccount === ataY ? mintY : void 0;
528
+ if (sourceMint !== void 0 && sourceMint === destinationMint) return;
529
+ return {
530
+ program,
531
+ protocol: "meteora-dlmm",
532
+ authority,
533
+ sourceAccount,
534
+ destinationAccount,
535
+ sourceMint,
536
+ destinationMint,
537
+ ...destinationMint === void 0 ? {} : { destinationTokenProgram: destinationMint === mintX ? programX : programY },
538
+ maxIn: u64$4.decode(data, 8),
539
+ minOut: u64$4.decode(data, 16)
540
+ };
541
+ }
542
+ };
116
543
  // src/solana/assets.ts
117
544
  /** The native asset id in `AssetFlow` and simulated asset changes. */
118
545
  const SOLANA_NATIVE_ASSET = "lamports";
546
+ // src/solana/core.ts
547
+ /** Metaplex Core — recognised by program id; the transfer authority is verified on-chain. */
548
+ const MPL_CORE_PROGRAM = "CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d";
549
+ /**
550
+ * `MplAssetInstruction::TransferV1` = 14; `TransferV1Args` is `{ compressionProof: Option }` — a
551
+ * bare transfer carries `[14, 0]` (no compression proof). mpl-core IDL at c8732ac.
552
+ */
553
+ const TRANSFER_V1 = Uint8Array.from([14, 0]);
554
+ /**
555
+ * `TransferV1` with the accounts `[asset, collection, payer, authority, newOwner, systemProgram,
556
+ * logWrapper]` — omitted optionals carry the program id itself (the Shank convention). The
557
+ * authority slot resolves to the payer when it is the placeholder; it must be the signer for the
558
+ * call to count as a transfer the wallet is doing.
559
+ */
560
+ const decodeCoreIx = (ix, named) => {
561
+ const { data } = ix;
562
+ const account = (i) => {
563
+ const index = ix.accountIndices[i];
564
+ return index === void 0 ? void 0 : named.at(index);
565
+ };
566
+ if (data.length !== 2 || data[0] !== TRANSFER_V1[0] || data[1] !== TRANSFER_V1[1]) return;
567
+ const asset = account(0);
568
+ const payer = account(2);
569
+ const authoritySlot = account(3);
570
+ const newOwner = account(4);
571
+ if (asset === void 0 || payer === void 0 || authoritySlot === void 0 || newOwner === void 0) return;
572
+ return {
573
+ authority: authoritySlot === "CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d" ? payer : authoritySlot,
574
+ asset,
575
+ newOwner
576
+ };
577
+ };
578
+ // src/solana/jupiter.ts
579
+ /** Jupiter's swap program — recognised by program id like Lido's upgradeable proxies. */
580
+ const JUPITER_PROGRAM = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";
581
+ const WSOL_MINT = "So11111111111111111111111111111111111111112";
582
+ const ROUTE_V2 = Uint8Array.from([
583
+ 187,
584
+ 100,
585
+ 250,
586
+ 204,
587
+ 49,
588
+ 196,
589
+ 175,
590
+ 20
591
+ ]);
592
+ const SHARED_ROUTE_V2 = Uint8Array.from([
593
+ 209,
594
+ 152,
595
+ 83,
596
+ 147,
597
+ 124,
598
+ 254,
599
+ 216,
600
+ 233
601
+ ]);
602
+ const u16$1 = getU16Decoder();
603
+ const u32$2 = getU32Decoder();
604
+ const u64$3 = getU64Decoder();
605
+ const startsWith = (data, prefix) => data.length >= prefix.length && prefix.every((b, i) => data[i] === b);
606
+ const MAX_BPS = 1e4;
607
+ /** A slippage above 100% has no "minimum received" — such a route is not recognised. */
608
+ const withinSlippage = (swap) => swap.slippageBps > MAX_BPS ? void 0 : swap;
609
+ /**
610
+ * `route_v2` / `shared_accounts_route_v2`: the fixed scalar prefix must be exact and the trailing
611
+ * `route_plan` vector must declare enough remaining bytes (each step is ≥ 5 bytes on the wire). The
612
+ * route steps themselves stay opaque — the intent only shows the inputs and the floor.
613
+ */
614
+ const decodeJupiterIx = (ix, named) => {
615
+ const { data } = ix;
616
+ const account = (i) => {
617
+ const index = ix.accountIndices[i];
618
+ return index === void 0 ? void 0 : named.at(index);
619
+ };
620
+ const accountAt = (i) => account(i) ?? "";
621
+ const planWellFormed = (offset) => {
622
+ if (data.length < offset + 4) return false;
623
+ const count = u32$2.decode(data, offset);
624
+ return data.length >= offset + 4 + count * 5;
625
+ };
626
+ const scalars = (o) => ({
627
+ inAmount: u64$3.decode(data, o),
628
+ quotedOut: u64$3.decode(data, o + 8),
629
+ slippageBps: u16$1.decode(data, o + 16),
630
+ platformFeeBps: u16$1.decode(data, o + 18)
631
+ });
632
+ if (startsWith(data, ROUTE_V2)) {
633
+ if (ix.accountIndices.length < 10 || !planWellFormed(30)) return;
634
+ const optionalDest = accountAt(7);
635
+ return withinSlippage({
636
+ authority: accountAt(0),
637
+ sourceMint: accountAt(3),
638
+ destinationMint: accountAt(4),
639
+ destinationTokenProgram: accountAt(6),
640
+ destinationAccount: optionalDest === "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4" ? accountAt(2) : optionalDest,
641
+ ...scalars(8)
642
+ });
643
+ }
644
+ if (startsWith(data, SHARED_ROUTE_V2)) {
645
+ if (ix.accountIndices.length < 12 || !planWellFormed(31)) return;
646
+ return withinSlippage({
647
+ authority: accountAt(1),
648
+ sourceMint: accountAt(6),
649
+ destinationMint: accountAt(7),
650
+ destinationTokenProgram: accountAt(9),
651
+ destinationAccount: accountAt(5),
652
+ ...scalars(9)
653
+ });
654
+ }
655
+ };
656
+ // src/solana/stake.ts
657
+ /** Sysvars the legacy stake account lists carry; post-SIMD-0490 forms omit them. */
658
+ const SYSVAR_IDS = Object.freeze({
659
+ clock: "SysvarC1ock11111111111111111111111111111111",
660
+ rent: "SysvarRent111111111111111111111111111111111",
661
+ stakeHistory: "SysvarStakeHistory1111111111111111111111111",
662
+ stakeConfig: "StakeConfig11111111111111111111111111111111"
663
+ });
664
+ const ZERO_ADDRESS = "11111111111111111111111111111111";
665
+ const u32$1 = getU32Decoder();
666
+ const u64$2 = getU64Decoder();
667
+ const i64 = getI64Decoder();
668
+ const addr = getAddressEncoder();
669
+ const u32le$1 = (data, offset) => u32$1.decode(data, offset);
670
+ const u64le$1 = (data, offset) => u64$2.decode(data, offset);
671
+ const i64le = (data, offset) => i64.decode(data, offset);
672
+ const key = (data, offset) => solanaAddressFromPublicKey(data.subarray(offset, offset + 32));
673
+ const account = (ix, index, named) => index < ix.accountIndices.length ? named.at(ix.accountIndices[index] ?? 0) : void 0;
674
+ const role = (data, offset) => {
675
+ const r = u32le$1(data, offset);
676
+ return r === 0 ? "staker" : r === 1 ? "withdrawer" : "other";
677
+ };
678
+ /**
679
+ * Decodes one Stake-program instruction — exact data length and an account list matching either the
680
+ * current form or the legacy one (recognised by the sysvar it inserts at a fixed position).
681
+ * Anything else is `undefined` → the whole transaction decodes `opaque`.
682
+ */
683
+ const decodeStakeIx = (ix, named) => {
684
+ const { data } = ix;
685
+ const acc = (i) => account(ix, i, named);
686
+ const n = ix.accountIndices.length;
687
+ if (data.length < 4) return;
688
+ switch (u32le$1(data, 0)) {
689
+ case 0: {
690
+ if (data.length !== 116 || !(n === 1 || n === 2 && acc(1) === SYSVAR_IDS.rent)) return;
691
+ const stake = acc(0);
692
+ if (stake === void 0) return;
693
+ const lockup = i64le(data, 68) !== 0n || u64le$1(data, 76) !== 0n || key(data, 84) !== ZERO_ADDRESS;
694
+ return {
695
+ kind: "initialize",
696
+ account: stake,
697
+ staker: key(data, 4),
698
+ withdrawer: key(data, 36),
699
+ lockup
700
+ };
701
+ }
702
+ case 1: {
703
+ if (data.length !== 40) return;
704
+ const r = role(data, 36);
705
+ const legacy = acc(1) === SYSVAR_IDS.clock;
706
+ const stake = acc(0);
707
+ if (r === "other" || stake === void 0 || !(legacy ? n === 3 || n === 4 : n === 2 || n === 3)) return;
708
+ return {
709
+ kind: "authorize",
710
+ account: stake,
711
+ role: r,
712
+ authority: key(data, 4)
713
+ };
714
+ }
715
+ case 2: {
716
+ const legacy = acc(2) === SYSVAR_IDS.clock;
717
+ const stake = acc(0);
718
+ const vote = acc(1);
719
+ if (data.length !== 4 || stake === void 0 || vote === void 0 || !(n === 3 || n === 6 && legacy && acc(3) === SYSVAR_IDS.stakeHistory && acc(4) === SYSVAR_IDS.stakeConfig)) return;
720
+ return {
721
+ kind: "delegate",
722
+ account: stake,
723
+ validator: vote
724
+ };
725
+ }
726
+ case 3: {
727
+ const stake = acc(0);
728
+ const to = acc(1);
729
+ const staker = acc(2);
730
+ if (data.length !== 12 || stake === void 0 || to === void 0 || staker === void 0 || n !== 3) return;
731
+ return {
732
+ kind: "split",
733
+ account: stake,
734
+ to,
735
+ staker,
736
+ lamports: u64le$1(data, 4)
737
+ };
738
+ }
739
+ case 4: {
740
+ const legacy = acc(2) === SYSVAR_IDS.clock && acc(3) === SYSVAR_IDS.stakeHistory;
741
+ const stake = acc(0);
742
+ const to = acc(1);
743
+ if (data.length !== 12 || stake === void 0 || to === void 0 || !(n === 3 || n === 4 || (n === 5 || n === 6) && legacy)) return;
744
+ return {
745
+ kind: "withdraw",
746
+ account: stake,
747
+ to,
748
+ lamports: u64le$1(data, 4)
749
+ };
750
+ }
751
+ case 5: {
752
+ const stake = acc(0);
753
+ if (data.length !== 4 || stake === void 0 || !(n === 2 || n === 3 && acc(1) === SYSVAR_IDS.clock)) return;
754
+ return {
755
+ kind: "deactivate",
756
+ account: stake
757
+ };
758
+ }
759
+ case 9: {
760
+ const legacy = acc(1) === SYSVAR_IDS.rent;
761
+ const stake = acc(0);
762
+ const staker = acc(legacy ? 2 : 1);
763
+ const withdrawer = acc(legacy ? 3 : 2);
764
+ if (data.length !== 4 || stake === void 0 || staker === void 0 || withdrawer === void 0 || !(n === 3 || n === 4 && legacy)) return;
765
+ return {
766
+ kind: "initialize-checked",
767
+ account: stake,
768
+ staker,
769
+ withdrawer
770
+ };
771
+ }
772
+ case 10: {
773
+ if (data.length !== 8) return;
774
+ const r = role(data, 4);
775
+ const legacy = acc(1) === SYSVAR_IDS.clock;
776
+ const stake = acc(0);
777
+ const authority = acc(legacy ? 3 : 2);
778
+ if (r === "other" || stake === void 0 || authority === void 0 || !(legacy ? n === 4 || n === 5 : n === 3 || n === 4)) return;
779
+ return {
780
+ kind: "authorize",
781
+ account: stake,
782
+ role: r,
783
+ authority
784
+ };
785
+ }
786
+ default: return;
787
+ }
788
+ };
789
+ /**
790
+ * One system-program account creation (CreateAccount or CreateAccountWithSeed), for any new-owner
791
+ * program. A with-seed creation whose `account` does not match sha256(base ‖ seed ‖ owner) is
792
+ * `"other"` — the program itself would reject it. `undefined` when the data is neither instruction;
793
+ * `"other"` when it is one but malformed.
794
+ */
795
+ const decodeSystemCreate = (ix, named) => {
796
+ const { data } = ix;
797
+ const acc = (i) => account(ix, i, named);
798
+ if (data.length < 4) return "other";
799
+ if (u32le$1(data, 0) === 0) {
800
+ if (data.length !== 52 || ix.accountIndices.length !== 2) return "other";
801
+ const from = acc(0);
802
+ const to = acc(1);
803
+ if (from === void 0 || to === void 0) return "other";
804
+ return {
805
+ account: to,
806
+ from,
807
+ lamports: u64le$1(data, 4),
808
+ owner: key(data, 20)
809
+ };
810
+ }
811
+ if (u32le$1(data, 0) === 3) {
812
+ const seedLen = data.length >= 44 ? Number(u64le$1(data, 36)) : -1;
813
+ const end = 92 + seedLen;
814
+ if (seedLen < 0 || seedLen > 32 || data.length !== end) return "other";
815
+ const from = acc(0);
816
+ const to = acc(1);
817
+ if (from === void 0 || to === void 0 || !(ix.accountIndices.length === 2 || ix.accountIndices.length === 3)) return "other";
818
+ const base = key(data, 4);
819
+ const owner = key(data, end - 32);
820
+ if (ix.accountIndices.length === 3 && acc(2) !== base) return "other";
821
+ if (ix.accountIndices.length === 2 && base !== from) return "other";
822
+ const seed = data.subarray(44, 44 + seedLen);
823
+ return to === solanaAddressFromPublicKey(sha256(new Uint8Array([
824
+ ...addr.encode(address(base)),
825
+ ...seed,
826
+ ...addr.encode(address(owner))
827
+ ]))) ? {
828
+ account: to,
829
+ from,
830
+ lamports: u64le$1(data, 44 + seedLen),
831
+ owner
832
+ } : "other";
833
+ }
834
+ };
119
835
  // src/solana/tokens.ts
120
836
  const USDC = Object.freeze({
121
837
  symbol: "USDC",
@@ -141,15 +857,30 @@ const u64le = (data, offset) => u64$1.decode(data, offset);
141
857
  * Classifies one instruction by program + discriminator/layout. Exact-length checks keep
142
858
  * non-canonical encodings out of the `full` class.
143
859
  */
144
- const classifyIx = (parsed, ix, lookups) => {
145
- const program = resolveAccountIndex(parsed, ix.programAddressIndex, lookups);
860
+ const classifyIx = (ix, named, signer) => {
861
+ const program = named.at(ix.programAddressIndex);
146
862
  const { data } = ix;
147
863
  if (program === PROGRAM_IDS.system) {
148
864
  if (data.length === 12 && u32le(data, 0) === 2) return {
149
865
  kind: "system-transfer",
150
- to: resolveAccountIndex(parsed, ix.accountIndices[1] ?? 0, lookups),
866
+ from: named.at(ix.accountIndices[0] ?? 0),
867
+ to: named.at(ix.accountIndices[1] ?? 0),
151
868
  amount: u64le(data, 4)
152
869
  };
870
+ const create = decodeSystemCreate(ix, named);
871
+ if (create !== void 0 && create !== "other") return create.owner === PROGRAM_IDS.stake ? {
872
+ kind: "system-create",
873
+ stake: {
874
+ kind: "create",
875
+ account: create.account,
876
+ from: create.from,
877
+ lamports: create.lamports
878
+ }
879
+ } : {
880
+ kind: "system-create-account",
881
+ create
882
+ };
883
+ if (data.length === 4 && u32le(data, 0) === 4 && ix.accountIndices.length === 3) return { kind: "system-advance-nonce" };
153
884
  return { kind: "other" };
154
885
  }
155
886
  if (program === PROGRAM_IDS.computeBudget) {
@@ -173,42 +904,125 @@ const classifyIx = (parsed, ix, lookups) => {
173
904
  kind: "token-transfer-checked",
174
905
  amount: u64le(data, 1),
175
906
  decimals: data[9],
176
- to: resolveAccountIndex(parsed, ix.accountIndices[2] ?? 0, lookups),
177
- mint: resolveAccountIndex(parsed, ix.accountIndices[1] ?? 0, lookups)
907
+ to: named.at(ix.accountIndices[2] ?? 0),
908
+ mint: named.at(ix.accountIndices[1] ?? 0)
178
909
  };
910
+ if (data.length === 9 && data[0] === 3 && ix.accountIndices.length === 3) return named.at(ix.accountIndices[2] ?? 0) === signer ? {
911
+ kind: "token-transfer",
912
+ amount: u64le(data, 1),
913
+ tokenAccount: named.at(ix.accountIndices[0] ?? 0),
914
+ to: named.at(ix.accountIndices[1] ?? 0)
915
+ } : { kind: "other" };
916
+ if ((data.length === 9 && data[0] === 8 || data.length === 10 && data[0] === 15) && ix.accountIndices.length === 3) return named.at(ix.accountIndices[2] ?? 0) === signer ? {
917
+ kind: "token-burn",
918
+ amount: u64le(data, 1),
919
+ mint: named.at(ix.accountIndices[1] ?? 0),
920
+ to: named.at(ix.accountIndices[0] ?? 0)
921
+ } : { kind: "other" };
179
922
  if (data.length === 9 && data[0] === 4) return {
180
923
  kind: "token-approve",
181
924
  amount: u64le(data, 1),
182
- to: resolveAccountIndex(parsed, ix.accountIndices[1] ?? 0, lookups)
925
+ to: named.at(ix.accountIndices[1] ?? 0)
926
+ };
927
+ if (data.length === 1 && data[0] === 17) return { kind: "token-sync-native" };
928
+ if (data.length === 1 && data[0] === 9) return named.at(ix.accountIndices[1] ?? 0) === signer ? { kind: "token-close-account" } : { kind: "other" };
929
+ if (data.length === 1 && data[0] === 1 && ix.accountIndices.length === 4) return { kind: "token-initialize-account" };
930
+ if (data.length === 1 && data[0] === 5 && ix.accountIndices.length === 2) return {
931
+ kind: "token-revoke",
932
+ tokenAccount: named.at(ix.accountIndices[0] ?? 0)
183
933
  };
184
934
  return { kind: "other" };
185
935
  }
186
- if (program === PROGRAM_IDS.stake) return { kind: "stake" };
936
+ if (program === "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4") {
937
+ const jupiter = decodeJupiterIx(ix, named);
938
+ return jupiter !== void 0 && jupiter.authority === signer ? {
939
+ kind: "jupiter",
940
+ jupiter
941
+ } : { kind: "other" };
942
+ }
943
+ if (program === "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8" || program === "CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK" || program === "CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C" || program === "whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc" || program === "LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo") {
944
+ const amm = decodeAmmSwapIx(ix, named, program);
945
+ return amm !== void 0 && amm.authority === signer ? {
946
+ kind: "amm-swap",
947
+ amm
948
+ } : { kind: "other" };
949
+ }
950
+ if (program === "CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d") {
951
+ const core = decodeCoreIx(ix, named);
952
+ return core !== void 0 && core.authority === signer ? {
953
+ kind: "core-transfer",
954
+ core
955
+ } : { kind: "other" };
956
+ }
957
+ if (program === PROGRAM_IDS.stake) {
958
+ const stake = decodeStakeIx(ix, named);
959
+ return stake === void 0 || stake.kind === "split" && stake.staker !== signer ? { kind: "other" } : {
960
+ kind: "stake",
961
+ stake
962
+ };
963
+ }
187
964
  return { kind: "other" };
188
965
  };
189
966
  const FULL_KINDS = /* @__PURE__ */ new Set([
190
967
  "system-transfer",
968
+ "system-create",
969
+ "system-create-account",
970
+ "system-advance-nonce",
191
971
  "cu-limit",
192
972
  "cu-price",
193
973
  "ata-create",
194
- "ata-create-idempotent"
974
+ "ata-create-idempotent",
975
+ "stake",
976
+ "token-sync-native",
977
+ "token-close-account",
978
+ "token-initialize-account",
979
+ "token-revoke"
195
980
  ]);
196
981
  const classify = (parsed, calls, network, table) => {
197
- if (parsed.lookups.length > 0) return "partial";
198
982
  let allFull = true;
199
983
  for (const call of calls) {
200
984
  if (call.kind === "other") return "opaque";
201
- if (call.kind === "token-approve" || call.kind === "stake") allFull = false;
985
+ if (call.kind === "token-approve" || call.kind === "jupiter" || call.kind === "amm-swap" || call.kind === "core-transfer" || call.kind === "token-transfer" || call.kind === "token-burn") allFull = false;
202
986
  if (call.kind === "token-transfer-checked" && lookupSolanaToken(table, network, call.mint ?? "") === void 0) allFull = false;
203
- if (!FULL_KINDS.has(call.kind) && call.kind !== "token-transfer-checked" && call.kind !== "token-approve" && call.kind !== "stake") return "opaque";
987
+ if (!FULL_KINDS.has(call.kind) && call.kind !== "token-transfer-checked" && call.kind !== "token-transfer" && call.kind !== "token-burn" && call.kind !== "token-approve" && call.kind !== "jupiter" && call.kind !== "amm-swap" && call.kind !== "core-transfer") return "opaque";
204
988
  }
989
+ if (parsed.lookups.length > 0) return "partial";
205
990
  return allFull ? "full" : "partial";
206
991
  };
207
- const feeLamports = (parsed, calls) => {
208
- const limit = calls.find((c) => c.kind === "cu-limit")?.units;
209
- const price = calls.find((c) => c.kind === "cu-price")?.microLamports;
210
- if (limit === void 0 || price === void 0) return;
211
- return 5000n * BigInt(parsed.numRequiredSignatures) + (price * BigInt(limit) + 999999n) / 1000000n;
992
+ /** The most writable accounts a signer-taking unknown program may reach before it is blind. */
993
+ const PROGRAM_AUTHORITY_ACCOUNTS = 12;
994
+ const DEFAULT_INSTRUCTION_CU = 200000n;
995
+ const BUILTIN_INSTRUCTION_CU = 3000n;
996
+ const MAX_COMPUTE_UNIT_LIMIT = 1400000n;
997
+ const PRECOMPILE_PROGRAMS = /* @__PURE__ */ new Set([
998
+ PROGRAM_IDS.ed25519,
999
+ PROGRAM_IDS.secp256k1,
1000
+ PROGRAM_IDS.secp256r1
1001
+ ]);
1002
+ const signatureCount = (parsed, lookups) => {
1003
+ let count = parsed.numRequiredSignatures;
1004
+ for (const ix of parsed.instructions) {
1005
+ const program = resolveAccountIndex(parsed, ix.programAddressIndex, lookups);
1006
+ if (program !== void 0 && PRECOMPILE_PROGRAMS.has(program)) count += ix.data[0] ?? 0;
1007
+ }
1008
+ return count;
1009
+ };
1010
+ const feeLamports = (parsed, lookups, calls) => {
1011
+ const signatures = BigInt(signatureCount(parsed, lookups));
1012
+ if (parsed.version === 1) return 5000n * signatures + (parsed.config.priorityFeeLamports ?? 0n);
1013
+ const price = calls.find((c) => c.kind === "cu-price")?.microLamports ?? 0n;
1014
+ let builtin = 0n;
1015
+ let other = 0n;
1016
+ for (const ix of parsed.instructions) {
1017
+ const program = resolveAccountIndex(parsed, ix.programAddressIndex, lookups);
1018
+ if (program === PROGRAM_IDS.system || program === PROGRAM_IDS.computeBudget) builtin += 1n;
1019
+ else other += 1n;
1020
+ }
1021
+ const defaultLimit = BUILTIN_INSTRUCTION_CU * builtin + DEFAULT_INSTRUCTION_CU * other;
1022
+ const requested = calls.find((c) => c.kind === "cu-limit")?.units;
1023
+ const limit = requested === void 0 ? defaultLimit : BigInt(requested);
1024
+ const capped = limit < MAX_COMPUTE_UNIT_LIMIT ? limit : MAX_COMPUTE_UNIT_LIMIT;
1025
+ return 5000n * signatures + (price * capped + 999999n) / 1000000n;
212
1026
  };
213
1027
  const decodeTransaction = (request, signer, table) => {
214
1028
  const parsed = parseCompiledMessage(request.message);
@@ -216,14 +1030,28 @@ const decodeTransaction = (request, signer, table) => {
216
1030
  address: t.address,
217
1031
  addresses: t.addresses
218
1032
  }));
219
- const calls = parsed.instructions.map((ix) => classifyIx(parsed, ix, lookups));
220
- const verifiability = classify(parsed, calls, request.network, table);
1033
+ const named = namedAccounts(parsed, lookups);
1034
+ const calls = parsed.instructions.map((ix) => classifyIx(ix, named, signer));
1035
+ if (calls.filter((c) => c.kind === "cu-limit").length > 1 || calls.filter((c) => c.kind === "cu-price").length > 1) throw new ChainError("invalid-request", "duplicate compute-budget instruction");
1036
+ const verifiability = named.fromTable.size === 0 ? classify(parsed, calls, request.network, table) : "opaque";
221
1037
  const outflows = [];
222
1038
  const approvals = [];
1039
+ const staking = [];
1040
+ const warnings = [];
1041
+ const minimumReceived = [];
1042
+ const wsolAtas = /* @__PURE__ */ new Set([associatedTokenAddress(signer, WSOL_MINT, PROGRAM_IDS.token), associatedTokenAddress(signer, WSOL_MINT, PROGRAM_IDS.token2022)]);
1043
+ const atas = /* @__PURE__ */ new Map();
1044
+ for (const [mint, token] of Object.entries(table[request.network] ?? {})) for (const tokenProgram of [PROGRAM_IDS.token, PROGRAM_IDS.token2022]) atas.set(associatedTokenAddress(signer, mint, tokenProgram), {
1045
+ mint,
1046
+ symbol: token.symbol
1047
+ });
1048
+ let otherTransfer = false;
1049
+ let hasSwap = false;
223
1050
  for (const call of calls) {
224
- if (call.kind === "system-transfer" && call.to !== void 0) {
225
- const [source] = parsed.staticAccounts;
226
- if (source === signer) outflows.push({
1051
+ if (call.kind === "system-transfer" && call.to !== void 0 && call.from === signer) {
1052
+ if (wsolAtas.has(call.to)) continue;
1053
+ otherTransfer = true;
1054
+ outflows.push({
227
1055
  to: call.to,
228
1056
  amount: {
229
1057
  asset: SOLANA_NATIVE_ASSET,
@@ -232,7 +1060,126 @@ const decodeTransaction = (request, signer, table) => {
232
1060
  verified: true
233
1061
  });
234
1062
  }
1063
+ if (call.kind === "system-create-account" && call.create !== void 0 && call.create.from === signer) {
1064
+ otherTransfer = true;
1065
+ outflows.push({
1066
+ to: call.create.account,
1067
+ amount: {
1068
+ asset: SOLANA_NATIVE_ASSET,
1069
+ amount: call.create.lamports.toString(10)
1070
+ },
1071
+ verified: true
1072
+ });
1073
+ }
1074
+ if (call.kind === "jupiter" && call.jupiter !== void 0) {
1075
+ const swap = call.jupiter;
1076
+ hasSwap = true;
1077
+ otherTransfer = true;
1078
+ const sell = lookupSolanaToken(table, request.network, swap.sourceMint);
1079
+ outflows.push({
1080
+ to: JUPITER_PROGRAM,
1081
+ amount: swap.sourceMint === "So11111111111111111111111111111111111111112" ? {
1082
+ asset: SOLANA_NATIVE_ASSET,
1083
+ amount: swap.inAmount.toString(10)
1084
+ } : {
1085
+ asset: swap.sourceMint,
1086
+ amount: swap.inAmount.toString(10),
1087
+ ...sell === void 0 ? {} : {
1088
+ symbol: sell.symbol,
1089
+ decimals: sell.decimals
1090
+ }
1091
+ },
1092
+ verified: swap.sourceMint === "So11111111111111111111111111111111111111112" || sell !== void 0
1093
+ });
1094
+ const minimum = (swap.quotedOut * BigInt(1e4 - swap.slippageBps) + 9999n) / 10000n;
1095
+ const buy = lookupSolanaToken(table, request.network, swap.destinationMint);
1096
+ const destAta = swap.destinationMint === "" || swap.destinationTokenProgram === "" ? void 0 : associatedTokenAddress(signer, swap.destinationMint, swap.destinationTokenProgram);
1097
+ minimumReceived.push({
1098
+ to: swap.destinationAccount === "" ? "" : swap.destinationAccount === destAta ? signer : swap.destinationAccount,
1099
+ amount: swap.destinationMint === "So11111111111111111111111111111111111111112" ? {
1100
+ asset: SOLANA_NATIVE_ASSET,
1101
+ amount: minimum.toString(10)
1102
+ } : {
1103
+ asset: swap.destinationMint,
1104
+ amount: minimum.toString(10),
1105
+ ...buy === void 0 ? {} : {
1106
+ symbol: buy.symbol,
1107
+ decimals: buy.decimals
1108
+ }
1109
+ }
1110
+ });
1111
+ if (swap.platformFeeBps > 0) warnings.push({
1112
+ kind: "platform-fee",
1113
+ message: `${swap.platformFeeBps} bps`
1114
+ });
1115
+ }
1116
+ if (call.kind === "amm-swap" && call.amm !== void 0) {
1117
+ const swap = call.amm;
1118
+ hasSwap = true;
1119
+ otherTransfer = true;
1120
+ const sourceMint = swap.sourceMint ?? atas.get(swap.sourceAccount)?.mint;
1121
+ const sourceToken = sourceMint === void 0 ? void 0 : lookupSolanaToken(table, request.network, sourceMint);
1122
+ outflows.push(sourceMint === void 0 ? {
1123
+ to: swap.program,
1124
+ amount: {
1125
+ asset: swap.sourceAccount,
1126
+ amount: swap.maxIn.toString(10)
1127
+ },
1128
+ verified: false,
1129
+ tokenAccount: swap.sourceAccount
1130
+ } : {
1131
+ to: swap.program,
1132
+ amount: sourceMint === "So11111111111111111111111111111111111111112" ? {
1133
+ asset: SOLANA_NATIVE_ASSET,
1134
+ amount: swap.maxIn.toString(10)
1135
+ } : {
1136
+ asset: sourceMint,
1137
+ amount: swap.maxIn.toString(10),
1138
+ ...sourceToken === void 0 ? {} : {
1139
+ symbol: sourceToken.symbol,
1140
+ decimals: sourceToken.decimals
1141
+ }
1142
+ },
1143
+ verified: sourceMint === "So11111111111111111111111111111111111111112" || sourceToken !== void 0
1144
+ });
1145
+ const destinationMint = swap.destinationMint ?? atas.get(swap.destinationAccount)?.mint;
1146
+ const destinationToken = destinationMint === void 0 ? void 0 : lookupSolanaToken(table, request.network, destinationMint);
1147
+ const ownAta = (swap.destinationTokenProgram === void 0 ? [PROGRAM_IDS.token, PROGRAM_IDS.token2022].map((p) => destinationMint === void 0 ? "" : associatedTokenAddress(signer, destinationMint, p)) : [destinationMint === void 0 ? "" : associatedTokenAddress(signer, destinationMint, swap.destinationTokenProgram)]).includes(swap.destinationAccount);
1148
+ minimumReceived.push(destinationMint === void 0 ? {
1149
+ to: swap.destinationAccount,
1150
+ amount: {
1151
+ asset: swap.destinationAccount,
1152
+ amount: swap.minOut.toString(10)
1153
+ },
1154
+ tokenAccount: swap.destinationAccount
1155
+ } : {
1156
+ to: ownAta ? signer : swap.destinationAccount,
1157
+ amount: destinationMint === "So11111111111111111111111111111111111111112" ? {
1158
+ asset: SOLANA_NATIVE_ASSET,
1159
+ amount: swap.minOut.toString(10)
1160
+ } : {
1161
+ asset: destinationMint,
1162
+ amount: swap.minOut.toString(10),
1163
+ ...destinationToken === void 0 ? {} : {
1164
+ symbol: destinationToken.symbol,
1165
+ decimals: destinationToken.decimals
1166
+ }
1167
+ }
1168
+ });
1169
+ }
1170
+ if (call.kind === "core-transfer" && call.core !== void 0) {
1171
+ otherTransfer = true;
1172
+ outflows.push({
1173
+ to: call.core.newOwner,
1174
+ amount: {
1175
+ asset: call.core.asset,
1176
+ amount: "1"
1177
+ },
1178
+ verified: false
1179
+ });
1180
+ }
235
1181
  if (call.kind === "token-transfer-checked") {
1182
+ otherTransfer = true;
236
1183
  const token = lookupSolanaToken(table, request.network, call.mint ?? "");
237
1184
  outflows.push({
238
1185
  to: call.to ?? "",
@@ -247,6 +1194,40 @@ const decodeTransaction = (request, signer, table) => {
247
1194
  verified: token !== void 0
248
1195
  });
249
1196
  }
1197
+ if (call.kind === "token-transfer" && call.tokenAccount !== void 0) {
1198
+ otherTransfer = true;
1199
+ outflows.push({
1200
+ to: call.to ?? "",
1201
+ amount: {
1202
+ asset: call.tokenAccount,
1203
+ amount: (call.amount ?? 0n).toString(10)
1204
+ },
1205
+ verified: false,
1206
+ tokenAccount: call.tokenAccount
1207
+ });
1208
+ }
1209
+ if (call.kind === "token-burn") {
1210
+ otherTransfer = true;
1211
+ const token = lookupSolanaToken(table, request.network, call.mint ?? "");
1212
+ outflows.push({
1213
+ to: call.to ?? "",
1214
+ amount: {
1215
+ asset: call.mint ?? "",
1216
+ amount: (call.amount ?? 0n).toString(10),
1217
+ ...token === void 0 ? {} : {
1218
+ symbol: token.symbol,
1219
+ decimals: token.decimals
1220
+ }
1221
+ },
1222
+ verified: token !== void 0,
1223
+ burn: true
1224
+ });
1225
+ }
1226
+ if (call.kind === "token-revoke" && call.tokenAccount !== void 0) approvals.push({
1227
+ kind: "delegate-revoked",
1228
+ account: call.tokenAccount,
1229
+ verified: false
1230
+ });
250
1231
  if (call.kind === "token-approve") {
251
1232
  const token = lookupSolanaToken(table, request.network, call.mint ?? "");
252
1233
  approvals.push({
@@ -265,25 +1246,165 @@ const decodeTransaction = (request, signer, table) => {
265
1246
  verified: token !== void 0
266
1247
  });
267
1248
  }
1249
+ const { stake } = call;
1250
+ if (stake !== void 0) switch (stake.kind) {
1251
+ case "create":
1252
+ staking.push({
1253
+ kind: "create",
1254
+ account: stake.account,
1255
+ lamports: stake.lamports.toString(10)
1256
+ });
1257
+ if (stake.from === signer) outflows.push({
1258
+ to: stake.account,
1259
+ amount: {
1260
+ asset: SOLANA_NATIVE_ASSET,
1261
+ amount: stake.lamports.toString(10)
1262
+ },
1263
+ verified: true
1264
+ });
1265
+ break;
1266
+ case "initialize":
1267
+ case "initialize-checked":
1268
+ if (stake.staker !== signer) approvals.push({
1269
+ kind: "stake-authority",
1270
+ account: stake.account,
1271
+ role: "staker",
1272
+ authority: stake.staker,
1273
+ verified: false
1274
+ });
1275
+ if (stake.withdrawer !== signer) approvals.push({
1276
+ kind: "stake-authority",
1277
+ account: stake.account,
1278
+ role: "withdrawer",
1279
+ authority: stake.withdrawer,
1280
+ verified: false
1281
+ });
1282
+ if (stake.kind === "initialize" && stake.lockup) warnings.push({
1283
+ kind: "stake-lockup",
1284
+ message: "the stake account is locked — funds cannot move until the lockup expires"
1285
+ });
1286
+ break;
1287
+ case "delegate":
1288
+ staking.push({
1289
+ kind: "delegate",
1290
+ account: stake.account,
1291
+ validator: stake.validator
1292
+ });
1293
+ break;
1294
+ case "deactivate":
1295
+ staking.push({
1296
+ kind: "deactivate",
1297
+ account: stake.account
1298
+ });
1299
+ break;
1300
+ case "withdraw":
1301
+ staking.push({
1302
+ kind: "withdraw",
1303
+ account: stake.account,
1304
+ to: stake.to,
1305
+ lamports: stake.lamports.toString(10)
1306
+ });
1307
+ if (stake.to !== signer) outflows.push({
1308
+ to: stake.to,
1309
+ amount: {
1310
+ asset: SOLANA_NATIVE_ASSET,
1311
+ amount: stake.lamports.toString(10)
1312
+ },
1313
+ verified: true
1314
+ });
1315
+ break;
1316
+ case "split":
1317
+ staking.push({
1318
+ kind: "split",
1319
+ account: stake.account,
1320
+ to: stake.to,
1321
+ lamports: stake.lamports.toString(10)
1322
+ });
1323
+ break;
1324
+ case "authorize": if (stake.authority !== signer) approvals.push({
1325
+ kind: "stake-authority",
1326
+ account: stake.account,
1327
+ role: stake.role,
1328
+ authority: stake.authority,
1329
+ verified: false
1330
+ });
1331
+ }
1332
+ }
1333
+ const fee = parsed.staticAccounts[0] === signer ? feeLamports(parsed, lookups, calls) : 0n;
1334
+ const programs = [];
1335
+ const authority = /* @__PURE__ */ new Map();
1336
+ const signerIndex = parsed.staticAccounts.indexOf(signer);
1337
+ const signerWritable = signerIndex !== -1 && isWritableIndex(parsed, signerIndex);
1338
+ const altWritable = parsed.lookups.some((l) => l.writableIndexes.length > 0);
1339
+ let blind;
1340
+ for (const [index, ix] of parsed.instructions.entries()) {
1341
+ const program = calls[index]?.kind === "other" ? resolveAccountIndex(parsed, ix.programAddressIndex, lookups) : void 0;
1342
+ if (program === void 0) continue;
1343
+ if (!programs.includes(program)) programs.push(program);
1344
+ const row = RECOGNISED_FAMILIES.find((family) => family.claim.surface === "solana.instruction" && PROGRAM_IDS[family.claim.program] === program);
1345
+ const takesSigner = ix.accountIndices.some((i) => i < parsed.numRequiredSignatures && parsed.staticAccounts[i] === signer);
1346
+ if (row !== void 0) {
1347
+ if (blind === void 0 && takesSigner) blind = row.nearMiss;
1348
+ continue;
1349
+ }
1350
+ if (!takesSigner) continue;
1351
+ const entry = authority.get(program) ?? {
1352
+ sol: signerWritable,
1353
+ accounts: []
1354
+ };
1355
+ for (const i of ix.accountIndices) {
1356
+ const account = resolveAccountIndex(parsed, i, lookups);
1357
+ if (account === void 0 || account === signer || !isWritableIndex(parsed, i) || entry.accounts.includes(account)) continue;
1358
+ entry.accounts.push(account);
1359
+ }
1360
+ authority.set(program, entry);
1361
+ if (blind === void 0 && (altWritable || entry.accounts.length > 12)) blind = "program-authority";
268
1362
  }
269
- const fee = feeLamports(parsed, calls);
1363
+ if (named.fromTable.size > 0) blind = "lookup-table-account";
1364
+ const summary = hasSwap ? "swap" : otherTransfer ? "transfer" : staking.length > 0 ? "stake" : outflows.length > 0 ? "transfer" : "contract-call";
1365
+ const programAuthority = [...authority.entries()].map(([program, entry]) => ({
1366
+ program,
1367
+ sol: entry.sol,
1368
+ accounts: entry.accounts.map((a) => {
1369
+ const token = atas.get(a);
1370
+ return token === void 0 ? { address: a } : {
1371
+ address: a,
1372
+ token
1373
+ };
1374
+ })
1375
+ }));
270
1376
  return {
271
1377
  family: "solana",
272
1378
  network: request.network,
273
- summary: outflows.length > 0 ? "transfer" : "contract-call",
1379
+ summary,
274
1380
  outflows,
275
1381
  approvals,
276
- fee: fee === void 0 ? void 0 : { max: {
1382
+ fee: { max: {
277
1383
  asset: SOLANA_NATIVE_ASSET,
278
1384
  amount: fee.toString(10)
279
1385
  } },
280
- warnings: [],
281
- verifiability
1386
+ ...staking.length === 0 ? {} : { staking },
1387
+ ...minimumReceived.length === 0 ? {} : { minimumReceived },
1388
+ ...programs.length === 0 ? {} : { programs },
1389
+ ...programAuthority.length === 0 ? {} : { programAuthority },
1390
+ warnings,
1391
+ verifiability,
1392
+ ...blind === void 0 ? {} : { blind }
282
1393
  };
283
1394
  };
1395
+ /**
1396
+ * The resolved addresses a decoded intent would name to the user that come from caller-supplied
1397
+ * lookup-table contents — the set a transaction builder must keep as static keys so the intent it
1398
+ * signs is verifiable.
1399
+ */
1400
+ const namedTableAccounts = (parsed, lookups) => {
1401
+ const named = namedAccounts(parsed, lookups);
1402
+ for (const ix of parsed.instructions) classifyIx(ix, named, "");
1403
+ return [...named.fromTable];
1404
+ };
284
1405
  /** What the signer can guarantee about this request (WD-065). */
285
1406
  const verifiabilitySolana = (request, signer, table = solanaTokenTable) => decodeSolana(request, signer, table).verifiability;
286
- const decodeSolana = (request, signer, table = solanaTokenTable) => decodeTransaction(request, signer, table);
1407
+ const decodeSolana = (request, signer, table = solanaTokenTable) => withNativeAmountMeta(decodeTransaction(request, signer, table), request.network, SOLANA_NATIVE_ASSET, 9);
287
1408
  // src/solana/token2022.ts
288
1409
  /** SPL Token-2022 mint-account TLV extension type numbers (spl-token-2022 `ExtensionType`). */
289
1410
  const EXT = {
@@ -374,6 +1495,69 @@ const verifySolanaSignature = (message, signature, accountAddress) => {
374
1495
  return false;
375
1496
  }
376
1497
  };
1498
+ // src/solana/transaction.ts
1499
+ const transactionDecoder = getTransactionDecoder();
1500
+ /**
1501
+ * The structural half of `decodeSolanaTransaction`: the wire decodes, the message is a supported
1502
+ * compiled message, the whole transaction fits the version's size limit (a v1 wire puts the message
1503
+ * first and is 4096; legacy/v0 put a signature-count shortvec first and are 1232) and the signature
1504
+ * count agrees with the message header. Yields the message, its parse, and the signature slots in
1505
+ * wire order — the slot byte offset follows from them.
1506
+ */
1507
+ const readTransaction = (wire) => {
1508
+ const limit = wire[0] === 129 ? V1_TRANSACTION_MAX_BYTES : LEGACY_TRANSACTION_MAX_BYTES;
1509
+ if (wire.length > limit) throw new ChainError("invalid-request", `transaction exceeds the ${limit}-byte limit`);
1510
+ let tx;
1511
+ try {
1512
+ tx = transactionDecoder.decode(wire);
1513
+ } catch {
1514
+ throw new ChainError("invalid-request", "transaction failed to decode");
1515
+ }
1516
+ const message = new Uint8Array(tx.messageBytes);
1517
+ const parsed = parseCompiledMessage(message);
1518
+ const slots = Object.entries(tx.signatures).map(([account, signature]) => [account, signature ?? void 0]);
1519
+ if (slots.length !== parsed.numRequiredSignatures) throw new ChainError("invalid-request", `transaction carries ${slots.length} signatures for ${parsed.numRequiredSignatures} required signers`);
1520
+ return {
1521
+ message,
1522
+ parsed,
1523
+ slots
1524
+ };
1525
+ };
1526
+ /**
1527
+ * Reads the serialized transaction `wire` as the Wallet Standard passes it for `signer`: the
1528
+ * compiled message the signer signs, and which other required signers have not signed yet. Every
1529
+ * non-empty signature slot must verify over the message — one that cannot land is refused.
1530
+ */
1531
+ const decodeSolanaTransaction = (wire, signer) => {
1532
+ const { message, slots } = readTransaction(wire);
1533
+ if (!slots.some(([account]) => account === signer)) throw new ChainError("invalid-request", "signer is not a required signer of the transaction");
1534
+ const unsigned = [];
1535
+ for (const [account, signature] of slots) {
1536
+ if (signature === void 0) {
1537
+ if (account !== signer) unsigned.push(account);
1538
+ continue;
1539
+ }
1540
+ if (!verifySolanaSignature(message, signature, account)) throw new ChainError("invalid-request", `the signature of ${account} does not verify`);
1541
+ }
1542
+ return {
1543
+ message,
1544
+ unsigned
1545
+ };
1546
+ };
1547
+ /**
1548
+ * `wire` with `signer`'s slot set to `signature`; every other byte unchanged — the other signatures
1549
+ * and their order are preserved byte for byte (a v1 wire keeps the message first).
1550
+ */
1551
+ const addSolanaSignature = (wire, signer, signature) => {
1552
+ if (signature.length !== 64) throw new ChainError("invalid-request", "signature must be 64 bytes");
1553
+ const { message, parsed, slots } = readTransaction(wire);
1554
+ const index = slots.findIndex(([account]) => account === signer);
1555
+ if (index === -1) throw new ChainError("invalid-request", "signer is not a required signer of the transaction");
1556
+ const offset = parsed.version === 1 ? message.length + 64 * index : wire.length - message.length - 64 * (slots.length - index);
1557
+ const out = new Uint8Array(wire);
1558
+ out.set(signature, offset);
1559
+ return out;
1560
+ };
377
1561
  // src/solana/index.ts
378
1562
  const solanaNetwork = (network) => {
379
1563
  if (!SOLANA_NETWORKS.includes(network)) throw new ChainError("unsupported", `unknown Solana network: ${network}`);
@@ -396,10 +1580,12 @@ const validateLookupTablesRaw = (tables) => {
396
1580
  return parsed;
397
1581
  };
398
1582
  /**
399
- * Normalizes a Solana sign request. `signer` is the fee payer the wallet is about to sign for — the
400
- * message must declare it as the first static account. A raw `solana.message` must not decode as a
401
- * transaction message (chains.mdx rule — a bytes-to-sign path that produces a transaction signature
402
- * is a wallet-drainer). Idempotent.
1583
+ * Normalizes a Solana sign request. `signer` is a required signer the wallet is about to sign for —
1584
+ * the message must declare it among the first `numRequiredSignatures` static accounts.
1585
+ * `lookupTables` may only accompany a v0 message — v1 has no address lookup tables (SIMD-0385). A
1586
+ * raw `solana.message` must not decode as a transaction message (chains.mdx rule — a bytes-to-sign
1587
+ * path that produces a transaction signature is a wallet-drainer): bytes a cluster could accept as
1588
+ * a transaction message of any version are never signed as a raw message. Idempotent.
403
1589
  */
404
1590
  const normalizeSolana = (request, signer) => {
405
1591
  if (!isRecord$1(request) || typeof request["kind"] !== "string") throw new ChainError("invalid-request", "request must be an object with a kind");
@@ -410,8 +1596,9 @@ const normalizeSolana = (request, signer) => {
410
1596
  if (!(message instanceof Uint8Array)) throw new ChainError("invalid-request", "message must be bytes");
411
1597
  if (request["kind"] === "solana.transaction") {
412
1598
  const lookupTables = validateLookupTablesRaw(request["lookupTables"]);
413
- const [feePayer] = parseCompiledMessage(message).staticAccounts;
414
- if (feePayer === void 0 || feePayer !== signer) throw new ChainError("fee-payer-mismatch", "signer is not the transaction fee payer");
1599
+ const parsed = parseCompiledMessage(message);
1600
+ if (parsed.version === 1 && lookupTables !== void 0) throw new ChainError("invalid-request", "a v1 transaction has no address lookup tables");
1601
+ if (!parsed.staticAccounts.slice(0, parsed.numRequiredSignatures).includes(signer)) throw new ChainError("invalid-request", "signer is not a required signer of the transaction");
415
1602
  return {
416
1603
  kind: "solana.transaction",
417
1604
  network,
@@ -426,13 +1613,7 @@ const normalizeSolana = (request, signer) => {
426
1613
  if (request["kind"] === "solana.message") {
427
1614
  const { format } = request;
428
1615
  if (format !== "raw") throw new ChainError("unsupported", `solana.message format ${String(format)} is not supported`);
429
- let decodesAsTransaction = true;
430
- try {
431
- parseCompiledMessage(message);
432
- } catch {
433
- decodesAsTransaction = false;
434
- }
435
- if (decodesAsTransaction) throw new ChainError("invalid-request", "solana.message payload decodes as a transaction message — use solana.transaction");
1616
+ if (mayBeTransactionMessage(message)) throw new ChainError("invalid-request", "solana.message payload could be a transaction message — use solana.transaction");
436
1617
  return {
437
1618
  kind: "solana.message",
438
1619
  network,
@@ -443,6 +1624,6 @@ const normalizeSolana = (request, signer) => {
443
1624
  }
444
1625
  throw new ChainError("unsupported", `unsupported sign request kind: ${request["kind"]}`);
445
1626
  };
446
- export { EXT, PROGRAM_IDS, SOLANA_NATIVE_ASSET, SOLANA_NETWORKS, decodeSolana, isSolanaAddress, netAfterTransferFee, normalizeSolana, parseCompiledMessage, parseMintExtensions, solanaAddressFromPublicKey, solanaTokenTable, verifiabilitySolana, verifySolanaSignature };
1627
+ export { EXT, JUPITER_PROGRAM, MPL_CORE_PROGRAM, PROGRAM_AUTHORITY_ACCOUNTS, PROGRAM_IDS, SOLANA_NATIVE_ASSET, SOLANA_NETWORKS, WSOL_MINT, addSolanaSignature, assembleSolana, associatedTokenAddress, decodeCoreIx, decodeSolana, decodeSolanaTransaction, isSolanaAddress, isWritableIndex, mayBeTransactionMessage, namedTableAccounts, netAfterTransferFee, normalizeSolana, parseCompiledMessage, parseMintExtensions, planSolana, solanaAddressFromPublicKey, solanaTokenTable, verifiabilitySolana, verifySolanaSignature };
447
1628
 
448
1629
  //# sourceMappingURL=solana.mjs.map