-
Notifications
You must be signed in to change notification settings - Fork 1
/
Litencrypt.tsx
137 lines (123 loc) · 3.49 KB
/
Litencrypt.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use client";
import { useState } from 'react';
import {
Button,
ChakraProvider,
Container,
Input,
VStack,
} from '@chakra-ui/react';
import * as LitJsSdk from '@lit-protocol/lit-node-client';
function Litencrypt() {
const [str, setStr] = useState('🙈🙈 This is a secret 🙈🙈');
const [encryptedStr, setEncryptedStr] = useState('');
const [decryptedStr, setDecryptedStr] = useState('');
const encrypt = async () => {
try {
const litNodeClient = new LitJsSdk.LitNodeClient({
litNetwork: 'cayenne',
});
await litNodeClient.connect();
const authRes = await LitJsSdk.checkAndSignAuthMessage({
chain: 'ethereum',
});
const accss = [
{
contractAddress: '',
standardContractType: '',
chain: 'ethereum',
method: 'eth_getBalance',
parameters: [':userAddress', 'latest'],
returnValueTest: {
comparator: '>=',
value: '0',
},
},
];
const accs = [
{
contractAddress: '',
standardContractType: '',
chain: 'ethereum',
method: '',
parameters: [
':userAddress',
],
returnValueTest: {
comparator: '=',
value: '0xCF8D2Da12A032b3f3EaDC686AB18551D8fD6c132'
}
}
]
const encryptRes = await LitJsSdk.encryptString({
accessControlConditions: accs,
authSig: authRes,
chain: 'ethereum',
dataToEncrypt: str,
}, litNodeClient);
console.log('encryptRes', encryptRes);
setEncryptedStr(encryptRes.ciphertext);
} catch (error) {
console.error('Encryption error:', error);
}
};
const decrypt = async () => {
try {
const litNodeClient = new LitJsSdk.LitNodeClient({
litNetwork: 'cayenne',
});
await litNodeClient.connect();
const authRes = await LitJsSdk.checkAndSignAuthMessage({
chain: 'ethereum',
});
const accs = [
{
contractAddress: '',
standardContractType: '',
chain: 'ethereum',
method: 'eth_getBalance',
parameters: [':userAddress', 'latest'],
returnValueTest: {
comparator: '>=',
value: '0',
},
},
];
// You should replace 'encryptedStr' with the actual ciphertext.
const decryptRes = await LitJsSdk.decryptToString({
accessControlConditions: accs,
ciphertext: encryptedStr, // Replace with actual ciphertext
dataToEncryptHash: '', // Replace with the hash
authSig: authRes,
chain: 'ethereum',
}, litNodeClient);
console.log('decryptRes', decryptRes);
setDecryptedStr(decryptRes);
} catch (error) {
console.error('Decryption error:', error);
}
};
return (
<Container maxW="lg" p={4}>
<VStack spacing={4}>
<h4>Encrypt your API Key</h4>
<Input
type="text"
value={str}
onChange={(e) => {
setStr(e.target.value);
}}
/>
<Button colorScheme="blue" onClick={encrypt}>
Encrypt Key
</Button>
{/* <Button colorScheme="green" onClick={decrypt}>
Decrypt String
</Button> */}
{encryptedStr && <p>Encrypted String: {encryptedStr}</p>}
{/* {decryptedStr && <p>Decrypted String: {decryptedStr}</p>} */}
</VStack>
</Container>
);
}
export default Litencrypt;