-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #534 from LIT-Protocol/feat-update-wrapped-keys-ex…
…port Feat: Update Wrapped keys Export to use Lit Actions
- Loading branch information
Showing
16 changed files
with
174 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,39 @@ | ||
import { decryptToString } from '@lit-protocol/encryption'; | ||
import { exportPrivateKeyWithLitAction } from '../lit-actions-client'; | ||
|
||
import { CHAIN_ETHEREUM, LIT_PREFIX } from '../constants'; | ||
import { fetchPrivateKeyMetadata } from '../service-client'; | ||
import { ExportPrivateKeyParams, ExportPrivateKeyResult } from '../types'; | ||
import { | ||
getFirstSessionSig, | ||
getPkpAccessControlCondition, | ||
getPkpAddressFromSessionSig, | ||
} from '../utils'; | ||
import { getFirstSessionSig, getPkpAccessControlCondition } from '../utils'; | ||
import { getLitActionCid } from '../lit-actions-client/utils'; | ||
|
||
/** Exports a previously persisted private key from the wrapped keys service for direct use by the caller, along with the keys metadata | ||
/** | ||
* Exports a previously persisted private key from the wrapped keys service for direct use by the caller, along with the keys metadata. | ||
* This method fetches the encrypted key from the wrapped keys service, then executes a Lit Action that decrypts the key inside the LIT action and | ||
* removes the salt from the decrypted key. | ||
* | ||
* @param { ExportPrivateKeyParams } params Parameters required to export the private key | ||
* | ||
* @returns { Promise<ExportPrivateKeyResult> } - The decrypted private key of the Wrapped Key along with all the associated key info and LIT PKP Address associated with the Wrapped Key | ||
*/ | ||
export async function exportPrivateKey( | ||
params: ExportPrivateKeyParams | ||
): Promise<ExportPrivateKeyResult> { | ||
const { pkpSessionSigs, litNodeClient } = params; | ||
const { litNodeClient, network, pkpSessionSigs } = params; | ||
|
||
const sessionSig = getFirstSessionSig(pkpSessionSigs); | ||
const pkpAddress = getPkpAddressFromSessionSig(sessionSig); | ||
const allowPkpAddressToDecrypt = getPkpAccessControlCondition(pkpAddress); | ||
|
||
const privateKeyMetadata = await fetchPrivateKeyMetadata({ | ||
const storedKeyMetadata = await fetchPrivateKeyMetadata({ | ||
sessionSig, | ||
litNetwork: litNodeClient.config.litNetwork, | ||
}); | ||
|
||
const { ciphertext, dataToEncryptHash, ...privateKeyMetadataMinusEncrypted } = | ||
privateKeyMetadata; | ||
|
||
const decryptedPrivateKey = await decryptToString( | ||
{ | ||
accessControlConditions: [allowPkpAddressToDecrypt], | ||
chain: CHAIN_ETHEREUM, | ||
ciphertext, | ||
dataToEncryptHash, | ||
sessionSigs: pkpSessionSigs, | ||
}, | ||
litNodeClient | ||
const allowPkpAddressToDecrypt = getPkpAccessControlCondition( | ||
storedKeyMetadata.pkpAddress | ||
); | ||
|
||
// It will be of the form lit_<privateKey> | ||
if (!decryptedPrivateKey.startsWith(LIT_PREFIX)) { | ||
throw new Error( | ||
`PKey was not encrypted with salt; all wrapped keys must be prefixed with '${LIT_PREFIX}'` | ||
); | ||
} | ||
|
||
return { | ||
decryptedPrivateKey: decryptedPrivateKey.slice(LIT_PREFIX.length), | ||
...privateKeyMetadataMinusEncrypted, | ||
}; | ||
return exportPrivateKeyWithLitAction({ | ||
...params, | ||
litActionIpfsCid: getLitActionCid(network, 'exportPrivateKey'), | ||
accessControlConditions: [allowPkpAddressToDecrypt], | ||
pkpSessionSigs, | ||
storedKeyMetadata, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/wrapped-keys/src/lib/lit-actions-client/export-private-key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { AccessControlConditions } from '@lit-protocol/types'; | ||
|
||
import { postLitActionValidation } from './utils'; | ||
import { ExportPrivateKeyParams, StoredKeyMetadata } from '../types'; | ||
|
||
interface SignMessageWithLitActionParams extends ExportPrivateKeyParams { | ||
accessControlConditions: AccessControlConditions; | ||
storedKeyMetadata: StoredKeyMetadata; | ||
litActionIpfsCid: string; | ||
} | ||
|
||
export async function exportPrivateKeyWithLitAction( | ||
args: SignMessageWithLitActionParams | ||
) { | ||
const { | ||
accessControlConditions, | ||
litNodeClient, | ||
pkpSessionSigs, | ||
litActionIpfsCid, | ||
storedKeyMetadata, | ||
} = args; | ||
|
||
const { | ||
pkpAddress, | ||
ciphertext, | ||
dataToEncryptHash, | ||
...storeKeyMetadataMinusEncryptedAndPkp | ||
} = storedKeyMetadata; | ||
const result = await litNodeClient.executeJs({ | ||
sessionSigs: pkpSessionSigs, | ||
ipfsId: litActionIpfsCid, | ||
jsParams: { | ||
pkpAddress, | ||
ciphertext, | ||
dataToEncryptHash, | ||
accessControlConditions, | ||
}, | ||
}); | ||
|
||
const decryptedPrivateKey = postLitActionValidation(result); | ||
|
||
return { | ||
decryptedPrivateKey, | ||
pkpAddress, | ||
...storeKeyMetadataMinusEncryptedAndPkp, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { generateKeyWithLitAction } from './generate-key'; | ||
import { signMessageWithLitAction } from './sign-message'; | ||
import { signTransactionWithLitAction } from './sign-transaction'; | ||
import { exportPrivateKeyWithLitAction } from './export-private-key'; | ||
|
||
export { | ||
generateKeyWithLitAction, | ||
signTransactionWithLitAction, | ||
signMessageWithLitAction, | ||
exportPrivateKeyWithLitAction, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/wrapped-keys/src/lib/litActions/common/src/exportPrivateKey.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const { removeSaltFromDecryptedKey } = require('../../utils'); | ||
|
||
/** | ||
* | ||
* Exports the private key after decrypting and removing the salt from it. | ||
* | ||
* @jsParam pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key | ||
* @jsParam ciphertext - For the encrypted Wrapped Key | ||
* @jsParam dataToEncryptHash - For the encrypted Wrapped Key | ||
* @jsParam accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key | ||
* | ||
* @returns { Promise<string> } - Returns a decrypted private key. | ||
*/ | ||
|
||
(async () => { | ||
let decryptedPrivateKey; | ||
try { | ||
decryptedPrivateKey = await Lit.Actions.decryptToSingleNode({ | ||
accessControlConditions, | ||
ciphertext, | ||
dataToEncryptHash, | ||
chain: 'ethereum', | ||
authSig: null, | ||
}); | ||
} catch (err) { | ||
const errorMessage = | ||
'Error: When decrypting to a single node- ' + err.message; | ||
Lit.Actions.setResponse({ response: errorMessage }); | ||
return; | ||
} | ||
|
||
if (!decryptedPrivateKey) { | ||
// Exit the nodes which don't have the decryptedData | ||
return; | ||
} | ||
|
||
try { | ||
const privateKey = removeSaltFromDecryptedKey(decryptedPrivateKey); | ||
Lit.Actions.setResponse({ response: privateKey }); | ||
} catch (err) { | ||
Lit.Actions.setResponse({ response: err.message }); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters