Skip to content

Fetch gasPrice and estimate gas in Wrapped Keys LA if not provided #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions local-tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ import { testExecuteJsBroadcastAndCollect } from './tests/testExecuteJsBroadcast
import { testEthereumSignMessageGeneratedKey } from './tests/testEthereumSignMessageGeneratedKey';
import { testEthereumBroadcastTransactionGeneratedKey } from './tests/testEthereumBroadcastTransactionGeneratedKey';
import { testEthereumSignMessageWrappedKey } from './tests/testEthereumSignMessageWrappedKey';
import { testFailEthereumSignTransactionWrappedKeyInvalidDecryption } from './tests/testFailEthereumSignTransactionWrappedKeyInvalidDecryption';
// import { testFailWrappedKeySigningTimeout } from './tests/testFailWrappedKeySigningTimeout';
import { testEthereumSignTransactionWrappedKey } from './tests/testEthereumSignTransactionWrappedKey';
import { testFailEthereumSignTransactionWrappedKeyWithInvalidParam } from './tests/testFailEthereumSignTransactionWrappedKeyWithInvalidParam';
import { testFailEthereumSignTransactionWrappedKeyWithMissingParam } from './tests/testFailEthereumSignTransactionWrappedKeyWithMissingParam';
import { testEthereumBroadcastTransactionWrappedKey } from './tests/testEthereumBroadcastTransactionWrappedKey';
import { testEthereumBroadcastWrappedKeyWithFetchGasParams } from './tests/testEthereumBroadcastWrappedKeyWithFetchGasParams';
import { testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds } from './tests/testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds';
import { testImportWrappedKey } from './tests/testImportWrappedKey';
import { testGenerateWrappedKey } from './tests/testGenerateWrappedKey';
Expand All @@ -110,6 +112,7 @@ import { testExportWrappedKey } from './tests/testExportWrappedKey';
await devEnv.init();

