Skip to content

Commit a294bc4

Browse files
refactor(wrapped-keys-lit-actions): LIT-3920 - Extract private key generation from _encrypted_ private key generation for direct use of generated, un-encrypted keys for signing messages in new executeWrappedKeyOperations method
1 parent 9f9a670 commit a294bc4

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

packages/wrapped-keys-lit-actions/src/lib/common/internal/getDecryptedKey.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ export async function getDecryptedKey({
77
}) {
88
try {
99
// May be undefined, since we're using `decryptToSingleNode`
10-
const privateKey = await Lit.Actions.decryptToSingleNode({
10+
return await Lit.Actions.decryptToSingleNode({
1111
accessControlConditions,
1212
ciphertext,
1313
dataToEncryptHash,
1414
chain: 'ethereum',
1515
authSig: null,
1616
});
17-
18-
return privateKey;
1917
} catch (err) {
2018
throw new Error(`When decrypting key to a single node - ${err.message}`);
2119
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/* global ethers, Lit */
1+
import { generateEthereumPrivateKey } from './generatePrivateKey';
2+
import { LIT_PREFIX } from '../../constants';
3+
4+
/* global Lit */
25

36
/**
47
* Generates a random Ethers private key that only allows the provided PKP to decrypt it
@@ -7,25 +10,20 @@
710
* @private
811
* @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.
912
*/
10-
import { LIT_PREFIX } from '../../constants';
1113

1214
export async function generateEncryptedEthereumPrivateKey({
1315
accessControlConditions,
1416
}) {
15-
const wallet = ethers.Wallet.createRandom();
16-
const privateKey = LIT_PREFIX + wallet.privateKey.toString();
17-
18-
let utf8Encode = new TextEncoder();
19-
const to_encrypt = utf8Encode.encode(privateKey);
17+
const { privateKey, publicKey } = generateEthereumPrivateKey();
2018

2119
const { ciphertext, dataToEncryptHash } = await Lit.Actions.encrypt({
2220
accessControlConditions,
23-
to_encrypt,
21+
to_encrypt: new TextEncoder().encode(LIT_PREFIX + privateKey),
2422
});
2523

2624
return {
2725
ciphertext,
2826
dataToEncryptHash,
29-
publicKey: wallet.publicKey,
27+
publicKey,
3028
};
3129
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* global ethers */
2+
3+
export function generateEthereumPrivateKey() {
4+
const wallet = ethers.Wallet.createRandom();
5+
6+
return {
7+
privateKey: wallet.privateKey.toString(),
8+
publicKey: wallet.publicKey,
9+
};
10+
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Keypair } from '@solana/web3.js';
2-
1+
import { generateSolanaPrivateKey } from './generatePrivateKey';
32
import { LIT_PREFIX } from '../../constants';
43

54
/* global Lit */
@@ -14,20 +13,16 @@ import { LIT_PREFIX } from '../../constants';
1413
export async function generateEncryptedSolanaPrivateKey({
1514
accessControlConditions,
1615
}) {
17-
const solanaKeypair = Keypair.generate();
18-
const privateKey =
19-
LIT_PREFIX + Buffer.from(solanaKeypair.secretKey).toString('hex');
20-
let utf8Encode = new TextEncoder();
21-
const to_encrypt = utf8Encode.encode(privateKey);
16+
const { privateKey, publicKey } = generateSolanaPrivateKey();
2217

2318
const { ciphertext, dataToEncryptHash } = await Lit.Actions.encrypt({
2419
accessControlConditions,
25-
to_encrypt,
20+
to_encrypt: new TextEncoder().encode(LIT_PREFIX + privateKey),
2621
});
2722

2823
return {
2924
ciphertext,
3025
dataToEncryptHash,
31-
publicKey: solanaKeypair.publicKey.toString(),
26+
publicKey,
3227
};
3328
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Keypair } from '@solana/web3.js';
2+
3+
export function generateSolanaPrivateKey() {
4+
const solanaKeypair = Keypair.generate();
5+
6+
return {
7+
privateKey: Buffer.from(solanaKeypair.secretKey).toString('hex'),
8+
publicKey: solanaKeypair.publicKey.toString(),
9+
};
10+
}

0 commit comments

Comments
 (0)