-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
126 lines (115 loc) · 3.43 KB
/
index.jsx
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
import { useCallback, useState } from 'react'
import { createRoot } from 'react-dom/client'
import LitJsSdk from '@lit-protocol/sdk-browser'
const chain = 'mumbai'
const evmContractConditions = [
{
contractAddress: "0x093d00c17dA59Ec0Bf265Aad61424360aE055f30",
functionName: "ownerOfBlock",
functionParams: [
"0xdcc5e7df34d6f1fafe3d8510e852d280b2f700d59d62fd17fd9c32d9ef71f9bc",
":userAddress"
],
functionAbi: {
name: "ownerOfBlock",
inputs: [
{
name: "cid",
type: "bytes32"
},
{
name: "owner",
type: "address"
}
],
outputs: [
{
name: "",
type: "bool"
}
],
constant: false,
stateMutability: "view",
type: "function"
},
chain,
returnValueTest: {
comparator: "=",
value: "true"
}
}
]
const client = new LitJsSdk.LitNodeClient()
const App = () => {
const [key, setKey] = useState()
const [payload, setPayload] = useState(JSON.stringify({
test: "test"
}))
const [error, setError] = useState()
const [encryptedKey, setEncryptedKey] = useState()
const encrypt = useCallback(async () => {
const data = LitJsSdk.uint8arrayFromString(payload)
const { encryptedFile, symmetricKey } = await LitJsSdk.encryptFile({ file: new Blob([data]) })
const buffer = await new Response(encryptedFile).arrayBuffer()
const fileUint = new Uint8Array(buffer)
setKey(LitJsSdk.uint8arrayToString(symmetricKey, 'hex'))
setPayload(LitJsSdk.uint8arrayToString(fileUint, 'hex'))
}, [payload, key])
const save = useCallback(async () => {
const authSig = await LitJsSdk.checkAndSignAuthMessage({ chain })
await client.connect()
try {
const encryptedSymmetricKey = await client.saveEncryptionKey({
authSig,
chain,
evmContractConditions,
symmetricKey: LitJsSdk.uint8arrayFromString(key, 'hex'),
})
setEncryptedKey(LitJsSdk.uint8arrayToString(encryptedSymmetricKey, 'hex'))
} catch(err) {
setError(err.message)
}
}, [key])
const decrypt = useCallback(async () => {
const authSig = await LitJsSdk.checkAndSignAuthMessage({ chain })
await client.connect()
try {
const decryptedKey = await client.getEncryptionKey({
authSig,
chain,
evmContractConditions,
toDecrypt: encryptedKey,
})
setEncryptedKey(LitJsSdk.uint8arrayToString(decryptedKey, 'utf8'))
} catch(e) {
setError(e.message)
}
}, [encryptedKey])
return (
<div>
<h2>Lit Protocol Encrypt/Decrypt Example</h2>
<p>Click the buttons in procession. Encrypt {'->'} Save {'->'} Decrypt.</p>
<p>Expected: Error regarding a failed conditions of some sort. Or a success if you own the account that passes the condition</p>
<p>Result: 422 Error complaining about JSON being malformatted</p>
<div>
<button onClick={encrypt}>Encrypt</button>
</div>
<div>
<button onClick={save}>Save</button>
</div>
<div>
<button onClick={decrypt}>Decrypt</button>
</div>
<div>
<h3>Results:</h3>
<p>Key: {key}</p>
<p>Encrypted Key: {encryptedKey}</p>
<p>Payload: {payload}</p>
</div>
{error && <p style={{ color: 'red' }}>{error}</p>}
</div>
)
}
const main = document.querySelector('main#app')
const root = createRoot(main)
root.render(<App />)