const wrappedKeysTests = {
testFailEthereumSignTransactionWrappedKeyInvalidDecryption,
testEthereumSignMessageGeneratedKey,
testEthereumBroadcastTransactionGeneratedKey,
// testRemoveDecryptFailingOutsideLitAction,
Expand All @@ -118,6 +121,7 @@ import { testExportWrappedKey } from './tests/testExportWrappedKey';
testFailEthereumSignTransactionWrappedKeyWithInvalidParam,
testFailEthereumSignTransactionWrappedKeyWithMissingParam,
testEthereumBroadcastTransactionWrappedKey,
testEthereumBroadcastWrappedKeyWithFetchGasParams,
testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds,
testImportWrappedKey,
testGenerateWrappedKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { log } from '@lit-protocol/misc';
import { ethers } from 'ethers';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
import {
importPrivateKey,
EthereumLitTransaction,
signTransactionWithEthereumEncryptedKeyLitAction,
signTransactionWithEncryptedKey,
} from '@lit-protocol/wrapped-keys';
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';

/**
* Test Commands:
* ✅ NETWORK=cayenne yarn test:local --filter=testEthereumBroadcastWrappedKeyWithFetchGasParams
* ❌ NETWORK=manzano yarn test:local --filter=testEthereumBroadcastWrappedKeyWithFetchGasParams Fails with Error: Error: There should be exactly 1 element in the capabilities array but there are: 2
* ❌ NETWORK=localchain yarn test:local --filter=testEthereumBroadcastWrappedKeyWithFetchGasParams
Comment on lines +15 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'll fix it in the SDK and then there's corr fix in the Lambda as well @MaximusHaximus

*/
export const testEthereumBroadcastWrappedKeyWithFetchGasParams = async (
devEnv: TinnyEnvironment
) => {
const alice = await devEnv.createRandomPerson();

const pkpSessionSigs = await getPkpSessionSigs(
devEnv,
alice,
null,
new Date(Date.now() + 1000 * 60 * 10).toISOString()
); // 10 mins expiry

console.log(pkpSessionSigs);

const wrappedKeysWallet = ethers.Wallet.createRandom();
const wrappedKeysWalletPrivateKey = wrappedKeysWallet.privateKey;

const wrappedKeysWalletAddress = wrappedKeysWallet.address;
console.log(`Sending funds to ${wrappedKeysWalletAddress}`);
await devEnv.getFunds(wrappedKeysWallet.address, '0.005');

const pkpAddress = await importPrivateKey({
pkpSessionSigs,
privateKey: wrappedKeysWalletPrivateKey,
litNodeClient: devEnv.litNodeClient,
});

const alicePkpAddress = alice.authMethodOwnedPkp.ethAddress;
if (pkpAddress !== alicePkpAddress) {
throw new Error(
`Received address: ${pkpAddress} doesn't match Alice's PKP address: ${alicePkpAddress}`
);
}

const pkpSessionSigsSigning = await getPkpSessionSigs(
devEnv,
alice,
null,
new Date(Date.now() + 1000 * 60 * 10).toISOString()
); // 10 mins expiry

console.log(pkpSessionSigsSigning);

const unsignedTransaction: EthereumLitTransaction = {
toAddress: alice.wallet.address,
value: '0.0001', // in ethers (Lit tokens)
chainId: 175177, // Chronicle
dataHex: ethers.utils.hexlify(
ethers.utils.toUtf8Bytes('Test transaction from Alice to bob')
),
chain: 'chronicleTestnet',
};

const signedTx = await signTransactionWithEncryptedKey({
pkpSessionSigs: pkpSessionSigsSigning,
litActionCode: signTransactionWithEthereumEncryptedKeyLitAction,
unsignedTransaction,
broadcast: true,
litNodeClient: devEnv.litNodeClient,
});

console.log('signedTx');
console.log(signedTx);

// TODO!: Convert hex signedTx to UTF-8 and assert that it contains "Test transaction from Alice to bob"
if (!ethers.utils.isHexString(signedTx)) {
throw new Error(`signedTx isn't hex: ${signedTx}`);
}

log('✅ testEthereumBroadcastWrappedKeyWithDefaultGasParams');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { log } from '@lit-protocol/misc';
import { ethers } from 'ethers';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
import {
EthereumLitTransaction,
signTransactionWithEthereumEncryptedKeyLitAction,
} from '@lit-protocol/wrapped-keys';
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';
import { getPkpAccessControlCondition } from 'packages/wrapped-keys/src/lib/utils';
import { encryptString } from '@lit-protocol/encryption';
import { LIT_PREFIX } from 'packages/wrapped-keys/src/lib/constants';

/**
* Test Commands:
* ✅ NETWORK=cayenne yarn test:local --filter=testFailEthereumSignTransactionWrappedKeyInvalidDecryption
* ✅ NETWORK=manzano yarn test:local --filter=testFailEthereumSignTransactionWrappedKeyInvalidDecryption
* ✅ NETWORK=localchain yarn test:local --filter=testFailEthereumSignTransactionWrappedKeyInvalidDecryption
*/
export const testFailEthereumSignTransactionWrappedKeyInvalidDecryption =
async (devEnv: TinnyEnvironment) => {
const alice = await devEnv.createRandomPerson();
const privateKey = ethers.Wallet.createRandom().privateKey;
const alicePkpAddress = alice.authMethodOwnedPkp.ethAddress;
const decryptionAccessControlConditions =
getPkpAccessControlCondition(alicePkpAddress);
const { ciphertext, dataToEncryptHash } = await encryptString(
{
accessControlConditions: decryptionAccessControlConditions,
dataToEncrypt: LIT_PREFIX + privateKey,
},
devEnv.litNodeClient
);

const bob = await devEnv.createRandomPerson();
const pkpSessionSigsSigning = await getPkpSessionSigs(
devEnv,
bob,
null,
new Date(Date.now() + 1000 * 60 * 10).toISOString()
); // 10 mins expiry
console.log(pkpSessionSigsSigning);

const unsignedTransaction: EthereumLitTransaction = {
toAddress: alice.wallet.address,
value: '0.0001', // in ethers (Lit tokens)
chainId: 175177, // Chronicle
gasPrice: '50',
gasLimit: 21000,
dataHex: ethers.utils.hexlify(
ethers.utils.toUtf8Bytes('Test transaction from Alice to bob')
),
chain: 'chronicleTestnet',
};

try {
const _res = await devEnv.litNodeClient.executeJs({
sessionSigs: pkpSessionSigsSigning,
code: signTransactionWithEthereumEncryptedKeyLitAction,
jsParams: {
ciphertext,
dataToEncryptHash,
unsignedTransaction,
accessControlConditions: decryptionAccessControlConditions,
},
});
} catch (e: any) {
console.log('❌ THIS IS EXPECTED: ', e);
console.log(e.message);

// TODO!: Add appropriate assertions based on the above error message
// if (
// e.message.includes(
// 'Error executing the Signing Lit Action: Error: When signing transaction- processing response'
// ) &&
// e.message.includes('insufficient FPE funds for gas * price + value')
// ) {
// console.log(
// '✅ testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds is expected to have an error'
// );
// } else {
// throw e;
// }
}

log('✅ testFailEthereumSignTransactionWrappedKeyInvalidDecryption');
};
6 changes: 2 additions & 4 deletions packages/constants/src/lib/constants/autogen_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ export const INTERNAL_DEV = [
'https://167.114.17.201:443',
'https://64.131.85.108:443',
'https://167.114.17.202:443',
'https://167.114.17.205:443',
'https://64.131.85.106:443',
'https://167.114.17.203:443',
'https://108.62.0.105:443',
'https://167.114.17.204:443',
'https://64.131.85.106:443',
];

export const INTERNAL_MIN_NODE_COUNT = 2;
Expand All @@ -20,11 +19,10 @@ export const INTERNAL_DEFAULT_CONFIG = {
'https://167.114.17.201:443',
'https://64.131.85.108:443',
'https://167.114.17.202:443',
'https://167.114.17.205:443',
'https://64.131.85.106:443',
'https://167.114.17.203:443',
'https://108.62.0.105:443',
'https://167.114.17.204:443',
'https://64.131.85.106:443',
],
litNetwork: 'internalDev',
connectTimeout: 20000,
Expand Down
Loading
Loading