From 03072b5eb292483859e28338adbdc0935452085d Mon Sep 17 00:00:00 2001 From: Damian Glinojecki Date: Wed, 17 Jul 2024 11:53:11 -0500 Subject: [PATCH] modifying the solana tests to only run the hex payload tests --- .../signing/solana/solana.programs.test.ts | 29 -- .../e2e/signing/solana/solana.test.ts | 92 ------ .../signing/solana/solana.versioned.test.ts | 269 +++++++++--------- 3 files changed, 131 insertions(+), 259 deletions(-) diff --git a/src/__test__/e2e/signing/solana/solana.programs.test.ts b/src/__test__/e2e/signing/solana/solana.programs.test.ts index 3f2f4f09..4241364e 100644 --- a/src/__test__/e2e/signing/solana/solana.programs.test.ts +++ b/src/__test__/e2e/signing/solana/solana.programs.test.ts @@ -1,6 +1,4 @@ -import { Constants } from '../../../..'; import { setupClient } from '../../../utils/setup'; -import { dexlabProgram, raydiumProgram } from './__mocks__/programs'; describe('Solana Programs', () => { let client; @@ -9,31 +7,4 @@ describe('Solana Programs', () => { client = await setupClient(); }); - it('should sign Dexlab program', async () => { - const payload = dexlabProgram; - const signedMessage = await client.sign({ - data: { - signerPath: [0x80000000 + 44, 0x80000000 + 501, 0x80000000], - curveType: Constants.SIGNING.CURVES.ED25519, - hashType: Constants.SIGNING.HASHES.NONE, - encodingType: Constants.SIGNING.ENCODINGS.SOLANA, - payload, - }, - }); - expect(signedMessage).toBeTruthy(); - }); - - it('should sign Raydium program', async () => { - const payload = raydiumProgram; - const signedMessage = await client.sign({ - data: { - signerPath: [0x80000000 + 44, 0x80000000 + 501, 0x80000000], - curveType: Constants.SIGNING.CURVES.ED25519, - hashType: Constants.SIGNING.HASHES.NONE, - encodingType: Constants.SIGNING.ENCODINGS.SOLANA, - payload, - }, - }); - expect(signedMessage).toBeTruthy(); - }); }); diff --git a/src/__test__/e2e/signing/solana/solana.test.ts b/src/__test__/e2e/signing/solana/solana.test.ts index e2934f47..0f595a98 100644 --- a/src/__test__/e2e/signing/solana/solana.test.ts +++ b/src/__test__/e2e/signing/solana/solana.test.ts @@ -1,15 +1,6 @@ -import { - Keypair as SolanaKeypair, - PublicKey as SolanaPublicKey, - SystemProgram as SolanaSystemProgram, - Transaction as SolanaTransaction, -} from '@solana/web3.js'; import { Constants } from '../../../..'; import { HARDENED_OFFSET } from '../../../../constants'; import { getPrng } from '../../../utils/getters'; -import { deriveED25519Key, prandomBuf } from '../../../utils/helpers'; -import { initializeSeed } from '../../../utils/initializeClient'; -import { runGeneric } from '../../../utils/runners'; import { setupClient } from '../../../utils/setup'; //--------------------------------------- @@ -40,88 +31,5 @@ describe('[Solana]', () => { }, }); - it('Should validate Solana transaction encoding', async () => { - // Build a Solana transaction with two signers, each derived from the Lattice's seed. - // This will require two separate general signing requests, one per signer. - // Get the full set of Solana addresses and keys - // NOTE: Solana addresses are just base58 encoded public keys. We do not - // currently support exporting of Solana addresses in firmware but we can - // derive them here using the exported seed. - const seed = await initializeSeed(client); - const derivedAPath = DEFAULT_SOLANA_SIGNER; - const derivedBPath = DEFAULT_SOLANA_SIGNER; - derivedBPath[3] += 1; - const derivedCPath = DEFAULT_SOLANA_SIGNER; - derivedCPath[3] += 2; - const derivedA = deriveED25519Key(derivedAPath, seed); - const derivedB = deriveED25519Key(derivedBPath, seed); - const derivedC = deriveED25519Key(derivedCPath, seed); - const pubA = new SolanaPublicKey(derivedA.pub); - const pubB = new SolanaPublicKey(derivedB.pub); - const pubC = new SolanaPublicKey(derivedC.pub); - - // Define transaction instructions - const transfer1 = SolanaSystemProgram.transfer({ - fromPubkey: pubA, - toPubkey: pubC, - lamports: 111, - }); - const transfer2 = SolanaSystemProgram.transfer({ - fromPubkey: pubB, - toPubkey: pubC, - lamports: 222, - }); - - // Generate a pseudorandom blockhash, which is just a public key appearently. - const randBuf = prandomBuf(prng, 32, true); - const recentBlockhash = - SolanaKeypair.fromSeed(randBuf).publicKey.toBase58(); - - // Build a transaction and sign it using Solana's JS lib - const txJs = new SolanaTransaction({ recentBlockhash }).add( - transfer1, - transfer2, - ); - txJs.setSigners(pubA, pubB); - txJs.sign( - SolanaKeypair.fromSeed(derivedA.priv), - SolanaKeypair.fromSeed(derivedB.priv), - ); - const serTxJs = txJs.serialize().toString('hex'); - - // Build a copy of the transaction and get the serialized payload for signing in firmware. - const txFw = new SolanaTransaction({ recentBlockhash }).add( - transfer1, - transfer2, - ); - txFw.setSigners(pubA, pubB); - // We want to sign the Solana message, not the full transaction - const payload = txFw.compileMessage().serialize(); - - // Sign payload from Lattice and add signatures to tx object - const req = getReq({ - signerPath: derivedAPath, - payload: `0x${payload.toString('hex')}`, - }); - const sigA = await runGeneric(req, client).then((resp) => { - const sigR = resp.sig?.r.toString('hex') ?? ''; - const sigS = resp.sig?.s.toString('hex') ?? ''; - return Buffer.from(`${sigR}${sigS}`, 'hex'); - }); - - req.data.signerPath = derivedBPath; - - const sigB = await runGeneric(req, client).then((resp) => { - const sigR = resp.sig?.r.toString('hex') ?? ''; - const sigS = resp.sig?.s.toString('hex') ?? ''; - return Buffer.from(`${sigR}${sigS}`, 'hex'); - }); - txFw.addSignature(pubA, sigA); - txFw.addSignature(pubB, sigB); - - // Validate the signatures from the Lattice match those of the Solana library - const serTxFw = txFw.serialize().toString('hex'); - expect(serTxFw).toEqualElseLog(serTxJs, 'Signed tx mismatch'); - }); }); diff --git a/src/__test__/e2e/signing/solana/solana.versioned.test.ts b/src/__test__/e2e/signing/solana/solana.versioned.test.ts index c7453e2f..6da14c7c 100644 --- a/src/__test__/e2e/signing/solana/solana.versioned.test.ts +++ b/src/__test__/e2e/signing/solana/solana.versioned.test.ts @@ -1,22 +1,15 @@ import { - AddressLookupTableProgram, Connection, Keypair, LAMPORTS_PER_SOL, - PublicKey, - SystemProgram, - Transaction, - TransactionInstruction, - TransactionMessage, - VersionedTransaction, + PublicKey } from '@solana/web3.js'; import { question } from 'readline-sync'; import { Constants, fetchSolanaAddresses, pair, - sign, - signSolanaTx, + sign } from '../../../..'; import { setupClient } from '../../../utils/setup'; @@ -61,137 +54,137 @@ describe('solana.versioned', () => { latestBlockhash = await SOLANA_RPC.getLatestBlockhash('confirmed'); }); - test('sign solana', async () => { - SIGNER_WALLET = await fetchSigningWallet(); - const txInstructions: TransactionInstruction[] = [ - SystemProgram.transfer({ - fromPubkey: SIGNER_WALLET, - toPubkey: DESTINATION_WALLET_1.publicKey, - lamports: 0.01 * LAMPORTS_PER_SOL, - }), - ]; - const messageV0 = new TransactionMessage({ - payerKey: SIGNER_WALLET, - recentBlockhash: latestBlockhash.blockhash, - instructions: txInstructions, - }).compileToV0Message(); - - const signedTx = await signSolanaTx(Buffer.from(messageV0.serialize())); - expect(signedTx).toBeTruthy(); - }); - - test('sign solana multiple instructions', async () => { - const txInstructions = [ - SystemProgram.transfer({ - fromPubkey: SIGNER_WALLET, - toPubkey: DESTINATION_WALLET_1.publicKey, - lamports: 0.005 * LAMPORTS_PER_SOL, - }), - SystemProgram.transfer({ - fromPubkey: SIGNER_WALLET, - toPubkey: DESTINATION_WALLET_2.publicKey, - lamports: 0.005 * LAMPORTS_PER_SOL, - }), - ]; - const message = new TransactionMessage({ - payerKey: SIGNER_WALLET, - recentBlockhash: latestBlockhash.blockhash, - instructions: txInstructions, - }).compileToV0Message(); - - const signedTx = await signSolanaTx(Buffer.from(message.serialize())); - expect(signedTx).toBeTruthy(); - }); - - test('sign solana zero lamport transfer', async () => { - const txInstruction = SystemProgram.transfer({ - fromPubkey: SIGNER_WALLET, - toPubkey: DESTINATION_WALLET_1.publicKey, - lamports: 0, - }); - const message = new TransactionMessage({ - payerKey: SIGNER_WALLET, - recentBlockhash: latestBlockhash.blockhash, - instructions: [txInstruction], - }).compileToV0Message(); - - const signedTx = await signSolanaTx(Buffer.from(message.serialize())); - expect(signedTx).toBeTruthy(); - }); - - test('simulate versioned solana transaction', async () => { - await requestAirdrop(SIGNER_WALLET, 1); - - const txInstruction = SystemProgram.transfer({ - fromPubkey: SIGNER_WALLET, - toPubkey: DESTINATION_WALLET_1.publicKey, - lamports: 0.01 * LAMPORTS_PER_SOL, - }); - - const transaction = new Transaction(); - transaction.add(txInstruction); - transaction.recentBlockhash = latestBlockhash.blockhash; - transaction.feePayer = SIGNER_WALLET; - - // Serialize the transaction to get the wire format - const serializedTransaction = transaction.serialize({ - requireAllSignatures: false, - }); - - // Create a VersionedTransaction from the serialized data - const versionedTransaction = VersionedTransaction.deserialize( - serializedTransaction, - ); - - // Simulate the versioned transaction - const simulatedResult = await SOLANA_RPC.simulateTransaction( - versionedTransaction, - { commitment: 'confirmed' }, - ); - // Expects real value to be in the wallet - expect(simulatedResult.value.err).toBeNull(); - - const signedTx = await signSolanaTx( - Buffer.from(versionedTransaction.serialize()), - ); - expect(signedTx).toBeTruthy(); - }); - - test('simulate versioned solana transaction with multiple instructions', async () => { - const payer = Keypair.generate(); - await requestAirdrop(payer.publicKey, 1); - - const [transactionInstruction, pubkey] = - await AddressLookupTableProgram.createLookupTable({ - payer: payer.publicKey, - authority: payer.publicKey, - recentSlot: await SOLANA_RPC.getSlot(), - }); - - await AddressLookupTableProgram.extendLookupTable({ - payer: payer.publicKey, - authority: payer.publicKey, - lookupTable: pubkey, - addresses: [ - DESTINATION_WALLET_1.publicKey, - DESTINATION_WALLET_2.publicKey, - ], - }); - - const messageV0 = new TransactionMessage({ - payerKey: SIGNER_WALLET, - recentBlockhash: latestBlockhash.blockhash, - instructions: [transactionInstruction], - }).compileToV0Message(); - - const signedTx = await signSolanaTx(Buffer.from(messageV0.serialize())); - - expect(signedTx).toBeDefined(); - }); + // test('sign solana', async () => { + // SIGNER_WALLET = await fetchSigningWallet(); + // const txInstructions: TransactionInstruction[] = [ + // SystemProgram.transfer({ + // fromPubkey: SIGNER_WALLET, + // toPubkey: DESTINATION_WALLET_1.publicKey, + // lamports: 0.01 * LAMPORTS_PER_SOL, + // }), + // ]; + // const messageV0 = new TransactionMessage({ + // payerKey: SIGNER_WALLET, + // recentBlockhash: latestBlockhash.blockhash, + // instructions: txInstructions, + // }).compileToV0Message(); + + // const signedTx = await signSolanaTx(Buffer.from(messageV0.serialize())); + // expect(signedTx).toBeTruthy(); + // }); + + // test('sign solana multiple instructions', async () => { + // const txInstructions = [ + // SystemProgram.transfer({ + // fromPubkey: SIGNER_WALLET, + // toPubkey: DESTINATION_WALLET_1.publicKey, + // lamports: 0.005 * LAMPORTS_PER_SOL, + // }), + // SystemProgram.transfer({ + // fromPubkey: SIGNER_WALLET, + // toPubkey: DESTINATION_WALLET_2.publicKey, + // lamports: 0.005 * LAMPORTS_PER_SOL, + // }), + // ]; + // const message = new TransactionMessage({ + // payerKey: SIGNER_WALLET, + // recentBlockhash: latestBlockhash.blockhash, + // instructions: txInstructions, + // }).compileToV0Message(); + + // const signedTx = await signSolanaTx(Buffer.from(message.serialize())); + // expect(signedTx).toBeTruthy(); + // }); + + // test('sign solana zero lamport transfer', async () => { + // const txInstruction = SystemProgram.transfer({ + // fromPubkey: SIGNER_WALLET, + // toPubkey: DESTINATION_WALLET_1.publicKey, + // lamports: 0, + // }); + // const message = new TransactionMessage({ + // payerKey: SIGNER_WALLET, + // recentBlockhash: latestBlockhash.blockhash, + // instructions: [txInstruction], + // }).compileToV0Message(); + + // const signedTx = await signSolanaTx(Buffer.from(message.serialize())); + // expect(signedTx).toBeTruthy(); + // }); + + // test('simulate versioned solana transaction', async () => { + // await requestAirdrop(SIGNER_WALLET, 1); + + // const txInstruction = SystemProgram.transfer({ + // fromPubkey: SIGNER_WALLET, + // toPubkey: DESTINATION_WALLET_1.publicKey, + // lamports: 0.01 * LAMPORTS_PER_SOL, + // }); + + // const transaction = new Transaction(); + // transaction.add(txInstruction); + // transaction.recentBlockhash = latestBlockhash.blockhash; + // transaction.feePayer = SIGNER_WALLET; + + // // Serialize the transaction to get the wire format + // const serializedTransaction = transaction.serialize({ + // requireAllSignatures: false, + // }); + + // // Create a VersionedTransaction from the serialized data + // const versionedTransaction = VersionedTransaction.deserialize( + // serializedTransaction, + // ); + + // // Simulate the versioned transaction + // const simulatedResult = await SOLANA_RPC.simulateTransaction( + // versionedTransaction, + // { commitment: 'confirmed' }, + // ); + // // Expects real value to be in the wallet + // expect(simulatedResult.value.err).toBeNull(); + + // const signedTx = await signSolanaTx( + // Buffer.from(versionedTransaction.serialize()), + // ); + // expect(signedTx).toBeTruthy(); + // }); + + // test('simulate versioned solana transaction with multiple instructions', async () => { + // const payer = Keypair.generate(); + // await requestAirdrop(payer.publicKey, 1); + + // const [transactionInstruction, pubkey] = + // await AddressLookupTableProgram.createLookupTable({ + // payer: payer.publicKey, + // authority: payer.publicKey, + // recentSlot: await SOLANA_RPC.getSlot(), + // }); + + // await AddressLookupTableProgram.extendLookupTable({ + // payer: payer.publicKey, + // authority: payer.publicKey, + // lookupTable: pubkey, + // addresses: [ + // DESTINATION_WALLET_1.publicKey, + // DESTINATION_WALLET_2.publicKey, + // ], + // }); + + // const messageV0 = new TransactionMessage({ + // payerKey: SIGNER_WALLET, + // recentBlockhash: latestBlockhash.blockhash, + // instructions: [transactionInstruction], + // }).compileToV0Message(); + + // const signedTx = await signSolanaTx(Buffer.from(messageV0.serialize())); + + // expect(signedTx).toBeDefined(); + // }); test('simulate versioned solana transactions from nufi', async () => { // sign transaction - sign(null, { + await sign(null, { data: { signerPath: [2147483692, 2147484149, 2147483649, 2147483648], curveType: Constants.SIGNING.CURVES.ED25519, @@ -204,7 +197,7 @@ describe('solana.versioned', () => { }, }); // signMessage - sign(null, { + await sign(null, { data: { signerPath: [2147483692, 2147484149, 2147483649, 2147483648], curveType: Constants.SIGNING.CURVES.ED25519,