Skip to content

Commit ed2fdd4

Browse files
authored
Merge pull request #501 from LIT-Protocol/wyatt/fetch-gas-price-la
Fetch `gasPrice` and estimate gas in Wrapped Keys LA if not provided
2 parents 62e95ea + d542ef5 commit ed2fdd4

File tree

5 files changed

+407
-166
lines changed

5 files changed

+407
-166
lines changed

local-tests/test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ import { testExecuteJsBroadcastAndCollect } from './tests/testExecuteJsBroadcast
8686
import { testEthereumSignMessageGeneratedKey } from './tests/testEthereumSignMessageGeneratedKey';
8787
import { testEthereumBroadcastTransactionGeneratedKey } from './tests/testEthereumBroadcastTransactionGeneratedKey';
8888
import { testEthereumSignMessageWrappedKey } from './tests/testEthereumSignMessageWrappedKey';
89+
import { testFailEthereumSignTransactionWrappedKeyInvalidDecryption } from './tests/testFailEthereumSignTransactionWrappedKeyInvalidDecryption';
8990
// import { testFailWrappedKeySigningTimeout } from './tests/testFailWrappedKeySigningTimeout';
9091
import { testEthereumSignTransactionWrappedKey } from './tests/testEthereumSignTransactionWrappedKey';
9192
import { testFailEthereumSignTransactionWrappedKeyWithInvalidParam } from './tests/testFailEthereumSignTransactionWrappedKeyWithInvalidParam';
9293
import { testFailEthereumSignTransactionWrappedKeyWithMissingParam } from './tests/testFailEthereumSignTransactionWrappedKeyWithMissingParam';
9394
import { testEthereumBroadcastTransactionWrappedKey } from './tests/testEthereumBroadcastTransactionWrappedKey';
95+
import { testEthereumBroadcastWrappedKeyWithFetchGasParams } from './tests/testEthereumBroadcastWrappedKeyWithFetchGasParams';
9496
import { testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds } from './tests/testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds';
9597
import { testImportWrappedKey } from './tests/testImportWrappedKey';
9698
import { testGenerateWrappedKey } from './tests/testGenerateWrappedKey';
@@ -110,6 +112,7 @@ import { testExportWrappedKey } from './tests/testExportWrappedKey';
110112
await devEnv.init();
111113

