Skip to content

Commit

Permalink
fix(wrapped-keys-lit-actions): LIT-3995 - Add test for signing tx wit…
Browse files Browse the repository at this point in the history
…h batchGeneratePrivateKeys

- This doesn't work for Solana yet, because our test requires the keyPair from the generated key to serialize the TX
  • Loading branch information
MaximusHaximus committed Oct 22, 2024
1 parent 780ebc4 commit 8f8cc8e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 38 deletions.
40 changes: 38 additions & 2 deletions local-tests/tests/wrapped-keys/testBatchGeneratePrivateKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import nacl from 'tweetnacl';
import bs58 from 'bs58';
import { ethers } from 'ethers';
import { BatchGeneratePrivateKeysActionResult } from '../../../packages/wrapped-keys/src/lib/types';
import { getBaseTransactionForNetwork, getSolanaTransaction } from './util';
import { Keypair } from '@solana/web3.js';

const { batchGeneratePrivateKeys } = api;

Expand Down Expand Up @@ -81,6 +83,13 @@ export const testBatchGeneratePrivateKeys = async (
new Date(Date.now() + 1000 * 60 * 10).toISOString()
); // 10 mins expiry

const solanaKeypair = Keypair.generate();

const {
solanaTransaction,
unsignedTransaction: solanaUnsignedTransaction,
} = await getSolanaTransaction({ solanaKeypair });

