From 31afd67bd9f70a3ba5c3e95193342ac50f0c7c91 Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Thu, 24 Sep 2026 01:03:36 +0000 Subject: [PATCH 1/5] fix(wasm-solana): require configuration for Jito staking Reject JITO stake and unstake intents without stakePoolConfig instead of allowing them to produce native Solana operations. Require the configuration in the TypeScript intent union and test both protocol paths through the public intent builder. This prevents explicit Jito requests from silently changing protocols. Ticket: WCN-1886 Session-Id: 76b47acc-4819-46b2-9c75-deeed43d65a9 Task-Id: 3149eea2-6c79-4b21-a55e-d5e05a4eac7f --- packages/wasm-solana/js/index.ts | 1 + packages/wasm-solana/js/intentBuilder.ts | 42 ++++- packages/wasm-solana/src/intent/build.rs | 67 +++++-- packages/wasm-solana/test/intentBuilder.ts | 194 ++++++++++++++++++++- 4 files changed, 283 insertions(+), 21 deletions(-) diff --git a/packages/wasm-solana/js/index.ts b/packages/wasm-solana/js/index.ts index 6ba44860c00..786d3361b69 100644 --- a/packages/wasm-solana/js/index.ts +++ b/packages/wasm-solana/js/index.ts @@ -27,6 +27,7 @@ export { buildFromIntent, buildFromIntent as buildTransactionFromIntent } from " export type { BaseIntent, PaymentIntent, + GoUnstakeIntent, StakeIntent, UnstakeIntent, ClaimIntent, diff --git a/packages/wasm-solana/js/intentBuilder.ts b/packages/wasm-solana/js/intentBuilder.ts index 5357855d262..28e1ae2aaa5 100644 --- a/packages/wasm-solana/js/intentBuilder.ts +++ b/packages/wasm-solana/js/intentBuilder.ts @@ -103,14 +103,28 @@ export interface PaymentIntent extends BaseIntent { recipients?: Recipient[]; } +/** goUnstake intent (payment-shaped unstaking operation) */ +export interface GoUnstakeIntent extends BaseIntent { + intentType: "goUnstake"; + recipients?: Recipient[]; +} + /** Stake intent */ -export interface StakeIntent extends BaseIntent { +type StakeIntentFields = BaseIntent & { intentType: "stake"; validatorAddress: string; amount?: { value: bigint }; - stakingType?: "NATIVE" | "JITO" | "MARINADE"; - stakePoolConfig?: StakePoolConfig; -} +}; + +export type StakeIntent = + | (StakeIntentFields & { + stakingType?: "NATIVE" | "MARINADE"; + stakePoolConfig?: StakePoolConfig; + }) + | (StakeIntentFields & { + stakingType: "JITO"; + stakePoolConfig: StakePoolConfig; + }); /** Stake pool configuration (for Jito) */ export interface StakePoolConfig { @@ -126,15 +140,24 @@ export interface StakePoolConfig { } /** Unstake intent */ -export interface UnstakeIntent extends BaseIntent { +type UnstakeIntentFields = BaseIntent & { intentType: "unstake"; stakingAddress: string; validatorAddress?: string; amount?: { value: bigint }; remainingStakingAmount?: { value: bigint }; - stakingType?: "NATIVE" | "JITO" | "MARINADE"; - stakePoolConfig?: StakePoolConfig; -} +}; + +export type UnstakeIntent = + | (UnstakeIntentFields & { + stakingType?: "NATIVE" | "MARINADE"; + stakePoolConfig?: StakePoolConfig; + }) + | (UnstakeIntentFields & { + stakingType: "JITO"; + validatorAddress: string; + stakePoolConfig: StakePoolConfig & { validatorList: string }; + }); /** Claim intent (withdraw from deactivated stake) */ export interface ClaimIntent extends BaseIntent { @@ -223,6 +246,7 @@ export interface CustomTxKey { /** Union of all supported intent types */ export type SolanaIntent = | PaymentIntent + | GoUnstakeIntent | StakeIntent | UnstakeIntent | ClaimIntent @@ -272,7 +296,7 @@ export type SolanaIntent = * ``` */ export function buildFromIntent( - intent: BaseIntent, + intent: SolanaIntent, params: BuildFromIntentParams, ): BuildFromIntentResult { const result = IntentNamespace.build_from_intent(intent, params) as WasmBuildResult; diff --git a/packages/wasm-solana/src/intent/build.rs b/packages/wasm-solana/src/intent/build.rs index d23664bd040..f5d10d372cd 100644 --- a/packages/wasm-solana/src/intent/build.rs +++ b/packages/wasm-solana/src/intent/build.rs @@ -312,6 +312,19 @@ fn build_stake( let intent: StakeIntent = serde_json::from_value(intent_json.clone()) .map_err(|e| WasmSolanaError::new(&format!("Failed to parse stake intent: {}", e)))?; + let jito_config = if intent.staking_type == Some(StakingType::Jito) { + Some( + intent + .stake_pool_config + .as_ref() + .ok_or_else(|| { + WasmSolanaError::new("stakePoolConfig is required for JITO staking") + })?, + ) + } else { + None + }; + let fee_payer: Pubkey = params .fee_payer .parse() @@ -319,11 +332,8 @@ fn build_stake( let amount: u64 = intent.amount.as_ref().map(|a| a.value).unwrap_or(0); - // Check if Jito staking - if intent.staking_type == Some(StakingType::Jito) { - if let Some(config) = &intent.stake_pool_config { - return build_jito_stake(config, &fee_payer, &intent.validator_address, amount); - } + if let Some(config) = jito_config { + return build_jito_stake(config, &fee_payer, &intent.validator_address, amount); } // Generate stake account keypair (used by both native and Marinade) @@ -524,6 +534,14 @@ fn build_unstake( let intent: UnstakeIntent = serde_json::from_value(intent_json.clone()) .map_err(|e| WasmSolanaError::new(&format!("Failed to parse unstake intent: {}", e)))?; + let jito_config = if intent.staking_type == Some(StakingType::Jito) { + Some(intent.stake_pool_config.as_ref().ok_or_else(|| { + WasmSolanaError::new("stakePoolConfig is required for JITO unstaking") + })?) + } else { + None + }; + let fee_payer: Pubkey = params .fee_payer .parse() @@ -543,12 +561,9 @@ fn build_unstake( .parse() .map_err(|_| WasmSolanaError::new("Invalid stakingAddress"))?; - // Check if Jito unstaking - if intent.staking_type == Some(StakingType::Jito) { - if let Some(config) = &intent.stake_pool_config { - let amount: u64 = intent.amount.as_ref().map(|a| a.value).unwrap_or(0); - return build_jito_unstake(config, &fee_payer, &intent.validator_address, amount); - } + if let Some(config) = jito_config { + let amount: u64 = intent.amount.as_ref().map(|a| a.value).unwrap_or(0); + return build_jito_unstake(config, &fee_payer, &intent.validator_address, amount); } // Check if partial unstake @@ -1313,6 +1328,21 @@ mod tests { ); } + #[test] + fn test_jito_stake_requires_stake_pool_config() { + let intent = serde_json::json!({ + "intentType": "stake", + "validatorAddress": "invalid-validator", + "stakingType": "JITO", + "amount": { "value": "1000000000" } + }); + + let error = build_from_intent(&intent, &test_params()).unwrap_err(); + assert!(error + .to_string() + .contains("stakePoolConfig is required for JITO staking")); + } + #[test] fn test_stake_with_durable_nonce_structure() { use crate::transaction::TransactionExt; @@ -1356,6 +1386,21 @@ mod tests { ); } + #[test] + fn test_jito_unstake_requires_stake_pool_config() { + let intent = serde_json::json!({ + "intentType": "unstake", + "stakingType": "JITO", + "stakingAddress": "invalid-staking-address", + "amount": { "value": "1000000000" } + }); + + let error = build_from_intent(&intent, &test_params()).unwrap_err(); + assert!(error + .to_string() + .contains("stakePoolConfig is required for JITO unstaking")); + } + #[test] fn test_build_deactivate_intent() { let intent = serde_json::json!({ diff --git a/packages/wasm-solana/test/intentBuilder.ts b/packages/wasm-solana/test/intentBuilder.ts index c0e298cc6f2..a95b2e84fc0 100644 --- a/packages/wasm-solana/test/intentBuilder.ts +++ b/packages/wasm-solana/test/intentBuilder.ts @@ -9,17 +9,34 @@ import assert from "assert"; import { - buildFromIntent, + buildFromIntent as buildFromIntentApi, Transaction, parseTransaction, getAssociatedTokenAddress, } from "../dist/cjs/js/index.js"; +import type { BaseIntent, BuildFromIntentParams, SolanaIntent } from "../js/intentBuilder.js"; + +// Keep runtime-invalid inputs available to exercise Rust/WASM validation. +function buildFromIntent(intent: T, params: BuildFromIntentParams) { + return buildFromIntentApi(intent as SolanaIntent, params); +} describe("buildFromIntent", function () { // Common test params const feePayer = "DgT9qyYwYKBRDyDw3EfR12LHQCQjtNrKu2qMsXHuosmB"; const blockhash = "GWaQEymC3Z9SHM2gkh8u12xL1zJPMHPCSVR3pSDpEXE4"; const splTokenProgramId = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"; + const stakePoolProgramId = "SPoo1Ku8WFXoNDMHPsrGSTSG1Y47rzgn41SLUNakuHy"; + const jitoStakePoolConfig = { + stakePoolAddress: feePayer, + withdrawAuthority: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + reserveStake: feePayer, + destinationPoolAccount: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + managerFeeAccount: feePayer, + poolMint: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + validatorList: feePayer, + sourcePoolAccount: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + }; describe("payment intent", function () { it("should build a simple payment transaction", function () { @@ -145,6 +162,181 @@ describe("buildFromIntent", function () { const stakeDelegate = parsed.instructionsData.find((i: any) => i.type === "StakingDelegate"); assert(stakeDelegate, "Should have StakingDelegate instruction"); }); + + it("should preserve explicitly selected native staking", function () { + const result = buildFromIntent( + { + intentType: "stake", + validatorAddress: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + amount: { value: 1000000000n }, + stakingType: "NATIVE", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + + assert.equal(result.generatedKeypairs.length, 1); + assert(parsed.instructionsData.some((i: any) => i.type === "CreateAccount")); + assert(parsed.instructionsData.some((i: any) => i.type === "StakeInitialize")); + assert(parsed.instructionsData.some((i: any) => i.type === "StakingDelegate")); + }); + }); + + describe("Jito stake intent", function () { + + it("should reject missing pool configuration before native stake construction", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "stake", + validatorAddress: "invalid-validator", + amount: { value: 1000000000n }, + stakingType: "JITO", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /stakePoolConfig is required for JITO staking/, + ); + }); + + it("should retain required-field errors for incomplete pool configuration", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "stake", + validatorAddress: feePayer, + amount: { value: 1000000000n }, + stakingType: "JITO", + stakePoolConfig: {}, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /Missing reserveStake/, + ); + }); + + it("should build a stake-pool deposit for complete Jito configuration", function () { + const result = buildFromIntent( + { + intentType: "stake", + validatorAddress: feePayer, + amount: { value: 1000000000n }, + stakingType: "JITO", + stakePoolConfig: jitoStakePoolConfig, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + const deposit = parsed.instructionsData.find( + (i: any) => i.type === "StakePoolDepositSol", + ); + + assert.equal( + result.generatedKeypairs.length, + 0, + "Jito deposit should not generate a stake keypair", + ); + assert.equal( + result.transaction.instructions.length, + 1, + "Should emit only the pool deposit", + ); + assert.equal(result.transaction.instructions[0].programId, stakePoolProgramId); + assert(deposit, "Should emit StakePoolDepositSol"); + assert.equal((deposit as any).lamports, 1000000000n); + assert( + !parsed.instructionsData.some((i: any) => i.type === "StakingDelegate"), + "Jito deposit must not contain native delegation", + ); + }); + }); + + describe("Jito unstake intent", function () { + + it("should reject missing pool configuration before native unstake construction", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "unstake", + stakingAddress: "invalid-staking-address", + amount: { value: 1000000n }, + stakingType: "JITO", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /stakePoolConfig is required for JITO unstaking/, + ); + }); + + it("should retain required-field errors for incomplete pool configuration", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "unstake", + stakingAddress: feePayer, + validatorAddress: feePayer, + amount: { value: 1000000n }, + stakingType: "JITO", + stakePoolConfig: { stakePoolAddress: feePayer }, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /Missing validatorList/, + ); + }); + + it("should build a stake-pool withdrawal for complete Jito configuration", function () { + const result = buildFromIntent( + { + intentType: "unstake", + stakingAddress: feePayer, + validatorAddress: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + amount: { value: 1000000n }, + stakingType: "JITO", + stakePoolConfig: jitoStakePoolConfig, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + const withdrawal = parsed.instructionsData.find( + (i: any) => i.type === "StakePoolWithdrawStake", + ); + + assert.equal( + result.transaction.instructions.some((i) => i.programId === stakePoolProgramId), + true, + ); + assert(withdrawal, "Should emit StakePoolWithdrawStake"); + assert.equal((withdrawal as any).poolTokens, 1000000n); + }); + }); + + describe("native unstake intent", function () { + it("should preserve explicit native deactivation", function () { + const result = buildFromIntent( + { + intentType: "unstake", + stakingAddress: "FKjSjCqByQRwSzZoMXA7bKnDbJe41YgJTHFFzBeC42bH", + stakingType: "NATIVE", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + + assert.equal(result.generatedKeypairs.length, 0); + assert( + parsed.instructionsData.some((i: any) => i.type === "StakingDeactivate"), + "Native unstake should still deactivate the existing stake account", + ); + assert( + !result.transaction.instructions.some((i) => i.programId === stakePoolProgramId), + "Native unstake must not use the Jito stake-pool program", + ); + }); }); describe("deactivate intent", function () { From 202b03c10c0f149c520409b1c90517437371c44b Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Thu, 24 Sep 2026 01:05:09 +0000 Subject: [PATCH 2/5] fix(wasm-solana): validate Jito unstake config first Validate all Jito unstake account fields before generating the output stake and transfer-authority keypairs. This preserves field-specific errors while ensuring malformed pool metadata has no key-generation side effects. Ticket: WCN-1886 Session-Id: 76b47acc-4819-46b2-9c75-deeed43d65a9 Task-Id: 3149eea2-6c79-4b21-a55e-d5e05a4eac7f --- packages/wasm-solana/src/intent/build.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/wasm-solana/src/intent/build.rs b/packages/wasm-solana/src/intent/build.rs index f5d10d372cd..14910a2c035 100644 --- a/packages/wasm-solana/src/intent/build.rs +++ b/packages/wasm-solana/src/intent/build.rs @@ -678,16 +678,6 @@ fn build_jito_unstake( let ata_program: Pubkey = SPL_ATA_PROGRAM_ID.parse().unwrap(); let clock_sysvar: Pubkey = solana_sdk::sysvar::clock::ID; - // Generate destination stake account - let unstake_keypair = Keypair::new(); - let unstake_address = unstake_keypair.address(); - let unstake_pubkey: Pubkey = unstake_address.parse().unwrap(); - - // Generate transfer authority - let transfer_authority_keypair = Keypair::new(); - let transfer_authority_address = transfer_authority_keypair.address(); - let transfer_authority_pubkey: Pubkey = transfer_authority_address.parse().unwrap(); - // Parse config addresses (with derivation for missing fields) let stake_pool: Pubkey = config .stake_pool_address @@ -746,6 +736,15 @@ fn build_jito_unstake( .parse() .map_err(|_| WasmSolanaError::new("Invalid managerFeeAccount"))?; + // Generate keys only after all Jito configuration fields are validated. + let unstake_keypair = Keypair::new(); + let unstake_address = unstake_keypair.address(); + let unstake_pubkey: Pubkey = unstake_address.parse().unwrap(); + + let transfer_authority_keypair = Keypair::new(); + let transfer_authority_address = transfer_authority_keypair.address(); + let transfer_authority_pubkey: Pubkey = transfer_authority_address.parse().unwrap(); + // 1. Approve: allow transfer_authority to spend pool tokens from user's ATA // SPL Token Approve instruction (index 4): [4u8] + amount as u64 LE let mut approve_data = vec![4u8]; From ad975e835a0bcbe97ec7e4c030d60add64a49788 Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Thu, 24 Sep 2026 01:11:50 +0000 Subject: [PATCH 3/5] style(wasm-solana): remove blank lines from Jito tests Remove empty lines after the Jito describe declarations to satisfy the repository's source formatting check. Ticket: WCN-1886 Session-Id: 76b47acc-4819-46b2-9c75-deeed43d65a9 Task-Id: 3149eea2-6c79-4b21-a55e-d5e05a4eac7f --- packages/wasm-solana/test/intentBuilder.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/wasm-solana/test/intentBuilder.ts b/packages/wasm-solana/test/intentBuilder.ts index a95b2e84fc0..1f8a628bde8 100644 --- a/packages/wasm-solana/test/intentBuilder.ts +++ b/packages/wasm-solana/test/intentBuilder.ts @@ -183,7 +183,6 @@ describe("buildFromIntent", function () { }); describe("Jito stake intent", function () { - it("should reject missing pool configuration before native stake construction", function () { assert.throws( () => @@ -254,7 +253,6 @@ describe("buildFromIntent", function () { }); describe("Jito unstake intent", function () { - it("should reject missing pool configuration before native unstake construction", function () { assert.throws( () => From 54d8bdfe652e6a257106541b1f758fdab5e59e3a Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Thu, 24 Sep 2026 01:20:56 +0000 Subject: [PATCH 4/5] style(wasm-solana): align Jito tests with Prettier Collapse short Jito parser assertions while keeping long assertions wrapped at the configured print width. This matches the package's Prettier check. Ticket: WCN-1886 Session-Id: 76b47acc-4819-46b2-9c75-deeed43d65a9 Task-Id: 3149eea2-6c79-4b21-a55e-d5e05a4eac7f --- packages/wasm-solana/test/intentBuilder.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/wasm-solana/test/intentBuilder.ts b/packages/wasm-solana/test/intentBuilder.ts index 1f8a628bde8..1ba82c69183 100644 --- a/packages/wasm-solana/test/intentBuilder.ts +++ b/packages/wasm-solana/test/intentBuilder.ts @@ -228,20 +228,14 @@ describe("buildFromIntent", function () { { feePayer, nonce: { type: "blockhash", value: blockhash } }, ); const parsed = parseTransaction(result.transaction); - const deposit = parsed.instructionsData.find( - (i: any) => i.type === "StakePoolDepositSol", - ); + const deposit = parsed.instructionsData.find((i: any) => i.type === "StakePoolDepositSol"); assert.equal( result.generatedKeypairs.length, 0, "Jito deposit should not generate a stake keypair", ); - assert.equal( - result.transaction.instructions.length, - 1, - "Should emit only the pool deposit", - ); + assert.equal(result.transaction.instructions.length, 1, "Should emit only the pool deposit"); assert.equal(result.transaction.instructions[0].programId, stakePoolProgramId); assert(deposit, "Should emit StakePoolDepositSol"); assert.equal((deposit as any).lamports, 1000000000n); From e7e8734eca52b8b626bff578680cb5904f950889 Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Thu, 24 Sep 2026 01:28:15 +0000 Subject: [PATCH 5/5] style(wasm-solana): format Jito config conditional Match rustfmt's layout for the Jito configuration conditional. This keeps the wasm-solana source formatting check passing. Ticket: WCN-1886 Session-Id: 76b47acc-4819-46b2-9c75-deeed43d65a9 Task-Id: 3149eea2-6c79-4b21-a55e-d5e05a4eac7f --- packages/wasm-solana/src/intent/build.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/wasm-solana/src/intent/build.rs b/packages/wasm-solana/src/intent/build.rs index 14910a2c035..0635dcecd54 100644 --- a/packages/wasm-solana/src/intent/build.rs +++ b/packages/wasm-solana/src/intent/build.rs @@ -312,18 +312,14 @@ fn build_stake( let intent: StakeIntent = serde_json::from_value(intent_json.clone()) .map_err(|e| WasmSolanaError::new(&format!("Failed to parse stake intent: {}", e)))?; - let jito_config = if intent.staking_type == Some(StakingType::Jito) { - Some( - intent - .stake_pool_config - .as_ref() - .ok_or_else(|| { - WasmSolanaError::new("stakePoolConfig is required for JITO staking") - })?, - ) - } else { - None - }; + let jito_config = + if intent.staking_type == Some(StakingType::Jito) { + Some(intent.stake_pool_config.as_ref().ok_or_else(|| { + WasmSolanaError::new("stakePoolConfig is required for JITO staking") + })?) + } else { + None + }; let fee_payer: Pubkey = params .fee_payer