Skip to content

Commit 9f9a670

Browse files
refactor(wrapped-keys-lit-actions): LIT-3920 - Extract usage of getDecryptedKey() to entrypoint logic and pass privateKey into child flows for signing messages and transactions
- Rename `generateXXXX` methods that encrypt the generated keys in preparation of separating key generation from key encryption
1 parent d928051 commit 9f9a670

12 files changed

+98
-107
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
*
1010
* @returns { Promise<string> } - Returns a stringified JSON object with ciphertext & dataToEncryptHash which are the result of the encryption. Also returns the publicKey of the newly generated Ethers Wrapped Key.
1111
*/
12-
import { generateEthereumPrivateKey } from './internal/generateEncryptedPrivateKey';
12+
import { generateEncryptedEthereumPrivateKey } from './internal/generateEncryptedPrivateKey';
1313

1414
(async () => {
1515
const generatedKeyResultStr = await Lit.Actions.runOnce(
1616
{ waitForResponse: true, name: 'generateEthereumPrivateKey' },
1717
async () =>
18-
JSON.stringify(generateEthereumPrivateKey({ accessControlConditions }))
18+
JSON.stringify(
19+
generateEncryptedEthereumPrivateKey({ accessControlConditions })
20+
)
1921
);
2022

2123
Lit.Actions.setResponse({

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
*/
1010
import { LIT_PREFIX } from '../../constants';
1111

12-
export async function generateEthereumPrivateKey({ accessControlConditions }) {
12+
export async function generateEncryptedEthereumPrivateKey({
13+
accessControlConditions,
14+
}) {
1315
const wallet = ethers.Wallet.createRandom();
1416
const privateKey = LIT_PREFIX + wallet.privateKey.toString();
1517

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
/* global ethers */
22

3-
import { getDecryptedKey } from '../../common/internal/getDecryptedKey';
4-
import { removeSaltFromDecryptedKey } from '../../utils';
5-
63
async function signMessage({ privateKey, messageToSign }) {
74
try {
85
const wallet = new ethers.Wallet(privateKey);
@@ -22,25 +19,10 @@ function verifyMessageSignature(messageToSign, signature) {
2219
}
2320
}
2421

25-
export async function signMessageWithEncryptedKey({
26-
accessControlConditions,
27-
ciphertext,
28-
dataToEncryptHash,
22+
export async function signMessageWithEncryptedEthereumKey({
23+
privateKey,
2924
messageToSign,
3025
}) {
31-
const decryptedPrivateKey = await getDecryptedKey({
32-
accessControlConditions,
33-
ciphertext,
34-
dataToEncryptHash,
35-
});
36-
37-
if (!decryptedPrivateKey) {
38-
// Silently exit on nodes which didn't run the `decryptToSingleNode` code
39-
return;
40-
}
41-
42-
const privateKey = removeSaltFromDecryptedKey(decryptedPrivateKey);
43-
4426
const { signature, walletAddress } = await signMessage({
4527
privateKey,
4628
messageToSign,

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,12 @@ async function broadcastTransaction({ provider, signedTx }) {
100100
}
101101

102102
export async function signTransactionWithEncryptedKey({
103-
accessControlConditions,
104-
ciphertext,
105-
dataToEncryptHash,
106-
unsignedTransaction,
107103
broadcast,
104+
privateKey,
105+
unsignedTransaction,
108106
}) {
109107
const tx = getValidatedUnsignedTx(unsignedTransaction);
110108

111-
const decryptedPrivateKey = await getDecryptedKey({
112-
accessControlConditions,
113-
ciphertext,
114-
dataToEncryptHash,
115-
});
116-
117-
if (!decryptedPrivateKey) {
118-
// Silently exit on nodes which didn't run the `decryptToSingleNode` code
119-
return;
120-
}
121-
122-
const privateKey = removeSaltFromDecryptedKey(decryptedPrivateKey);
123109
const wallet = new ethers.Wallet(privateKey);
124110

125111
tx.from = wallet.address;

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
/* global accessControlConditions, ciphertext, dataToEncryptHash, messageToSign, Lit */
2-
31
const {
4-
signMessageWithEncryptedKey,
2+
signMessageWithEncryptedEthereumKey,
53
} = require('./internal/signMessageWithEncryptedKey');
4+
const { getDecryptedKey } = require('../common/internal/getDecryptedKey');
5+
const { removeSaltFromDecryptedKey } = require('../utils');
6+
7+
/* global accessControlConditions, ciphertext, dataToEncryptHash, messageToSign, Lit */
68

79
/**
810
* Signs a message with the Ethers wallet which is also decrypted inside the Lit Action.
@@ -18,16 +20,25 @@ const {
1820

1921
(async () => {
2022
try {
21-
const signature = await signMessageWithEncryptedKey({
23+
const decryptedPrivateKey = await getDecryptedKey({
2224
accessControlConditions,
2325
ciphertext,
2426
dataToEncryptHash,
25-
messageToSign,
2627
});
2728

28-
if (signature) {
29-
Lit.Actions.setResponse({ response: signature });
29+
if (!decryptedPrivateKey) {
30+
// Silently exit on nodes which didn't run the `decryptToSingleNode` code
31+
return;
3032
}
33+
34+
const privateKey = removeSaltFromDecryptedKey(decryptedPrivateKey);
35+
36+
const signature = await signMessageWithEncryptedEthereumKey({
37+
privateKey,
38+
messageToSign,
39+
});
40+
41+
Lit.Actions.setResponse({ response: signature });
3142
} catch (err) {
3243
Lit.Actions.setResponse({ response: `Error: ${err.message}` });
3344
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const {
22
signTransactionWithEncryptedKey,
33
} = require('./internal/signTransactionWithEncryptedKey');
4+
const { getDecryptedKey } = require('../common/internal/getDecryptedKey');
5+
const { removeSaltFromDecryptedKey } = require('../utils');
46

57
/* global accessControlConditions, ciphertext, dataToEncryptHash, unsignedTransaction, broadcast, Lit */
68

@@ -19,17 +21,26 @@ const {
1921
*/
2022
(async () => {
2123
try {
22-
const txResult = await signTransactionWithEncryptedKey({
24+
const decryptedPrivateKey = await getDecryptedKey({
2325
accessControlConditions,
2426
ciphertext,
2527
dataToEncryptHash,
26-
unsignedTransaction,
27-
broadcast,
2828
});
2929

30-
if (txResult) {
31-
Lit.Actions.setResponse({ response: txResult });
30+
if (!decryptedPrivateKey) {
31+
// Silently exit on nodes which didn't run the `decryptToSingleNode` code
32+
return;
3233
}
34+
35+
const privateKey = removeSaltFromDecryptedKey(decryptedPrivateKey);
36+
37+
const txResult = await signTransactionWithEncryptedKey({
38+
broadcast,
39+
privateKey,
40+
unsignedTransaction,
41+
});
42+
43+
Lit.Actions.setResponse({ response: txResult });
3344
} catch (err) {
3445
Lit.Actions.setResponse({
3546
response: `Error: ${err.message}`,

packages/wrapped-keys-lit-actions/src/lib/solana/generateEncryptedSolanaPrivateKey.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {
2-
generateSolanaPrivateKey,
2+
generateEncryptedSolanaPrivateKey,
33
} = require('./internal/generateEncryptedPrivateKey');
44

55
/* global accessControlConditions, Lit */
@@ -16,7 +16,10 @@ const {
1616
(async () => {
1717
const generatedKeyResultStr = await Lit.Actions.runOnce(
1818
{ waitForResponse: true, name: 'generateSolanaPrivateKey' },
19-
() => JSON.stringify(generateSolanaPrivateKey({ accessControlConditions }))
19+
() =>
20+
JSON.stringify(
21+
generateEncryptedSolanaPrivateKey({ accessControlConditions })
22+
)
2023
);
2124

2225
Lit.Actions.setResponse({

packages/wrapped-keys-lit-actions/src/lib/solana/internal/generateEncryptedPrivateKey.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { LIT_PREFIX } from '../../constants';
1111
* @private
1212
* @returns { Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> } - The ciphertext & dataToEncryptHash which are the result of the encryption, and the publicKey of the newly generated Ethers Wrapped Key.
1313
*/
14-
export async function generateSolanaPrivateKey({ accessControlConditions }) {
14+
export async function generateEncryptedSolanaPrivateKey({
15+
accessControlConditions,
16+
}) {
1517
const solanaKeypair = Keypair.generate();
1618
const privateKey =
1719
LIT_PREFIX + Buffer.from(solanaKeypair.secretKey).toString('hex');

packages/wrapped-keys-lit-actions/src/lib/solana/internal/signMessageWithEncryptedKey.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,11 @@ function verifyMessageSignature({ signature, solanaKeyPair, messageToSign }) {
3232
}
3333
}
3434

35-
export async function signMessageWithEncryptedKey({
36-
accessControlConditions,
37-
ciphertext,
38-
dataToEncryptHash,
35+
export async function signMessageWithEncryptedSolanaKey({
3936
messageToSign,
37+
privateKey,
4038
}) {
41-
const decryptedPrivateKey = await getDecryptedKey({
42-
accessControlConditions,
43-
ciphertext,
44-
dataToEncryptHash,
45-
});
46-
47-
if (!decryptedPrivateKey) {
48-
// Silently exit on nodes which didn't run the `decryptToSingleNode` code
49-
return;
50-
}
51-
52-
const solanaKeyPair = Keypair.fromSecretKey(
53-
Buffer.from(removeSaltFromDecryptedKey(decryptedPrivateKey), 'hex')
54-
);
39+
const solanaKeyPair = Keypair.fromSecretKey(Buffer.from(privateKey, 'hex'));
5540

5641
const { signature } = await signMessage({
5742
messageToSign,

packages/wrapped-keys-lit-actions/src/lib/solana/internal/signTransactionWithEncryptedKey.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,13 @@ async function sendTransaction({ chain, transaction }) {
4747
}
4848

4949
export async function signTransactionWithEncryptedSolanaKey({
50-
accessControlConditions,
51-
ciphertext,
52-
dataToEncryptHash,
53-
unsignedTransaction,
5450
broadcast,
51+
privateKey,
52+
unsignedTransaction,
5553
}) {
5654
validateUnsignedTransaction(unsignedTransaction);
5755

58-
const decryptedPrivateKey = await getDecryptedKey({
59-
accessControlConditions,
60-
ciphertext,
61-
dataToEncryptHash,
62-
});
63-
64-
if (!decryptedPrivateKey) {
65-
// Silently exit on nodes which didn't run the `decryptToSingleNode` code
66-
return;
67-
}
68-
69-
const solanaKeyPair = Keypair.fromSecretKey(
70-
Buffer.from(removeSaltFromDecryptedKey(decryptedPrivateKey), 'hex')
71-
);
56+
const solanaKeyPair = Keypair.fromSecretKey(Buffer.from(privateKey, 'hex'));
7257

7358
const transaction = Transaction.from(
7459
Buffer.from(unsignedTransaction.serializedTransaction, 'base64')
@@ -78,10 +63,10 @@ export async function signTransactionWithEncryptedSolanaKey({
7863

7964
if (!broadcast) {
8065
return signature;
81-
} else {
82-
return await sendTransaction({
83-
chain: unsignedTransaction.chain,
84-
transaction,
85-
});
8666
}
67+
68+
return await sendTransaction({
69+
chain: unsignedTransaction.chain,
70+
transaction,
71+
});
8772
}

0 commit comments

Comments
 (0)