Skip to content

Commit 278b0f6

Browse files
refactor(wrapped-keys-lit-actions): LIT-3920 - Extract encryption of keys to common encryptPrivateKey method and rename signMessage... and signTransaction... methods to clarify that they use _already decrypted_ keys
1 parent 4c90fbe commit 278b0f6

12 files changed

+39
-78
lines changed
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
import { generateEthereumPrivateKey } from './generatePrivateKey';
21
import { LIT_PREFIX } from '../../constants';
32

43
/* global Lit */
54

65
/**
7-
* Generates a random Ethers private key that only allows the provided PKP to decrypt it
8-
* This should be executed using `runOnce` to avoid generating `n` new private keys where we only want 1.
9-
*
106
* @private
117
* @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.
128
*/
13-
14-
export async function generateEncryptedEthereumPrivateKey({
9+
export async function encryptPrivateKey({
1510
accessControlConditions,
11+
privateKey,
12+
publicKey,
1613
}) {
17-
const { privateKey, publicKey } = generateEthereumPrivateKey();
18-
1914
const { ciphertext, dataToEncryptHash } = await Lit.Actions.encrypt({
2015
accessControlConditions,
2116
to_encrypt: new TextEncoder().encode(LIT_PREFIX + privateKey),

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
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 { generateEncryptedEthereumPrivateKey } from './internal/generateEncryptedPrivateKey';
12+
import { generateEthereumPrivateKey } from './internal/generatePrivateKey';
13+
import { encryptPrivateKey } from '../common/internal/encryptKey';
1314

1415
(async () => {
15-
const generatedKeyResultStr = await Lit.Actions.runOnce(
16-
{ waitForResponse: true, name: 'generateEthereumPrivateKey' },
16+
const { privateKey, publicKey } = generateEthereumPrivateKey();
17+
18+
const encryptedKeyResultStr = await Lit.Actions.runOnce(
19+
{ waitForResponse: true, name: 'encryptEthereumPrivateKey' },
1720
async () =>
1821
JSON.stringify(
19-
generateEncryptedEthereumPrivateKey({ accessControlConditions })
22+
encryptPrivateKey({
23+
accessControlConditions,
24+
privateKey,
25+
publicKey,
26+
})
2027
)
2128
);
2229

2330
Lit.Actions.setResponse({
24-
response: generatedKeyResultStr,
31+
response: encryptedKeyResultStr,
2532
});
2633
})();
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ function verifyMessageSignature(messageToSign, signature) {
1919
}
2020
}
2121

22-
export async function signMessageWithEncryptedEthereumKey({
23-
privateKey,
24-
messageToSign,
25-
}) {
22+
export async function signMessageEthereumKey({ privateKey, messageToSign }) {
2623
const { signature, walletAddress } = await signMessage({
2724
privateKey,
2825
messageToSign,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async function broadcastTransaction({ provider, signedTx }) {
9999
}
100100
}
101101

102-
export async function signTransactionWithEncryptedKey({
102+
export async function signTransactionEthereumKey({
103103
broadcast,
104104
privateKey,
105105
unsignedTransaction,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
signMessageWithEncryptedEthereumKey,
3-
} = require('./internal/signMessageWithEncryptedKey');
1+
const { signMessageEthereumKey } = require('./internal/signMessage');
42
const { getDecryptedKey } = require('../common/internal/getDecryptedKey');
53
const { removeSaltFromDecryptedKey } = require('../utils');
64

@@ -33,7 +31,7 @@ const { removeSaltFromDecryptedKey } = require('../utils');
3331

3432
const privateKey = removeSaltFromDecryptedKey(decryptedPrivateKey);
3533

36-
const signature = await signMessageWithEncryptedEthereumKey({
34+
const signature = await signMessageEthereumKey({
3735
privateKey,
3836
messageToSign,
3937
});

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
signTransactionWithEncryptedKey,
3-
} = require('./internal/signTransactionWithEncryptedKey');
1+
const { signTransactionEthereumKey } = require('./internal/signTransaction');
42
const { getDecryptedKey } = require('../common/internal/getDecryptedKey');
53
const { removeSaltFromDecryptedKey } = require('../utils');
64

@@ -34,7 +32,7 @@ const { removeSaltFromDecryptedKey } = require('../utils');
3432

3533
const privateKey = removeSaltFromDecryptedKey(decryptedPrivateKey);
3634

37-
const txResult = await signTransactionWithEncryptedKey({
35+
const txResult = await signTransactionEthereumKey({
3836
broadcast,
3937
privateKey,
4038
unsignedTransaction,
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
const {
2-
generateEncryptedSolanaPrivateKey,
3-
} = require('./internal/generateEncryptedPrivateKey');
1+
const { generateSolanaPrivateKey } = require('./internal/generatePrivateKey');
2+
const { encryptPrivateKey } = require('../common/internal/encryptKey');
43

54
/* global accessControlConditions, Lit */
65

76
/**
8-
*
97
* Bundles solana/web3.js package as it's required to generate a random Solana key and only allows the provided PKP to decrypt it
108
*
119
* @jsParam pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key
@@ -14,15 +12,21 @@ const {
1412
* @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 Solana Wrapped Key.
1513
*/
1614
(async () => {
17-
const generatedKeyResultStr = await Lit.Actions.runOnce(
18-
{ waitForResponse: true, name: 'generateSolanaPrivateKey' },
15+
const { privateKey, publicKey } = generateSolanaPrivateKey();
16+
17+
const encryptedKeyResultStr = await Lit.Actions.runOnce(
18+
{ waitForResponse: true, name: 'encryptSolanaPrivateKey' },
1919
() =>
2020
JSON.stringify(
21-
generateEncryptedSolanaPrivateKey({ accessControlConditions })
21+
encryptPrivateKey({
22+
accessControlConditions,
23+
publicKey,
24+
privateKey,
25+
})
2226
)
2327
);
2428

2529
Lit.Actions.setResponse({
26-
response: generatedKeyResultStr,
30+
response: encryptedKeyResultStr,
2731
});
2832
})();

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

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { Keypair } from '@solana/web3.js';
22
import bs58 from 'bs58';
33
import nacl from 'tweetnacl';
44

5-
import { getDecryptedKey } from '../../common/internal/getDecryptedKey';
6-
import { removeSaltFromDecryptedKey } from '../../utils';
7-
85
async function signMessage({ messageToSign, solanaKeyPair }) {
96
try {
107
const signature = nacl.sign.detached(
@@ -32,10 +29,7 @@ function verifyMessageSignature({ signature, solanaKeyPair, messageToSign }) {
3229
}
3330
}
3431

35-
export async function signMessageWithEncryptedSolanaKey({
36-
messageToSign,
37-
privateKey,
38-
}) {
32+
export async function signMessageSolanaKey({ messageToSign, privateKey }) {
3933
const solanaKeyPair = Keypair.fromSecretKey(Buffer.from(privateKey, 'hex'));
4034

4135
const { signature } = await signMessage({
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function sendTransaction({ chain, transaction }) {
4646
}
4747
}
4848

49-
export async function signTransactionWithEncryptedSolanaKey({
49+
export async function signTransactionSolanaKey({
5050
broadcast,
5151
privateKey,
5252
unsignedTransaction,

0 commit comments

Comments
 (0)