const solanaMessageToSign = 'This is a test solana message';
const evmMessageToSign = 'This is a test evm message';
const { results } = await batchGeneratePrivateKeys({
Expand All @@ -90,11 +99,20 @@ export const testBatchGeneratePrivateKeys = async (
network: 'evm',
signMessageParams: { messageToSign: evmMessageToSign },
generateKeyParams: { memo: 'Test evm key' },
signTransactionParams: {
unsignedTransaction: getBaseTransactionForNetwork({
network: devEnv.litNodeClient.config.litNetwork,
toAddress: alice.wallet.address,
}),
},
},
{
network: 'solana',
signMessageParams: { messageToSign: solanaMessageToSign },
generateKeyParams: { memo: 'Test solana key' },
// signTransactionParams: {
// unsignedTransaction: solanaUnsignedTransaction,
// },
},
],
litNodeClient: devEnv.litNodeClient,
Expand Down Expand Up @@ -122,13 +140,31 @@ export const testBatchGeneratePrivateKeys = async (
throw new Error('Missing message signature in response');
}

console.log('solana verify sig');
console.log('solana verify message sig');
await verifySolanaSignature(results[1], solanaMessageToSign);

console.log('evm verify sig');
console.log('evm verify message sig');
await verifyEvmSignature(results[0], evmMessageToSign);
console.log('results', results);

const signedEthTx = results[0].signTransaction.signature;

// Test eth signed tx:
if (!ethers.utils.isHexString(signedEthTx)) {
throw new Error(`signedTx isn't hex: ${signedEthTx}`);
}

// test solana signed tx:
//
// const signatureBuffer = Buffer.from(ethers.utils.base58.decode(signedTx));
// solanaTransaction.addSignature(solanaKeypair.publicKey, signatureBuffer);
//
// if (!solanaTransaction.verifySignatures()) {
// throw new Error(
// `Signature: ${signedTx} doesn't validate for the Solana transaction.`
// );
// }

log('✅ testBatchGenerateEncryptedKeys');
} catch (err) {
console.log(err.message, err, err.stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@solana/web3.js';
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';
import { ethers } from 'ethers';
import { getSolanaTransaction } from './util';

const { importPrivateKey, signTransactionWithEncryptedKey } = api;

Expand Down Expand Up @@ -57,11 +58,6 @@ export const testSignTransactionWithSolanaEncryptedKey = async (
memo: 'Test key',
});

const solanaConnection = new Connection(
clusterApiUrl('devnet'),
'confirmed'
);

// Request Solana Airdrop
// const balance = await solanaConnection.getBalance(solanaKeypair.publicKey);
// console.log("balance- ", balance); // Should be 0, in fact if we get the balance right after the Air Drop it will also be 0 unless we wait. We're skipping the balance confirmation
Expand All @@ -81,30 +77,8 @@ export const testSignTransactionWithSolanaEncryptedKey = async (
new Date(Date.now() + 1000 * 60 * 10).toISOString()
); // 10 mins expiry

const solanaTransaction = new Transaction();
solanaTransaction.add(
SystemProgram.transfer({
fromPubkey: solanaKeypair.publicKey,
toPubkey: new PublicKey(solanaKeypair.publicKey),
lamports: LAMPORTS_PER_SOL / 100, // Transfer 0.01 SOL
})
);
solanaTransaction.feePayer = solanaKeypair.publicKey;

const { blockhash } = await solanaConnection.getLatestBlockhash();
solanaTransaction.recentBlockhash = blockhash;

const serializedTransaction = solanaTransaction
.serialize({
requireAllSignatures: false, // should be false as we're not signing the message
verifySignatures: false, // should be false as we're not signing the message
})
.toString('base64');

const unsignedTransaction: SerializedTransaction = {
serializedTransaction,
chain: 'devnet',
};
const { unsignedTransaction, solanaTransaction } =
await getSolanaTransaction({ solanaKeypair });

const signedTx = await signTransactionWithEncryptedKey({
pkpSessionSigs: pkpSessionSigsSigning,
Expand Down
62 changes: 55 additions & 7 deletions local-tests/tests/wrapped-keys/util.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { LIT_NETWORKS_KEYS } from '@lit-protocol/types';
import { LIT_CHAINS } from '@lit-protocol/constants';
import { ethers } from 'ethers';
import { config } from '@lit-protocol/wrapped-keys';
import {
litActionRepositoryCommon,
litActionRepository,
} from '@lit-protocol/wrapped-keys-lit-actions';

import type {
EthereumLitTransaction,
LitActionCodeRepository,
LitActionCodeRepositoryCommon,
EthereumLitTransaction,
} from '@lit-protocol/wrapped-keys';
import { config, SerializedTransaction } from '@lit-protocol/wrapped-keys';
import {
litActionRepository,
litActionRepositoryCommon,
} from '@lit-protocol/wrapped-keys-lit-actions';
import {
clusterApiUrl,
Connection,
Keypair,
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
Transaction,
} from '@solana/web3.js';

const emptyLitActionRepositoryCommon: LitActionCodeRepositoryCommon = {
batchGenerateEncryptedKeys: '',
Expand Down Expand Up @@ -118,3 +126,43 @@ export function getBaseTransactionForNetwork({
),
};
}

export async function getSolanaTransaction({
solanaKeypair,
}: {
solanaKeypair: Keypair;
}): Promise<{
unsignedTransaction: SerializedTransaction;
solanaTransaction: Transaction;
}> {
const solanaConnection = new Connection(clusterApiUrl('devnet'), 'confirmed');

const solanaTransaction = new Transaction();

solanaTransaction.add(
SystemProgram.transfer({
fromPubkey: solanaKeypair.publicKey,
toPubkey: new PublicKey(solanaKeypair.publicKey),
lamports: LAMPORTS_PER_SOL / 100, // Transfer 0.01 SOL
})
);
solanaTransaction.feePayer = solanaKeypair.publicKey;

const { blockhash } = await solanaConnection.getLatestBlockhash();
solanaTransaction.recentBlockhash = blockhash;

const serializedTransaction = solanaTransaction
.serialize({
requireAllSignatures: false, // should be false as we're not signing the message
verifySignatures: false, // should be false as we're not signing the message
})
.toString('base64');

return {
solanaTransaction,
unsignedTransaction: {
serializedTransaction,
chain: 'devnet',
},
};
}

0 comments on commit 8f8cc8e

Please sign in to comment.