112114
const wrappedKeysTests = {
115+
testFailEthereumSignTransactionWrappedKeyInvalidDecryption,
113116
testEthereumSignMessageGeneratedKey,
114117
testEthereumBroadcastTransactionGeneratedKey,
115118
// testRemoveDecryptFailingOutsideLitAction,
@@ -118,6 +121,7 @@ import { testExportWrappedKey } from './tests/testExportWrappedKey';
118121
testFailEthereumSignTransactionWrappedKeyWithInvalidParam,
119122
testFailEthereumSignTransactionWrappedKeyWithMissingParam,
120123
testEthereumBroadcastTransactionWrappedKey,
124+
testEthereumBroadcastWrappedKeyWithFetchGasParams,
121125
testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds,
122126
testImportWrappedKey,
123127
testGenerateWrappedKey,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { log } from '@lit-protocol/misc';
2+
import { ethers } from 'ethers';
3+
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
4+
import {
5+
importPrivateKey,
6+
EthereumLitTransaction,
7+
signTransactionWithEthereumEncryptedKeyLitAction,
8+
signTransactionWithEncryptedKey,
9+
} from '@lit-protocol/wrapped-keys';
10+
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';
11+
12+
/**
13+
* Test Commands:
14+
* ✅ NETWORK=cayenne yarn test:local --filter=testEthereumBroadcastWrappedKeyWithFetchGasParams
15+
* ❌ 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
16+
* ❌ NETWORK=localchain yarn test:local --filter=testEthereumBroadcastWrappedKeyWithFetchGasParams
17+
*/
18+
export const testEthereumBroadcastWrappedKeyWithFetchGasParams = async (
19+
devEnv: TinnyEnvironment
20+
) => {
21+
const alice = await devEnv.createRandomPerson();
22+
23+
const pkpSessionSigs = await getPkpSessionSigs(
24+
devEnv,
25+
alice,
26+
null,
27+
new Date(Date.now() + 1000 * 60 * 10).toISOString()
28+
); // 10 mins expiry
29+
30+
console.log(pkpSessionSigs);
31+
32+
const wrappedKeysWallet = ethers.Wallet.createRandom();
33+
const wrappedKeysWalletPrivateKey = wrappedKeysWallet.privateKey;
34+
35+
const wrappedKeysWalletAddress = wrappedKeysWallet.address;
36+
console.log(`Sending funds to ${wrappedKeysWalletAddress}`);
37+
await devEnv.getFunds(wrappedKeysWallet.address, '0.005');
38+
39+
const pkpAddress = await importPrivateKey({
40+
pkpSessionSigs,
41+
privateKey: wrappedKeysWalletPrivateKey,
42+
litNodeClient: devEnv.litNodeClient,
43+
});
44+
45+
const alicePkpAddress = alice.authMethodOwnedPkp.ethAddress;
46+
if (pkpAddress !== alicePkpAddress) {
47+
throw new Error(
48+
`Received address: ${pkpAddress} doesn't match Alice's PKP address: ${alicePkpAddress}`
49+
);
50+
}
51+
52+
const pkpSessionSigsSigning = await getPkpSessionSigs(
53+
devEnv,
54+
alice,
55+
null,
56+
new Date(Date.now() + 1000 * 60 * 10).toISOString()
57+
); // 10 mins expiry
58+
59+
console.log(pkpSessionSigsSigning);
60+
61+
const unsignedTransaction: EthereumLitTransaction = {
62+
toAddress: alice.wallet.address,
63+
value: '0.0001', // in ethers (Lit tokens)
64+
chainId: 175177, // Chronicle
65+
dataHex: ethers.utils.hexlify(
66+
ethers.utils.toUtf8Bytes('Test transaction from Alice to bob')
67+
),
68+
chain: 'chronicleTestnet',
69+
};
70+
71+
const signedTx = await signTransactionWithEncryptedKey({
72+
pkpSessionSigs: pkpSessionSigsSigning,
73+
litActionCode: signTransactionWithEthereumEncryptedKeyLitAction,
74+
unsignedTransaction,
75+
broadcast: true,
76+
litNodeClient: devEnv.litNodeClient,
77+
});
78+
79+
console.log('signedTx');
80+
console.log(signedTx);
81+
82+
// TODO!: Convert hex signedTx to UTF-8 and assert that it contains "Test transaction from Alice to bob"
83+
if (!ethers.utils.isHexString(signedTx)) {
84+
throw new Error(`signedTx isn't hex: ${signedTx}`);
85+
}
86+
87+
log('✅ testEthereumBroadcastWrappedKeyWithDefaultGasParams');
88+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { log } from '@lit-protocol/misc';
2+
import { ethers } from 'ethers';
3+
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
4+
import {
5+
EthereumLitTransaction,
6+
signTransactionWithEthereumEncryptedKeyLitAction,
7+
} from '@lit-protocol/wrapped-keys';
8+
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';
9+
import { getPkpAccessControlCondition } from 'packages/wrapped-keys/src/lib/utils';
10+
import { encryptString } from '@lit-protocol/encryption';
11+
import { LIT_PREFIX } from 'packages/wrapped-keys/src/lib/constants';
12+
13+
/**
14+
* Test Commands:
15+
* ✅ NETWORK=cayenne yarn test:local --filter=testFailEthereumSignTransactionWrappedKeyInvalidDecryption
16+
* ✅ NETWORK=manzano yarn test:local --filter=testFailEthereumSignTransactionWrappedKeyInvalidDecryption
17+
* ✅ NETWORK=localchain yarn test:local --filter=testFailEthereumSignTransactionWrappedKeyInvalidDecryption
18+
*/
19+
export const testFailEthereumSignTransactionWrappedKeyInvalidDecryption =
20+
async (devEnv: TinnyEnvironment) => {
21+
const alice = await devEnv.createRandomPerson();
22+
const privateKey = ethers.Wallet.createRandom().privateKey;
23+
const alicePkpAddress = alice.authMethodOwnedPkp.ethAddress;
24+
const decryptionAccessControlConditions =
25+
getPkpAccessControlCondition(alicePkpAddress);
26+
const { ciphertext, dataToEncryptHash } = await encryptString(
27+
{
28+
accessControlConditions: decryptionAccessControlConditions,
29+
dataToEncrypt: LIT_PREFIX + privateKey,
30+
},
31+
devEnv.litNodeClient
32+
);
33+
34+
const bob = await devEnv.createRandomPerson();
35+
const pkpSessionSigsSigning = await getPkpSessionSigs(
36+
devEnv,
37+
bob,
38+
null,
39+
new Date(Date.now() + 1000 * 60 * 10).toISOString()
40+
); // 10 mins expiry
41+
console.log(pkpSessionSigsSigning);
42+
43+
const unsignedTransaction: EthereumLitTransaction = {
44+
toAddress: alice.wallet.address,
45+
value: '0.0001', // in ethers (Lit tokens)
46+
chainId: 175177, // Chronicle
47+
gasPrice: '50',
48+
gasLimit: 21000,
49+
dataHex: ethers.utils.hexlify(
50+
ethers.utils.toUtf8Bytes('Test transaction from Alice to bob')
51+
),
52+
chain: 'chronicleTestnet',
53+
};
54+
55+
try {
56+
const _res = await devEnv.litNodeClient.executeJs({
57+
sessionSigs: pkpSessionSigsSigning,
58+
code: signTransactionWithEthereumEncryptedKeyLitAction,
59+
jsParams: {
60+
ciphertext,
61+
dataToEncryptHash,
62+
unsignedTransaction,
63+
accessControlConditions: decryptionAccessControlConditions,
64+
},
65+
});
66+
} catch (e: any) {
67+
console.log('❌ THIS IS EXPECTED: ', e);
68+
console.log(e.message);
69+
70+
// TODO!: Add appropriate assertions based on the above error message
71+
// if (
72+
// e.message.includes(
73+
// 'Error executing the Signing Lit Action: Error: When signing transaction- processing response'
74+
// ) &&
75+
// e.message.includes('insufficient FPE funds for gas * price + value')
76+
// ) {
77+
// console.log(
78+
// '✅ testFailEthereumBroadcastTransactionWrappedKeysInsufficientFunds is expected to have an error'
79+
// );
80+
// } else {
81+
// throw e;
82+
// }
83+
}
84+
85+
log('✅ testFailEthereumSignTransactionWrappedKeyInvalidDecryption');
86+
};

packages/constants/src/lib/constants/autogen_internal.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ export const INTERNAL_DEV = [
33
'https://167.114.17.201:443',
44
'https://64.131.85.108:443',
55
'https://167.114.17.202:443',
6-
'https://167.114.17.205:443',
6+
'https://64.131.85.106:443',
77
'https://167.114.17.203:443',
88
'https://108.62.0.105:443',
99
'https://167.114.17.204:443',
10-
'https://64.131.85.106:443',
1110
];
1211

1312
export const INTERNAL_MIN_NODE_COUNT = 2;
@@ -20,11 +19,10 @@ export const INTERNAL_DEFAULT_CONFIG = {
2019
'https://167.114.17.201:443',
2120
'https://64.131.85.108:443',
2221
'https://167.114.17.202:443',
23-
'https://167.114.17.205:443',
22+
'https://64.131.85.106:443',
2423
'https://167.114.17.203:443',
2524
'https://108.62.0.105:443',
2625
'https://167.114.17.204:443',
27-
'https://64.131.85.106:443',
2826
],
2927
litNetwork: 'internalDev',
3028
connectTimeout: 20000,

0 commit comments

Comments
 (0)