Skip to content

Commit 8f8cc8e

Browse files
fix(wrapped-keys-lit-actions): LIT-3995 - Add test for signing tx with batchGeneratePrivateKeys
- This doesn't work for Solana yet, because our test requires the keyPair from the generated key to serialize the TX
1 parent 780ebc4 commit 8f8cc8e

File tree

3 files changed

+96
-38
lines changed

3 files changed

+96
-38
lines changed

local-tests/tests/wrapped-keys/testBatchGeneratePrivateKeys.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import nacl from 'tweetnacl';
66
import bs58 from 'bs58';
77
import { ethers } from 'ethers';
88
import { BatchGeneratePrivateKeysActionResult } from '../../../packages/wrapped-keys/src/lib/types';
9+
import { getBaseTransactionForNetwork, getSolanaTransaction } from './util';
10+
import { Keypair } from '@solana/web3.js';
911

1012
const { batchGeneratePrivateKeys } = api;
1113

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

86+
const solanaKeypair = Keypair.generate();
87+
88+
const {
89+
solanaTransaction,
90+
unsignedTransaction: solanaUnsignedTransaction,
91+
} = await getSolanaTransaction({ solanaKeypair });
92+
8493
const solanaMessageToSign = 'This is a test solana message';
8594
const evmMessageToSign = 'This is a test evm message';
8695
const { results } = await batchGeneratePrivateKeys({
@@ -90,11 +99,20 @@ export const testBatchGeneratePrivateKeys = async (
9099
network: 'evm',
91100
signMessageParams: { messageToSign: evmMessageToSign },
92101
generateKeyParams: { memo: 'Test evm key' },
102+
signTransactionParams: {
103+
unsignedTransaction: getBaseTransactionForNetwork({
104+
network: devEnv.litNodeClient.config.litNetwork,
105+
toAddress: alice.wallet.address,
106+
}),
107+
},
93108
},
94109
{
95110
network: 'solana',
96111
signMessageParams: { messageToSign: solanaMessageToSign },
97112
generateKeyParams: { memo: 'Test solana key' },
113+
// signTransactionParams: {
114+
// unsignedTransaction: solanaUnsignedTransaction,
115+
// },
98116
},
99117
],
100118
litNodeClient: devEnv.litNodeClient,
@@ -122,13 +140,31 @@ export const testBatchGeneratePrivateKeys = async (
122140
throw new Error('Missing message signature in response');
123141
}
124142

125-
console.log('solana verify sig');
143+
console.log('solana verify message sig');
126144
await verifySolanaSignature(results[1], solanaMessageToSign);
127145

128-
console.log('evm verify sig');
146+
console.log('evm verify message sig');
129147
await verifyEvmSignature(results[0], evmMessageToSign);
130148
console.log('results', results);
131149

150+
const signedEthTx = results[0].signTransaction.signature;
151+
152+
// Test eth signed tx:
153+
if (!ethers.utils.isHexString(signedEthTx)) {
154+
throw new Error(`signedTx isn't hex: ${signedEthTx}`);
155+
}
156+
157+
// test solana signed tx:
158+
//
159+
// const signatureBuffer = Buffer.from(ethers.utils.base58.decode(signedTx));
160+
// solanaTransaction.addSignature(solanaKeypair.publicKey, signatureBuffer);
161+
//
162+
// if (!solanaTransaction.verifySignatures()) {
163+
// throw new Error(
164+
// `Signature: ${signedTx} doesn't validate for the Solana transaction.`
165+
// );
166+
// }
167+
132168
log('✅ testBatchGenerateEncryptedKeys');
133169
} catch (err) {
134170
console.log(err.message, err, err.stack);

local-tests/tests/wrapped-keys/testSignTransactionWithSolanaEncryptedKey.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@solana/web3.js';
1313
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';
1414
import { ethers } from 'ethers';
15+
import { getSolanaTransaction } from './util';
1516

1617
const { importPrivateKey, signTransactionWithEncryptedKey } = api;
1718

@@ -57,11 +58,6 @@ export const testSignTransactionWithSolanaEncryptedKey = async (
5758
memo: 'Test key',
5859
});
5960

60-
const solanaConnection = new Connection(
61-
clusterApiUrl('devnet'),
62-
'confirmed'
63-
);
64-
6561
// Request Solana Airdrop
6662
// const balance = await solanaConnection.getBalance(solanaKeypair.publicKey);
6763
// 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
@@ -81,30 +77,8 @@ export const testSignTransactionWithSolanaEncryptedKey = async (
8177
new Date(Date.now() + 1000 * 60 * 10).toISOString()
8278
); // 10 mins expiry
8379

