From faa4c69fd807488f73dbf4822d29f15dbe6638a0 Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 17 Oct 2024 17:48:04 +0100 Subject: [PATCH 01/15] feat: add types and convert lit actions to typescripts --- .../esbuild.config.js | 46 ++- packages/wrapped-keys-lit-actions/global.d.ts | 389 ++++++++++++++++++ .../src/lib/{abortError.js => abortError.ts} | 0 .../src/lib/{constants.js => constants.ts} | 0 .../common/{encryptKey.js => encryptKey.ts} | 6 +- ...Node.js => getDecryptedKeyToSingleNode.ts} | 18 +- ...atePrivateKey.js => generatePrivateKey.ts} | 1 + .../{signMessage.js => signMessage.ts} | 24 +- ...{signTransaction.js => signTransaction.ts} | 57 ++- ...atePrivateKey.js => generatePrivateKey.ts} | 0 .../solana/{signMessage.js => signMessage.ts} | 26 +- ...{signTransaction.js => signTransaction.ts} | 43 +- ...itActionHandler.js => litActionHandler.ts} | 4 +- ...dKeys.js => batchGenerateEncryptedKeys.ts} | 49 ++- .../common/exportPrivateKey.js | 27 -- .../common/exportPrivateKey.ts | 31 ++ ...=> generateEncryptedEthereumPrivateKey.ts} | 10 +- .../signMessageWithEncryptedEthereumKey.js | 36 -- .../signMessageWithEncryptedEthereumKey.ts | 32 ++ ...signTransactionWithEncryptedEthereumKey.js | 43 -- ...signTransactionWithEncryptedEthereumKey.ts | 38 ++ .../src/lib/raw-action-functions/index.js | 33 -- .../src/lib/raw-action-functions/index.ts | 19 + ...s => generateEncryptedSolanaPrivateKey.ts} | 15 +- .../signMessageWithEncryptedSolanaKey.js | 35 -- .../signMessageWithEncryptedSolanaKey.ts | 33 ++ .../signTransactionWithEncryptedSolanaKey.js | 43 -- .../signTransactionWithEncryptedSolanaKey.ts | 40 ++ ...dKeys.js => batchGenerateEncryptedKeys.ts} | 0 ...xportPrivateKey.js => exportPrivateKey.ts} | 0 ...=> generateEncryptedEthereumPrivateKey.ts} | 0 ...=> signMessageWithEncryptedEthereumKey.ts} | 0 ...ignTransactionWithEncryptedEthereumKey.ts} | 0 ...s => generateEncryptedSolanaPrivateKey.ts} | 0 ...s => signMessageWithEncryptedSolanaKey.ts} | 0 ... signTransactionWithEncryptedSolanaKey.ts} | 0 .../wrapped-keys-lit-actions/tsconfig.json | 5 +- 37 files changed, 780 insertions(+), 323 deletions(-) create mode 100644 packages/wrapped-keys-lit-actions/global.d.ts rename packages/wrapped-keys-lit-actions/src/lib/{abortError.js => abortError.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/{constants.js => constants.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/internal/common/{encryptKey.js => encryptKey.ts} (79%) rename packages/wrapped-keys-lit-actions/src/lib/internal/common/{getDecryptedKeyToSingleNode.js => getDecryptedKeyToSingleNode.ts} (65%) rename packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/{generatePrivateKey.js => generatePrivateKey.ts} (99%) rename packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/{signMessage.js => signMessage.ts} (52%) rename packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/{signTransaction.js => signTransaction.ts} (61%) rename packages/wrapped-keys-lit-actions/src/lib/internal/solana/{generatePrivateKey.js => generatePrivateKey.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/internal/solana/{signMessage.js => signMessage.ts} (60%) rename packages/wrapped-keys-lit-actions/src/lib/internal/solana/{signTransaction.js => signTransaction.ts} (55%) rename packages/wrapped-keys-lit-actions/src/lib/{litActionHandler.js => litActionHandler.ts} (88%) rename packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/{batchGenerateEncryptedKeys.js => batchGenerateEncryptedKeys.ts} (77%) delete mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.js create mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts rename packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/{generateEncryptedEthereumPrivateKey.js => generateEncryptedEthereumPrivateKey.ts} (63%) delete mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.js create mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts delete mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.js create mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts delete mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.js create mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.ts rename packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/{generateEncryptedSolanaPrivateKey.js => generateEncryptedSolanaPrivateKey.ts} (56%) delete mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.js create mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts delete mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.js create mode 100644 packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/{batchGenerateEncryptedKeys.js => batchGenerateEncryptedKeys.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/{exportPrivateKey.js => exportPrivateKey.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/{generateEncryptedEthereumPrivateKey.js => generateEncryptedEthereumPrivateKey.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/{signMessageWithEncryptedEthereumKey.js => signMessageWithEncryptedEthereumKey.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/{signTransactionWithEncryptedEthereumKey.js => signTransactionWithEncryptedEthereumKey.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/{generateEncryptedSolanaPrivateKey.js => generateEncryptedSolanaPrivateKey.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/{signMessageWithEncryptedSolanaKey.js => signMessageWithEncryptedSolanaKey.ts} (100%) rename packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/{signTransactionWithEncryptedSolanaKey.js => signTransactionWithEncryptedSolanaKey.ts} (100%) diff --git a/packages/wrapped-keys-lit-actions/esbuild.config.js b/packages/wrapped-keys-lit-actions/esbuild.config.js index 52e2d6f368..6803b4250a 100644 --- a/packages/wrapped-keys-lit-actions/esbuild.config.js +++ b/packages/wrapped-keys-lit-actions/esbuild.config.js @@ -48,24 +48,30 @@ module.exports = { }; (async () => { - await esbuild.build({ - entryPoints: [ - './src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.js', - './src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.js', - './src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.js', - './src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.js', - './src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.js', - './src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.js', - './src/lib/self-executing-actions/common/exportPrivateKey.js', - './src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.js', - ], - bundle: true, - minify: true, - sourcemap: false, - treeShaking: true, - outdir: './src/generated/', - inject: ['./buffer.shim.js'], - plugins: [wrapIIFEInStringPlugin], - platform: 'browser', - }); + try { + await esbuild.build({ + entryPoints: [ + './src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts', + './src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts', + './src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts', + './src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts', + './src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts', + './src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts', + './src/lib/self-executing-actions/common/exportPrivateKey.ts', + './src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts', + ], + bundle: true, + minify: true, + sourcemap: false, + treeShaking: true, + outdir: './src/generated/', + inject: ['./buffer.shim.js'], + plugins: [wrapIIFEInStringPlugin], + platform: 'browser', + }); + console.log('✅ Lit actions built successfully'); + } catch (e) { + console.error('❌ Error building lit actions: ', e); + process.exit(1); + } })(); diff --git a/packages/wrapped-keys-lit-actions/global.d.ts b/packages/wrapped-keys-lit-actions/global.d.ts new file mode 100644 index 0000000000..960a968994 --- /dev/null +++ b/packages/wrapped-keys-lit-actions/global.d.ts @@ -0,0 +1,389 @@ +import { ethers as EthersType } from 'ethers'; + +// @ts-nocheck - got this directly from the lit-assets repo +declare global { + + export const ethers: typeof EthersType; + + export declare namespace Lit { + export namespace Actions { + /** + * Check if a given IPFS ID is permitted to sign using a given PKP tokenId + * @function isPermittedAction + * @param {Object} params + * @param {string} params.tokenId The tokenId to check + * @param {string} params.ipfsId The IPFS ID of some JS code (a lit action) + * @returns {Promise} A boolean indicating whether the IPFS ID is permitted to sign using the PKP tokenId + */ + function isPermittedAction({ + tokenId, + ipfsId, + }: { + tokenId: string; + ipfsId: string; + }): Promise; + /** + * Check if a given wallet address is permitted to sign using a given PKP tokenId + * @function isPermittedAddress + * @param {Object} params + * @param {string} params.tokenId The tokenId to check + * @param {string} params.address The wallet address to check + * @returns {Promise} A boolean indicating whether the wallet address is permitted to sign using the PKP tokenId + */ + function isPermittedAddress({ + tokenId, + address, + }: { + tokenId: string; + address: string; + }): Promise; + /** + * Check if a given auth method is permitted to sign using a given PKP tokenId + * @function isPermittedAuthMethod + * @param {Object} params + * @param {string} params.tokenId The tokenId to check + * @param {number} params.authMethodType The auth method type. This is an integer. This mapping shows the initial set but this set may be expanded over time without updating this contract: https://github.com/LIT-Protocol/LitNodeContracts/blob/main/contracts/PKPPermissions.sol#L25 + * @param {Uint8Array} params.userId The id of the auth method to check expressed as an array of unsigned 8-bit integers (a Uint8Array) + * @returns {Promise} A boolean indicating whether the auth method is permitted to sign using the PKP tokenId + */ + function isPermittedAuthMethod({ + tokenId, + authMethodType, + userId, + }: { + tokenId: string; + authMethodType: number; + userId: Uint8Array; + }): Promise; + /** + * Get the full list of actions that are permitted to sign using a given PKP tokenId + * @function getPermittedActions + * @param {Object} params + * @param {string} params.tokenId The tokenId to check + * @returns {Promise>} An array of IPFS IDs of lit actions that are permitted to sign using the PKP tokenId + */ + function getPermittedActions({ + tokenId, + }: { + tokenId: string; + }): Promise>; + /** + * Get the full list of addresses that are permitted to sign using a given PKP tokenId + * @function getPermittedAddresses + * @param {Object} params + * @param {string} params.tokenId The tokenId to check + * @returns {Promise>} An array of addresses that are permitted to sign using the PKP tokenId + */ + function getPermittedAddresses({ + tokenId, + }: { + tokenId: string; + }): Promise>; + /** + * Get the full list of auth methods that are permitted to sign using a given PKP tokenId + * @function getPermittedAuthMethods + * @param {Object} params + * @param {string} params.tokenId The tokenId to check + * @returns {Promise>} An array of auth methods that are permitted to sign using the PKP tokenId. Each auth method is an object with the following properties: auth_method_type, id, and user_pubkey (used for web authn, this is the pubkey of the user's authentication keypair) + */ + function getPermittedAuthMethods({ + tokenId, + }: { + tokenId: string; + }): Promise>; + /** + * Get the permitted auth method scopes for a given PKP tokenId and auth method type + id + * @function getPermittedAuthMethodScopes + * @param {Object} params + * @param {string} params.tokenId The tokenId to check + * @param {string} params.authMethodType The auth method type to look up + * @param {Uint8Array} params.userId The id of the auth method to check expressed as an array of unsigned 8-bit integers (a Uint8Array) + * @param {number} params.maxScopeId The maximum scope id to check. This is an integer. + * @returns {Promise>} An array of booleans that define if a given scope id is turned on. The index of the array is the scope id. For example, if the array is [true, false, true], then scope ids 0 and 2 are turned on, but scope id 1 is turned off. + */ + function getPermittedAuthMethodScopes({ + tokenId, + authMethodType, + userId, + maxScopeId, + }: { + tokenId: string; + authMethodType: string; + userId: Uint8Array; + maxScopeId: number; + }): Promise>; + /** + * Converts a PKP public key to a PKP token ID by hashing it with keccak256 + * @function pubkeyToTokenId + * @param {Object} params + * @param {string} params.publicKey The public key to convert + * @returns {Promise} The token ID as a string + */ + function pubkeyToTokenId({ + publicKey, + }: { + publicKey: string; + }): Promise; + /** + * Gets latest nonce for the given address on a supported chain + * @function getLatestNonce + * @param {Object} params + * @param {string} params.address The wallet address for getting the nonce + * @param {string} params.chain The chain of which the nonce is fetched + * @returns {Promise} The token ID as a string + */ + function getLatestNonce({ + address, + chain, + }: { + address: string; + chain: string; + }): Promise; + /** + * Ask the Lit Node to sign any data using the ECDSA Algorithm with it's private key share. The resulting signature share will be returned to the Lit JS SDK which will automatically combine the shares and give you the full signature to use. + * @function signEcdsa + * @param {Object} params + * @param {Uint8Array} params.toSign The data to sign. Should be an array of 8-bit integers. + * @param {string} params.publicKey The public key of the PKP you wish to sign with + * @param {string} params.sigName You can put any string here. This is used to identify the signature in the response by the Lit JS SDK. This is useful if you are signing multiple messages at once. When you get the final signature out, it will be in an object with this signature name as the key. + * @returns {Promise} This function will return the string "success" if it works. The signature share is returned behind the scenes to the Lit JS SDK which will automatically combine the shares and give you the full signature to use. + */ + function signEcdsa({ + toSign, + publicKey, + sigName, + }: { + toSign: Uint8Array; + publicKey: string; + sigName: string; + }): Promise; + /** + * Ask the Lit Node to sign a message using the eth_personalSign algorithm. The resulting signature share will be returned to the Lit JS SDK which will automatically combine the shares and give you the full signature to use. + * @function ethPersonalSignMessageEcdsa + * @param {Object} params + * @param {string} params.message The message to sign. Should be a string. + * @param {string} params.publicKey The public key of the PKP you wish to sign with + * @param {string} params.sigName You can put any string here. This is used to identify the signature in the response by the Lit JS SDK. This is useful if you are signing multiple messages at once. When you get the final signature out, it will be in an object with this signature name as the key. + * @returns {Promise} This function will return the string "success" if it works. The signature share is returned behind the scenes to the Lit JS SDK which will automatically combine the shares and give you the full signature to use. + */ + function ethPersonalSignMessageEcdsa({ + message, + publicKey, + sigName, + }: { + message: string; + publicKey: string; + sigName: string; + }): Promise; + /** + * Checks a condition using the Lit condition checking engine. This is the same engine that powers our Access Control product. You can use this to check any condition that you can express in our condition language. This is a powerful tool that allows you to build complex conditions that can be checked in a decentralized way. Visit https://developer.litprotocol.com and click on the "Access Control" section to learn more. + * @function checkConditions + * @param {Object} params + * @param {Array} params.conditions An array of access control condition objects + * @param {Object} params.authSig The AuthSig to use for the condition check. For example, if you were checking for NFT ownership, this AuthSig would be the signature from the NFT owner's wallet. + * @param {string} params.chain The chain this AuthSig comes from + * @returns {Promise} A boolean indicating whether the condition check passed or failed + */ + function checkConditions({ + conditions, + authSig, + chain, + }: { + conditions: Array; + authSig: any; + chain: string; + }): Promise; + /** + * Set the response returned to the client + * @function setResponse + * @param {Object} params + * @param {string} params.response The response to send to the client. You can put any string here, like you could use JSON.stringify on a JS object and send it here. + */ + function setResponse({ response }: { response: string }): any; + /** + * Call a child Lit Action + * @function call + * @param {Object} params + * @param {string} params.ipfsId The IPFS ID of the Lit Action to call + * @param {Object=} params.params Optional parameters to pass to the child Lit Action + * @returns {Promise} The response from the child Lit Action. Note that any signatures performed by the child Lit Action will be automatically combined and returned with the parent Lit Action to the Lit JS SDK client. + */ + function call({ + ipfsId, + params, + }: { + ipfsId: string; + params?: any | undefined; + }): Promise; + /** + * Call a smart contract + * @function callContract + * @param {Object} params + * @param {string} params.chain The name of the chain to use. Check out the lit docs "Supported Blockchains" page to find the name. For example, "ethereum" + * @param {string} params.txn The RLP Encoded txn, as a hex string + * @returns {Promise} The response from calling the contract + */ + function callContract({ + chain, + txn, + }: { + chain: string; + txn: string; + }): Promise; + /** + * Convert a Uint8Array to a string. This is a re-export of this function: https://www.npmjs.com/package/uint8arrays#tostringarray-encoding--utf8 + * @function uint8arrayToString + * @param {Uint8Array} array The Uint8Array to convert + * @param {string} encoding The encoding to use. Defaults to "utf8" + * @returns {string} The string representation of the Uint8Array + */ + function uint8arrayToString(...args: any[]): string; + /** + * Convert a string to a Uint8Array. This is a re-export of this function: https://www.npmjs.com/package/uint8arrays#fromstringstring-encoding--utf8 + * @function uint8arrayFromString + * @param {string} string The string to convert + * @param {string} encoding The encoding to use. Defaults to "utf8" + * @returns {Uint8Array} The Uint8Array representation of the string + */ + function uint8arrayFromString(...args: any[]): Uint8Array; + function aesDecrypt({ + symmetricKey, + ciphertext, + }: { + symmetricKey: any; + ciphertext: any; + }): any; + /** + * Claim a key through a key identifier, the result of the claim will be added to `claim_id` + * under the `keyId` given. + * @param {Object} params + * @param {string} params.keyId user id of the claim + */ + function claimKey({ keyId }: { keyId: string }): any; + /** + * Broadcast a message to all connected clients and collect their responses + * @param {string} name The name of the broadcast + * @param {string} value The value to broadcast + * @returns {string} The collected responses as a json array + */ + function broadcastAndCollect({ name, value }: string): string; + /** + * Decrypt and combine the provided + * @param {string} accessControlConditions The access control conditions + * @param {string} ciphertext The ciphertext to decrypt + * @param {string} dataToEncryptHash The hash of the data to + @ @param {string} authSig The auth signature + * @param {string} chain The chain + * @returns {string} The combined data + */ + function decryptAndCombine({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + authSig, + chain, + }: string): string; + /** + * Decrypt to a single node. + * @param {string} accessControlConditions The access control conditions + * @param {string} ciphertext The ciphertext to decrypt + * @param {string} dataToEncryptHash The hash of the data to + @ @param { any} authSig The auth signature + * @param {string} chain The chain + * @returns {string} The combined data + */ + function decryptToSingleNode({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + authSig, + chain, + }: { + accessControlConditions: string; + ciphertext: string; + dataToEncryptHash: string; + chain: string; + authSig: any; + }): Promise; + /** + * @param {Uint8array} toSign the message to sign + * @param {string} publicKey the public key of the PKP + * @param {string} sigName the name of the signature + * @returns {Uint8array} The resulting signature + */ + function signAndCombineEcdsa({ + toSign, + publicKey, + sigName, + }: Uint8array): Uint8array; + /** + * + * @param {bool} waitForResponse Whether to wait for a response or not - if false, the function will return immediately. + * @returns {bool} Whether the node can run the code in the next block or not. + */ + function runOnce({ waitForResponse, name }: bool, async_fn: any): bool; + /** + * + * @param {string} chain The chain to get the RPC URL for + * @returns {string} The RPC URL for the chain + */ + function getRpcUrl({ chain }: { chain: string }): Promise; + /** + * + * @param {string} accessControlConditions The access control conditions + * @param {string} to_encrypt The message to encrypt + * @returns { {ciphertext: string, dataToEncryptHash: string} } Contains two items: The ciphertext result after encryption, named "ciphertext" and the dataToEncryptHash, named "dataToEncryptHash" + */ + function encrypt({ accessControlConditions, to_encrypt }: { + accessControlConditions: string; + to_encrypt: Uint8Array; + }): { + ciphertext: string; + dataToEncryptHash: string; + }; + } + + export namespace Auth { + + /** + * Array of action IPFS IDs. + * @type {Array<`Qm${string}` | string>} + */ + const actionIpfsIds: Array<`Qm${string}` | string>; + + /** + * Array of authentication method contexts. + * @type {Array<{ + * userId: string; + * appId: string; + * authMethodType: number; + * lastRetrievedAt: string; + * expiration: number; + * usedForSignSessionKeyRequest: boolean; + * }>} + */ + const authMethodContexts: { + userId: string; + appId: string; + authMethodType: number; + lastRetrievedAt: string; + expiration: number; + usedForSignSessionKeyRequest: boolean; + }[]; + + /** + * Array of resources. + * @type {Array} + */ + const resources: Array; + + /** + * Custom authentication resource. + * @type {string | `"\\(true,${string})\\"`} + */ + const customAuthResource: string | `"\\(true,${string})\\"`; + } + } +} + +export { }; diff --git a/packages/wrapped-keys-lit-actions/src/lib/abortError.js b/packages/wrapped-keys-lit-actions/src/lib/abortError.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/abortError.js rename to packages/wrapped-keys-lit-actions/src/lib/abortError.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/constants.js b/packages/wrapped-keys-lit-actions/src/lib/constants.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/constants.js rename to packages/wrapped-keys-lit-actions/src/lib/constants.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.js b/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.ts similarity index 79% rename from packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.ts index 42af63a266..724c5e7028 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.js +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.ts @@ -10,7 +10,11 @@ export async function encryptPrivateKey({ accessControlConditions, privateKey, publicKey, -}) { +}: { + accessControlConditions: string; + privateKey: string; + publicKey: string; +}): Promise<{ ciphertext: string; dataToEncryptHash: string; publicKey: string; }> { const { ciphertext, dataToEncryptHash } = await Lit.Actions.encrypt({ accessControlConditions, to_encrypt: new TextEncoder().encode(LIT_PREFIX + privateKey), diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.js b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts similarity index 65% rename from packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts index ae2924c75d..93b8728529 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.js +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts @@ -3,11 +3,17 @@ import { AbortError } from '../../abortError'; import { removeSaltFromDecryptedKey } from '../../utils'; +interface TryDecryptToSingleNodeParams { + accessControlConditions: any; + ciphertext: string; + dataToEncryptHash: string; +} + async function tryDecryptToSingleNode({ accessControlConditions, ciphertext, dataToEncryptHash, -}) { +}: TryDecryptToSingleNodeParams): Promise { try { // May be undefined, since we're using `decryptToSingleNode` return await Lit.Actions.decryptToSingleNode({ @@ -17,16 +23,22 @@ async function tryDecryptToSingleNode({ chain: 'ethereum', authSig: null, }); - } catch (err) { + } catch (err: any) { throw new Error(`When decrypting key to a single node - ${err.message}`); } } +interface GetDecryptedKeyToSingleNodeParams { + accessControlConditions: string; // Define a more specific type if possible + ciphertext: string; // Define a more specific type if possible + dataToEncryptHash: string; // Define a more specific type if possible +} + export async function getDecryptedKeyToSingleNode({ accessControlConditions, ciphertext, dataToEncryptHash, -}) { +}: GetDecryptedKeyToSingleNodeParams): Promise { const decryptedPrivateKey = await tryDecryptToSingleNode({ accessControlConditions, ciphertext, diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts similarity index 99% rename from packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts index 299816e039..382729a595 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.js +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts @@ -1,6 +1,7 @@ /* global ethers */ export function generateEthereumPrivateKey() { + const wallet = ethers.Wallet.createRandom(); return { diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.js b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts similarity index 52% rename from packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts index 5cc30864f0..543323133d 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.js +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts @@ -1,27 +1,37 @@ /* global ethers */ -async function signMessage({ privateKey, messageToSign }) { +interface SignMessageParams { + privateKey: string; + messageToSign: string; +} + +interface VerifyMessageSignatureParams { + messageToSign: string; + signature: string; +} + +async function signMessage({ privateKey, messageToSign }: SignMessageParams): Promise<{ signature: string; walletAddress: string }> { try { const wallet = new ethers.Wallet(privateKey); const signature = await wallet.signMessage(messageToSign); return { signature, walletAddress: wallet.address }; - } catch (err) { - throw new Error(`When signing message - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When signing message - ${(err as Error).message}`); } } -function verifyMessageSignature({ messageToSign, signature }) { +function verifyMessageSignature({ messageToSign, signature }: VerifyMessageSignatureParams): string { try { return ethers.utils.verifyMessage(messageToSign, signature); - } catch (err) { + } catch (err: unknown) { throw new Error( - `When validating signed Ethereum message is valid: ${err.message}` + `When validating signed Ethereum message is valid: ${(err as Error).message}` ); } } -export async function signMessageEthereumKey({ privateKey, messageToSign }) { +export async function signMessageEthereumKey({ privateKey, messageToSign }: SignMessageParams): Promise { const { signature, walletAddress } = await signMessage({ privateKey, messageToSign, diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.js b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts similarity index 61% rename from packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts index 192f133692..bfd447e88a 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.js +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts @@ -1,6 +1,16 @@ /* global ethers, Lit */ -export function getValidatedUnsignedTx(unsignedTransaction) { +interface UnsignedTransaction { + toAddress: string; + chain: string; + value: string; + chainId: number; + dataHex?: string; + gasPrice?: string; + gasLimit?: number; +} + +export function getValidatedUnsignedTx(unsignedTransaction: UnsignedTransaction) { try { if (!unsignedTransaction.toAddress) { throw new Error('Missing required field: toAddress'); @@ -26,12 +36,12 @@ export function getValidatedUnsignedTx(unsignedTransaction) { chainId: unsignedTransaction.chainId, data: unsignedTransaction.dataHex, }; - } catch (err) { - throw new Error(`Invalid unsignedTransaction - ${err.message}`); + } catch (err: unknown) { + throw new Error(`Invalid unsignedTransaction - ${(err as Error).message}`); } } -async function getLatestNonce({ walletAddress, chain }) { +async function getLatestNonce({ walletAddress, chain }: { walletAddress: string; chain: string }) { try { const nonce = await Lit.Actions.getLatestNonce({ address: walletAddress, @@ -39,60 +49,60 @@ async function getLatestNonce({ walletAddress, chain }) { }); return nonce; - } catch (err) { - throw new Error(`Unable to get latest nonce - ${err.message}`); + } catch (err: unknown) { + throw new Error(`Unable to get latest nonce - ${(err as Error).message}`); } } -async function getEthersRPCProvider({ chain }) { +async function getEthersRPCProvider({ chain }: { chain: string }) { try { const rpcUrl = await Lit.Actions.getRpcUrl({ chain, }); return new ethers.providers.JsonRpcProvider(rpcUrl); - } catch (err) { - throw new Error(`Getting the rpc for the chain: ${chain} - ${err.message}`); + } catch (err: unknown) { + throw new Error(`Getting the rpc for the chain: ${chain} - ${(err as Error).message}`); } } -async function getGasPrice({ userProvidedGasPrice, provider }) { +async function getGasPrice({ userProvidedGasPrice, provider }: { userProvidedGasPrice?: string; provider: any }) { try { if (userProvidedGasPrice) { return ethers.utils.parseUnits(userProvidedGasPrice, 'gwei'); } else { return await provider.getGasPrice(); } - } catch (err) { - throw new Error(`When getting gas price - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When getting gas price - ${(err as Error).message}`); } } -async function getGasLimit({ provider, userProvidedGasLimit, validatedTx }) { +async function getGasLimit({ provider, userProvidedGasLimit, validatedTx }: { provider: any; userProvidedGasLimit?: number; validatedTx: any }) { if (userProvidedGasLimit) { return userProvidedGasLimit; } else { try { return await provider.estimateGas(validatedTx); - } catch (err) { - throw new Error(`When estimating gas - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When estimating gas - ${(err as Error).message}`); } } } -async function signTransaction({ validatedTx, wallet }) { +async function signTransaction({ validatedTx, wallet }: { validatedTx: any; wallet: any }) { try { return await wallet.signTransaction(validatedTx); - } catch (err) { - throw new Error(`When signing transaction - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When signing transaction - ${(err as Error).message}`); } } -async function broadcastTransaction({ provider, signedTx }) { +async function broadcastTransaction({ provider, signedTx }: { provider: any; signedTx: string }) { try { return await provider.sendTransaction(signedTx); - } catch (err) { - throw new Error(`When sending transaction - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When sending transaction - ${(err as Error).message}`); } } @@ -101,6 +111,11 @@ export async function signTransactionEthereumKey({ privateKey, validatedTx, unsignedTransaction, +}: { + broadcast: boolean; + privateKey: string; + validatedTx: any; + unsignedTransaction: UnsignedTransaction; }) { const wallet = new ethers.Wallet(privateKey); diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/generatePrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/generatePrivateKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/internal/solana/generatePrivateKey.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/solana/generatePrivateKey.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.js b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts similarity index 60% rename from packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts index aee2d285cf..2f977727bc 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.js +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts @@ -1,9 +1,21 @@ import { Keypair } from '@solana/web3.js'; import nacl from 'tweetnacl'; +import { Buffer } from 'buffer'; /* global ethers */ -function signMessage({ messageToSign, solanaKeyPair }) { +interface SignMessageParams { + messageToSign: string; + solanaKeyPair: Keypair; +} + +interface VerifyMessageSignatureParams { + signature: Uint8Array; + solanaKeyPair: Keypair; + messageToSign: string; +} + +function signMessage({ messageToSign, solanaKeyPair }: SignMessageParams): { signature: Uint8Array } { try { const signature = nacl.sign.detached( new TextEncoder().encode(messageToSign), @@ -11,12 +23,12 @@ function signMessage({ messageToSign, solanaKeyPair }) { ); return { signature }; - } catch (err) { - throw new Error(`When signing message - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When signing message - ${(err as Error).message}`); } } -function verifyMessageSignature({ signature, solanaKeyPair, messageToSign }) { +function verifyMessageSignature({ signature, solanaKeyPair, messageToSign }: VerifyMessageSignatureParams): boolean { try { const isValid = nacl.sign.detached.verify( Buffer.from(messageToSign), @@ -25,14 +37,14 @@ function verifyMessageSignature({ signature, solanaKeyPair, messageToSign }) { ); return isValid; - } catch (err) { + } catch (err: unknown) { throw new Error( - `When validating signed Solana message is valid: ${err.message}` + `When validating signed Solana message is valid: ${(err as Error).message}` ); } } -export async function signMessageSolanaKey({ messageToSign, privateKey }) { +export async function signMessageSolanaKey({ messageToSign, privateKey }: { messageToSign: string; privateKey: string }): Promise { const solanaKeyPair = Keypair.fromSecretKey(Buffer.from(privateKey, 'hex')); const { signature } = signMessage({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.js b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts similarity index 55% rename from packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.js rename to packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts index c944426673..254fc4c01a 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.js +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts @@ -1,13 +1,13 @@ -const { - clusterApiUrl, - Connection, - Keypair, - Transaction, -} = require('@solana/web3.js'); +import { Cluster, clusterApiUrl, Connection, Keypair, Transaction } from '@solana/web3.js'; /* global ethers */ -export function validateUnsignedTransaction(unsignedTransaction) { +interface UnsignedTransaction { + chain: string; + serializedTransaction: string; +} + +export function validateUnsignedTransaction(unsignedTransaction: UnsignedTransaction) { const VALID_NETWORKS = ['devnet', 'testnet', 'mainnet-beta']; if (!VALID_NETWORKS.includes(unsignedTransaction.chain)) { @@ -16,7 +16,7 @@ export function validateUnsignedTransaction(unsignedTransaction) { if ( !unsignedTransaction.serializedTransaction || - !unsignedTransaction.serializedTransaction.length === 0 + unsignedTransaction.serializedTransaction.length === 0 ) { throw new Error( `Invalid serializedTransaction: ${unsignedTransaction.serializedTransaction}` @@ -24,22 +24,27 @@ export function validateUnsignedTransaction(unsignedTransaction) { } } -function signTransaction({ solanaKeyPair, transaction }) { +function signTransaction({ solanaKeyPair, transaction }: { solanaKeyPair: Keypair; transaction: Transaction }) { try { transaction.sign(solanaKeyPair); + if (!transaction.signature) { + throw new Error('Transaction signature is null'); + } + return { signature: ethers.utils.base58.encode(transaction.signature) }; - } catch (err) { - throw new Error(`When signing transaction - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When signing transaction - ${(err as Error).message}`); } } -async function sendTransaction({ chain, transaction }) { +async function sendTransaction({ chain, transaction }: { chain: Cluster; transaction: Transaction }) { + try { const solanaConnection = new Connection(clusterApiUrl(chain), 'confirmed'); return await solanaConnection.sendRawTransaction(transaction.serialize()); - } catch (err) { - throw new Error(`When sending transaction - ${err.message}`); + } catch (err: unknown) { + throw new Error(`When sending transaction - ${(err as Error).message}`); } } @@ -47,6 +52,10 @@ export async function signTransactionSolanaKey({ broadcast, privateKey, unsignedTransaction, +}: { + broadcast: boolean; + privateKey: string; + unsignedTransaction: UnsignedTransaction; }) { // Be sure you call validateUnsignedTransaction(unsignedTransaction); before calling this method! @@ -61,9 +70,11 @@ export async function signTransactionSolanaKey({ if (!broadcast) { return signature; } + // Ensure the chain is a valid Cluster type + const chain: Cluster = unsignedTransaction.chain as Cluster; return await sendTransaction({ - chain: unsignedTransaction.chain, + chain, transaction, }); -} +} \ No newline at end of file diff --git a/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.js b/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts similarity index 88% rename from packages/wrapped-keys-lit-actions/src/lib/litActionHandler.js rename to packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts index 68a306e8f9..2e90ce1859 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.js +++ b/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts @@ -2,7 +2,7 @@ import { AbortError } from './abortError'; -export async function litActionHandler(actionFunc) { +export async function litActionHandler(actionFunc: () => Promise) { try { const litActionResult = await actionFunc(); // Don't re-stringify a string; we don't want to double-escape it @@ -12,7 +12,7 @@ export async function litActionHandler(actionFunc) { : JSON.stringify(litActionResult); Lit.Actions.setResponse({ response }); - } catch (err) { + } catch (err: any) { // 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. diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts similarity index 77% rename from packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.js rename to packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts index e7d8db8c41..674c4ab125 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.js +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts @@ -1,16 +1,26 @@ -const { encryptPrivateKey } = require('../../internal/common/encryptKey'); -const { +import { encryptPrivateKey } from '../../internal/common/encryptKey'; +import { generateEthereumPrivateKey, -} = require('../../internal/ethereum/generatePrivateKey'); -const { +} from '../../internal/ethereum/generatePrivateKey'; +import { signMessageEthereumKey, -} = require('../../internal/ethereum/signMessage'); -const { +} from '../../internal/ethereum/signMessage'; +import { generateSolanaPrivateKey, -} = require('../../internal/solana/generatePrivateKey'); -const { signMessageSolanaKey } = require('../../internal/solana/signMessage'); +} from '../../internal/solana/generatePrivateKey'; +import { signMessageSolanaKey } from '../../internal/solana/signMessage'; -async function processEthereumAction({ action, accessControlConditions }) { +interface Action { + network: 'evm' | 'solana'; + generateKeyParams: { + memo: string; + }; + signMessageParams?: { + messageToSign?: string; + }; +} + +async function processEthereumAction({ action, accessControlConditions }: { action: Action; accessControlConditions: any }) { const { network, generateKeyParams } = action; const messageToSign = action.signMessageParams?.messageToSign; @@ -24,9 +34,9 @@ async function processEthereumAction({ action, accessControlConditions }) { }), messageToSign ? signMessageEthereumKey({ - messageToSign: messageToSign, - privateKey: ethereumKey.privateKey, - }) + messageToSign: messageToSign, + privateKey: ethereumKey.privateKey, + }) : Promise.resolve(), ]); @@ -42,7 +52,7 @@ async function processEthereumAction({ action, accessControlConditions }) { }; } -async function processSolanaAction({ action, accessControlConditions }) { +async function processSolanaAction({ action, accessControlConditions }: { action: Action; accessControlConditions: any }) { const { network, generateKeyParams } = action; const messageToSign = action.signMessageParams?.messageToSign; @@ -57,9 +67,9 @@ async function processSolanaAction({ action, accessControlConditions }) { }), messageToSign ? signMessageSolanaKey({ - messageToSign: messageToSign, - privateKey: solanaKey.privateKey, - }) + messageToSign: messageToSign, + privateKey: solanaKey.privateKey, + }) : Promise.resolve(), ]); @@ -75,7 +85,7 @@ async function processSolanaAction({ action, accessControlConditions }) { }; } -async function processActions({ actions, accessControlConditions }) { +async function processActions({ actions, accessControlConditions }: { actions: Action[]; accessControlConditions: any }) { return Promise.all( actions.map(async (action, ndx) => { const { network } = action; @@ -98,7 +108,7 @@ async function processActions({ actions, accessControlConditions }) { ); } -function validateParams(actions) { +function validateParams(actions: Action[]) { if (!actions) { throw new Error('Missing required field: actions'); } @@ -137,6 +147,9 @@ function validateParams(actions) { export async function batchGenerateEncryptedKeys({ actions, accessControlConditions, +}: { + actions: Action[]; + accessControlConditions: any; }) { validateParams(actions); diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.js deleted file mode 100644 index 1d85015bd3..0000000000 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.js +++ /dev/null @@ -1,27 +0,0 @@ -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 } - Returns a decrypted private key. - */ - -export async function exportPrivateKey({ - accessControlConditions, - ciphertext, - dataToEncryptHash, -}) { - return getDecryptedKeyToSingleNode({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - }); -} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts new file mode 100644 index 0000000000..1965b07ace --- /dev/null +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts @@ -0,0 +1,31 @@ +import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; + +interface ExportPrivateKeyParams { + accessControlConditions: any; + ciphertext: string; + dataToEncryptHash: string; +} + +/** + * + * Exports the private key after decrypting and removing the salt from it. + * + * @param {string} pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key + * @param {string} ciphertext - For the encrypted Wrapped Key + * @param {string} dataToEncryptHash - For the encrypted Wrapped Key + * @param {any} accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key + * + * @returns { Promise } - Returns a decrypted private key. + */ + +export async function exportPrivateKey({ + accessControlConditions, + ciphertext, + dataToEncryptHash, +}: ExportPrivateKeyParams): Promise { + return getDecryptedKeyToSingleNode({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + }); +} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts similarity index 63% rename from packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.js rename to packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts index f11cd6c1b1..8db85d57b1 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.js +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -2,17 +2,21 @@ * * 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 + * @param {string} pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key + * @param {any} 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'; +interface GenerateEncryptedEthereumPrivateKeyParams { + accessControlConditions: any; +} + export async function generateEncryptedEthereumPrivateKey({ accessControlConditions, -}) { +}: GenerateEncryptedEthereumPrivateKeyParams): Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> { const { privateKey, publicKey } = generateEthereumPrivateKey(); return encryptPrivateKey({ accessControlConditions, diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.js deleted file mode 100644 index 4f651b1072..0000000000 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.js +++ /dev/null @@ -1,36 +0,0 @@ -const { - getDecryptedKeyToSingleNode, -} = require('../../internal/common/getDecryptedKeyToSingleNode'); -const { - signMessageEthereumKey, -} = require('../../internal/ethereum/signMessage'); - -/** - * Signs a message with the Ethers wallet which is also decrypted inside the Lit Action. - * - * @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 messageToSign - The unsigned message to be signed by the Wrapped Key - * @jsParam accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key - * - * @returns { Promise } - Returns a message signed by the Ethers Wrapped key. Or returns errors if any. - */ - -export async function signMessageWithEncryptedEthereumKey({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - messageToSign, -}) { - const privateKey = await getDecryptedKeyToSingleNode({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - }); - - return signMessageEthereumKey({ - privateKey, - messageToSign, - }); -} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts new file mode 100644 index 0000000000..f663b3562d --- /dev/null +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -0,0 +1,32 @@ +import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; +import { signMessageEthereumKey } from '../../internal/ethereum/signMessage'; + +interface SignMessageWithEncryptedEthereumKeyParams { + accessControlConditions: any; + ciphertext: string; + dataToEncryptHash: string; + messageToSign: string; +} + +/** + * Signs a message with the Ethers wallet which is also decrypted inside the Lit Action. + * @param {string} pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key + * @returns {Promise} - Returns a message signed by the Ethers Wrapped key. Or returns errors if any. + */ +export async function signMessageWithEncryptedEthereumKey({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + messageToSign, +}: SignMessageWithEncryptedEthereumKeyParams): Promise { + const privateKey = await getDecryptedKeyToSingleNode({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + }); + + return signMessageEthereumKey({ + privateKey, + messageToSign, + }); +} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.js deleted file mode 100644 index 81616575f5..0000000000 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.js +++ /dev/null @@ -1,43 +0,0 @@ -const { - getDecryptedKeyToSingleNode, -} = require('../../internal/common/getDecryptedKeyToSingleNode'); -const { - signTransactionEthereumKey, - getValidatedUnsignedTx, -} = require('../../internal/ethereum/signTransaction'); - -/** - * - * Signs a transaction with the Ethers wallet whose private key is decrypted inside the Lit Action. - * - * @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 unsignedTransaction - The unsigned message to be signed by the Wrapped Key - * @jsParam broadcast - Flag used to determine whether to just sign the message or also to broadcast it using the node's RPC. Note, if the RPC doesn't exist for the chain then the Lit Action will throw an unsupported error. - * @jsParam accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key - * - * @returns { Promise } - Returns the transaction hash if broadcast is set as true else returns only the signed transaction. Or returns errors if any. - */ -export async function signTransactionWithEncryptedEthereumKey({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - unsignedTransaction, - broadcast, -}) { - const validatedTx = getValidatedUnsignedTx(unsignedTransaction); - - const privateKey = await getDecryptedKeyToSingleNode({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - }); - - return signTransactionEthereumKey({ - broadcast, - privateKey, - unsignedTransaction, - validatedTx, - }); -} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts new file mode 100644 index 0000000000..485976dd29 --- /dev/null +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -0,0 +1,38 @@ +import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; +import { signTransactionEthereumKey, getValidatedUnsignedTx } from '../../internal/ethereum/signTransaction'; + +interface SignTransactionWithEncryptedEthereumKeyParams { + accessControlConditions: any; + ciphertext: string; + dataToEncryptHash: string; + unsignedTransaction: any; + broadcast: boolean; +} + +/** + * + * Signs a transaction with the Ethers wallet whose private key is decrypted inside the Lit Action. + * @returns {Promise} - Returns the transaction hash if broadcast is set as true else returns only the signed transaction. Or returns errors if any. + */ +export async function signTransactionWithEncryptedEthereumKey({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + unsignedTransaction, + broadcast, +}: SignTransactionWithEncryptedEthereumKeyParams): Promise { + const validatedTx = getValidatedUnsignedTx(unsignedTransaction); + + const privateKey = await getDecryptedKeyToSingleNode({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + }); + + return signTransactionEthereumKey({ + broadcast, + privateKey, + unsignedTransaction, + validatedTx, + }); +} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.js deleted file mode 100644 index f5c88f070d..0000000000 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.js +++ /dev/null @@ -1,33 +0,0 @@ -const { - batchGenerateEncryptedKeys, -} = require('./common/batchGenerateEncryptedKeys'); -const { exportPrivateKey } = require('./common/exportPrivateKey'); -const { - generateEncryptedEthereumPrivateKey, -} = require('./ethereum/generateEncryptedEthereumPrivateKey'); -const { - signMessageWithEncryptedEthereumKey, -} = require('./ethereum/signMessageWithEncryptedEthereumKey'); -const { - signTransactionWithEncryptedEthereumKey, -} = require('./ethereum/signTransactionWithEncryptedEthereumKey'); -const { - generateEncryptedSolanaPrivateKey, -} = require('./solana/generateEncryptedSolanaPrivateKey'); -const { - signMessageWithEncryptedSolanaKey, -} = require('./solana/signMessageWithEncryptedSolanaKey'); -const { - signTransactionWithEncryptedSolanaKey, -} = require('./solana/signTransactionWithEncryptedSolanaKey'); - -export const rawActionFunctions = { - exportPrivateKey, - batchGenerateEncryptedKeys, - generateEncryptedEthereumPrivateKey, - signMessageWithEncryptedEthereumKey, - signTransactionWithEncryptedEthereumKey, - generateEncryptedSolanaPrivateKey, - signMessageWithEncryptedSolanaKey, - signTransactionWithEncryptedSolanaKey, -}; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.ts new file mode 100644 index 0000000000..f08e02708e --- /dev/null +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/index.ts @@ -0,0 +1,19 @@ +import { batchGenerateEncryptedKeys } from './common/batchGenerateEncryptedKeys'; +import { exportPrivateKey } from './common/exportPrivateKey'; +import { generateEncryptedEthereumPrivateKey } from './ethereum/generateEncryptedEthereumPrivateKey'; +import { signMessageWithEncryptedEthereumKey } from './ethereum/signMessageWithEncryptedEthereumKey'; +import { signTransactionWithEncryptedEthereumKey } from './ethereum/signTransactionWithEncryptedEthereumKey'; +import { generateEncryptedSolanaPrivateKey } from './solana/generateEncryptedSolanaPrivateKey'; +import { signMessageWithEncryptedSolanaKey } from './solana/signMessageWithEncryptedSolanaKey'; +import { signTransactionWithEncryptedSolanaKey } from './solana/signTransactionWithEncryptedSolanaKey'; + +export const rawActionFunctions = { + exportPrivateKey, + batchGenerateEncryptedKeys, + generateEncryptedEthereumPrivateKey, + signMessageWithEncryptedEthereumKey, + signTransactionWithEncryptedEthereumKey, + generateEncryptedSolanaPrivateKey, + signMessageWithEncryptedSolanaKey, + signTransactionWithEncryptedSolanaKey, +}; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts similarity index 56% rename from packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.js rename to packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts index 02277722d9..52a9b25fff 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.js +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts @@ -1,19 +1,20 @@ -const { encryptPrivateKey } = require('../../internal/common/encryptKey'); -const { - generateSolanaPrivateKey, -} = require('../../internal/solana/generatePrivateKey'); +import { encryptPrivateKey } from '../../internal/common/encryptKey'; +import { generateSolanaPrivateKey } from '../../internal/solana/generatePrivateKey'; /** * Bundles solana/web3.js package as it's required to generate a random Solana 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 + * @param {any} accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key * * @returns { Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> } - Returns JSON object with ciphertext & dataToEncryptHash which are the result of the encryption. Also returns the publicKey of the newly generated Solana Wrapped Key. */ +interface GenerateEncryptedSolanaPrivateKeyParams { + accessControlConditions: any; +} + export async function generateEncryptedSolanaPrivateKey({ accessControlConditions, -}) { +}: GenerateEncryptedSolanaPrivateKeyParams): Promise<{ ciphertext: string, dataToEncryptHash: string, publicKey: string }> { const { privateKey, publicKey } = generateSolanaPrivateKey(); return encryptPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.js deleted file mode 100644 index ec9ade6448..0000000000 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.js +++ /dev/null @@ -1,35 +0,0 @@ -const { - getDecryptedKeyToSingleNode, -} = require('../../internal/common/getDecryptedKeyToSingleNode'); -const { signMessageSolanaKey } = require('../../internal/solana/signMessage'); - -/** - * - * Bundles solana/web3.js package as it's required to sign a message with the Solana wallet which is also decrypted inside the Lit Action. - * - * @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 messageToSign - The unsigned message to be signed by the Wrapped Key - * @jsParam accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key - * - * @returns { Promise } - Returns a message signed by the Solana Wrapped key. Or returns errors if any. - */ - -export async function signMessageWithEncryptedSolanaKey({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - messageToSign, -}) { - const privateKey = await getDecryptedKeyToSingleNode({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - }); - - return signMessageSolanaKey({ - messageToSign, - privateKey, - }); -} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts new file mode 100644 index 0000000000..5a086a93f1 --- /dev/null +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts @@ -0,0 +1,33 @@ +import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; +import { signMessageSolanaKey } from '../../internal/solana/signMessage'; + +interface SignMessageWithEncryptedSolanaKeyParams { + accessControlConditions: any; + ciphertext: string; + dataToEncryptHash: string; + messageToSign: string; +} + +/** + * + * Bundles solana/web3.js package as it's required to sign a message with the Solana wallet which is also decrypted inside the Lit Action. + * @param { SignMessageWithEncryptedSolanaKeyParams } params - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key + * @returns {Promise} - Returns a message signed by the Solana Wrapped key. Or returns errors if any. + */ +export async function signMessageWithEncryptedSolanaKey({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + messageToSign, +}: SignMessageWithEncryptedSolanaKeyParams): Promise { + const privateKey = await getDecryptedKeyToSingleNode({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + }); + + return signMessageSolanaKey({ + messageToSign, + privateKey, + }); +} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.js b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.js deleted file mode 100644 index d737ec1315..0000000000 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.js +++ /dev/null @@ -1,43 +0,0 @@ -const { - getDecryptedKeyToSingleNode, -} = require('../../internal/common/getDecryptedKeyToSingleNode'); -const { - signTransactionSolanaKey, - validateUnsignedTransaction, -} = require('../../internal/solana/signTransaction'); - -/** - * - * Bundles solana/web3.js package as it's required to sign a transaction with the Solana wallet which is also decrypted inside the Lit Action. - * - * @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 unsignedTransaction - The unsigned message to be signed by the Wrapped Key - * @jsParam broadcast - Flag used to determine whether to just sign the message or also to broadcast it using the node's RPC. - * @jsParam accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key - * - * @returns { Promise } - Returns the transaction signature. Or returns errors if any. - */ - -export async function signTransactionWithEncryptedSolanaKey({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - unsignedTransaction, - broadcast, -}) { - validateUnsignedTransaction(unsignedTransaction); - - const privateKey = await getDecryptedKeyToSingleNode({ - accessControlConditions, - ciphertext, - dataToEncryptHash, - }); - - return signTransactionSolanaKey({ - broadcast, - privateKey, - unsignedTransaction, - }); -} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts new file mode 100644 index 0000000000..a7f6dfa4e3 --- /dev/null +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -0,0 +1,40 @@ +import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; +import { signTransactionSolanaKey, validateUnsignedTransaction } from '../../internal/solana/signTransaction'; + +interface SignTransactionWithEncryptedSolanaKeyParams { + accessControlConditions: any; // Define a more specific type if possible + ciphertext: string; // The encrypted Wrapped Key + dataToEncryptHash: string; // The hash of the data to encrypt + unsignedTransaction: any; // Define a more specific type if possible + broadcast: boolean; // Flag to determine if the transaction should be broadcasted +} + +/** + * + * Bundles solana/web3.js package as it's required to sign a transaction with the Solana wallet which is also decrypted inside the Lit Action. + * + * @param {SignTransactionWithEncryptedSolanaKeyParams} params - The parameters for signing the transaction + * @returns { Promise } - Returns the transaction signature. Or returns errors if any. + */ + +export async function signTransactionWithEncryptedSolanaKey({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + unsignedTransaction, + broadcast, +}: SignTransactionWithEncryptedSolanaKeyParams): Promise { + validateUnsignedTransaction(unsignedTransaction); + + const privateKey = await getDecryptedKeyToSingleNode({ + accessControlConditions, + ciphertext, + dataToEncryptHash, + }); + + return signTransactionSolanaKey({ + broadcast, + privateKey, + unsignedTransaction, + }); +} diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.js b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts similarity index 100% rename from packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.js rename to packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts diff --git a/packages/wrapped-keys-lit-actions/tsconfig.json b/packages/wrapped-keys-lit-actions/tsconfig.json index dd441376c9..e820883627 100644 --- a/packages/wrapped-keys-lit-actions/tsconfig.json +++ b/packages/wrapped-keys-lit-actions/tsconfig.json @@ -12,7 +12,10 @@ "checkJs": false }, "files": [], - "include": [], + "include": [ + "global.d.ts", + "node_modules/ethers/lib/ethers.d.ts", + ], "references": [ { "path": "./tsconfig.lib.json" From b6f004daee77e223eda13035a861f454b298ff80 Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 17 Oct 2024 17:55:24 +0100 Subject: [PATCH 02/15] feat: declare global variables --- .../common/batchGenerateEncryptedKeys.ts | 6 ++++++ .../lib/self-executing-actions/common/exportPrivateKey.ts | 6 ++++++ .../ethereum/generateEncryptedEthereumPrivateKey.ts | 4 ++++ .../ethereum/signMessageWithEncryptedEthereumKey.ts | 7 +++++++ .../ethereum/signTransactionWithEncryptedEthereumKey.ts | 8 ++++++++ .../solana/generateEncryptedSolanaPrivateKey.ts | 4 ++++ .../solana/signMessageWithEncryptedSolanaKey.ts | 7 +++++++ .../solana/signTransactionWithEncryptedSolanaKey.ts | 8 ++++++++ 8 files changed, 50 insertions(+) diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts index 1e658a0d5c..5ba229bb8b 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts @@ -3,7 +3,13 @@ import { batchGenerateEncryptedKeys } from '../../raw-action-functions/common/ba /* global actions accessControlConditions */ +declare global { + var actions: any[]; + var accessControlConditions: any; +} + (async () => litActionHandler(async () => + batchGenerateEncryptedKeys({ actions, accessControlConditions }) ))(); diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts index 1b2ecec252..ad1349c999 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts @@ -3,6 +3,12 @@ import { litActionHandler } from '../../litActionHandler'; import { exportPrivateKey } from '../../raw-action-functions/common/exportPrivateKey'; +declare global { + var accessControlConditions: any; + var ciphertext: any; + var dataToEncryptHash: any; +} + (async () => litActionHandler(async () => exportPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts index c79eba28c2..1472070115 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -3,6 +3,10 @@ import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedEthereumPrivateKey } from '../../raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey'; +declare global { + var accessControlConditions: any; +} + (async () => litActionHandler(async () => generateEncryptedEthereumPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts index 5ad3e679d1..593c00d36f 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -3,6 +3,13 @@ import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey'; +declare global { + var accessControlConditions: any; + var ciphertext: any; + var dataToEncryptHash: any; + var messageToSign: any; +} + (async () => litActionHandler(async () => signMessageWithEncryptedEthereumKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts index 3c83c0fb35..1a144d1e8b 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -3,6 +3,14 @@ import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey'; +declare global { + var accessControlConditions: any; + var ciphertext: any; + var dataToEncryptHash: any; + var unsignedTransaction: any; + var broadcast: any; +} + (async () => litActionHandler(async () => signTransactionWithEncryptedEthereumKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts index 2382514531..5c17563104 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts @@ -3,6 +3,10 @@ import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedSolanaPrivateKey } from '../../raw-action-functions/solana/generateEncryptedSolanaPrivateKey'; +declare global { + var accessControlConditions: any; +} + (async () => litActionHandler(async () => generateEncryptedSolanaPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts index 2e3603ba67..0227d3c097 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts @@ -3,6 +3,13 @@ import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signMessageWithEncryptedSolanaKey'; +declare global { + var accessControlConditions: any; + var ciphertext: any; + var dataToEncryptHash: any; + var messageToSign: any; +} + (async () => litActionHandler(async () => signMessageWithEncryptedSolanaKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts index 236b930be2..ebf36900b8 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -3,6 +3,14 @@ import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signTransactionWithEncryptedSolanaKey'; +declare global { + var accessControlConditions: any; + var ciphertext: any; + var dataToEncryptHash: any; + var unsignedTransaction: any; + var broadcast: any; +} + (async () => litActionHandler(async () => signTransactionWithEncryptedSolanaKey({ From 94329e7b3d681650121089ea251f45ccc9cf5d15 Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 17 Oct 2024 17:56:17 +0100 Subject: [PATCH 03/15] fix: type --- packages/wrapped-keys-lit-actions/src/lib/abortError.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/wrapped-keys-lit-actions/src/lib/abortError.ts b/packages/wrapped-keys-lit-actions/src/lib/abortError.ts index 189c782bd4..eaaae31234 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/abortError.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/abortError.ts @@ -1,8 +1,8 @@ export class AbortError extends Error { - name = 'AbortError'; + override name = 'AbortError'; } -export const rethrowIfAbortError = (err) => { +export const rethrowIfAbortError = (err: any) => { if (err instanceof AbortError) { throw err; } From 2092f2030b9988eadd2555c5329cdad5fd4ca33a Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 17 Oct 2024 18:12:55 +0100 Subject: [PATCH 04/15] fml --- packages/wrapped-keys-lit-actions/global.d.ts | 9 ++-- .../src/lib/internal/common/encryptKey.ts | 6 ++- .../internal/ethereum/generatePrivateKey.ts | 1 - .../src/lib/internal/ethereum/signMessage.ts | 19 +++++-- .../lib/internal/ethereum/signTransaction.ts | 50 ++++++++++++++++--- .../src/lib/internal/solana/signMessage.ts | 22 ++++++-- .../lib/internal/solana/signTransaction.ts | 31 +++++++++--- .../common/batchGenerateEncryptedKeys.ts | 48 +++++++++++------- .../generateEncryptedEthereumPrivateKey.ts | 6 ++- .../signMessageWithEncryptedEthereumKey.ts | 2 +- ...signTransactionWithEncryptedEthereumKey.ts | 11 ++-- .../generateEncryptedSolanaPrivateKey.ts | 6 ++- .../signMessageWithEncryptedSolanaKey.ts | 2 +- .../signTransactionWithEncryptedSolanaKey.ts | 5 +- .../common/batchGenerateEncryptedKeys.ts | 1 - .../wrapped-keys-lit-actions/tsconfig.json | 5 +- 16 files changed, 165 insertions(+), 59 deletions(-) diff --git a/packages/wrapped-keys-lit-actions/global.d.ts b/packages/wrapped-keys-lit-actions/global.d.ts index 960a968994..c3e396d150 100644 --- a/packages/wrapped-keys-lit-actions/global.d.ts +++ b/packages/wrapped-keys-lit-actions/global.d.ts @@ -2,7 +2,6 @@ import { ethers as EthersType } from 'ethers'; // @ts-nocheck - got this directly from the lit-assets repo declare global { - export const ethers: typeof EthersType; export declare namespace Lit { @@ -334,7 +333,10 @@ declare global { * @param {string} to_encrypt The message to encrypt * @returns { {ciphertext: string, dataToEncryptHash: string} } Contains two items: The ciphertext result after encryption, named "ciphertext" and the dataToEncryptHash, named "dataToEncryptHash" */ - function encrypt({ accessControlConditions, to_encrypt }: { + function encrypt({ + accessControlConditions, + to_encrypt, + }: { accessControlConditions: string; to_encrypt: Uint8Array; }): { @@ -344,7 +346,6 @@ declare global { } export namespace Auth { - /** * Array of action IPFS IDs. * @type {Array<`Qm${string}` | string>} @@ -386,4 +387,4 @@ declare global { } } -export { }; +export {}; diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.ts index 724c5e7028..61a8081925 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/common/encryptKey.ts @@ -14,7 +14,11 @@ export async function encryptPrivateKey({ accessControlConditions: string; privateKey: string; publicKey: string; -}): Promise<{ ciphertext: string; dataToEncryptHash: string; publicKey: string; }> { +}): Promise<{ + ciphertext: string; + dataToEncryptHash: string; + publicKey: string; +}> { const { ciphertext, dataToEncryptHash } = await Lit.Actions.encrypt({ accessControlConditions, to_encrypt: new TextEncoder().encode(LIT_PREFIX + privateKey), diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts index 382729a595..299816e039 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts @@ -1,7 +1,6 @@ /* global ethers */ export function generateEthereumPrivateKey() { - const wallet = ethers.Wallet.createRandom(); return { diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts index 543323133d..bc3cd4dcec 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts @@ -10,7 +10,10 @@ interface VerifyMessageSignatureParams { signature: string; } -async function signMessage({ privateKey, messageToSign }: SignMessageParams): Promise<{ signature: string; walletAddress: string }> { +async function signMessage({ + privateKey, + messageToSign, +}: SignMessageParams): Promise<{ signature: string; walletAddress: string }> { try { const wallet = new ethers.Wallet(privateKey); const signature = await wallet.signMessage(messageToSign); @@ -21,17 +24,25 @@ async function signMessage({ privateKey, messageToSign }: SignMessageParams): Pr } } -function verifyMessageSignature({ messageToSign, signature }: VerifyMessageSignatureParams): string { +function verifyMessageSignature({ + messageToSign, + signature, +}: VerifyMessageSignatureParams): string { try { return ethers.utils.verifyMessage(messageToSign, signature); } catch (err: unknown) { throw new Error( - `When validating signed Ethereum message is valid: ${(err as Error).message}` + `When validating signed Ethereum message is valid: ${ + (err as Error).message + }` ); } } -export async function signMessageEthereumKey({ privateKey, messageToSign }: SignMessageParams): Promise { +export async function signMessageEthereumKey({ + privateKey, + messageToSign, +}: SignMessageParams): Promise { const { signature, walletAddress } = await signMessage({ privateKey, messageToSign, diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts index bfd447e88a..04400815bb 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts @@ -10,7 +10,9 @@ interface UnsignedTransaction { gasLimit?: number; } -export function getValidatedUnsignedTx(unsignedTransaction: UnsignedTransaction) { +export function getValidatedUnsignedTx( + unsignedTransaction: UnsignedTransaction +) { try { if (!unsignedTransaction.toAddress) { throw new Error('Missing required field: toAddress'); @@ -41,7 +43,13 @@ export function getValidatedUnsignedTx(unsignedTransaction: UnsignedTransaction) } } -async function getLatestNonce({ walletAddress, chain }: { walletAddress: string; chain: string }) { +async function getLatestNonce({ + walletAddress, + chain, +}: { + walletAddress: string; + chain: string; +}) { try { const nonce = await Lit.Actions.getLatestNonce({ address: walletAddress, @@ -62,11 +70,19 @@ async function getEthersRPCProvider({ chain }: { chain: string }) { return new ethers.providers.JsonRpcProvider(rpcUrl); } catch (err: unknown) { - throw new Error(`Getting the rpc for the chain: ${chain} - ${(err as Error).message}`); + throw new Error( + `Getting the rpc for the chain: ${chain} - ${(err as Error).message}` + ); } } -async function getGasPrice({ userProvidedGasPrice, provider }: { userProvidedGasPrice?: string; provider: any }) { +async function getGasPrice({ + userProvidedGasPrice, + provider, +}: { + userProvidedGasPrice?: string; + provider: any; +}) { try { if (userProvidedGasPrice) { return ethers.utils.parseUnits(userProvidedGasPrice, 'gwei'); @@ -78,7 +94,15 @@ async function getGasPrice({ userProvidedGasPrice, provider }: { userProvidedGas } } -async function getGasLimit({ provider, userProvidedGasLimit, validatedTx }: { provider: any; userProvidedGasLimit?: number; validatedTx: any }) { +async function getGasLimit({ + provider, + userProvidedGasLimit, + validatedTx, +}: { + provider: any; + userProvidedGasLimit?: number; + validatedTx: any; +}) { if (userProvidedGasLimit) { return userProvidedGasLimit; } else { @@ -90,7 +114,13 @@ async function getGasLimit({ provider, userProvidedGasLimit, validatedTx }: { pr } } -async function signTransaction({ validatedTx, wallet }: { validatedTx: any; wallet: any }) { +async function signTransaction({ + validatedTx, + wallet, +}: { + validatedTx: any; + wallet: any; +}) { try { return await wallet.signTransaction(validatedTx); } catch (err: unknown) { @@ -98,7 +128,13 @@ async function signTransaction({ validatedTx, wallet }: { validatedTx: any; wall } } -async function broadcastTransaction({ provider, signedTx }: { provider: any; signedTx: string }) { +async function broadcastTransaction({ + provider, + signedTx, +}: { + provider: any; + signedTx: string; +}) { try { return await provider.sendTransaction(signedTx); } catch (err: unknown) { diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts index 2f977727bc..8b4676e571 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts @@ -15,7 +15,9 @@ interface VerifyMessageSignatureParams { messageToSign: string; } -function signMessage({ messageToSign, solanaKeyPair }: SignMessageParams): { signature: Uint8Array } { +function signMessage({ messageToSign, solanaKeyPair }: SignMessageParams): { + signature: Uint8Array; +} { try { const signature = nacl.sign.detached( new TextEncoder().encode(messageToSign), @@ -28,7 +30,11 @@ function signMessage({ messageToSign, solanaKeyPair }: SignMessageParams): { sig } } -function verifyMessageSignature({ signature, solanaKeyPair, messageToSign }: VerifyMessageSignatureParams): boolean { +function verifyMessageSignature({ + signature, + solanaKeyPair, + messageToSign, +}: VerifyMessageSignatureParams): boolean { try { const isValid = nacl.sign.detached.verify( Buffer.from(messageToSign), @@ -39,12 +45,20 @@ function verifyMessageSignature({ signature, solanaKeyPair, messageToSign }: Ver return isValid; } catch (err: unknown) { throw new Error( - `When validating signed Solana message is valid: ${(err as Error).message}` + `When validating signed Solana message is valid: ${ + (err as Error).message + }` ); } } -export async function signMessageSolanaKey({ messageToSign, privateKey }: { messageToSign: string; privateKey: string }): Promise { +export async function signMessageSolanaKey({ + messageToSign, + privateKey, +}: { + messageToSign: string; + privateKey: string; +}): Promise { const solanaKeyPair = Keypair.fromSecretKey(Buffer.from(privateKey, 'hex')); const { signature } = signMessage({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts index 254fc4c01a..d233502975 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts @@ -1,4 +1,10 @@ -import { Cluster, clusterApiUrl, Connection, Keypair, Transaction } from '@solana/web3.js'; +import { + Cluster, + clusterApiUrl, + Connection, + Keypair, + Transaction, +} from '@solana/web3.js'; /* global ethers */ @@ -7,7 +13,9 @@ interface UnsignedTransaction { serializedTransaction: string; } -export function validateUnsignedTransaction(unsignedTransaction: UnsignedTransaction) { +export function validateUnsignedTransaction( + unsignedTransaction: UnsignedTransaction +) { const VALID_NETWORKS = ['devnet', 'testnet', 'mainnet-beta']; if (!VALID_NETWORKS.includes(unsignedTransaction.chain)) { @@ -24,7 +32,13 @@ export function validateUnsignedTransaction(unsignedTransaction: UnsignedTransac } } -function signTransaction({ solanaKeyPair, transaction }: { solanaKeyPair: Keypair; transaction: Transaction }) { +function signTransaction({ + solanaKeyPair, + transaction, +}: { + solanaKeyPair: Keypair; + transaction: Transaction; +}) { try { transaction.sign(solanaKeyPair); @@ -38,8 +52,13 @@ function signTransaction({ solanaKeyPair, transaction }: { solanaKeyPair: Keypai } } -async function sendTransaction({ chain, transaction }: { chain: Cluster; transaction: Transaction }) { - +async function sendTransaction({ + chain, + transaction, +}: { + chain: Cluster; + transaction: Transaction; +}) { try { const solanaConnection = new Connection(clusterApiUrl(chain), 'confirmed'); return await solanaConnection.sendRawTransaction(transaction.serialize()); @@ -77,4 +96,4 @@ export async function signTransactionSolanaKey({ chain, transaction, }); -} \ No newline at end of file +} diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts index 674c4ab125..02bc37d209 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts @@ -1,13 +1,7 @@ import { encryptPrivateKey } from '../../internal/common/encryptKey'; -import { - generateEthereumPrivateKey, -} from '../../internal/ethereum/generatePrivateKey'; -import { - signMessageEthereumKey, -} from '../../internal/ethereum/signMessage'; -import { - generateSolanaPrivateKey, -} from '../../internal/solana/generatePrivateKey'; +import { generateEthereumPrivateKey } from '../../internal/ethereum/generatePrivateKey'; +import { signMessageEthereumKey } from '../../internal/ethereum/signMessage'; +import { generateSolanaPrivateKey } from '../../internal/solana/generatePrivateKey'; import { signMessageSolanaKey } from '../../internal/solana/signMessage'; interface Action { @@ -20,7 +14,13 @@ interface Action { }; } -async function processEthereumAction({ action, accessControlConditions }: { action: Action; accessControlConditions: any }) { +async function processEthereumAction({ + action, + accessControlConditions, +}: { + action: Action; + accessControlConditions: any; +}) { const { network, generateKeyParams } = action; const messageToSign = action.signMessageParams?.messageToSign; @@ -34,9 +34,9 @@ async function processEthereumAction({ action, accessControlConditions }: { acti }), messageToSign ? signMessageEthereumKey({ - messageToSign: messageToSign, - privateKey: ethereumKey.privateKey, - }) + messageToSign: messageToSign, + privateKey: ethereumKey.privateKey, + }) : Promise.resolve(), ]); @@ -52,7 +52,13 @@ async function processEthereumAction({ action, accessControlConditions }: { acti }; } -async function processSolanaAction({ action, accessControlConditions }: { action: Action; accessControlConditions: any }) { +async function processSolanaAction({ + action, + accessControlConditions, +}: { + action: Action; + accessControlConditions: any; +}) { const { network, generateKeyParams } = action; const messageToSign = action.signMessageParams?.messageToSign; @@ -67,9 +73,9 @@ async function processSolanaAction({ action, accessControlConditions }: { action }), messageToSign ? signMessageSolanaKey({ - messageToSign: messageToSign, - privateKey: solanaKey.privateKey, - }) + messageToSign: messageToSign, + privateKey: solanaKey.privateKey, + }) : Promise.resolve(), ]); @@ -85,7 +91,13 @@ async function processSolanaAction({ action, accessControlConditions }: { action }; } -async function processActions({ actions, accessControlConditions }: { actions: Action[]; accessControlConditions: any }) { +async function processActions({ + actions, + accessControlConditions, +}: { + actions: Action[]; + accessControlConditions: any; +}) { return Promise.all( actions.map(async (action, ndx) => { const { network } = action; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts index 8db85d57b1..a6b2d23643 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -16,7 +16,11 @@ interface GenerateEncryptedEthereumPrivateKeyParams { export async function generateEncryptedEthereumPrivateKey({ accessControlConditions, -}: GenerateEncryptedEthereumPrivateKeyParams): Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> { +}: GenerateEncryptedEthereumPrivateKeyParams): Promise<{ + ciphertext: string; + dataToEncryptHash: string; + publicKey: string; +}> { const { privateKey, publicKey } = generateEthereumPrivateKey(); return encryptPrivateKey({ accessControlConditions, diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts index f663b3562d..cc698edb3a 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -2,7 +2,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedK import { signMessageEthereumKey } from '../../internal/ethereum/signMessage'; interface SignMessageWithEncryptedEthereumKeyParams { - accessControlConditions: any; + accessControlConditions: any; ciphertext: string; dataToEncryptHash: string; messageToSign: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts index 485976dd29..8297ace844 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -1,16 +1,19 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; -import { signTransactionEthereumKey, getValidatedUnsignedTx } from '../../internal/ethereum/signTransaction'; +import { + signTransactionEthereumKey, + getValidatedUnsignedTx, +} from '../../internal/ethereum/signTransaction'; interface SignTransactionWithEncryptedEthereumKeyParams { - accessControlConditions: any; + accessControlConditions: any; ciphertext: string; dataToEncryptHash: string; - unsignedTransaction: any; + unsignedTransaction: any; broadcast: boolean; } /** - * + * * Signs a transaction with the Ethers wallet whose private key is decrypted inside the Lit Action. * @returns {Promise} - Returns the transaction hash if broadcast is set as true else returns only the signed transaction. Or returns errors if any. */ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts index 52a9b25fff..9bed10304c 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts @@ -14,7 +14,11 @@ interface GenerateEncryptedSolanaPrivateKeyParams { export async function generateEncryptedSolanaPrivateKey({ accessControlConditions, -}: GenerateEncryptedSolanaPrivateKeyParams): Promise<{ ciphertext: string, dataToEncryptHash: string, publicKey: string }> { +}: GenerateEncryptedSolanaPrivateKeyParams): Promise<{ + ciphertext: string; + dataToEncryptHash: string; + publicKey: string; +}> { const { privateKey, publicKey } = generateSolanaPrivateKey(); return encryptPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts index 5a086a93f1..80a044118f 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts @@ -9,7 +9,7 @@ interface SignMessageWithEncryptedSolanaKeyParams { } /** - * + * * Bundles solana/web3.js package as it's required to sign a message with the Solana wallet which is also decrypted inside the Lit Action. * @param { SignMessageWithEncryptedSolanaKeyParams } params - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key * @returns {Promise} - Returns a message signed by the Solana Wrapped key. Or returns errors if any. diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts index a7f6dfa4e3..20360d4d33 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -1,5 +1,8 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; -import { signTransactionSolanaKey, validateUnsignedTransaction } from '../../internal/solana/signTransaction'; +import { + signTransactionSolanaKey, + validateUnsignedTransaction, +} from '../../internal/solana/signTransaction'; interface SignTransactionWithEncryptedSolanaKeyParams { accessControlConditions: any; // Define a more specific type if possible diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts index 5ba229bb8b..769ef99226 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts @@ -10,6 +10,5 @@ declare global { (async () => litActionHandler(async () => - batchGenerateEncryptedKeys({ actions, accessControlConditions }) ))(); diff --git a/packages/wrapped-keys-lit-actions/tsconfig.json b/packages/wrapped-keys-lit-actions/tsconfig.json index e820883627..413a500716 100644 --- a/packages/wrapped-keys-lit-actions/tsconfig.json +++ b/packages/wrapped-keys-lit-actions/tsconfig.json @@ -12,10 +12,7 @@ "checkJs": false }, "files": [], - "include": [ - "global.d.ts", - "node_modules/ethers/lib/ethers.d.ts", - ], + "include": ["global.d.ts", "node_modules/ethers/lib/ethers.d.ts"], "references": [ { "path": "./tsconfig.lib.json" From 8c8c8c886f770527d4d29b2ecbec91fc7b47005f Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 18 Oct 2024 16:22:49 +0100 Subject: [PATCH 05/15] feat: show the size of the bundle files --- .../esbuild.config.js | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/packages/wrapped-keys-lit-actions/esbuild.config.js b/packages/wrapped-keys-lit-actions/esbuild.config.js index 6803b4250a..c6ff19e500 100644 --- a/packages/wrapped-keys-lit-actions/esbuild.config.js +++ b/packages/wrapped-keys-lit-actions/esbuild.config.js @@ -49,26 +49,40 @@ module.exports = { (async () => { try { - await esbuild.build({ - entryPoints: [ - './src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts', - './src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts', - './src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts', - './src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts', - './src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts', - './src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts', - './src/lib/self-executing-actions/common/exportPrivateKey.ts', - './src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts', - ], - bundle: true, - minify: true, - sourcemap: false, - treeShaking: true, - outdir: './src/generated/', - inject: ['./buffer.shim.js'], - plugins: [wrapIIFEInStringPlugin], - platform: 'browser', - }); + await esbuild + .build({ + entryPoints: [ + './src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts', + './src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts', + './src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts', + './src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts', + './src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts', + './src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts', + './src/lib/self-executing-actions/common/exportPrivateKey.ts', + './src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts', + ], + bundle: true, + minify: true, + sourcemap: false, + treeShaking: true, + outdir: './src/generated/', + inject: ['./buffer.shim.js'], + plugins: [wrapIIFEInStringPlugin], + platform: 'browser', + }) + .then((result) => { + result.outputFiles.forEach((file) => { + const bytes = file.text.length; + const mbInBinary = (bytes / (1024 * 1024)).toFixed(4); + const mbInDecimal = (bytes / 1_000_000).toFixed(4); + + console.log( + `✅ ${file.path + .split('/') + .pop()}\n- ${mbInDecimal} MB (in decimal)\n- ${mbInBinary} MB (in binary)` + ); + }); + }); console.log('✅ Lit actions built successfully'); } catch (e) { console.error('❌ Error building lit actions: ', e); From 985e069341ee95b1a12f552a2562d8d111106199 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 18 Oct 2024 16:59:13 +0100 Subject: [PATCH 06/15] feat: import `ethers` to lit actions BUT making it `external` in esBuild config --- .../wrapped-keys-lit-actions/esbuild.config.js | 1 + packages/wrapped-keys-lit-actions/global.d.ts | 5 +---- packages/wrapped-keys-lit-actions/package.json | 2 +- .../common/getDecryptedKeyToSingleNode.ts | 2 +- .../lib/internal/ethereum/generatePrivateKey.ts | 5 ++++- .../src/lib/internal/ethereum/signMessage.ts | 14 ++++++++++---- .../src/lib/internal/ethereum/signTransaction.ts | 15 +++++++++++---- .../src/lib/internal/solana/signMessage.ts | 14 ++++++++++---- .../src/lib/internal/solana/signTransaction.ts | 11 +++++------ .../common/batchGenerateEncryptedKeys.ts | 8 ++++---- .../common/exportPrivateKey.ts | 2 +- .../generateEncryptedEthereumPrivateKey.ts | 2 +- .../signMessageWithEncryptedEthereumKey.ts | 2 +- .../signTransactionWithEncryptedEthereumKey.ts | 6 ++++-- .../solana/generateEncryptedSolanaPrivateKey.ts | 2 +- .../solana/signMessageWithEncryptedSolanaKey.ts | 2 +- .../signTransactionWithEncryptedSolanaKey.ts | 10 ++++++++-- .../common/batchGenerateEncryptedKeys.ts | 2 +- .../common/exportPrivateKey.ts | 2 +- .../generateEncryptedEthereumPrivateKey.ts | 2 +- .../signMessageWithEncryptedEthereumKey.ts | 2 +- .../signTransactionWithEncryptedEthereumKey.ts | 5 +++-- .../solana/generateEncryptedSolanaPrivateKey.ts | 2 +- .../solana/signMessageWithEncryptedSolanaKey.ts | 2 +- .../signTransactionWithEncryptedSolanaKey.ts | 5 +++-- .../wrapped-keys-lit-actions/tsconfig.lib.json | 14 +++++++++++--- 26 files changed, 88 insertions(+), 51 deletions(-) diff --git a/packages/wrapped-keys-lit-actions/esbuild.config.js b/packages/wrapped-keys-lit-actions/esbuild.config.js index c6ff19e500..35c63eaf42 100644 --- a/packages/wrapped-keys-lit-actions/esbuild.config.js +++ b/packages/wrapped-keys-lit-actions/esbuild.config.js @@ -67,6 +67,7 @@ module.exports = { treeShaking: true, outdir: './src/generated/', inject: ['./buffer.shim.js'], + external: ['ethers'], plugins: [wrapIIFEInStringPlugin], platform: 'browser', }) diff --git a/packages/wrapped-keys-lit-actions/global.d.ts b/packages/wrapped-keys-lit-actions/global.d.ts index c3e396d150..63ab4d5a78 100644 --- a/packages/wrapped-keys-lit-actions/global.d.ts +++ b/packages/wrapped-keys-lit-actions/global.d.ts @@ -1,8 +1,5 @@ -import { ethers as EthersType } from 'ethers'; - // @ts-nocheck - got this directly from the lit-assets repo declare global { - export const ethers: typeof EthersType; export declare namespace Lit { export namespace Actions { @@ -387,4 +384,4 @@ declare global { } } -export {}; +export { }; diff --git a/packages/wrapped-keys-lit-actions/package.json b/packages/wrapped-keys-lit-actions/package.json index 768c8050c3..b7cadd8656 100644 --- a/packages/wrapped-keys-lit-actions/package.json +++ b/packages/wrapped-keys-lit-actions/package.json @@ -29,4 +29,4 @@ "version": "6.10.0", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" -} +} \ No newline at end of file diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts index 93b8728529..09008dec5a 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts @@ -4,7 +4,7 @@ import { AbortError } from '../../abortError'; import { removeSaltFromDecryptedKey } from '../../utils'; interface TryDecryptToSingleNodeParams { - accessControlConditions: any; + accessControlConditions: string ciphertext: string; dataToEncryptHash: string; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts index 299816e039..bfc67309ca 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts @@ -1,4 +1,7 @@ -/* global ethers */ +/** + * The global ethers library (5.7.0) is available on Lit Action (Unbundled) + */ +import { ethers } from 'ethers'; export function generateEthereumPrivateKey() { const wallet = ethers.Wallet.createRandom(); diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts index bc3cd4dcec..d936146544 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts @@ -1,4 +1,7 @@ -/* global ethers */ +/** + * The global ethers library (5.7.0) is available on Lit Action (Unbundled) + */ +import { ethers } from 'ethers'; interface SignMessageParams { privateKey: string; @@ -20,7 +23,11 @@ async function signMessage({ return { signature, walletAddress: wallet.address }; } catch (err: unknown) { - throw new Error(`When signing message - ${(err as Error).message}`); + if (err instanceof Error) { + throw new Error(`When signing message - ${err.message}`); + } else { + throw new Error(`An unexpected error occurred: ${err}`); + } } } @@ -32,8 +39,7 @@ function verifyMessageSignature({ return ethers.utils.verifyMessage(messageToSign, signature); } catch (err: unknown) { throw new Error( - `When validating signed Ethereum message is valid: ${ - (err as Error).message + `When validating signed Ethereum message is valid: ${(err as Error).message }` ); } diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts index 04400815bb..bd9c4ba12a 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts @@ -1,6 +1,12 @@ /* global ethers, Lit */ -interface UnsignedTransaction { +/** + * The global ethers library (5.7.0) is available on Lit Action (Unbundled) + */ +import { ethers } from 'ethers'; + +// This is weird - ethers.UnsignedTransaction is not the same as the one being used here.. +export interface UnsignedTransaction { toAddress: string; chain: string; value: string; @@ -8,6 +14,7 @@ interface UnsignedTransaction { dataHex?: string; gasPrice?: string; gasLimit?: number; + serializedTransaction?: any; } export function getValidatedUnsignedTx( @@ -81,7 +88,7 @@ async function getGasPrice({ provider, }: { userProvidedGasPrice?: string; - provider: any; + provider: ethers.providers.JsonRpcProvider; }) { try { if (userProvidedGasPrice) { @@ -99,7 +106,7 @@ async function getGasLimit({ userProvidedGasLimit, validatedTx, }: { - provider: any; + provider: ethers.providers.JsonRpcProvider; userProvidedGasLimit?: number; validatedTx: any; }) { @@ -132,7 +139,7 @@ async function broadcastTransaction({ provider, signedTx, }: { - provider: any; + provider: ethers.providers.JsonRpcProvider; signedTx: string; }) { try { diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts index 8b4676e571..c098194cc9 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts @@ -2,7 +2,10 @@ import { Keypair } from '@solana/web3.js'; import nacl from 'tweetnacl'; import { Buffer } from 'buffer'; -/* global ethers */ +/** + * The global ethers library (5.7.0) is available on Lit Action (Unbundled) + */ +import { ethers } from 'ethers'; interface SignMessageParams { messageToSign: string; @@ -26,7 +29,11 @@ function signMessage({ messageToSign, solanaKeyPair }: SignMessageParams): { return { signature }; } catch (err: unknown) { - throw new Error(`When signing message - ${(err as Error).message}`); + if (err instanceof Error) { + throw new Error(`When signing message - ${err.message}`); + } else { + throw new Error(`An unexpected error occurred: ${err}`); + } } } @@ -45,8 +52,7 @@ function verifyMessageSignature({ return isValid; } catch (err: unknown) { throw new Error( - `When validating signed Solana message is valid: ${ - (err as Error).message + `When validating signed Solana message is valid: ${(err as Error).message }` ); } diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts index d233502975..500f257803 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts @@ -6,12 +6,11 @@ import { Transaction, } from '@solana/web3.js'; -/* global ethers */ - -interface UnsignedTransaction { - chain: string; - serializedTransaction: string; -} +/** + * The global ethers library (5.7.0) is available on Lit Action (Unbundled) + */ +import { ethers } from 'ethers'; +import { UnsignedTransaction } from '../ethereum/signTransaction'; export function validateUnsignedTransaction( unsignedTransaction: UnsignedTransaction diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts index 02bc37d209..062e686262 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts @@ -19,7 +19,7 @@ async function processEthereumAction({ accessControlConditions, }: { action: Action; - accessControlConditions: any; + accessControlConditions: string }) { const { network, generateKeyParams } = action; const messageToSign = action.signMessageParams?.messageToSign; @@ -57,7 +57,7 @@ async function processSolanaAction({ accessControlConditions, }: { action: Action; - accessControlConditions: any; + accessControlConditions: string }) { const { network, generateKeyParams } = action; @@ -96,7 +96,7 @@ async function processActions({ accessControlConditions, }: { actions: Action[]; - accessControlConditions: any; + accessControlConditions: string }) { return Promise.all( actions.map(async (action, ndx) => { @@ -161,7 +161,7 @@ export async function batchGenerateEncryptedKeys({ accessControlConditions, }: { actions: Action[]; - accessControlConditions: any; + accessControlConditions: string }) { validateParams(actions); diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts index 1965b07ace..ab94e09c35 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts @@ -1,7 +1,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; interface ExportPrivateKeyParams { - accessControlConditions: any; + accessControlConditions: string ciphertext: string; dataToEncryptHash: string; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts index a6b2d23643..7f91c1c03f 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -11,7 +11,7 @@ import { encryptPrivateKey } from '../../internal/common/encryptKey'; import { generateEthereumPrivateKey } from '../../internal/ethereum/generatePrivateKey'; interface GenerateEncryptedEthereumPrivateKeyParams { - accessControlConditions: any; + accessControlConditions: string } export async function generateEncryptedEthereumPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts index cc698edb3a..4d845d1914 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -2,7 +2,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedK import { signMessageEthereumKey } from '../../internal/ethereum/signMessage'; interface SignMessageWithEncryptedEthereumKeyParams { - accessControlConditions: any; + accessControlConditions: string ciphertext: string; dataToEncryptHash: string; messageToSign: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts index 8297ace844..a89b061adb 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -2,13 +2,15 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedK import { signTransactionEthereumKey, getValidatedUnsignedTx, + UnsignedTransaction, } from '../../internal/ethereum/signTransaction'; + interface SignTransactionWithEncryptedEthereumKeyParams { - accessControlConditions: any; + accessControlConditions: string ciphertext: string; dataToEncryptHash: string; - unsignedTransaction: any; + unsignedTransaction: UnsignedTransaction; broadcast: boolean; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts index 9bed10304c..d64bc57c1e 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts @@ -9,7 +9,7 @@ import { generateSolanaPrivateKey } from '../../internal/solana/generatePrivateK * @returns { Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> } - Returns JSON object with ciphertext & dataToEncryptHash which are the result of the encryption. Also returns the publicKey of the newly generated Solana Wrapped Key. */ interface GenerateEncryptedSolanaPrivateKeyParams { - accessControlConditions: any; + accessControlConditions: string } export async function generateEncryptedSolanaPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts index 80a044118f..5dc23ca676 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts @@ -2,7 +2,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedK import { signMessageSolanaKey } from '../../internal/solana/signMessage'; interface SignMessageWithEncryptedSolanaKeyParams { - accessControlConditions: any; + accessControlConditions: string ciphertext: string; dataToEncryptHash: string; messageToSign: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts index 20360d4d33..f921a29313 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -1,14 +1,20 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; +import { UnsignedTransaction } from '../../internal/ethereum/signTransaction'; import { signTransactionSolanaKey, validateUnsignedTransaction, } from '../../internal/solana/signTransaction'; +/** + * The global ethers library (5.7.0) is available on Lit Action (Unbundled) + */ +import { ethers } from 'ethers'; + interface SignTransactionWithEncryptedSolanaKeyParams { - accessControlConditions: any; // Define a more specific type if possible + accessControlConditions: string ciphertext: string; // The encrypted Wrapped Key dataToEncryptHash: string; // The hash of the data to encrypt - unsignedTransaction: any; // Define a more specific type if possible + unsignedTransaction: UnsignedTransaction; broadcast: boolean; // Flag to determine if the transaction should be broadcasted } diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts index 769ef99226..dfb1715efa 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts @@ -5,7 +5,7 @@ import { batchGenerateEncryptedKeys } from '../../raw-action-functions/common/ba declare global { var actions: any[]; - var accessControlConditions: any; + var accessControlConditions: string } (async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts index ad1349c999..b45563c01c 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { exportPrivateKey } from '../../raw-action-functions/common/exportPrivateKey'; declare global { - var accessControlConditions: any; + var accessControlConditions: string var ciphertext: any; var dataToEncryptHash: any; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts index 1472070115..9288657b9c 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedEthereumPrivateKey } from '../../raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey'; declare global { - var accessControlConditions: any; + var accessControlConditions: string } (async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts index 593c00d36f..03220987e4 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey'; declare global { - var accessControlConditions: any; + var accessControlConditions: string var ciphertext: any; var dataToEncryptHash: any; var messageToSign: any; diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts index 1a144d1e8b..ffb2b27e16 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -1,13 +1,14 @@ /* global accessControlConditions, ciphertext, dataToEncryptHash, unsignedTransaction, broadcast */ +import { UnsignedTransaction } from '../../internal/ethereum/signTransaction'; import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey'; declare global { - var accessControlConditions: any; + var accessControlConditions: string var ciphertext: any; var dataToEncryptHash: any; - var unsignedTransaction: any; + var unsignedTransaction: UnsignedTransaction; var broadcast: any; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts index 5c17563104..65f762a5b8 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedSolanaPrivateKey } from '../../raw-action-functions/solana/generateEncryptedSolanaPrivateKey'; declare global { - var accessControlConditions: any; + var accessControlConditions: string } (async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts index 0227d3c097..29abb551af 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signMessageWithEncryptedSolanaKey'; declare global { - var accessControlConditions: any; + var accessControlConditions: string var ciphertext: any; var dataToEncryptHash: any; var messageToSign: any; diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts index ebf36900b8..01c9a48f86 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -1,13 +1,14 @@ /* global accessControlConditions, ciphertext, dataToEncryptHash, unsignedTransaction, broadcast */ +import { UnsignedTransaction } from '../../internal/ethereum/signTransaction'; import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signTransactionWithEncryptedSolanaKey'; declare global { - var accessControlConditions: any; + var accessControlConditions: string var ciphertext: any; var dataToEncryptHash: any; - var unsignedTransaction: any; + var unsignedTransaction: UnsignedTransaction; var broadcast: any; } diff --git a/packages/wrapped-keys-lit-actions/tsconfig.lib.json b/packages/wrapped-keys-lit-actions/tsconfig.lib.json index 8261486edc..e5f895f1e7 100644 --- a/packages/wrapped-keys-lit-actions/tsconfig.lib.json +++ b/packages/wrapped-keys-lit-actions/tsconfig.lib.json @@ -7,6 +7,14 @@ "allowJs": true, "checkJs": false }, - "include": ["**/*.ts"], - "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"] -} + "include": [ + "**/*.ts", + "esbuild.config.js", + "esbuild.config.js" + ], + "exclude": [ + "jest.config.ts", + "**/*.spec.ts", + "**/*.test.ts" + ] +} \ No newline at end of file From a41448eefdbcf04b0e44eb940b7b2d850ec3d566 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 18 Oct 2024 17:49:39 +0100 Subject: [PATCH 07/15] trigger my lost changes From 828a72c8accfe725e0c94cac395e0e93fd0f6e66 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 18 Oct 2024 17:58:46 +0100 Subject: [PATCH 08/15] doc: add comment --- .../src/lib/internal/ethereum/signTransaction.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts index bd9c4ba12a..d6ebe5bb40 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts @@ -6,6 +6,7 @@ import { ethers } from 'ethers'; // This is weird - ethers.UnsignedTransaction is not the same as the one being used here.. +// We should fix this soon, but not a hard blocker export interface UnsignedTransaction { toAddress: string; chain: string; From 4ff536f170b94a89e6664f9dc1c543825916514f Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 18 Oct 2024 18:05:08 +0100 Subject: [PATCH 09/15] fmt --- .../wrapped-keys-lit-actions/esbuild.config.js | 2 +- packages/wrapped-keys-lit-actions/global.d.ts | 3 +-- packages/wrapped-keys-lit-actions/package.json | 2 +- .../internal/common/getDecryptedKeyToSingleNode.ts | 2 +- .../src/lib/internal/ethereum/signMessage.ts | 3 ++- .../src/lib/internal/solana/signMessage.ts | 3 ++- .../common/batchGenerateEncryptedKeys.ts | 8 ++++---- .../common/exportPrivateKey.ts | 2 +- .../generateEncryptedEthereumPrivateKey.ts | 2 +- .../signMessageWithEncryptedEthereumKey.ts | 2 +- .../signTransactionWithEncryptedEthereumKey.ts | 3 +-- .../solana/generateEncryptedSolanaPrivateKey.ts | 2 +- .../solana/signMessageWithEncryptedSolanaKey.ts | 2 +- .../signTransactionWithEncryptedSolanaKey.ts | 2 +- .../common/batchGenerateEncryptedKeys.ts | 2 +- .../common/exportPrivateKey.ts | 2 +- .../generateEncryptedEthereumPrivateKey.ts | 2 +- .../signMessageWithEncryptedEthereumKey.ts | 2 +- .../signTransactionWithEncryptedEthereumKey.ts | 2 +- .../solana/generateEncryptedSolanaPrivateKey.ts | 2 +- .../solana/signMessageWithEncryptedSolanaKey.ts | 2 +- .../signTransactionWithEncryptedSolanaKey.ts | 2 +- .../wrapped-keys-lit-actions/tsconfig.lib.json | 14 +++----------- 23 files changed, 30 insertions(+), 38 deletions(-) diff --git a/packages/wrapped-keys-lit-actions/esbuild.config.js b/packages/wrapped-keys-lit-actions/esbuild.config.js index 35c63eaf42..8d2d772d58 100644 --- a/packages/wrapped-keys-lit-actions/esbuild.config.js +++ b/packages/wrapped-keys-lit-actions/esbuild.config.js @@ -67,7 +67,7 @@ module.exports = { treeShaking: true, outdir: './src/generated/', inject: ['./buffer.shim.js'], - external: ['ethers'], + external: ['ethers'], plugins: [wrapIIFEInStringPlugin], platform: 'browser', }) diff --git a/packages/wrapped-keys-lit-actions/global.d.ts b/packages/wrapped-keys-lit-actions/global.d.ts index 63ab4d5a78..6c8e9d7c4b 100644 --- a/packages/wrapped-keys-lit-actions/global.d.ts +++ b/packages/wrapped-keys-lit-actions/global.d.ts @@ -1,6 +1,5 @@ // @ts-nocheck - got this directly from the lit-assets repo declare global { - export declare namespace Lit { export namespace Actions { /** @@ -384,4 +383,4 @@ declare global { } } -export { }; +export {}; diff --git a/packages/wrapped-keys-lit-actions/package.json b/packages/wrapped-keys-lit-actions/package.json index b7cadd8656..768c8050c3 100644 --- a/packages/wrapped-keys-lit-actions/package.json +++ b/packages/wrapped-keys-lit-actions/package.json @@ -29,4 +29,4 @@ "version": "6.10.0", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" -} \ No newline at end of file +} diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts index 09008dec5a..692a3fc7bb 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts @@ -4,7 +4,7 @@ import { AbortError } from '../../abortError'; import { removeSaltFromDecryptedKey } from '../../utils'; interface TryDecryptToSingleNodeParams { - accessControlConditions: string + accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts index d936146544..1db19d1460 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts @@ -39,7 +39,8 @@ function verifyMessageSignature({ return ethers.utils.verifyMessage(messageToSign, signature); } catch (err: unknown) { throw new Error( - `When validating signed Ethereum message is valid: ${(err as Error).message + `When validating signed Ethereum message is valid: ${ + (err as Error).message }` ); } diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts index c098194cc9..13f5640e12 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts @@ -52,7 +52,8 @@ function verifyMessageSignature({ return isValid; } catch (err: unknown) { throw new Error( - `When validating signed Solana message is valid: ${(err as Error).message + `When validating signed Solana message is valid: ${ + (err as Error).message }` ); } diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts index 062e686262..aa1ed8326b 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts @@ -19,7 +19,7 @@ async function processEthereumAction({ accessControlConditions, }: { action: Action; - accessControlConditions: string + accessControlConditions: string; }) { const { network, generateKeyParams } = action; const messageToSign = action.signMessageParams?.messageToSign; @@ -57,7 +57,7 @@ async function processSolanaAction({ accessControlConditions, }: { action: Action; - accessControlConditions: string + accessControlConditions: string; }) { const { network, generateKeyParams } = action; @@ -96,7 +96,7 @@ async function processActions({ accessControlConditions, }: { actions: Action[]; - accessControlConditions: string + accessControlConditions: string; }) { return Promise.all( actions.map(async (action, ndx) => { @@ -161,7 +161,7 @@ export async function batchGenerateEncryptedKeys({ accessControlConditions, }: { actions: Action[]; - accessControlConditions: string + accessControlConditions: string; }) { validateParams(actions); diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts index ab94e09c35..21d176de41 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts @@ -1,7 +1,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; interface ExportPrivateKeyParams { - accessControlConditions: string + accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts index 7f91c1c03f..b32027ead3 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -11,7 +11,7 @@ import { encryptPrivateKey } from '../../internal/common/encryptKey'; import { generateEthereumPrivateKey } from '../../internal/ethereum/generatePrivateKey'; interface GenerateEncryptedEthereumPrivateKeyParams { - accessControlConditions: string + accessControlConditions: string; } export async function generateEncryptedEthereumPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts index 4d845d1914..360746bd6e 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -2,7 +2,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedK import { signMessageEthereumKey } from '../../internal/ethereum/signMessage'; interface SignMessageWithEncryptedEthereumKeyParams { - accessControlConditions: string + accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; messageToSign: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts index a89b061adb..16f0184d7b 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -5,9 +5,8 @@ import { UnsignedTransaction, } from '../../internal/ethereum/signTransaction'; - interface SignTransactionWithEncryptedEthereumKeyParams { - accessControlConditions: string + accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; unsignedTransaction: UnsignedTransaction; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts index d64bc57c1e..e612091810 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts @@ -9,7 +9,7 @@ import { generateSolanaPrivateKey } from '../../internal/solana/generatePrivateK * @returns { Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> } - Returns JSON object with ciphertext & dataToEncryptHash which are the result of the encryption. Also returns the publicKey of the newly generated Solana Wrapped Key. */ interface GenerateEncryptedSolanaPrivateKeyParams { - accessControlConditions: string + accessControlConditions: string; } export async function generateEncryptedSolanaPrivateKey({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts index 5dc23ca676..7a053f203b 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts @@ -2,7 +2,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedK import { signMessageSolanaKey } from '../../internal/solana/signMessage'; interface SignMessageWithEncryptedSolanaKeyParams { - accessControlConditions: string + accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; messageToSign: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts index f921a29313..05529da4b9 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -11,7 +11,7 @@ import { import { ethers } from 'ethers'; interface SignTransactionWithEncryptedSolanaKeyParams { - accessControlConditions: string + accessControlConditions: string; ciphertext: string; // The encrypted Wrapped Key dataToEncryptHash: string; // The hash of the data to encrypt unsignedTransaction: UnsignedTransaction; diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts index dfb1715efa..111a87baff 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts @@ -5,7 +5,7 @@ import { batchGenerateEncryptedKeys } from '../../raw-action-functions/common/ba declare global { var actions: any[]; - var accessControlConditions: string + var accessControlConditions: string; } (async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts index b45563c01c..042d8c46aa 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { exportPrivateKey } from '../../raw-action-functions/common/exportPrivateKey'; declare global { - var accessControlConditions: string + var accessControlConditions: string; var ciphertext: any; var dataToEncryptHash: any; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts index 9288657b9c..344e0a6e67 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedEthereumPrivateKey } from '../../raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey'; declare global { - var accessControlConditions: string + var accessControlConditions: string; } (async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts index 03220987e4..38a11dec9b 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey'; declare global { - var accessControlConditions: string + var accessControlConditions: string; var ciphertext: any; var dataToEncryptHash: any; var messageToSign: any; diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts index ffb2b27e16..72e28f919d 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -5,7 +5,7 @@ import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey'; declare global { - var accessControlConditions: string + var accessControlConditions: string; var ciphertext: any; var dataToEncryptHash: any; var unsignedTransaction: UnsignedTransaction; diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts index 65f762a5b8..c985b05e38 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedSolanaPrivateKey } from '../../raw-action-functions/solana/generateEncryptedSolanaPrivateKey'; declare global { - var accessControlConditions: string + var accessControlConditions: string; } (async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts index 29abb551af..a7a56d897e 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts @@ -4,7 +4,7 @@ import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signMessageWithEncryptedSolanaKey'; declare global { - var accessControlConditions: string + var accessControlConditions: string; var ciphertext: any; var dataToEncryptHash: any; var messageToSign: any; diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts index 01c9a48f86..e400241dca 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -5,7 +5,7 @@ import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signTransactionWithEncryptedSolanaKey'; declare global { - var accessControlConditions: string + var accessControlConditions: string; var ciphertext: any; var dataToEncryptHash: any; var unsignedTransaction: UnsignedTransaction; diff --git a/packages/wrapped-keys-lit-actions/tsconfig.lib.json b/packages/wrapped-keys-lit-actions/tsconfig.lib.json index e5f895f1e7..c89e6dbca4 100644 --- a/packages/wrapped-keys-lit-actions/tsconfig.lib.json +++ b/packages/wrapped-keys-lit-actions/tsconfig.lib.json @@ -7,14 +7,6 @@ "allowJs": true, "checkJs": false }, - "include": [ - "**/*.ts", - "esbuild.config.js", - "esbuild.config.js" - ], - "exclude": [ - "jest.config.ts", - "**/*.spec.ts", - "**/*.test.ts" - ] -} \ No newline at end of file + "include": ["**/*.ts", "esbuild.config.js", "esbuild.config.js"], + "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"] +} From 809dd4e808089231d8774214f3a8fb14cadc9980 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 18 Oct 2024 19:07:21 +0100 Subject: [PATCH 10/15] Update packages/wrapped-keys-lit-actions/tsconfig.json Signed-off-by: Anson --- packages/wrapped-keys-lit-actions/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wrapped-keys-lit-actions/tsconfig.json b/packages/wrapped-keys-lit-actions/tsconfig.json index 413a500716..8155e40bad 100644 --- a/packages/wrapped-keys-lit-actions/tsconfig.json +++ b/packages/wrapped-keys-lit-actions/tsconfig.json @@ -12,7 +12,7 @@ "checkJs": false }, "files": [], - "include": ["global.d.ts", "node_modules/ethers/lib/ethers.d.ts"], + "include": ["global.d.ts"], "references": [ { "path": "./tsconfig.lib.json" From 55339ecd5aa29e166c2d7875e4b14408dd159543 Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 21 Oct 2024 22:28:37 +0100 Subject: [PATCH 11/15] fix: https://github.com/LIT-Protocol/js-sdk/pull/699#issuecomment-2423837193 --- .../wrapped-keys-lit-actions/esbuild.config.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/wrapped-keys-lit-actions/esbuild.config.js b/packages/wrapped-keys-lit-actions/esbuild.config.js index 8d2d772d58..2f2a5ecccf 100644 --- a/packages/wrapped-keys-lit-actions/esbuild.config.js +++ b/packages/wrapped-keys-lit-actions/esbuild.config.js @@ -25,6 +25,20 @@ const wrapIIFEInStringPlugin = { result.outputFiles.forEach((outputFile) => { let content = outputFile.text; + // IMPORTANT: if minify is disabled, we need to: + // 1. remove var import_ethers = __require("ethers"); + // 2. remove import_ethers. + content = content + .replace(/var import_ethers = __require\("ethers"\);/g, '') + .replace(/import_ethers\./g, ''); + + // IMPORTANT: if minify is enabled, we need to: + // 1. remove var t=o(\"ethers\"); + // 2. replace t.ethers to ethers + content = content + .replace(/var\s+\w+=\w+\("ethers"\);/g, '') + .replace(/[a-zA-Z]\.ethers/g, 'ethers'); + // Use JSON.stringify to safely encode the content const wrappedContent = `/** * DO NOT EDIT THIS FILE. IT IS GENERATED ON BUILD. RUN \`yarn generate-lit-actions\` IN THE ROOT DIRECTORY TO UPDATE THIS FILE. From 52fffc916bc581aa937125401536ad19a9dbc35c Mon Sep 17 00:00:00 2001 From: Daryl Collins Date: Tue, 22 Oct 2024 21:57:21 +0100 Subject: [PATCH 12/15] fix: Use global.d.ts to define `ethers` existence globally - Ran `eslint --fix` too. - Removed now-unnecessary manual string replace logic from esbuild - Update type references to use ethers types instead of relying on a reference to an actual imported instance of the classes --- .../esbuild.config.js | 15 ------------- packages/wrapped-keys-lit-actions/global.d.ts | 22 +++++++++++++------ .../internal/ethereum/generatePrivateKey.ts | 5 ----- .../src/lib/internal/ethereum/signMessage.ts | 5 ----- .../lib/internal/ethereum/signTransaction.ts | 13 +++-------- .../wrapped-keys-lit-actions/tsconfig.json | 5 +++-- 6 files changed, 21 insertions(+), 44 deletions(-) diff --git a/packages/wrapped-keys-lit-actions/esbuild.config.js b/packages/wrapped-keys-lit-actions/esbuild.config.js index 2f2a5ecccf..311ebc338e 100644 --- a/packages/wrapped-keys-lit-actions/esbuild.config.js +++ b/packages/wrapped-keys-lit-actions/esbuild.config.js @@ -24,21 +24,6 @@ const wrapIIFEInStringPlugin = { result.outputFiles.forEach((outputFile) => { let content = outputFile.text; - - // IMPORTANT: if minify is disabled, we need to: - // 1. remove var import_ethers = __require("ethers"); - // 2. remove import_ethers. - content = content - .replace(/var import_ethers = __require\("ethers"\);/g, '') - .replace(/import_ethers\./g, ''); - - // IMPORTANT: if minify is enabled, we need to: - // 1. remove var t=o(\"ethers\"); - // 2. replace t.ethers to ethers - content = content - .replace(/var\s+\w+=\w+\("ethers"\);/g, '') - .replace(/[a-zA-Z]\.ethers/g, 'ethers'); - // Use JSON.stringify to safely encode the content const wrappedContent = `/** * DO NOT EDIT THIS FILE. IT IS GENERATED ON BUILD. RUN \`yarn generate-lit-actions\` IN THE ROOT DIRECTORY TO UPDATE THIS FILE. diff --git a/packages/wrapped-keys-lit-actions/global.d.ts b/packages/wrapped-keys-lit-actions/global.d.ts index 6c8e9d7c4b..0ffd590d92 100644 --- a/packages/wrapped-keys-lit-actions/global.d.ts +++ b/packages/wrapped-keys-lit-actions/global.d.ts @@ -1,3 +1,6 @@ +import { ethers as Ethers } from 'ethers'; + +// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck - got this directly from the lit-assets repo declare global { export declare namespace Lit { @@ -61,7 +64,7 @@ declare global { tokenId, }: { tokenId: string; - }): Promise>; + }): Promise; /** * Get the full list of addresses that are permitted to sign using a given PKP tokenId * @function getPermittedAddresses @@ -73,7 +76,7 @@ declare global { tokenId, }: { tokenId: string; - }): Promise>; + }): Promise; /** * Get the full list of auth methods that are permitted to sign using a given PKP tokenId * @function getPermittedAuthMethods @@ -85,7 +88,7 @@ declare global { tokenId, }: { tokenId: string; - }): Promise>; + }): Promise; /** * Get the permitted auth method scopes for a given PKP tokenId and auth method type + id * @function getPermittedAuthMethodScopes @@ -106,7 +109,7 @@ declare global { authMethodType: string; userId: Uint8Array; maxScopeId: number; - }): Promise>; + }): Promise; /** * Converts a PKP public key to a PKP token ID by hashing it with keccak256 * @function pubkeyToTokenId @@ -184,7 +187,7 @@ declare global { authSig, chain, }: { - conditions: Array; + conditions: any[]; authSig: any; chain: string; }): Promise; @@ -346,7 +349,7 @@ declare global { * Array of action IPFS IDs. * @type {Array<`Qm${string}` | string>} */ - const actionIpfsIds: Array<`Qm${string}` | string>; + const actionIpfsIds: (`Qm${string}` | string)[]; /** * Array of authentication method contexts. @@ -372,7 +375,7 @@ declare global { * Array of resources. * @type {Array} */ - const resources: Array; + const resources: any[]; /** * Custom authentication resource. @@ -381,6 +384,11 @@ declare global { const customAuthResource: string | `"\\(true,${string})\\"`; } } + // eslint-disable-next-line import/export + export type ethers = Ethers; + export const ethers: typeof Ethers; } +global.ethers = Ethers; + export {}; diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts index bfc67309ca..57c39e2cd6 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/generatePrivateKey.ts @@ -1,8 +1,3 @@ -/** - * The global ethers library (5.7.0) is available on Lit Action (Unbundled) - */ -import { ethers } from 'ethers'; - export function generateEthereumPrivateKey() { const wallet = ethers.Wallet.createRandom(); diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts index 1db19d1460..d67f8994be 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signMessage.ts @@ -1,8 +1,3 @@ -/** - * The global ethers library (5.7.0) is available on Lit Action (Unbundled) - */ -import { ethers } from 'ethers'; - interface SignMessageParams { privateKey: string; messageToSign: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts index d6ebe5bb40..1ab7180487 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts @@ -1,10 +1,3 @@ -/* global ethers, Lit */ - -/** - * The global ethers library (5.7.0) is available on Lit Action (Unbundled) - */ -import { ethers } from 'ethers'; - // This is weird - ethers.UnsignedTransaction is not the same as the one being used here.. // We should fix this soon, but not a hard blocker export interface UnsignedTransaction { @@ -89,7 +82,7 @@ async function getGasPrice({ provider, }: { userProvidedGasPrice?: string; - provider: ethers.providers.JsonRpcProvider; + provider: ethers['providers']['JsonRpcProvider']; }) { try { if (userProvidedGasPrice) { @@ -107,7 +100,7 @@ async function getGasLimit({ userProvidedGasLimit, validatedTx, }: { - provider: ethers.providers.JsonRpcProvider; + provider: ethers['providers']['JsonRpcProvider']; userProvidedGasLimit?: number; validatedTx: any; }) { @@ -140,7 +133,7 @@ async function broadcastTransaction({ provider, signedTx, }: { - provider: ethers.providers.JsonRpcProvider; + provider: ethers['providers']['JsonRpcProvider']; signedTx: string; }) { try { diff --git a/packages/wrapped-keys-lit-actions/tsconfig.json b/packages/wrapped-keys-lit-actions/tsconfig.json index 8155e40bad..d3187ebeee 100644 --- a/packages/wrapped-keys-lit-actions/tsconfig.json +++ b/packages/wrapped-keys-lit-actions/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "module": "commonjs", + "module": "system", "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true, @@ -9,7 +9,8 @@ "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "allowJs": true, - "checkJs": false + "checkJs": false, + "resolveJsonModule": false }, "files": [], "include": ["global.d.ts"], From 15523a8dd1722ab017b2e6a40986e8ad7589c6e6 Mon Sep 17 00:00:00 2001 From: Daryl Collins Date: Tue, 22 Oct 2024 22:04:20 +0100 Subject: [PATCH 13/15] fix: Use local declarations instead of `declare global {}` in module scopes - Also removed now-unnecessary eslint hints for `/* global Lit */` etc. - Fixes mismatched type conflict with same name 'UnsignedTransaction'; ethereum's type is different than Solana. --- local-tests/test.ts | 32 +++++++++---------- .../lib/internal/ethereum/signTransaction.ts | 1 - .../src/lib/internal/solana/signMessage.ts | 8 ++--- .../lib/internal/solana/signTransaction.ts | 11 ++++--- .../src/lib/litActionHandler.ts | 2 -- .../common/batchGenerateEncryptedKeys.ts | 15 ++++----- .../common/exportPrivateKey.ts | 10 +++--- .../generateEncryptedEthereumPrivateKey.ts | 2 +- .../signMessageWithEncryptedEthereumKey.ts | 2 +- ...signTransactionWithEncryptedEthereumKey.ts | 2 +- .../generateEncryptedSolanaPrivateKey.ts | 2 +- .../signMessageWithEncryptedSolanaKey.ts | 2 +- .../signTransactionWithEncryptedSolanaKey.ts | 8 ++--- .../common/batchGenerateEncryptedKeys.ts | 9 +++--- .../common/exportPrivateKey.ts | 13 ++++---- .../generateEncryptedEthereumPrivateKey.ts | 9 +++--- .../signMessageWithEncryptedEthereumKey.ts | 15 ++++----- ...signTransactionWithEncryptedEthereumKey.ts | 20 ++++++------ .../generateEncryptedSolanaPrivateKey.ts | 9 +++--- .../signMessageWithEncryptedSolanaKey.ts | 15 ++++----- .../signTransactionWithEncryptedSolanaKey.ts | 20 ++++++------ 21 files changed, 95 insertions(+), 112 deletions(-) diff --git a/local-tests/test.ts b/local-tests/test.ts index c66998d30b..2f65903e6b 100644 --- a/local-tests/test.ts +++ b/local-tests/test.ts @@ -293,22 +293,22 @@ setLitActionsCodeToLocal(); tests: { // testExample, // testBundleSpeed, - ...eoaSessionSigsTests, - ...pkpSessionSigsTests, - ...litActionSessionSigsTests, - ...litActionIpfsIdSessionSigsTests, - ...capacityDelegationTests, - ...bareAuthSigTests, - - ...pkpEthersTest.eoaSessionSigs, - ...pkpEthersTest.pkpSessionSigs, - ...pkpEthersTest.litActionSessionSigs, - - ...litActionCombiningTests.broadcastAndCombine, - ...litActionCombiningTests.decryptAndCombine, - ...litActionCombiningTests.ecdsaSignAndCombine, - - ...relayerTests, + // ...eoaSessionSigsTests, + // ...pkpSessionSigsTests, + // ...litActionSessionSigsTests, + // ...litActionIpfsIdSessionSigsTests, + // ...capacityDelegationTests, + // ...bareAuthSigTests, + // + // ...pkpEthersTest.eoaSessionSigs, + // ...pkpEthersTest.pkpSessionSigs, + // ...pkpEthersTest.litActionSessionSigs, + // + // ...litActionCombiningTests.broadcastAndCombine, + // ...litActionCombiningTests.decryptAndCombine, + // ...litActionCombiningTests.ecdsaSignAndCombine, + // + // ...relayerTests, ...wrappedKeysTests, }, devEnv, diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts index 1ab7180487..9d73cd68e9 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts @@ -8,7 +8,6 @@ export interface UnsignedTransaction { dataHex?: string; gasPrice?: string; gasLimit?: number; - serializedTransaction?: any; } export function getValidatedUnsignedTx( diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts index 13f5640e12..ffc9cdc8c5 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signMessage.ts @@ -1,11 +1,7 @@ -import { Keypair } from '@solana/web3.js'; -import nacl from 'tweetnacl'; import { Buffer } from 'buffer'; -/** - * The global ethers library (5.7.0) is available on Lit Action (Unbundled) - */ -import { ethers } from 'ethers'; +import { Keypair } from '@solana/web3.js'; +import nacl from 'tweetnacl'; interface SignMessageParams { messageToSign: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts index 500f257803..1586f5ea0a 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/solana/signTransaction.ts @@ -6,11 +6,11 @@ import { Transaction, } from '@solana/web3.js'; -/** - * The global ethers library (5.7.0) is available on Lit Action (Unbundled) - */ -import { ethers } from 'ethers'; -import { UnsignedTransaction } from '../ethereum/signTransaction'; +// Solana transactions are pre-serialized; much simpler API than ethereum transactions +export interface UnsignedTransaction { + chain: string; + serializedTransaction: string; +} export function validateUnsignedTransaction( unsignedTransaction: UnsignedTransaction @@ -88,6 +88,7 @@ export async function signTransactionSolanaKey({ if (!broadcast) { return signature; } + // Ensure the chain is a valid Cluster type const chain: Cluster = unsignedTransaction.chain as Cluster; diff --git a/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts b/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts index 2e90ce1859..29c0caae70 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts @@ -1,5 +1,3 @@ -/* global Lit */ - import { AbortError } from './abortError'; export async function litActionHandler(actionFunc: () => Promise) { diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts index aa1ed8326b..26bbf16ae2 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/batchGenerateEncryptedKeys.ts @@ -14,6 +14,11 @@ interface Action { }; } +export interface BatchGenerateEncryptedKeysParams { + actions: Action[]; + accessControlConditions: string; +} + async function processEthereumAction({ action, accessControlConditions, @@ -94,10 +99,7 @@ async function processSolanaAction({ async function processActions({ actions, accessControlConditions, -}: { - actions: Action[]; - accessControlConditions: string; -}) { +}: BatchGenerateEncryptedKeysParams) { return Promise.all( actions.map(async (action, ndx) => { const { network } = action; @@ -159,10 +161,7 @@ function validateParams(actions: Action[]) { export async function batchGenerateEncryptedKeys({ actions, accessControlConditions, -}: { - actions: Action[]; - accessControlConditions: string; -}) { +}: BatchGenerateEncryptedKeysParams) { validateParams(actions); return processActions({ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts index 21d176de41..68812a9221 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/exportPrivateKey.ts @@ -1,6 +1,6 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; -interface ExportPrivateKeyParams { +export interface ExportPrivateKeyParams { accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; @@ -10,10 +10,10 @@ interface ExportPrivateKeyParams { * * Exports the private key after decrypting and removing the salt from it. * - * @param {string} pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key - * @param {string} ciphertext - For the encrypted Wrapped Key - * @param {string} dataToEncryptHash - For the encrypted Wrapped Key - * @param {any} accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key + * @param {object} params + * @param {string} params.ciphertext - For the encrypted Wrapped Key + * @param {string} params.dataToEncryptHash - For the encrypted Wrapped Key + * @param {string} params.accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key * * @returns { Promise } - Returns a decrypted private key. */ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts index b32027ead3..10ef9464e3 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -10,7 +10,7 @@ import { encryptPrivateKey } from '../../internal/common/encryptKey'; import { generateEthereumPrivateKey } from '../../internal/ethereum/generatePrivateKey'; -interface GenerateEncryptedEthereumPrivateKeyParams { +export interface GenerateEncryptedEthereumPrivateKeyParams { accessControlConditions: string; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts index 360746bd6e..bee7efcbb2 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -1,7 +1,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; import { signMessageEthereumKey } from '../../internal/ethereum/signMessage'; -interface SignMessageWithEncryptedEthereumKeyParams { +export interface SignMessageWithEncryptedEthereumKeyParams { accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts index 16f0184d7b..77b72be333 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -5,7 +5,7 @@ import { UnsignedTransaction, } from '../../internal/ethereum/signTransaction'; -interface SignTransactionWithEncryptedEthereumKeyParams { +export interface SignTransactionWithEncryptedEthereumKeyParams { accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts index e612091810..1720b9920a 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts @@ -8,7 +8,7 @@ import { generateSolanaPrivateKey } from '../../internal/solana/generatePrivateK * * @returns { Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> } - Returns JSON object with ciphertext & dataToEncryptHash which are the result of the encryption. Also returns the publicKey of the newly generated Solana Wrapped Key. */ -interface GenerateEncryptedSolanaPrivateKeyParams { +export interface GenerateEncryptedSolanaPrivateKeyParams { accessControlConditions: string; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts index 7a053f203b..eb72458b5f 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signMessageWithEncryptedSolanaKey.ts @@ -1,7 +1,7 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; import { signMessageSolanaKey } from '../../internal/solana/signMessage'; -interface SignMessageWithEncryptedSolanaKeyParams { +export interface SignMessageWithEncryptedSolanaKeyParams { accessControlConditions: string; ciphertext: string; dataToEncryptHash: string; diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts index 05529da4b9..b3efdcb4ed 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -1,16 +1,12 @@ import { getDecryptedKeyToSingleNode } from '../../internal/common/getDecryptedKeyToSingleNode'; -import { UnsignedTransaction } from '../../internal/ethereum/signTransaction'; import { signTransactionSolanaKey, validateUnsignedTransaction, } from '../../internal/solana/signTransaction'; -/** - * The global ethers library (5.7.0) is available on Lit Action (Unbundled) - */ -import { ethers } from 'ethers'; +import type { UnsignedTransaction } from '../../internal/solana/signTransaction'; -interface SignTransactionWithEncryptedSolanaKeyParams { +export interface SignTransactionWithEncryptedSolanaKeyParams { accessControlConditions: string; ciphertext: string; // The encrypted Wrapped Key dataToEncryptHash: string; // The hash of the data to encrypt diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts index 111a87baff..ee1fea8401 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/batchGenerateEncryptedKeys.ts @@ -1,12 +1,11 @@ import { litActionHandler } from '../../litActionHandler'; import { batchGenerateEncryptedKeys } from '../../raw-action-functions/common/batchGenerateEncryptedKeys'; -/* global actions accessControlConditions */ +import type { BatchGenerateEncryptedKeysParams } from '../../raw-action-functions/common/batchGenerateEncryptedKeys'; -declare global { - var actions: any[]; - var accessControlConditions: string; -} +// Using local declarations to avoid _every file_ thinking these are always in scope +declare const actions: BatchGenerateEncryptedKeysParams['actions']; +declare const accessControlConditions: BatchGenerateEncryptedKeysParams['accessControlConditions']; (async () => litActionHandler(async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts index 042d8c46aa..e7a6a7b4df 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/common/exportPrivateKey.ts @@ -1,13 +1,12 @@ -/* global accessControlConditions, ciphertext, dataToEncryptHash */ - import { litActionHandler } from '../../litActionHandler'; import { exportPrivateKey } from '../../raw-action-functions/common/exportPrivateKey'; -declare global { - var accessControlConditions: string; - var ciphertext: any; - var dataToEncryptHash: any; -} +import type { ExportPrivateKeyParams } from '../../raw-action-functions/common/exportPrivateKey'; + +// Using local declarations to avoid _every file_ thinking these are always in scope +declare const ciphertext: ExportPrivateKeyParams['ciphertext']; +declare const dataToEncryptHash: ExportPrivateKeyParams['dataToEncryptHash']; +declare const accessControlConditions: ExportPrivateKeyParams['accessControlConditions']; (async () => litActionHandler(async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts index 344e0a6e67..efb043bb35 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -1,11 +1,10 @@ -/* global accessControlConditions */ - import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedEthereumPrivateKey } from '../../raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey'; -declare global { - var accessControlConditions: string; -} +import type { GenerateEncryptedEthereumPrivateKeyParams } from '../../raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey'; + +// Using local declarations to avoid _every file_ thinking these are always in scope +declare const accessControlConditions: GenerateEncryptedEthereumPrivateKeyParams['accessControlConditions']; (async () => litActionHandler(async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts index 38a11dec9b..7dd6e18c42 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signMessageWithEncryptedEthereumKey.ts @@ -1,14 +1,13 @@ -/* global accessControlConditions, ciphertext, dataToEncryptHash, messageToSign */ - import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey'; -declare global { - var accessControlConditions: string; - var ciphertext: any; - var dataToEncryptHash: any; - var messageToSign: any; -} +import type { SignMessageWithEncryptedEthereumKeyParams } from '../../raw-action-functions/ethereum/signMessageWithEncryptedEthereumKey'; + +// Using local declarations to avoid _every file_ thinking these are always in scope +declare const accessControlConditions: SignMessageWithEncryptedEthereumKeyParams['accessControlConditions']; +declare const ciphertext: SignMessageWithEncryptedEthereumKeyParams['ciphertext']; +declare const dataToEncryptHash: SignMessageWithEncryptedEthereumKeyParams['dataToEncryptHash']; +declare const messageToSign: SignMessageWithEncryptedEthereumKeyParams['messageToSign']; (async () => litActionHandler(async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts index 72e28f919d..36bb3996b4 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/ethereum/signTransactionWithEncryptedEthereumKey.ts @@ -1,16 +1,16 @@ -/* global accessControlConditions, ciphertext, dataToEncryptHash, unsignedTransaction, broadcast */ - -import { UnsignedTransaction } from '../../internal/ethereum/signTransaction'; import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedEthereumKey } from '../../raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey'; -declare global { - var accessControlConditions: string; - var ciphertext: any; - var dataToEncryptHash: any; - var unsignedTransaction: UnsignedTransaction; - var broadcast: any; -} +import type { SignTransactionWithEncryptedEthereumKeyParams } from '../../raw-action-functions/ethereum/signTransactionWithEncryptedEthereumKey'; + +// Using local declarations to avoid _every file_ thinking these are always in scope +// Ugly, but using `declare global` causes conflicts with `solana`'s `unsignedTransaction` which is different +// as well as making all of our functions think all other functions args are valid globals in every file in the package +declare const accessControlConditions: SignTransactionWithEncryptedEthereumKeyParams['accessControlConditions']; +declare const ciphertext: SignTransactionWithEncryptedEthereumKeyParams['ciphertext']; +declare const dataToEncryptHash: SignTransactionWithEncryptedEthereumKeyParams['dataToEncryptHash']; +declare const unsignedTransaction: SignTransactionWithEncryptedEthereumKeyParams['unsignedTransaction']; +declare const broadcast: SignTransactionWithEncryptedEthereumKeyParams['broadcast']; (async () => litActionHandler(async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts index c985b05e38..ff073cc1d4 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/generateEncryptedSolanaPrivateKey.ts @@ -1,11 +1,10 @@ -/* global accessControlConditions */ - import { litActionHandler } from '../../litActionHandler'; import { generateEncryptedSolanaPrivateKey } from '../../raw-action-functions/solana/generateEncryptedSolanaPrivateKey'; -declare global { - var accessControlConditions: string; -} +import type { GenerateEncryptedSolanaPrivateKeyParams } from '../../raw-action-functions/solana/generateEncryptedSolanaPrivateKey'; + +// Using local declarations to avoid _every file_ thinking these are always in scope +declare const accessControlConditions: GenerateEncryptedSolanaPrivateKeyParams['accessControlConditions']; (async () => litActionHandler(async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts index a7a56d897e..070eeaaeb0 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signMessageWithEncryptedSolanaKey.ts @@ -1,14 +1,13 @@ -/* global accessControlConditions, ciphertext, dataToEncryptHash, messageToSign */ - import { litActionHandler } from '../../litActionHandler'; import { signMessageWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signMessageWithEncryptedSolanaKey'; -declare global { - var accessControlConditions: string; - var ciphertext: any; - var dataToEncryptHash: any; - var messageToSign: any; -} +import type { SignMessageWithEncryptedSolanaKeyParams } from '../../raw-action-functions/solana/signMessageWithEncryptedSolanaKey'; + +// Using local declarations to avoid _every file_ thinking these are always in scope +declare const accessControlConditions: SignMessageWithEncryptedSolanaKeyParams['accessControlConditions']; +declare const ciphertext: SignMessageWithEncryptedSolanaKeyParams['ciphertext']; +declare const dataToEncryptHash: SignMessageWithEncryptedSolanaKeyParams['dataToEncryptHash']; +declare const messageToSign: SignMessageWithEncryptedSolanaKeyParams['messageToSign']; (async () => litActionHandler(async () => diff --git a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts index e400241dca..164c8e1172 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/self-executing-actions/solana/signTransactionWithEncryptedSolanaKey.ts @@ -1,16 +1,16 @@ -/* global accessControlConditions, ciphertext, dataToEncryptHash, unsignedTransaction, broadcast */ - -import { UnsignedTransaction } from '../../internal/ethereum/signTransaction'; import { litActionHandler } from '../../litActionHandler'; import { signTransactionWithEncryptedSolanaKey } from '../../raw-action-functions/solana/signTransactionWithEncryptedSolanaKey'; -declare global { - var accessControlConditions: string; - var ciphertext: any; - var dataToEncryptHash: any; - var unsignedTransaction: UnsignedTransaction; - var broadcast: any; -} +import type { SignTransactionWithEncryptedSolanaKeyParams } from '../../raw-action-functions/solana/signTransactionWithEncryptedSolanaKey'; + +// Using local declarations to avoid _every file_ thinking these are always in scope +// Ugly, but using `declare global` causes conflicts with `ethereum`'s `unsignedTransaction` which is different +// as well as making all of our functions think all other functions args are valid globals in every file in the package +declare const accessControlConditions: SignTransactionWithEncryptedSolanaKeyParams['accessControlConditions']; +declare const ciphertext: SignTransactionWithEncryptedSolanaKeyParams['ciphertext']; +declare const dataToEncryptHash: SignTransactionWithEncryptedSolanaKeyParams['dataToEncryptHash']; +declare const unsignedTransaction: SignTransactionWithEncryptedSolanaKeyParams['unsignedTransaction']; +declare const broadcast: SignTransactionWithEncryptedSolanaKeyParams['broadcast']; (async () => litActionHandler(async () => From 45e19d7b513bb3e627af2324a041111acfc7d7f8 Mon Sep 17 00:00:00 2001 From: Daryl Collins Date: Tue, 22 Oct 2024 22:13:50 +0100 Subject: [PATCH 14/15] types: Replace `any` types with more explicit typing --- .../src/lib/abortError.ts | 2 +- .../common/getDecryptedKeyToSingleNode.ts | 6 ++++-- .../lib/internal/ethereum/signTransaction.ts | 21 ++++++++++++++----- .../src/lib/litActionHandler.ts | 6 +++--- .../generateEncryptedEthereumPrivateKey.ts | 2 +- .../generateEncryptedSolanaPrivateKey.ts | 2 +- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/wrapped-keys-lit-actions/src/lib/abortError.ts b/packages/wrapped-keys-lit-actions/src/lib/abortError.ts index eaaae31234..9755ab0add 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/abortError.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/abortError.ts @@ -2,7 +2,7 @@ export class AbortError extends Error { override name = 'AbortError'; } -export const rethrowIfAbortError = (err: any) => { +export const rethrowIfAbortError = (err: unknown) => { if (err instanceof AbortError) { throw err; } diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts index 692a3fc7bb..677152067c 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/common/getDecryptedKeyToSingleNode.ts @@ -23,8 +23,10 @@ async function tryDecryptToSingleNode({ chain: 'ethereum', authSig: null, }); - } catch (err: any) { - throw new Error(`When decrypting key to a single node - ${err.message}`); + } catch (err: unknown) { + throw new Error( + `When decrypting key to a single node - ${(err as Error).message}` + ); } } diff --git a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts index 9d73cd68e9..3c007b58d8 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/internal/ethereum/signTransaction.ts @@ -10,9 +10,20 @@ export interface UnsignedTransaction { gasLimit?: number; } +export interface ValidatedTransaction { + to: string; + value: string; + chainId: number; + data?: string; + from?: string; + nonce?: string; + gasPrice?: string; + gasLimit?: number; +} + export function getValidatedUnsignedTx( unsignedTransaction: UnsignedTransaction -) { +): ValidatedTransaction { try { if (!unsignedTransaction.toAddress) { throw new Error('Missing required field: toAddress'); @@ -101,7 +112,7 @@ async function getGasLimit({ }: { provider: ethers['providers']['JsonRpcProvider']; userProvidedGasLimit?: number; - validatedTx: any; + validatedTx: ValidatedTransaction; }) { if (userProvidedGasLimit) { return userProvidedGasLimit; @@ -118,8 +129,8 @@ async function signTransaction({ validatedTx, wallet, }: { - validatedTx: any; - wallet: any; + validatedTx: ValidatedTransaction; + wallet: ethers['wallet']; }) { try { return await wallet.signTransaction(validatedTx); @@ -150,7 +161,7 @@ export async function signTransactionEthereumKey({ }: { broadcast: boolean; privateKey: string; - validatedTx: any; + validatedTx: ValidatedTransaction; unsignedTransaction: UnsignedTransaction; }) { const wallet = new ethers.Wallet(privateKey); diff --git a/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts b/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts index 29c0caae70..53003cecca 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/litActionHandler.ts @@ -1,6 +1,6 @@ import { AbortError } from './abortError'; -export async function litActionHandler(actionFunc: () => Promise) { +export async function litActionHandler(actionFunc: () => Promise) { try { const litActionResult = await actionFunc(); // Don't re-stringify a string; we don't want to double-escape it @@ -10,7 +10,7 @@ export async function litActionHandler(actionFunc: () => Promise) { : JSON.stringify(litActionResult); Lit.Actions.setResponse({ response }); - } catch (err: any) { + } catch (err: unknown) { // 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. @@ -18,6 +18,6 @@ export async function litActionHandler(actionFunc: () => Promise) { return; } - Lit.Actions.setResponse({ response: `Error: ${err.message}` }); + Lit.Actions.setResponse({ response: `Error: ${(err as Error).message}` }); } } diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts index 10ef9464e3..812d6eb0e6 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/ethereum/generateEncryptedEthereumPrivateKey.ts @@ -3,7 +3,7 @@ * Generates a random Ethers private key and only allows the provided PKP to decrypt it * * @param {string} pkpAddress - The Eth address of the PKP which is associated with the Wrapped Key - * @param {any} accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key + * @param {string} 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. */ diff --git a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts index 1720b9920a..df6db24b1b 100644 --- a/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts +++ b/packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/solana/generateEncryptedSolanaPrivateKey.ts @@ -4,7 +4,7 @@ import { generateSolanaPrivateKey } from '../../internal/solana/generatePrivateK /** * Bundles solana/web3.js package as it's required to generate a random Solana key and only allows the provided PKP to decrypt it * - * @param {any} accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key + * @param {string} accessControlConditions - The access control condition that allows only the pkpAddress to decrypt the Wrapped Key * * @returns { Promise<{ciphertext: string, dataToEncryptHash: string, publicKey: string}> } - Returns JSON object with ciphertext & dataToEncryptHash which are the result of the encryption. Also returns the publicKey of the newly generated Solana Wrapped Key. */ From 73c25e2b82d9b915a2e4c932437d71414bf07c04 Mon Sep 17 00:00:00 2001 From: Daryl Collins Date: Tue, 22 Oct 2024 23:29:55 +0100 Subject: [PATCH 15/15] chore: Remove accidental comment --- local-tests/test.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/local-tests/test.ts b/local-tests/test.ts index 2f65903e6b..c66998d30b 100644 --- a/local-tests/test.ts +++ b/local-tests/test.ts @@ -293,22 +293,22 @@ setLitActionsCodeToLocal(); tests: { // testExample, // testBundleSpeed, - // ...eoaSessionSigsTests, - // ...pkpSessionSigsTests, - // ...litActionSessionSigsTests, - // ...litActionIpfsIdSessionSigsTests, - // ...capacityDelegationTests, - // ...bareAuthSigTests, - // - // ...pkpEthersTest.eoaSessionSigs, - // ...pkpEthersTest.pkpSessionSigs, - // ...pkpEthersTest.litActionSessionSigs, - // - // ...litActionCombiningTests.broadcastAndCombine, - // ...litActionCombiningTests.decryptAndCombine, - // ...litActionCombiningTests.ecdsaSignAndCombine, - // - // ...relayerTests, + ...eoaSessionSigsTests, + ...pkpSessionSigsTests, + ...litActionSessionSigsTests, + ...litActionIpfsIdSessionSigsTests, + ...capacityDelegationTests, + ...bareAuthSigTests, + + ...pkpEthersTest.eoaSessionSigs, + ...pkpEthersTest.pkpSessionSigs, + ...pkpEthersTest.litActionSessionSigs, + + ...litActionCombiningTests.broadcastAndCombine, + ...litActionCombiningTests.decryptAndCombine, + ...litActionCombiningTests.ecdsaSignAndCombine, + + ...relayerTests, ...wrappedKeysTests, }, devEnv,