@subly_fi/pay 0.1.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.
- package/README.md +54 -0
- package/dist/cli.js +26 -0
- package/dist/deposit.js +1216 -0
- package/dist/mcp-server.js +1916 -0
- package/dist/pay.js +1769 -0
- package/dist/withdraw.js +1157 -0
- package/package.json +38 -0
package/dist/deposit.js
ADDED
|
@@ -0,0 +1,1216 @@
|
|
|
1
|
+
// ../../src/client/agent-wallet-signer.ts
|
|
2
|
+
import { signBytes } from "@solana/kit";
|
|
3
|
+
import bs584 from "bs58";
|
|
4
|
+
|
|
5
|
+
// ../../src/solana/tx.ts
|
|
6
|
+
import bs58 from "bs58";
|
|
7
|
+
import {
|
|
8
|
+
appendTransactionMessageInstructions,
|
|
9
|
+
compileTransaction,
|
|
10
|
+
compressTransactionMessageUsingAddressLookupTables,
|
|
11
|
+
createTransactionMessage,
|
|
12
|
+
getBase64EncodedWireTransaction,
|
|
13
|
+
getTransactionDecoder,
|
|
14
|
+
partiallySignTransaction,
|
|
15
|
+
pipe,
|
|
16
|
+
setTransactionMessageFeePayer,
|
|
17
|
+
setTransactionMessageLifetimeUsingBlockhash
|
|
18
|
+
} from "@solana/kit";
|
|
19
|
+
|
|
20
|
+
// ../../src/lib/hash.ts
|
|
21
|
+
import { createHash } from "node:crypto";
|
|
22
|
+
function sha256TaggedHex(data) {
|
|
23
|
+
return `sha256-${createHash("sha256").update(data).digest("hex")}`;
|
|
24
|
+
}
|
|
25
|
+
function stableStringify(value) {
|
|
26
|
+
if (value === null) {
|
|
27
|
+
return "null";
|
|
28
|
+
}
|
|
29
|
+
if (typeof value === "bigint") {
|
|
30
|
+
return JSON.stringify(value.toString());
|
|
31
|
+
}
|
|
32
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
33
|
+
return JSON.stringify(value);
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(value)) {
|
|
36
|
+
return `[${value.map((item) => stableStringify(item)).join(",")}]`;
|
|
37
|
+
}
|
|
38
|
+
const keys = Object.keys(value).filter((key) => value[key] !== void 0).sort();
|
|
39
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(",")}}`;
|
|
40
|
+
}
|
|
41
|
+
function hashStableJson(value) {
|
|
42
|
+
return sha256TaggedHex(stableStringify(value));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ../../src/solana/tx.ts
|
|
46
|
+
function decodeSerializedTransaction(serializedBase64) {
|
|
47
|
+
return getTransactionDecoder().decode(Buffer.from(serializedBase64, "base64"));
|
|
48
|
+
}
|
|
49
|
+
async function addSignaturesToSerializedTransaction(params) {
|
|
50
|
+
const decoded = decodeSerializedTransaction(params.serializedBase64);
|
|
51
|
+
const signed2 = await partiallySignTransaction(params.signers, decoded);
|
|
52
|
+
return {
|
|
53
|
+
serializedBase64: getBase64EncodedWireTransaction(signed2),
|
|
54
|
+
transaction: signed2
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function signatureBase58ForSigner(transaction, signer2) {
|
|
58
|
+
const signature = transaction.signatures[signer2];
|
|
59
|
+
if (signature === null || signature === void 0) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
return bs58.encode(signature);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ../../src/client/transaction-intent-validator.ts
|
|
66
|
+
import bs583 from "bs58";
|
|
67
|
+
import { getCompiledTransactionMessageDecoder } from "@solana/kit";
|
|
68
|
+
|
|
69
|
+
// ../../src/config/constants.ts
|
|
70
|
+
var PAYMENT_SCHEME = "subly-yield-exact";
|
|
71
|
+
var SOLANA_MAINNET_NETWORK = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
|
|
72
|
+
var SPL_TOKEN_PROGRAM_ID = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
73
|
+
var ASSOCIATED_TOKEN_PROGRAM_ID = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
|
|
74
|
+
var SUBLY_VAULT = {
|
|
75
|
+
name: "Subly USDC Payment Vault Alpha",
|
|
76
|
+
address: "5kfkpQZ6AkQgizHVThqkxD4J3db2i7pE3mHdPNRbx7jr",
|
|
77
|
+
programId: "KvauGMspG5k6rtzrqqn7WNn3oZdyKqLKwK2XWQ8FLjd",
|
|
78
|
+
usdcMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
|
|
79
|
+
shareMint: "7hGX49So539MU9Rrah8nBNVYXswWVwEJvgWNYeBDYq3a",
|
|
80
|
+
lookupTable: "7UbXhDnpK7WVnwsfivzQRENoqKqAULQ5s19gS1xJrQEo",
|
|
81
|
+
farm: "E2Ct77LowkDAH1T9ubwPpb84pU2GSGrUdgH3KeTTpLX"
|
|
82
|
+
};
|
|
83
|
+
var USDC_DECIMALS = 6;
|
|
84
|
+
|
|
85
|
+
// ../../src/domain/request-binding.ts
|
|
86
|
+
function computeRequestBindingHash(fields) {
|
|
87
|
+
return hashStableJson({
|
|
88
|
+
sellerRequestId: fields.sellerRequestId,
|
|
89
|
+
httpMethod: fields.httpMethod.toUpperCase(),
|
|
90
|
+
canonicalResourceUrl: fields.canonicalResourceUrl,
|
|
91
|
+
requestBodyHash: fields.requestBodyHash,
|
|
92
|
+
seller: fields.seller,
|
|
93
|
+
asset: fields.asset,
|
|
94
|
+
amountRawUsdc: fields.amountRawUsdc,
|
|
95
|
+
payTo: fields.payTo,
|
|
96
|
+
sellerUsdcAta: fields.sellerUsdcAta
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ../../src/lib/associated-token-account.ts
|
|
101
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
102
|
+
import bs582 from "bs58";
|
|
103
|
+
var PDA_MARKER = Buffer.from("ProgramDerivedAddress", "utf8");
|
|
104
|
+
var ED25519_P = (1n << 255n) - 19n;
|
|
105
|
+
var ED25519_D = mod(-121665n * modPow(121666n, ED25519_P - 2n, ED25519_P), ED25519_P);
|
|
106
|
+
function deriveAssociatedTokenAddress(params) {
|
|
107
|
+
const owner = decodePublicKey(params.owner, "owner");
|
|
108
|
+
const mint = decodePublicKey(params.mint ?? SUBLY_VAULT.usdcMint, "mint");
|
|
109
|
+
const tokenProgramId = decodePublicKey(
|
|
110
|
+
params.tokenProgramId ?? SPL_TOKEN_PROGRAM_ID,
|
|
111
|
+
"tokenProgramId"
|
|
112
|
+
);
|
|
113
|
+
const associatedTokenProgramId = decodePublicKey(
|
|
114
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
115
|
+
"associatedTokenProgramId"
|
|
116
|
+
);
|
|
117
|
+
for (let bump = 255; bump >= 0; bump -= 1) {
|
|
118
|
+
const address2 = createProgramAddress(
|
|
119
|
+
[owner, tokenProgramId, mint, Uint8Array.of(bump)],
|
|
120
|
+
associatedTokenProgramId
|
|
121
|
+
);
|
|
122
|
+
if (address2 !== null) {
|
|
123
|
+
return bs582.encode(address2);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
throw new Error("Unable to derive associated token account address");
|
|
127
|
+
}
|
|
128
|
+
function createProgramAddress(seeds, programId) {
|
|
129
|
+
const hash = createHash2("sha256");
|
|
130
|
+
for (const seed of seeds) {
|
|
131
|
+
hash.update(seed);
|
|
132
|
+
}
|
|
133
|
+
hash.update(programId);
|
|
134
|
+
hash.update(PDA_MARKER);
|
|
135
|
+
const digest = hash.digest();
|
|
136
|
+
return isEd25519Point(digest) ? null : new Uint8Array(digest);
|
|
137
|
+
}
|
|
138
|
+
function decodePublicKey(value, fieldName) {
|
|
139
|
+
const decoded = bs582.decode(value);
|
|
140
|
+
if (decoded.length !== 32) {
|
|
141
|
+
throw new Error(`${fieldName} must be a 32-byte public key`);
|
|
142
|
+
}
|
|
143
|
+
return decoded;
|
|
144
|
+
}
|
|
145
|
+
function isEd25519Point(bytes) {
|
|
146
|
+
if (bytes.length !== 32) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
const yBytes = Uint8Array.from(bytes);
|
|
150
|
+
yBytes[31] = yBytes[31] & 127;
|
|
151
|
+
const y = littleEndianToBigInt(yBytes);
|
|
152
|
+
if (y >= ED25519_P) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
const ySquared = mod(y * y, ED25519_P);
|
|
156
|
+
const numerator = mod(ySquared - 1n, ED25519_P);
|
|
157
|
+
const denominator = mod(ED25519_D * ySquared + 1n, ED25519_P);
|
|
158
|
+
if (denominator === 0n) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
const xSquared = mod(
|
|
162
|
+
numerator * modPow(denominator, ED25519_P - 2n, ED25519_P),
|
|
163
|
+
ED25519_P
|
|
164
|
+
);
|
|
165
|
+
return xSquared === 0n || modPow(xSquared, (ED25519_P - 1n) / 2n, ED25519_P) === 1n;
|
|
166
|
+
}
|
|
167
|
+
function littleEndianToBigInt(bytes) {
|
|
168
|
+
let value = 0n;
|
|
169
|
+
for (let index = bytes.length - 1; index >= 0; index -= 1) {
|
|
170
|
+
value = (value << 8n) + BigInt(bytes[index]);
|
|
171
|
+
}
|
|
172
|
+
return value;
|
|
173
|
+
}
|
|
174
|
+
function mod(value, modulus) {
|
|
175
|
+
const result = value % modulus;
|
|
176
|
+
return result >= 0n ? result : result + modulus;
|
|
177
|
+
}
|
|
178
|
+
function modPow(base, exponent, modulus) {
|
|
179
|
+
let result = 1n;
|
|
180
|
+
let nextBase = mod(base, modulus);
|
|
181
|
+
let nextExponent = exponent;
|
|
182
|
+
while (nextExponent > 0n) {
|
|
183
|
+
if ((nextExponent & 1n) === 1n) {
|
|
184
|
+
result = mod(result * nextBase, modulus);
|
|
185
|
+
}
|
|
186
|
+
nextBase = mod(nextBase * nextBase, modulus);
|
|
187
|
+
nextExponent >>= 1n;
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ../../src/client/transaction-intent-validator.ts
|
|
193
|
+
var COMPUTE_BUDGET_PROGRAM_ID = "ComputeBudget111111111111111111111111111111";
|
|
194
|
+
var SYSTEM_PROGRAM_ID = "11111111111111111111111111111111";
|
|
195
|
+
var ASSOCIATED_TOKEN_PROGRAM_ID2 = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
|
|
196
|
+
var MEMO_PROGRAM_ID = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr";
|
|
197
|
+
var KVAULT_PROGRAM_ID = "KvauGMspG5k6rtzrqqn7WNn3oZdyKqLKwK2XWQ8FLjd";
|
|
198
|
+
var KAMINO_FARMS_PROGRAM_ID = "FarmsPZpWu9i7Kky8tPN37rs2TpmMrAZrC7S7vJa91Hr";
|
|
199
|
+
var KVAULT_WITHDRAW_DISCRIMINATOR = Uint8Array.from([
|
|
200
|
+
183,
|
|
201
|
+
18,
|
|
202
|
+
70,
|
|
203
|
+
156,
|
|
204
|
+
148,
|
|
205
|
+
109,
|
|
206
|
+
161,
|
|
207
|
+
34
|
|
208
|
+
]);
|
|
209
|
+
var KVAULT_WITHDRAW_FROM_AVAILABLE_DISCRIMINATOR = Uint8Array.from([
|
|
210
|
+
19,
|
|
211
|
+
131,
|
|
212
|
+
112,
|
|
213
|
+
155,
|
|
214
|
+
170,
|
|
215
|
+
220,
|
|
216
|
+
34,
|
|
217
|
+
57
|
|
218
|
+
]);
|
|
219
|
+
var KVAULT_DEPOSIT_DISCRIMINATOR = Uint8Array.from([
|
|
220
|
+
242,
|
|
221
|
+
35,
|
|
222
|
+
198,
|
|
223
|
+
137,
|
|
224
|
+
82,
|
|
225
|
+
225,
|
|
226
|
+
242,
|
|
227
|
+
182
|
|
228
|
+
]);
|
|
229
|
+
var U64_MAX = 18446744073709551615n;
|
|
230
|
+
var MAX_TEMP_ACCOUNT_LAMPORTS = 10000000n;
|
|
231
|
+
var DEFAULT_MAX_COMPUTE_UNIT_LIMIT = 14e5;
|
|
232
|
+
var DEFAULT_MAX_COMPUTE_UNIT_PRICE_MICRO_LAMPORTS = 100000n;
|
|
233
|
+
var IntentValidationError = class extends Error {
|
|
234
|
+
reason;
|
|
235
|
+
constructor(reason, message) {
|
|
236
|
+
super(message);
|
|
237
|
+
this.name = "IntentValidationError";
|
|
238
|
+
this.reason = reason;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
function reject(reason, message) {
|
|
242
|
+
throw new IntentValidationError(reason, message);
|
|
243
|
+
}
|
|
244
|
+
function decodeIntentTransaction(params) {
|
|
245
|
+
const wire = Buffer.from(params.serializedTransaction, "base64");
|
|
246
|
+
const signatureCount = readShortVec(wire, 0);
|
|
247
|
+
if (signatureCount === null) {
|
|
248
|
+
reject("invalid_transaction_encoding", "Cannot parse signature count");
|
|
249
|
+
}
|
|
250
|
+
const messageOffset = signatureCount.nextOffset + signatureCount.value * 64;
|
|
251
|
+
if (messageOffset >= wire.length) {
|
|
252
|
+
reject("invalid_transaction_encoding", "Transaction has no message bytes");
|
|
253
|
+
}
|
|
254
|
+
const messageBytes = wire.subarray(messageOffset);
|
|
255
|
+
const compiled = getCompiledTransactionMessageDecoder().decode(messageBytes);
|
|
256
|
+
if (compiled.version !== 0) {
|
|
257
|
+
reject("unsupported_transaction_version", "Only v0 transactions are supported");
|
|
258
|
+
}
|
|
259
|
+
const staticAccounts = compiled.staticAccounts.map(String);
|
|
260
|
+
const loadedWritable = [];
|
|
261
|
+
const loadedReadonly = [];
|
|
262
|
+
const lookups = compiled.addressTableLookups ?? [];
|
|
263
|
+
for (const rawLookup of lookups) {
|
|
264
|
+
const lookup = rawLookup;
|
|
265
|
+
const table = params.lookupTables?.[String(lookup.lookupTableAddress)];
|
|
266
|
+
if (table === void 0) {
|
|
267
|
+
reject(
|
|
268
|
+
"lookup_table_unresolved",
|
|
269
|
+
`Transaction references unknown lookup table ${lookup.lookupTableAddress}`
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
const writableIndexes = lookup.writableIndexes ?? lookup.writableIndices ?? [];
|
|
273
|
+
const readonlyIndexes = lookup.readonlyIndexes ?? lookup.readableIndices ?? [];
|
|
274
|
+
for (const index of writableIndexes) {
|
|
275
|
+
const resolved = table[index];
|
|
276
|
+
if (resolved === void 0) {
|
|
277
|
+
reject("lookup_table_unresolved", "Lookup table index out of range");
|
|
278
|
+
}
|
|
279
|
+
loadedWritable.push(String(resolved));
|
|
280
|
+
}
|
|
281
|
+
for (const index of readonlyIndexes) {
|
|
282
|
+
const resolved = table[index];
|
|
283
|
+
if (resolved === void 0) {
|
|
284
|
+
reject("lookup_table_unresolved", "Lookup table index out of range");
|
|
285
|
+
}
|
|
286
|
+
loadedReadonly.push(String(resolved));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const orderedAccounts = [...staticAccounts, ...loadedWritable, ...loadedReadonly];
|
|
290
|
+
const instructions = compiled.instructions.map(
|
|
291
|
+
(instruction) => {
|
|
292
|
+
const programAddress = orderedAccounts[instruction.programAddressIndex];
|
|
293
|
+
if (programAddress === void 0) {
|
|
294
|
+
reject("invalid_transaction_encoding", "Program index out of range");
|
|
295
|
+
}
|
|
296
|
+
const accounts = (instruction.accountIndices ?? []).map((index) => {
|
|
297
|
+
const account = orderedAccounts[index];
|
|
298
|
+
if (account === void 0) {
|
|
299
|
+
reject("invalid_transaction_encoding", "Account index out of range");
|
|
300
|
+
}
|
|
301
|
+
return account;
|
|
302
|
+
});
|
|
303
|
+
return {
|
|
304
|
+
programAddress,
|
|
305
|
+
accounts,
|
|
306
|
+
data: instruction.data === void 0 ? new Uint8Array() : Uint8Array.from(instruction.data)
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
);
|
|
310
|
+
const feePayer = staticAccounts[0];
|
|
311
|
+
if (feePayer === void 0) {
|
|
312
|
+
reject("invalid_transaction_encoding", "Transaction has no fee payer");
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
feePayer,
|
|
316
|
+
requiredSigners: staticAccounts.slice(0, compiled.header.numSignerAccounts),
|
|
317
|
+
instructions,
|
|
318
|
+
messageHash: sha256TaggedHex(Buffer.from(messageBytes))
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function validatePaymentIntentTransaction(params) {
|
|
322
|
+
const { intent } = params;
|
|
323
|
+
const now = params.nowMs ?? Date.now();
|
|
324
|
+
const policy = resolveIntentValidationPolicy(params.policy);
|
|
325
|
+
if (new Date(intent.expiresAt).getTime() <= now) {
|
|
326
|
+
reject("expired", "Payment intent has expired");
|
|
327
|
+
}
|
|
328
|
+
if (intent.scheme !== PAYMENT_SCHEME) {
|
|
329
|
+
reject("scheme_mismatch", `scheme must be ${PAYMENT_SCHEME}`);
|
|
330
|
+
}
|
|
331
|
+
if (intent.network !== SOLANA_MAINNET_NETWORK) {
|
|
332
|
+
reject("network_mismatch", "Unsupported network");
|
|
333
|
+
}
|
|
334
|
+
if (intent.vault !== SUBLY_VAULT.address) {
|
|
335
|
+
reject("vault_mismatch", "Unsupported vault");
|
|
336
|
+
}
|
|
337
|
+
if (intent.shareMint !== SUBLY_VAULT.shareMint) {
|
|
338
|
+
reject("share_mint_mismatch", "Unsupported share mint");
|
|
339
|
+
}
|
|
340
|
+
if (intent.asset !== SUBLY_VAULT.usdcMint) {
|
|
341
|
+
reject("asset_mismatch", "Only USDC payments are supported");
|
|
342
|
+
}
|
|
343
|
+
if (intent.memo !== intent.paymentId) {
|
|
344
|
+
reject("memo_mismatch", "Memo must equal the paymentId");
|
|
345
|
+
}
|
|
346
|
+
const expectedBinding = computeRequestBindingHash({
|
|
347
|
+
sellerRequestId: intent.sellerRequestId,
|
|
348
|
+
httpMethod: intent.httpMethod,
|
|
349
|
+
canonicalResourceUrl: intent.canonicalResourceUrl,
|
|
350
|
+
requestBodyHash: intent.requestBodyHash,
|
|
351
|
+
seller: intent.seller,
|
|
352
|
+
asset: intent.asset,
|
|
353
|
+
amountRawUsdc: intent.amountRawUsdc,
|
|
354
|
+
payTo: intent.payTo,
|
|
355
|
+
sellerUsdcAta: intent.sellerUsdcAta
|
|
356
|
+
});
|
|
357
|
+
if (expectedBinding !== intent.requestBindingHash) {
|
|
358
|
+
reject(
|
|
359
|
+
"request_binding_mismatch",
|
|
360
|
+
"requestBindingHash does not match the request fields"
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
const expectedSellerAta = deriveAssociatedTokenAddress({
|
|
364
|
+
owner: intent.payTo,
|
|
365
|
+
mint: intent.asset
|
|
366
|
+
});
|
|
367
|
+
if (expectedSellerAta !== intent.sellerUsdcAta) {
|
|
368
|
+
reject(
|
|
369
|
+
"seller_ata_mismatch",
|
|
370
|
+
"sellerUsdcAta must be the associated USDC account for payTo"
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
const expectedDustAta = deriveAssociatedTokenAddress({
|
|
374
|
+
owner: intent.wallet,
|
|
375
|
+
mint: intent.asset
|
|
376
|
+
});
|
|
377
|
+
if (expectedDustAta !== intent.dustRecipientUsdcAta) {
|
|
378
|
+
reject(
|
|
379
|
+
"dust_recipient_mismatch",
|
|
380
|
+
"dustRecipientUsdcAta must be the agent wallet's USDC ATA"
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
const decoded = decodeIntentTransaction({
|
|
384
|
+
serializedTransaction: params.serializedTransaction,
|
|
385
|
+
...params.lookupTables === void 0 ? {} : { lookupTables: params.lookupTables }
|
|
386
|
+
});
|
|
387
|
+
if (decoded.messageHash !== intent.preparedMessageHash) {
|
|
388
|
+
reject("message_hash_mismatch", "Prepared message hash mismatch");
|
|
389
|
+
}
|
|
390
|
+
if (decoded.feePayer !== intent.feePayer) {
|
|
391
|
+
reject("fee_payer_mismatch", "Transaction fee payer is not the Subly sponsor");
|
|
392
|
+
}
|
|
393
|
+
const expectedSigners = /* @__PURE__ */ new Set([
|
|
394
|
+
intent.feePayer,
|
|
395
|
+
intent.wallet,
|
|
396
|
+
intent.temporarySettlementTokenAccount
|
|
397
|
+
]);
|
|
398
|
+
if (decoded.requiredSigners.length !== expectedSigners.size || !decoded.requiredSigners.every((signer2) => expectedSigners.has(signer2))) {
|
|
399
|
+
reject(
|
|
400
|
+
"unexpected_signers",
|
|
401
|
+
"Transaction signers must be exactly the sponsor, the agent wallet, and the temporary settlement account"
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
const ixs = [...decoded.instructions];
|
|
405
|
+
expectComputeBudgetPair(ixs, policy);
|
|
406
|
+
expectCreateTemporaryAccount(ixs, intent, policy);
|
|
407
|
+
expectInitializeTemporaryAccount(ixs, intent);
|
|
408
|
+
consumeFarmInstructions(ixs, intent.wallet);
|
|
409
|
+
expectKvaultWithdraw(ixs, {
|
|
410
|
+
wallet: intent.wallet,
|
|
411
|
+
vault: intent.vault,
|
|
412
|
+
shareMint: intent.shareMint,
|
|
413
|
+
asset: intent.asset,
|
|
414
|
+
userTokenAccount: intent.temporarySettlementTokenAccount,
|
|
415
|
+
maxSharesToRedeemRaw: BigInt(intent.maxSharesToRedeemRaw),
|
|
416
|
+
allowFullExit: false
|
|
417
|
+
});
|
|
418
|
+
expectTransferChecked(ixs, {
|
|
419
|
+
source: intent.temporarySettlementTokenAccount,
|
|
420
|
+
mint: intent.asset,
|
|
421
|
+
destination: intent.sellerUsdcAta,
|
|
422
|
+
authority: intent.wallet,
|
|
423
|
+
amount: BigInt(intent.amountRawUsdc),
|
|
424
|
+
label: "seller transfer"
|
|
425
|
+
});
|
|
426
|
+
if (ixs[0] !== void 0 && ixs[0].programAddress === SPL_TOKEN_PROGRAM_ID && ixs[0].data[0] === 12) {
|
|
427
|
+
expectTransferChecked(ixs, {
|
|
428
|
+
source: intent.temporarySettlementTokenAccount,
|
|
429
|
+
mint: intent.asset,
|
|
430
|
+
destination: intent.dustRecipientUsdcAta,
|
|
431
|
+
authority: intent.wallet,
|
|
432
|
+
amount: null,
|
|
433
|
+
label: "dust sweep"
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
expectCloseAccount(ixs, {
|
|
437
|
+
account: intent.temporarySettlementTokenAccount,
|
|
438
|
+
destination: intent.feePayer,
|
|
439
|
+
owner: intent.wallet
|
|
440
|
+
});
|
|
441
|
+
expectMemo(ixs, intent.memo);
|
|
442
|
+
if (ixs.length > 0) {
|
|
443
|
+
reject(
|
|
444
|
+
"unexpected_instruction",
|
|
445
|
+
`Transaction contains ${ixs.length} unexpected trailing instruction(s)`
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function validateDepositIntentTransaction(params) {
|
|
450
|
+
const { intent } = params;
|
|
451
|
+
const now = params.nowMs ?? Date.now();
|
|
452
|
+
const policy = resolveIntentValidationPolicy(params.policy);
|
|
453
|
+
if (new Date(intent.expiresAt).getTime() <= now) {
|
|
454
|
+
reject("expired", "Deposit intent has expired");
|
|
455
|
+
}
|
|
456
|
+
assertVaultIntentTargets(intent);
|
|
457
|
+
const decoded = decodeIntentTransaction({
|
|
458
|
+
serializedTransaction: params.serializedTransaction,
|
|
459
|
+
...params.lookupTables === void 0 ? {} : { lookupTables: params.lookupTables }
|
|
460
|
+
});
|
|
461
|
+
if (decoded.messageHash !== intent.preparedMessageHash) {
|
|
462
|
+
reject("message_hash_mismatch", "Prepared message hash mismatch");
|
|
463
|
+
}
|
|
464
|
+
if (decoded.feePayer !== intent.feePayer) {
|
|
465
|
+
reject("fee_payer_mismatch", "Transaction fee payer is not the Subly sponsor");
|
|
466
|
+
}
|
|
467
|
+
let sawDeposit = false;
|
|
468
|
+
for (const ix of decoded.instructions) {
|
|
469
|
+
switch (ix.programAddress) {
|
|
470
|
+
case COMPUTE_BUDGET_PROGRAM_ID:
|
|
471
|
+
validateComputeBudgetInstruction(ix, policy);
|
|
472
|
+
break;
|
|
473
|
+
case ASSOCIATED_TOKEN_PROGRAM_ID2:
|
|
474
|
+
expectAtaCreateForOwner(ix, intent.wallet);
|
|
475
|
+
break;
|
|
476
|
+
case MEMO_PROGRAM_ID:
|
|
477
|
+
break;
|
|
478
|
+
case KVAULT_PROGRAM_ID: {
|
|
479
|
+
if (!bytesStartWith(ix.data, KVAULT_DEPOSIT_DISCRIMINATOR)) {
|
|
480
|
+
reject("unexpected_instruction", "Unexpected KVault instruction in deposit");
|
|
481
|
+
}
|
|
482
|
+
const maxAmount = readU64LE(ix.data, 8);
|
|
483
|
+
if (maxAmount !== BigInt(intent.amountRawUsdc)) {
|
|
484
|
+
reject("amount_mismatch", "Deposit amount does not match the intent");
|
|
485
|
+
}
|
|
486
|
+
if (ix.accounts[0] !== intent.wallet) {
|
|
487
|
+
reject("wallet_mismatch", "Deposit user is not the agent wallet");
|
|
488
|
+
}
|
|
489
|
+
if (ix.accounts[1] !== intent.vault) {
|
|
490
|
+
reject("vault_mismatch", "Deposit vault mismatch");
|
|
491
|
+
}
|
|
492
|
+
if (ix.accounts[3] !== intent.asset) {
|
|
493
|
+
reject("asset_mismatch", "Deposit token mint mismatch");
|
|
494
|
+
}
|
|
495
|
+
if (ix.accounts[5] !== intent.shareMint) {
|
|
496
|
+
reject("share_mint_mismatch", "Deposit share mint mismatch");
|
|
497
|
+
}
|
|
498
|
+
const expectedSourceAta = deriveAssociatedTokenAddress({
|
|
499
|
+
owner: intent.wallet,
|
|
500
|
+
mint: intent.asset
|
|
501
|
+
});
|
|
502
|
+
if (ix.accounts[6] !== expectedSourceAta) {
|
|
503
|
+
reject(
|
|
504
|
+
"source_ata_mismatch",
|
|
505
|
+
"Deposit source must be the agent wallet's USDC ATA"
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
sawDeposit = true;
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
default:
|
|
512
|
+
reject(
|
|
513
|
+
"unexpected_instruction",
|
|
514
|
+
`Unexpected program ${ix.programAddress} in deposit transaction`
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (!sawDeposit) {
|
|
519
|
+
reject("missing_instruction", "Deposit transaction has no KVault deposit");
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
function validateWithdrawalIntentTransaction(params) {
|
|
523
|
+
const { intent } = params;
|
|
524
|
+
const now = params.nowMs ?? Date.now();
|
|
525
|
+
const policy = resolveIntentValidationPolicy(params.policy);
|
|
526
|
+
if (new Date(intent.expiresAt).getTime() <= now) {
|
|
527
|
+
reject("expired", "Withdrawal intent has expired");
|
|
528
|
+
}
|
|
529
|
+
assertVaultIntentTargets(intent);
|
|
530
|
+
const expectedDestination = deriveAssociatedTokenAddress({
|
|
531
|
+
owner: intent.wallet,
|
|
532
|
+
mint: intent.asset
|
|
533
|
+
});
|
|
534
|
+
if (expectedDestination !== intent.destinationUsdcAta) {
|
|
535
|
+
reject(
|
|
536
|
+
"destination_mismatch",
|
|
537
|
+
"Withdrawal destination must be the agent wallet's USDC ATA"
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
const decoded = decodeIntentTransaction({
|
|
541
|
+
serializedTransaction: params.serializedTransaction,
|
|
542
|
+
...params.lookupTables === void 0 ? {} : { lookupTables: params.lookupTables }
|
|
543
|
+
});
|
|
544
|
+
if (decoded.messageHash !== intent.preparedMessageHash) {
|
|
545
|
+
reject("message_hash_mismatch", "Prepared message hash mismatch");
|
|
546
|
+
}
|
|
547
|
+
if (decoded.feePayer !== intent.feePayer) {
|
|
548
|
+
reject("fee_payer_mismatch", "Transaction fee payer is not the Subly sponsor");
|
|
549
|
+
}
|
|
550
|
+
let sawWithdraw = false;
|
|
551
|
+
for (const ix of decoded.instructions) {
|
|
552
|
+
switch (ix.programAddress) {
|
|
553
|
+
case COMPUTE_BUDGET_PROGRAM_ID:
|
|
554
|
+
validateComputeBudgetInstruction(ix, policy);
|
|
555
|
+
break;
|
|
556
|
+
case MEMO_PROGRAM_ID:
|
|
557
|
+
case KAMINO_FARMS_PROGRAM_ID:
|
|
558
|
+
break;
|
|
559
|
+
case ASSOCIATED_TOKEN_PROGRAM_ID2:
|
|
560
|
+
expectAtaCreateForOwner(ix, intent.wallet);
|
|
561
|
+
break;
|
|
562
|
+
case SPL_TOKEN_PROGRAM_ID: {
|
|
563
|
+
if (ix.data[0] !== 9) {
|
|
564
|
+
reject(
|
|
565
|
+
"unexpected_instruction",
|
|
566
|
+
"Only CloseAccount token instructions are allowed in withdrawals"
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
if (ix.accounts[1] !== intent.wallet || ix.accounts[2] !== intent.wallet) {
|
|
570
|
+
reject(
|
|
571
|
+
"unexpected_instruction",
|
|
572
|
+
"Withdrawal CloseAccount must pay out to the agent wallet"
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
case KVAULT_PROGRAM_ID: {
|
|
578
|
+
validateKvaultWithdrawInstruction(ix, {
|
|
579
|
+
wallet: intent.wallet,
|
|
580
|
+
vault: intent.vault,
|
|
581
|
+
shareMint: intent.shareMint,
|
|
582
|
+
asset: intent.asset,
|
|
583
|
+
userTokenAccount: intent.destinationUsdcAta,
|
|
584
|
+
maxSharesToRedeemRaw: BigInt(intent.maxSharesToRedeemRaw),
|
|
585
|
+
allowFullExit: intent.allowFullExit
|
|
586
|
+
});
|
|
587
|
+
sawWithdraw = true;
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
default:
|
|
591
|
+
reject(
|
|
592
|
+
"unexpected_instruction",
|
|
593
|
+
`Unexpected program ${ix.programAddress} in withdrawal transaction`
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (!sawWithdraw) {
|
|
598
|
+
reject("missing_instruction", "Withdrawal transaction has no KVault withdraw");
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function assertVaultIntentTargets(intent) {
|
|
602
|
+
if (intent.vault !== SUBLY_VAULT.address) {
|
|
603
|
+
reject("vault_mismatch", "Unsupported vault");
|
|
604
|
+
}
|
|
605
|
+
if (intent.shareMint !== SUBLY_VAULT.shareMint) {
|
|
606
|
+
reject("share_mint_mismatch", "Unsupported share mint");
|
|
607
|
+
}
|
|
608
|
+
if (intent.asset !== SUBLY_VAULT.usdcMint) {
|
|
609
|
+
reject("asset_mismatch", "Only USDC is supported");
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function resolveIntentValidationPolicy(policy) {
|
|
613
|
+
const resolved = {
|
|
614
|
+
maxComputeUnitLimit: policy?.maxComputeUnitLimit ?? DEFAULT_MAX_COMPUTE_UNIT_LIMIT,
|
|
615
|
+
maxComputeUnitPriceMicroLamports: policy?.maxComputeUnitPriceMicroLamports ?? DEFAULT_MAX_COMPUTE_UNIT_PRICE_MICRO_LAMPORTS,
|
|
616
|
+
maxTemporaryAccountLamports: policy?.maxTemporaryAccountLamports ?? MAX_TEMP_ACCOUNT_LAMPORTS
|
|
617
|
+
};
|
|
618
|
+
if (!Number.isSafeInteger(resolved.maxComputeUnitLimit) || resolved.maxComputeUnitLimit <= 0) {
|
|
619
|
+
reject("invalid_policy", "maxComputeUnitLimit must be a positive safe integer");
|
|
620
|
+
}
|
|
621
|
+
if (resolved.maxComputeUnitPriceMicroLamports < 0n) {
|
|
622
|
+
reject(
|
|
623
|
+
"invalid_policy",
|
|
624
|
+
"maxComputeUnitPriceMicroLamports must be non-negative"
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
if (resolved.maxTemporaryAccountLamports <= 0n) {
|
|
628
|
+
reject("invalid_policy", "maxTemporaryAccountLamports must be positive");
|
|
629
|
+
}
|
|
630
|
+
return resolved;
|
|
631
|
+
}
|
|
632
|
+
function expectComputeBudgetPair(ixs, policy) {
|
|
633
|
+
for (const discriminator of [2, 3]) {
|
|
634
|
+
const ix = ixs.shift();
|
|
635
|
+
if (ix === void 0 || ix.programAddress !== COMPUTE_BUDGET_PROGRAM_ID || ix.data[0] !== discriminator) {
|
|
636
|
+
reject(
|
|
637
|
+
"compute_budget_mismatch",
|
|
638
|
+
"Transaction must start with ComputeBudget limit and price instructions"
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
validateComputeBudgetInstruction(ix, policy);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
function validateComputeBudgetInstruction(ix, policy) {
|
|
645
|
+
switch (ix.data[0]) {
|
|
646
|
+
case 2: {
|
|
647
|
+
const units = readU32LE(ix.data, 1);
|
|
648
|
+
if (units <= 0 || units > policy.maxComputeUnitLimit) {
|
|
649
|
+
reject(
|
|
650
|
+
"compute_budget_mismatch",
|
|
651
|
+
`Compute unit limit ${units} exceeds policy maximum ${policy.maxComputeUnitLimit}`
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
case 3: {
|
|
657
|
+
const microLamports = readU64LE(ix.data, 1);
|
|
658
|
+
if (microLamports > policy.maxComputeUnitPriceMicroLamports) {
|
|
659
|
+
reject(
|
|
660
|
+
"compute_budget_mismatch",
|
|
661
|
+
`Compute unit price ${microLamports} exceeds policy maximum ${policy.maxComputeUnitPriceMicroLamports}`
|
|
662
|
+
);
|
|
663
|
+
}
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
default:
|
|
667
|
+
reject("compute_budget_mismatch", "Unexpected ComputeBudget instruction");
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
function expectCreateTemporaryAccount(ixs, intent, policy) {
|
|
671
|
+
const ix = ixs.shift();
|
|
672
|
+
if (ix === void 0 || ix.programAddress !== SYSTEM_PROGRAM_ID) {
|
|
673
|
+
reject("temp_account_mismatch", "Expected System createAccount instruction");
|
|
674
|
+
}
|
|
675
|
+
if (ix.data.length < 52 || readU32LE(ix.data, 0) !== 0) {
|
|
676
|
+
reject("temp_account_mismatch", "Expected createAccount discriminator");
|
|
677
|
+
}
|
|
678
|
+
const lamports = readU64LE(ix.data, 4);
|
|
679
|
+
const space = readU64LE(ix.data, 12);
|
|
680
|
+
const owner = bs583.encode(ix.data.subarray(20, 52));
|
|
681
|
+
if (space !== 165n) {
|
|
682
|
+
reject("temp_account_mismatch", "Temporary account space must be 165 bytes");
|
|
683
|
+
}
|
|
684
|
+
if (owner !== SPL_TOKEN_PROGRAM_ID) {
|
|
685
|
+
reject("temp_account_mismatch", "Temporary account owner must be the token program");
|
|
686
|
+
}
|
|
687
|
+
if (lamports > policy.maxTemporaryAccountLamports) {
|
|
688
|
+
reject("temp_account_mismatch", "Temporary account rent exceeds the cap");
|
|
689
|
+
}
|
|
690
|
+
if (ix.accounts[0] !== intent.feePayer) {
|
|
691
|
+
reject("temp_account_mismatch", "Temporary account must be funded by the sponsor");
|
|
692
|
+
}
|
|
693
|
+
if (ix.accounts[1] !== intent.temporarySettlementTokenAccount) {
|
|
694
|
+
reject("temp_account_mismatch", "createAccount target is not the temporary account");
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
function expectInitializeTemporaryAccount(ixs, intent) {
|
|
698
|
+
const ix = ixs.shift();
|
|
699
|
+
if (ix === void 0 || ix.programAddress !== SPL_TOKEN_PROGRAM_ID || ix.data[0] !== 18) {
|
|
700
|
+
reject("temp_account_mismatch", "Expected InitializeAccount3 instruction");
|
|
701
|
+
}
|
|
702
|
+
const owner = bs583.encode(ix.data.subarray(1, 33));
|
|
703
|
+
if (owner !== intent.wallet) {
|
|
704
|
+
reject(
|
|
705
|
+
"temp_account_mismatch",
|
|
706
|
+
"Temporary account token authority must be the agent wallet"
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
if (ix.accounts[0] !== intent.temporarySettlementTokenAccount) {
|
|
710
|
+
reject("temp_account_mismatch", "InitializeAccount3 target mismatch");
|
|
711
|
+
}
|
|
712
|
+
if (ix.accounts[1] !== intent.asset) {
|
|
713
|
+
reject("temp_account_mismatch", "Temporary account mint must be USDC");
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
function consumeFarmInstructions(ixs, wallet) {
|
|
717
|
+
while (ixs[0] !== void 0 && ixs[0].programAddress === KAMINO_FARMS_PROGRAM_ID) {
|
|
718
|
+
const ix = ixs.shift();
|
|
719
|
+
if (!ix.accounts.includes(wallet)) {
|
|
720
|
+
reject(
|
|
721
|
+
"farm_instruction_mismatch",
|
|
722
|
+
"Farm unstake instruction does not reference the agent wallet"
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
function expectKvaultWithdraw(ixs, expectation) {
|
|
728
|
+
const ix = ixs.shift();
|
|
729
|
+
if (ix === void 0 || ix.programAddress !== KVAULT_PROGRAM_ID) {
|
|
730
|
+
reject("withdraw_mismatch", "Expected KVault withdraw instruction");
|
|
731
|
+
}
|
|
732
|
+
validateKvaultWithdrawInstruction(ix, expectation);
|
|
733
|
+
}
|
|
734
|
+
function validateKvaultWithdrawInstruction(ix, expectation) {
|
|
735
|
+
const isWithdraw = bytesStartWith(ix.data, KVAULT_WITHDRAW_DISCRIMINATOR);
|
|
736
|
+
const isWithdrawFromAvailable = bytesStartWith(
|
|
737
|
+
ix.data,
|
|
738
|
+
KVAULT_WITHDRAW_FROM_AVAILABLE_DISCRIMINATOR
|
|
739
|
+
);
|
|
740
|
+
if (!isWithdraw && !isWithdrawFromAvailable) {
|
|
741
|
+
reject("withdraw_mismatch", "Unexpected KVault instruction");
|
|
742
|
+
}
|
|
743
|
+
const sharesAmount = readU64LE(ix.data, 8);
|
|
744
|
+
const fullExit = sharesAmount === U64_MAX;
|
|
745
|
+
if (fullExit && !expectation.allowFullExit) {
|
|
746
|
+
reject("withdraw_mismatch", "Full-exit share burn is not allowed for this intent");
|
|
747
|
+
}
|
|
748
|
+
if (!fullExit && sharesAmount > expectation.maxSharesToRedeemRaw) {
|
|
749
|
+
reject(
|
|
750
|
+
"shares_exceed_max",
|
|
751
|
+
`Withdraw burns ${sharesAmount} shares which exceeds the approved maximum ${expectation.maxSharesToRedeemRaw}`
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
if (ix.accounts[0] !== expectation.wallet) {
|
|
755
|
+
reject("withdraw_mismatch", "Withdraw user is not the agent wallet");
|
|
756
|
+
}
|
|
757
|
+
if (ix.accounts[1] !== expectation.vault) {
|
|
758
|
+
reject("withdraw_mismatch", "Withdraw vault mismatch");
|
|
759
|
+
}
|
|
760
|
+
if (ix.accounts[5] !== expectation.userTokenAccount) {
|
|
761
|
+
reject(
|
|
762
|
+
"withdraw_mismatch",
|
|
763
|
+
"Withdraw token destination is not the approved account"
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
if (ix.accounts[6] !== expectation.asset) {
|
|
767
|
+
reject("withdraw_mismatch", "Withdraw token mint mismatch");
|
|
768
|
+
}
|
|
769
|
+
const expectedSharesAta = deriveAssociatedTokenAddress({
|
|
770
|
+
owner: expectation.wallet,
|
|
771
|
+
mint: expectation.shareMint
|
|
772
|
+
});
|
|
773
|
+
if (ix.accounts[7] !== expectedSharesAta) {
|
|
774
|
+
reject("withdraw_mismatch", "Withdraw share source must be the agent share ATA");
|
|
775
|
+
}
|
|
776
|
+
if (ix.accounts[8] !== expectation.shareMint) {
|
|
777
|
+
reject("withdraw_mismatch", "Withdraw share mint mismatch");
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
function expectTransferChecked(ixs, expectation) {
|
|
781
|
+
const ix = ixs.shift();
|
|
782
|
+
if (ix === void 0 || ix.programAddress !== SPL_TOKEN_PROGRAM_ID || ix.data[0] !== 12) {
|
|
783
|
+
reject("transfer_mismatch", `Expected TransferChecked for ${expectation.label}`);
|
|
784
|
+
}
|
|
785
|
+
const amount = readU64LE(ix.data, 1);
|
|
786
|
+
const decimals = ix.data[9];
|
|
787
|
+
if (expectation.amount !== null && amount !== expectation.amount) {
|
|
788
|
+
reject(
|
|
789
|
+
"amount_mismatch",
|
|
790
|
+
`${expectation.label} amount ${amount} does not match ${expectation.amount}`
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
if (decimals !== USDC_DECIMALS) {
|
|
794
|
+
reject("transfer_mismatch", `${expectation.label} has unexpected decimals`);
|
|
795
|
+
}
|
|
796
|
+
if (ix.accounts[0] !== expectation.source) {
|
|
797
|
+
reject("transfer_mismatch", `${expectation.label} source mismatch`);
|
|
798
|
+
}
|
|
799
|
+
if (ix.accounts[1] !== expectation.mint) {
|
|
800
|
+
reject("transfer_mismatch", `${expectation.label} mint mismatch`);
|
|
801
|
+
}
|
|
802
|
+
if (ix.accounts[2] !== expectation.destination) {
|
|
803
|
+
reject("transfer_mismatch", `${expectation.label} destination mismatch`);
|
|
804
|
+
}
|
|
805
|
+
if (ix.accounts[3] !== expectation.authority) {
|
|
806
|
+
reject("transfer_mismatch", `${expectation.label} authority mismatch`);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
function expectCloseAccount(ixs, expectation) {
|
|
810
|
+
const ix = ixs.shift();
|
|
811
|
+
if (ix === void 0 || ix.programAddress !== SPL_TOKEN_PROGRAM_ID || ix.data[0] !== 9) {
|
|
812
|
+
reject("close_mismatch", "Expected CloseAccount instruction");
|
|
813
|
+
}
|
|
814
|
+
if (ix.accounts[0] !== expectation.account) {
|
|
815
|
+
reject("close_mismatch", "CloseAccount target is not the temporary account");
|
|
816
|
+
}
|
|
817
|
+
if (ix.accounts[1] !== expectation.destination) {
|
|
818
|
+
reject("close_mismatch", "CloseAccount rent destination must be the sponsor");
|
|
819
|
+
}
|
|
820
|
+
if (ix.accounts[2] !== expectation.owner) {
|
|
821
|
+
reject("close_mismatch", "CloseAccount authority must be the agent wallet");
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
function expectMemo(ixs, memo) {
|
|
825
|
+
const ix = ixs.shift();
|
|
826
|
+
if (ix === void 0 || ix.programAddress !== MEMO_PROGRAM_ID) {
|
|
827
|
+
reject("memo_mismatch", "Expected Memo instruction");
|
|
828
|
+
}
|
|
829
|
+
if (Buffer.from(ix.data).toString("utf8") !== memo) {
|
|
830
|
+
reject("memo_mismatch", "Memo content does not match the paymentId");
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
function expectAtaCreateForOwner(ix, owner) {
|
|
834
|
+
if (ix.accounts[2] !== owner) {
|
|
835
|
+
reject(
|
|
836
|
+
"unexpected_instruction",
|
|
837
|
+
"Associated token account creation for a foreign owner"
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
function bytesStartWith(data, prefix) {
|
|
842
|
+
if (data.length < prefix.length) {
|
|
843
|
+
return false;
|
|
844
|
+
}
|
|
845
|
+
return prefix.every((byte, index) => data[index] === byte);
|
|
846
|
+
}
|
|
847
|
+
function readU64LE(data, offset) {
|
|
848
|
+
if (data.length < offset + 8) {
|
|
849
|
+
reject("invalid_transaction_encoding", "Instruction data too short for u64");
|
|
850
|
+
}
|
|
851
|
+
return Buffer.from(data.subarray(offset, offset + 8)).readBigUInt64LE(0);
|
|
852
|
+
}
|
|
853
|
+
function readU32LE(data, offset) {
|
|
854
|
+
if (data.length < offset + 4) {
|
|
855
|
+
reject("invalid_transaction_encoding", "Instruction data too short for u32");
|
|
856
|
+
}
|
|
857
|
+
return Buffer.from(data.subarray(offset, offset + 4)).readUInt32LE(0);
|
|
858
|
+
}
|
|
859
|
+
function readShortVec(bytes, startOffset) {
|
|
860
|
+
let value = 0;
|
|
861
|
+
let shift = 0;
|
|
862
|
+
let offset = startOffset;
|
|
863
|
+
while (offset < bytes.length) {
|
|
864
|
+
const byte = bytes[offset];
|
|
865
|
+
value |= (byte & 127) << shift;
|
|
866
|
+
offset += 1;
|
|
867
|
+
if ((byte & 128) === 0) {
|
|
868
|
+
return { value, nextOffset: offset };
|
|
869
|
+
}
|
|
870
|
+
shift += 7;
|
|
871
|
+
if (shift > 28) {
|
|
872
|
+
return null;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return null;
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
// ../../src/client/agent-wallet-signer.ts
|
|
879
|
+
var LocalKeypairAgentWalletSigner = class {
|
|
880
|
+
validationMode = "structured_intent_transaction";
|
|
881
|
+
keyPairSigner;
|
|
882
|
+
validationPolicy;
|
|
883
|
+
constructor(keyPairSigner2, validationPolicy) {
|
|
884
|
+
this.keyPairSigner = keyPairSigner2;
|
|
885
|
+
this.validationPolicy = validationPolicy;
|
|
886
|
+
}
|
|
887
|
+
get walletAddress() {
|
|
888
|
+
return this.keyPairSigner.address;
|
|
889
|
+
}
|
|
890
|
+
async signPayment(params) {
|
|
891
|
+
this.assertIntentWallet(params.intent.wallet);
|
|
892
|
+
validatePaymentIntentTransaction({
|
|
893
|
+
...params,
|
|
894
|
+
...this.validationPolicy === void 0 ? {} : { policy: this.validationPolicy }
|
|
895
|
+
});
|
|
896
|
+
return this.sign(params.serializedTransaction);
|
|
897
|
+
}
|
|
898
|
+
async signDeposit(params) {
|
|
899
|
+
this.assertIntentWallet(params.intent.wallet);
|
|
900
|
+
validateDepositIntentTransaction({
|
|
901
|
+
...params,
|
|
902
|
+
...this.validationPolicy === void 0 ? {} : { policy: this.validationPolicy }
|
|
903
|
+
});
|
|
904
|
+
return this.sign(params.serializedTransaction);
|
|
905
|
+
}
|
|
906
|
+
async signWithdrawal(params) {
|
|
907
|
+
this.assertIntentWallet(params.intent.wallet);
|
|
908
|
+
validateWithdrawalIntentTransaction({
|
|
909
|
+
...params,
|
|
910
|
+
...this.validationPolicy === void 0 ? {} : { policy: this.validationPolicy }
|
|
911
|
+
});
|
|
912
|
+
return this.sign(params.serializedTransaction);
|
|
913
|
+
}
|
|
914
|
+
assertIntentWallet(wallet) {
|
|
915
|
+
if (wallet !== this.keyPairSigner.address) {
|
|
916
|
+
throw new IntentValidationError(
|
|
917
|
+
"wallet_mismatch",
|
|
918
|
+
"Intent wallet does not match this signer's wallet"
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
async signApiMessage(message) {
|
|
923
|
+
const signature = await signBytes(
|
|
924
|
+
this.keyPairSigner.keyPair.privateKey,
|
|
925
|
+
message
|
|
926
|
+
);
|
|
927
|
+
return bs584.encode(signature);
|
|
928
|
+
}
|
|
929
|
+
async sign(serializedTransaction) {
|
|
930
|
+
const { serializedBase64, transaction } = await addSignaturesToSerializedTransaction({
|
|
931
|
+
serializedBase64: serializedTransaction,
|
|
932
|
+
signers: [this.keyPairSigner.keyPair]
|
|
933
|
+
});
|
|
934
|
+
const agentSignature = signatureBase58ForSigner(
|
|
935
|
+
transaction,
|
|
936
|
+
this.keyPairSigner.address
|
|
937
|
+
);
|
|
938
|
+
if (agentSignature === null) {
|
|
939
|
+
throw new IntentValidationError(
|
|
940
|
+
"signing_failed",
|
|
941
|
+
"Agent signature was not produced"
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
return { serializedTransaction: serializedBase64, agentSignature };
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
// ../../src/api/wallet-auth.ts
|
|
949
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
950
|
+
import bs585 from "bs58";
|
|
951
|
+
import nacl from "tweetnacl";
|
|
952
|
+
var WALLET_AUTH_WALLET_HEADER = "x-subly-wallet";
|
|
953
|
+
var WALLET_AUTH_SIGNED_AT_HEADER = "x-subly-signed-at";
|
|
954
|
+
var WALLET_AUTH_SIGNATURE_HEADER = "x-subly-signature";
|
|
955
|
+
function sha256Hex(data) {
|
|
956
|
+
return createHash3("sha256").update(data, "utf8").digest("hex");
|
|
957
|
+
}
|
|
958
|
+
function walletAuthMessage(params) {
|
|
959
|
+
return new TextEncoder().encode(
|
|
960
|
+
`subly-api:${params.method.toUpperCase()}:${params.path}:${sha256Hex(
|
|
961
|
+
params.rawBody
|
|
962
|
+
)}:${params.signedAtMs}`
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// ../../src/client/wallet-auth-headers.ts
|
|
967
|
+
async function walletAuthHeaders(params) {
|
|
968
|
+
const signedAtMs = String(Date.now());
|
|
969
|
+
const message = walletAuthMessage({
|
|
970
|
+
method: params.method,
|
|
971
|
+
path: new URL(params.url).pathname,
|
|
972
|
+
rawBody: params.body ?? "",
|
|
973
|
+
signedAtMs
|
|
974
|
+
});
|
|
975
|
+
return {
|
|
976
|
+
[WALLET_AUTH_WALLET_HEADER]: params.signer.walletAddress,
|
|
977
|
+
[WALLET_AUTH_SIGNED_AT_HEADER]: signedAtMs,
|
|
978
|
+
[WALLET_AUTH_SIGNATURE_HEADER]: await params.signer.signApiMessage(message)
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// ../../src/client/onboarding.ts
|
|
983
|
+
var SELF_SERVE_POLICY_ID = "self-serve";
|
|
984
|
+
var OnboardingError = class extends Error {
|
|
985
|
+
constructor(step, message, detail = null) {
|
|
986
|
+
super(message);
|
|
987
|
+
this.step = step;
|
|
988
|
+
this.detail = detail;
|
|
989
|
+
this.name = "OnboardingError";
|
|
990
|
+
}
|
|
991
|
+
step;
|
|
992
|
+
detail;
|
|
993
|
+
};
|
|
994
|
+
async function ensureWalletOnboarded(params) {
|
|
995
|
+
const fetchImpl = params.fetchImpl ?? fetch;
|
|
996
|
+
const baseUrl = params.facilitatorBaseUrl.replace(/\/$/, "");
|
|
997
|
+
const post = async (step, path, body) => {
|
|
998
|
+
const url = `${baseUrl}${path}`;
|
|
999
|
+
const serialized = JSON.stringify(body);
|
|
1000
|
+
const response = await fetchImpl(url, {
|
|
1001
|
+
method: "POST",
|
|
1002
|
+
headers: {
|
|
1003
|
+
...await walletAuthHeaders({
|
|
1004
|
+
signer: params.signer,
|
|
1005
|
+
method: "POST",
|
|
1006
|
+
url,
|
|
1007
|
+
body: serialized
|
|
1008
|
+
}),
|
|
1009
|
+
"content-type": "application/json"
|
|
1010
|
+
},
|
|
1011
|
+
body: serialized
|
|
1012
|
+
});
|
|
1013
|
+
if (response.status !== 200) {
|
|
1014
|
+
let detail = null;
|
|
1015
|
+
try {
|
|
1016
|
+
detail = await response.json();
|
|
1017
|
+
} catch {
|
|
1018
|
+
detail = null;
|
|
1019
|
+
}
|
|
1020
|
+
throw new OnboardingError(
|
|
1021
|
+
step,
|
|
1022
|
+
`wallet onboarding ${step} failed with ${response.status}`,
|
|
1023
|
+
detail
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
const wallet = params.signer.walletAddress;
|
|
1028
|
+
await post("register", "/v1/wallets/agent", {
|
|
1029
|
+
wallet,
|
|
1030
|
+
signingPolicyId: SELF_SERVE_POLICY_ID,
|
|
1031
|
+
signingMode: "non_interactive",
|
|
1032
|
+
signerValidationMode: params.signer.validationMode,
|
|
1033
|
+
signerProvider: "local-keypair",
|
|
1034
|
+
activateForPayments: true
|
|
1035
|
+
});
|
|
1036
|
+
await post("sync", `/v1/wallets/${wallet}/sync`, { source: "chain" });
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// ../../src/client/lookup-tables.ts
|
|
1040
|
+
import { fetchAllMaybeAddressLookupTable } from "@solana-program/address-lookup-table";
|
|
1041
|
+
import { address, getCompiledTransactionMessageDecoder as getCompiledTransactionMessageDecoder2 } from "@solana/kit";
|
|
1042
|
+
function lookupTableAddressesForTransaction(serializedTransaction) {
|
|
1043
|
+
const wire = Buffer.from(serializedTransaction, "base64");
|
|
1044
|
+
let offset = 0;
|
|
1045
|
+
let signatureCount = 0;
|
|
1046
|
+
let shift = 0;
|
|
1047
|
+
while (offset < wire.length) {
|
|
1048
|
+
const byte = wire[offset];
|
|
1049
|
+
signatureCount |= (byte & 127) << shift;
|
|
1050
|
+
offset += 1;
|
|
1051
|
+
if ((byte & 128) === 0) {
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
shift += 7;
|
|
1055
|
+
}
|
|
1056
|
+
const messageBytes = wire.subarray(offset + signatureCount * 64);
|
|
1057
|
+
const compiled = getCompiledTransactionMessageDecoder2().decode(messageBytes);
|
|
1058
|
+
const lookups = compiled.addressTableLookups ?? [];
|
|
1059
|
+
return lookups.map((lookup) => String(lookup.lookupTableAddress));
|
|
1060
|
+
}
|
|
1061
|
+
async function fetchLookupTablesForTransaction(rpc2, serializedTransaction) {
|
|
1062
|
+
const addresses = lookupTableAddressesForTransaction(serializedTransaction);
|
|
1063
|
+
if (addresses.length === 0) {
|
|
1064
|
+
return {};
|
|
1065
|
+
}
|
|
1066
|
+
const tables = await fetchAllMaybeAddressLookupTable(
|
|
1067
|
+
rpc2,
|
|
1068
|
+
addresses.map((value) => address(value))
|
|
1069
|
+
);
|
|
1070
|
+
const result = {};
|
|
1071
|
+
for (const table of tables) {
|
|
1072
|
+
if (table.exists) {
|
|
1073
|
+
result[table.address] = table.data.addresses.map(String);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
return result;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// ../../src/solana/keys.ts
|
|
1080
|
+
import { readFileSync } from "node:fs";
|
|
1081
|
+
import bs586 from "bs58";
|
|
1082
|
+
import {
|
|
1083
|
+
createKeyPairSignerFromBytes
|
|
1084
|
+
} from "@solana/kit";
|
|
1085
|
+
async function loadKeyPairSigner(params) {
|
|
1086
|
+
const { base58Secret, jsonFilePath, label } = params;
|
|
1087
|
+
if (base58Secret !== void 0 && base58Secret.length > 0) {
|
|
1088
|
+
const bytes = bs586.decode(base58Secret);
|
|
1089
|
+
if (bytes.length !== 64) {
|
|
1090
|
+
throw new Error(`${label} base58 secret must decode to 64 bytes`);
|
|
1091
|
+
}
|
|
1092
|
+
return createKeyPairSignerFromBytes(bytes);
|
|
1093
|
+
}
|
|
1094
|
+
if (jsonFilePath !== void 0 && jsonFilePath.length > 0) {
|
|
1095
|
+
const raw = JSON.parse(readFileSync(jsonFilePath, "utf8"));
|
|
1096
|
+
if (!Array.isArray(raw) || raw.length !== 64) {
|
|
1097
|
+
throw new Error(`${label} keypair file must be a 64-byte JSON array`);
|
|
1098
|
+
}
|
|
1099
|
+
return createKeyPairSignerFromBytes(Uint8Array.from(raw));
|
|
1100
|
+
}
|
|
1101
|
+
throw new Error(`${label} keypair is not configured`);
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// ../../src/solana/rpc.ts
|
|
1105
|
+
import { createSolanaRpc } from "@solana/kit";
|
|
1106
|
+
function createRpcFromEnv(env = process.env) {
|
|
1107
|
+
const url = env.SOLANA_RPC_URL;
|
|
1108
|
+
if (url === void 0 || url.length === 0) {
|
|
1109
|
+
throw new Error("SOLANA_RPC_URL must be configured");
|
|
1110
|
+
}
|
|
1111
|
+
return createSolanaRpc(url);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
// ../../demo/shared.ts
|
|
1115
|
+
function fail(message) {
|
|
1116
|
+
console.error(message);
|
|
1117
|
+
process.exit(1);
|
|
1118
|
+
}
|
|
1119
|
+
function requireEnv(name) {
|
|
1120
|
+
const value = process.env[name];
|
|
1121
|
+
if (value === void 0 || value.length === 0) {
|
|
1122
|
+
fail(`Missing required environment variable: ${name}`);
|
|
1123
|
+
}
|
|
1124
|
+
return value;
|
|
1125
|
+
}
|
|
1126
|
+
function formatRawUsdc(raw) {
|
|
1127
|
+
const units = BigInt(raw);
|
|
1128
|
+
const whole = units / 1000000n;
|
|
1129
|
+
const fraction = (units % 1000000n).toString().padStart(6, "0");
|
|
1130
|
+
return `${whole}.${fraction}`;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
// ../../demo/deposit.ts
|
|
1134
|
+
requireEnv("SOLANA_RPC_URL");
|
|
1135
|
+
var facilitatorBaseUrl = process.env.SUBLY_FACILITATOR_URL ?? "http://localhost:3000";
|
|
1136
|
+
var amountRawUsdc = process.argv[2];
|
|
1137
|
+
if (amountRawUsdc === void 0 || !/^[1-9]\d*$/.test(amountRawUsdc)) {
|
|
1138
|
+
fail(
|
|
1139
|
+
"Usage: npm run demo:deposit -- <amountRawUsdc> (positive integer, e.g. 60000000 = 60 USDC)"
|
|
1140
|
+
);
|
|
1141
|
+
}
|
|
1142
|
+
var keyPairSigner = await loadKeyPairSigner({
|
|
1143
|
+
base58Secret: process.env.SUBLY_DEMO_AGENT_KEYPAIR,
|
|
1144
|
+
jsonFilePath: process.env.SUBLY_DEMO_AGENT_KEYPAIR_PATH,
|
|
1145
|
+
label: "SUBLY_DEMO_AGENT_KEYPAIR"
|
|
1146
|
+
});
|
|
1147
|
+
var signer = new LocalKeypairAgentWalletSigner(keyPairSigner);
|
|
1148
|
+
var rpc = createRpcFromEnv();
|
|
1149
|
+
async function postJson(path, body) {
|
|
1150
|
+
const url = `${facilitatorBaseUrl}${path}`;
|
|
1151
|
+
const serialized = JSON.stringify(body);
|
|
1152
|
+
const response = await fetch(url, {
|
|
1153
|
+
method: "POST",
|
|
1154
|
+
headers: {
|
|
1155
|
+
...await walletAuthHeaders({
|
|
1156
|
+
signer,
|
|
1157
|
+
method: "POST",
|
|
1158
|
+
url,
|
|
1159
|
+
body: serialized
|
|
1160
|
+
}),
|
|
1161
|
+
"content-type": "application/json"
|
|
1162
|
+
},
|
|
1163
|
+
body: serialized
|
|
1164
|
+
});
|
|
1165
|
+
const text = await response.text();
|
|
1166
|
+
if (response.status !== 200) {
|
|
1167
|
+
fail(`[deposit] ${path} failed with ${response.status}: ${text}`);
|
|
1168
|
+
}
|
|
1169
|
+
return JSON.parse(text);
|
|
1170
|
+
}
|
|
1171
|
+
console.log(`[deposit] agent wallet: ${signer.walletAddress}`);
|
|
1172
|
+
console.log(`[deposit] facilitator: ${facilitatorBaseUrl}`);
|
|
1173
|
+
console.log("\n[deposit] step 0: ensure the wallet is registered (self-serve)");
|
|
1174
|
+
await ensureWalletOnboarded({ facilitatorBaseUrl, signer });
|
|
1175
|
+
console.log("[deposit] wallet registered and synced");
|
|
1176
|
+
console.log(
|
|
1177
|
+
`
|
|
1178
|
+
[deposit] step 1: prepare deposit of ${formatRawUsdc(amountRawUsdc)} USDC`
|
|
1179
|
+
);
|
|
1180
|
+
var prepared = await postJson("/v1/deposits/prepare", {
|
|
1181
|
+
wallet: signer.walletAddress,
|
|
1182
|
+
amountRawUsdc
|
|
1183
|
+
});
|
|
1184
|
+
console.log(`[deposit] prepared (depositId=${prepared.depositId})`);
|
|
1185
|
+
console.log(
|
|
1186
|
+
"\n[deposit] step 2: validate the signing intent against the transaction, sign locally"
|
|
1187
|
+
);
|
|
1188
|
+
var signed = await signer.signDeposit({
|
|
1189
|
+
intent: prepared.signingIntent,
|
|
1190
|
+
serializedTransaction: prepared.serializedTransaction,
|
|
1191
|
+
lookupTables: await fetchLookupTablesForTransaction(
|
|
1192
|
+
rpc,
|
|
1193
|
+
prepared.serializedTransaction
|
|
1194
|
+
)
|
|
1195
|
+
});
|
|
1196
|
+
console.log("[deposit] agent signature attached");
|
|
1197
|
+
console.log("\n[deposit] step 3: submit (sponsor co-signs and broadcasts)");
|
|
1198
|
+
var submitted = await postJson("/v1/deposits/submit", {
|
|
1199
|
+
depositId: prepared.depositId,
|
|
1200
|
+
serializedTransaction: signed.serializedTransaction,
|
|
1201
|
+
agentSignature: signed.agentSignature
|
|
1202
|
+
});
|
|
1203
|
+
console.log(`[deposit] status: ${submitted.status}`);
|
|
1204
|
+
if (submitted.txSignature !== null) {
|
|
1205
|
+
console.log(
|
|
1206
|
+
`[deposit] transaction: https://solscan.io/tx/${submitted.txSignature}`
|
|
1207
|
+
);
|
|
1208
|
+
}
|
|
1209
|
+
if (submitted.status !== "confirmed") {
|
|
1210
|
+
fail(
|
|
1211
|
+
`[deposit] not confirmed (errorCode=${submitted.errorCode}); check GET /v1/deposits/${prepared.depositId} and the facilitator logs`
|
|
1212
|
+
);
|
|
1213
|
+
}
|
|
1214
|
+
console.log(
|
|
1215
|
+
`[deposit] confirmed: ${formatRawUsdc(submitted.actualDepositRawUsdc ?? "0")} USDC deposited, ${submitted.sharesMintedRaw ?? "0"} raw shares minted`
|
|
1216
|
+
);
|