84-
const solanaTransaction = new Transaction();
85-
solanaTransaction.add(
86-
SystemProgram.transfer({
87-
fromPubkey: solanaKeypair.publicKey,
88-
toPubkey: new PublicKey(solanaKeypair.publicKey),
89-
lamports: LAMPORTS_PER_SOL / 100, // Transfer 0.01 SOL
90-
})
91-
);
92-
solanaTransaction.feePayer = solanaKeypair.publicKey;
93-
94-
const { blockhash } = await solanaConnection.getLatestBlockhash();
95-
solanaTransaction.recentBlockhash = blockhash;
96-
97-
const serializedTransaction = solanaTransaction
98-
.serialize({
99-
requireAllSignatures: false, // should be false as we're not signing the message
100-
verifySignatures: false, // should be false as we're not signing the message
101-
})
102-
.toString('base64');
103-
104-
const unsignedTransaction: SerializedTransaction = {
105-
serializedTransaction,
106-
chain: 'devnet',
107-
};
80+
const { unsignedTransaction, solanaTransaction } =
81+
await getSolanaTransaction({ solanaKeypair });
10882

10983
const signedTx = await signTransactionWithEncryptedKey({
11084
pkpSessionSigs: pkpSessionSigsSigning,

local-tests/tests/wrapped-keys/util.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { LIT_NETWORKS_KEYS } from '@lit-protocol/types';
22
import { LIT_CHAINS } from '@lit-protocol/constants';
33
import { ethers } from 'ethers';
4-
import { config } from '@lit-protocol/wrapped-keys';
5-
import {
6-
litActionRepositoryCommon,
7-
litActionRepository,
8-
} from '@lit-protocol/wrapped-keys-lit-actions';
9-
104
import type {
5+
EthereumLitTransaction,
116
LitActionCodeRepository,
127
LitActionCodeRepositoryCommon,
13-
EthereumLitTransaction,
148
} from '@lit-protocol/wrapped-keys';
9+
import { config, SerializedTransaction } from '@lit-protocol/wrapped-keys';
10+
import {
11+
litActionRepository,
12+
litActionRepositoryCommon,
13+
} from '@lit-protocol/wrapped-keys-lit-actions';
14+
import {
15+
clusterApiUrl,
16+
Connection,
17+
Keypair,
18+
LAMPORTS_PER_SOL,
19+
PublicKey,
20+
SystemProgram,
21+
Transaction,
22+
} from '@solana/web3.js';
1523

1624
const emptyLitActionRepositoryCommon: LitActionCodeRepositoryCommon = {
1725
batchGenerateEncryptedKeys: '',
@@ -118,3 +126,43 @@ export function getBaseTransactionForNetwork({
118126
),
119127
};
120128
}
129+
130+
export async function getSolanaTransaction({
131+
solanaKeypair,
132+
}: {
133+
solanaKeypair: Keypair;
134+
}): Promise<{
135+
unsignedTransaction: SerializedTransaction;
136+
solanaTransaction: Transaction;
137+
}> {
138+
const solanaConnection = new Connection(clusterApiUrl('devnet'), 'confirmed');
139+
140+
const solanaTransaction = new Transaction();
141+
142+
solanaTransaction.add(
143+
SystemProgram.transfer({
144+
fromPubkey: solanaKeypair.publicKey,
145+
toPubkey: new PublicKey(solanaKeypair.publicKey),
146+
lamports: LAMPORTS_PER_SOL / 100, // Transfer 0.01 SOL
147+
})
148+
);
149+
solanaTransaction.feePayer = solanaKeypair.publicKey;
150+
151+
const { blockhash } = await solanaConnection.getLatestBlockhash();
152+
solanaTransaction.recentBlockhash = blockhash;
153+
154+
const serializedTransaction = solanaTransaction
155+
.serialize({
156+
requireAllSignatures: false, // should be false as we're not signing the message
157+
verifySignatures: false, // should be false as we're not signing the message
158+
})
159+
.toString('base64');
160+
161+
return {
162+
solanaTransaction,
163+
unsignedTransaction: {
164+
serializedTransaction,
165+
chain: 'devnet',
166+
},
167+
};
168+
}

0 commit comments

Comments
 (0)