-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
140 lines (116 loc) · 4.69 KB
/
app.js
File metadata and controls
140 lines (116 loc) · 4.69 KB
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
138
139
140
// App.js
import React, { useState, useEffect } from 'react';
import { ethers } from 'ethers';
import LitJsSdk from 'lit-js-sdk';
import { ChainSafeStorage } from '@chainsafe/files-api-js';
import pkNFT1155ABI from './pkNFT1155ABI';
const CONTRACT_ADDRESS = "0xYourContractAddress"; // Replace with your contract address
const App = () => {
const [apiKey, setApiKey] = useState('');
const [apiKeyName, setApiKeyName] = useState('');
const [fileURL, setFileURL] = useState('');
const [contract, setContract] = useState(null);
const [signer, setSigner] = useState(null);
useEffect(() => {
connectMetaMask();
}, []);
const connectMetaMask = async () => {
if (window.ethereum) {
await window.ethereum.request({ method: 'eth_requestAccounts' });
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
setSigner(signer);
const contract = new ethers.Contract(CONTRACT_ADDRESS, pkNFT1155ABI, signer);
setContract(contract);
} else {
console.log('MetaMask is not installed');
}
};
const encryptAndStoreAPIKey = async () => {
const litNodeClient = new LitJsSdk.LitNodeClient();
await litNodeClient.connect();
const authSig = await LitJsSdk.checkAndSignAuthMessage({ chain: 'ethereum' });
const { encryptedString, symmetricKey } = await LitJsSdk.encryptString(apiKey);
const accessControlConditions = [
{
contractAddress: '',
standardContractType: '',
chain: 'ethereum',
method: 'eth_getBalance',
parameters: [':userAddress'],
returnValueTest: {
comparator: '>=',
value: '0',
},
},
];
const encryptedSymmetricKey = await litNodeClient.saveEncryptionKey({
accessControlConditions,
symmetricKey,
authSig,
chain: 'ethereum',
});
const encryptedKey = {
encryptedString: LitJsSdk.uint8arrayToString(new Uint8Array(await encryptedString.arrayBuffer()), 'base64'),
encryptedSymmetricKey: LitJsSdk.uint8arrayToString(encryptedSymmetricKey, 'base16'),
};
const storage = new ChainSafeStorage({
token: 'YOUR_CHAINSAFE_TOKEN', // Replace with your ChainSafe API token
});
const result = await storage.upload({
fileName: `${apiKeyName}.json`,
fileBuffer: Buffer.from(JSON.stringify(encryptedKey)),
});
setFileURL(result.fileURL);
console.log('API Key encrypted and stored:', result.fileURL);
await contract.storeEncryptedAPIKey(signer.getAddress(), apiKeyName, result.fileURL);
};
const retrieveAndDecryptAPIKey = async () => {
const storage = new ChainSafeStorage({
token: 'YOUR_CHAINSAFE_TOKEN', // Replace with your ChainSafe API token
});
const file = await storage.get(fileURL);
const encryptedKey = JSON.parse(file);
const litNodeClient = new LitJsSdk.LitNodeClient();
await litNodeClient.connect();
const authSig = await LitJsSdk.checkAndSignAuthMessage({ chain: 'ethereum' });
const { symmetricKey } = await litNodeClient.getEncryptionKey({
accessControlConditions,
toDecrypt: encryptedKey.encryptedSymmetricKey,
chain: 'ethereum',
authSig,
});
const decryptedString = await LitJsSdk.decryptString(
LitJsSdk.uint8arrayFromString(encryptedKey.encryptedString, 'base64'),
symmetricKey
);
console.log('Decrypted API Key:', decryptedString);
};
return (
<div>
<h1>Encrypt and Store API Key</h1>
<input
type="text"
value={apiKeyName}
onChange={(e) => setApiKeyName(e.target.value)}
placeholder="API Key Name"
/>
<input
type="text"
value={apiKey}
onChange={(e) => setApiKey(e.target.value)}
placeholder="API Key"
/>
<button onClick={encryptAndStoreAPIKey}>Encrypt and Store API Key</button>
<h1>Retrieve and Decrypt API Key</h1>
<input
type="text"
value={fileURL}
onChange={(e) => setFileURL(e.target.value)}
placeholder="File URL"
/>
<button onClick={retrieveAndDecryptAPIKey}>Retrieve and Decrypt API Key</button>
</div>
);
};
export default App;