Skip to content

Commit 2175a8c

Browse files
chore(wrapped-keys-lit-actions): LIT-3920 - Update signTransactionWithEncryptedEthereumKey LA to validate unsigned tx before decrypting the private key
1 parent f79e3dc commit 2175a8c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

packages/wrapped-keys-lit-actions/src/lib/ethereum/internal/signTransaction.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global ethers, Lit */
22

3-
function getValidatedUnsignedTx(unsignedTransaction) {
3+
export function getValidatedUnsignedTx(unsignedTransaction) {
44
try {
55
if (!unsignedTransaction.toAddress) {
66
throw new Error('Missing required field: toAddress');
@@ -68,21 +68,21 @@ async function getGasPrice({ userProvidedGasPrice, provider }) {
6868
}
6969
}
7070

71-
async function getGasLimit({ provider, userProvidedGasLimit, tx }) {
71+
async function getGasLimit({ provider, userProvidedGasLimit, validatedTx }) {
7272
if (userProvidedGasLimit) {
7373
return userProvidedGasLimit;
7474
} else {
7575
try {
76-
return await provider.estimateGas(tx);
76+
return await provider.estimateGas(validatedTx);
7777
} catch (err) {
7878
throw new Error(`When estimating gas - ${err.message}`);
7979
}
8080
}
8181
}
8282

83-
async function signTransaction({ tx, wallet }) {
83+
async function signTransaction({ validatedTx, wallet }) {
8484
try {
85-
return await wallet.signTransaction(tx);
85+
return await wallet.signTransaction(validatedTx);
8686
} catch (err) {
8787
throw new Error(`When signing transaction - ${err.message}`);
8888
}
@@ -99,13 +99,12 @@ async function broadcastTransaction({ provider, signedTx }) {
9999
export async function signTransactionEthereumKey({
100100
broadcast,
101101
privateKey,
102+
validatedTx,
102103
unsignedTransaction,
103104
}) {
104-
const tx = getValidatedUnsignedTx(unsignedTransaction);
105-
106105
const wallet = new ethers.Wallet(privateKey);
107106

108-
tx.from = wallet.address;
107+
validatedTx.from = wallet.address;
109108

110109
const [nonce, provider] = await Promise.all([
111110
getLatestNonce({
@@ -117,20 +116,20 @@ export async function signTransactionEthereumKey({
117116
}),
118117
]);
119118

120-
tx.nonce = nonce;
119+
validatedTx.nonce = nonce;
121120

122-
tx.gasPrice = await getGasPrice({
121+
validatedTx.gasPrice = await getGasPrice({
123122
provider,
124123
userProvidedGasPrice: unsignedTransaction.gasPrice,
125124
});
126125

127-
tx.gasLimit = await getGasLimit({
126+
validatedTx.gasLimit = await getGasLimit({
128127
provider,
129-
tx,
128+
validatedTx,
130129
userProvidedGasLimit: unsignedTransaction.gasLimit,
131130
});
132131

133-
const signedTx = await signTransaction({ tx, wallet });
132+
const signedTx = await signTransaction({ validatedTx, wallet });
134133

135134
if (!broadcast) {
136135
return signedTx;

packages/wrapped-keys-lit-actions/src/lib/ethereum/signTransactionWithEncryptedEthereumKey.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const { signTransactionEthereumKey } = require('./internal/signTransaction');
1+
const {
2+
signTransactionEthereumKey,
3+
getValidatedUnsignedTx,
4+
} = require('./internal/signTransaction');
25
const {
36
getDecryptedKeyToSingleNode,
47
} = require('../common/internal/getDecryptedKeyToSingleNode');
@@ -21,6 +24,8 @@ const { removeSaltFromDecryptedKey } = require('../utils');
2124
*/
2225
(async () => {
2326
try {
27+
const validatedTx = getValidatedUnsignedTx(unsignedTransaction);
28+
2429
const decryptedPrivateKey = await getDecryptedKeyToSingleNode({
2530
accessControlConditions,
2631
ciphertext,
@@ -38,6 +43,7 @@ const { removeSaltFromDecryptedKey } = require('../utils');
3843
broadcast,
3944
privateKey,
4045
unsignedTransaction,
46+
validatedTx,
4147
});
4248

4349
Lit.Actions.setResponse({ response: txResult });

0 commit comments

Comments
 (0)