-
Notifications
You must be signed in to change notification settings - Fork 65
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 #687 from LIT-Protocol/LIT-3959-raw-wrapped-keys-l…
…itaction-functions LIT-3959 - Export raw wrapped-keys LIT action functions
- Loading branch information
Showing
39 changed files
with
468 additions
and
330 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export class AbortError extends Error { | ||
name = 'AbortError'; | ||
} | ||
|
||
export const rethrowIfAbortError = (err) => { | ||
if (err instanceof AbortError) { | ||
throw err; | ||
} | ||
}; |
38 changes: 0 additions & 38 deletions
38
packages/wrapped-keys-lit-actions/src/lib/common/exportPrivateKey.js
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
packages/wrapped-keys-lit-actions/src/lib/common/internal/getDecryptedKeyToSingleNode.js
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
packages/wrapped-keys-lit-actions/src/lib/ethereum/generateEncryptedEthereumPrivateKey.js
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
packages/wrapped-keys-lit-actions/src/lib/ethereum/signMessageWithEncryptedEthereumKey.js
This file was deleted.
Oops, something went wrong.
File renamed without changes.
42 changes: 42 additions & 0 deletions
42
packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.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,42 @@ | ||
/* global Lit */ | ||
|
||
import { AbortError } from '../../abortError'; | ||
import { removeSaltFromDecryptedKey } from '../../utils'; | ||
|
||
async function tryDecryptToSingleNode({ | ||
accessControlConditions, | ||
ciphertext, | ||
dataToEncryptHash, | ||
}) { | ||
try { | ||
// May be undefined, since we're using `decryptToSingleNode` | ||
return await Lit.Actions.decryptToSingleNode({ | ||
accessControlConditions, | ||
ciphertext, | ||
dataToEncryptHash, | ||
chain: 'ethereum', | ||
authSig: null, | ||
}); | ||
} catch (err) { | ||
throw new Error(`When decrypting key to a single node - ${err.message}`); | ||
} | ||
} | ||
|
||
export async function getDecryptedKeyToSingleNode({ | ||
accessControlConditions, | ||
ciphertext, | ||
dataToEncryptHash, | ||
}) { | ||
const decryptedPrivateKey = await tryDecryptToSingleNode({ | ||
accessControlConditions, | ||
ciphertext, | ||
dataToEncryptHash, | ||
}); | ||
|
||
if (!decryptedPrivateKey) { | ||
// Silently exit on nodes which didn't run the `decryptToSingleNode` code | ||
throw new AbortError(); | ||
} | ||
|
||
return removeSaltFromDecryptedKey(decryptedPrivateKey); | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions
25
packages/wrapped-keys-lit-actions/src/lib/litActionHandler.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,25 @@ | ||
/* global Lit */ | ||
|
||
import { AbortError } from './abortError'; | ||
|
||
export async function litActionHandler(actionFunc) { | ||
try { | ||
const litActionResult = await actionFunc(); | ||
// Don't re-stringify a string; we don't want to double-escape it | ||
const response = | ||
typeof litActionResult === 'string' | ||
? litActionResult | ||
: JSON.stringify(litActionResult); | ||
|
||
Lit.Actions.setResponse({ response }); | ||
} catch (err) { | ||
// AbortError means exit immediately and do _NOT_ set a response | ||
// Nested code should really only throw this in cases where using e.g. `decryptToSingleNode` | ||
// And this execution isn't that node. | ||
if (err instanceof AbortError) { | ||
return; | ||
} | ||
|
||
Lit.Actions.setResponse({ response: `Error: ${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
27 changes: 27 additions & 0 deletions
27
packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/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,27 @@ | ||
const { | ||
getDecryptedKeyToSingleNode, | ||
} = require('../../internal/common/getDecryptedKeyToSingleNode'); | ||
|
||
/** | ||
* | ||
* 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. | ||
*/ | ||
|
||
export async function exportPrivateKey({ | ||
accessControlConditions, | ||
ciphertext, | ||
dataToEncryptHash, | ||
}) { | ||
return getDecryptedKeyToSingleNode({ | ||
accessControlConditions, | ||
ciphertext, | ||
dataToEncryptHash, | ||
}); | ||
} |
22 changes: 22 additions & 0 deletions
22
...-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.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,22 @@ | ||
/** | ||
* | ||
* Generates a random Ethers private key and only allows the provided PKP to decrypt it | ||
* | ||
* @jsParam pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key | ||
* @jsParam accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key | ||
* | ||
* @returns { Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> } - Returns object with ciphertext & dataToEncryptHash which are the result of the encryption. Also returns the publicKey of the newly generated Ethers Wrapped Key. | ||
*/ | ||
import { encryptPrivateKey } from '../../internal/common/encryptKey'; | ||
import { generateEthereumPrivateKey } from '../../internal/ethereum/generatePrivateKey'; | ||
|
||
export async function generateEncryptedEthereumPrivateKey({ | ||
accessControlConditions, | ||
}) { | ||
const { privateKey, publicKey } = generateEthereumPrivateKey(); | ||
return encryptPrivateKey({ | ||
accessControlConditions, | ||
privateKey, | ||
publicKey, | ||
}); | ||
} |
Oops, something went wrong.