1
+ import { Button } from "@/components/button" ;
1
2
import lighthouse from "@lighthouse-web3/sdk" ;
3
+ import { ethers } from "ethers" ;
2
4
import { useState } from "react" ;
3
5
import { useForm } from "react-hook-form" ;
4
6
5
7
export function AddPaperForm ( ) {
6
- const [ paperFile , setPaperFile ] = useState ( null ) ;
8
+ const [ data , setData ] = useState ( null ) ;
9
+ const [ encrypted , setEncrypted ] = useState ( false ) ;
7
10
8
11
const { register, handleSubmit } = useForm ( ) ;
9
12
10
- const onSubmit = async ( data ) => {
11
- if ( ! paperFile ) throw Error ( "No file selected" ) ;
12
- const fileUrl = await deployImage ( ) ;
13
- console . log ( "fileUrl :>> " , fileUrl ) ;
14
- console . log ( "data :>> " , data ) ;
13
+ const encryptionSignature = async ( ) => {
14
+ const provider = new ethers . providers . Web3Provider ( window . ethereum ) ;
15
+ const signer = provider . getSigner ( ) ;
16
+ const address = await signer . getAddress ( ) ;
17
+ const messageRequested = ( await lighthouse . getAuthMessage ( address ) ) . data
18
+ . message ;
19
+ const signedMessage = await signer . signMessage ( messageRequested ) ;
20
+ return {
21
+ signedMessage : signedMessage ,
22
+ publicKey : address ,
23
+ } ;
15
24
} ;
16
25
17
- const onChangePaperFile = ( e ) => {
18
- setPaperFile ( URL . createObjectURL ( e . target . files [ 0 ] ) ) ;
26
+ //percentage of upload data done
27
+ const progressCallback = ( progressData ) => {
28
+ const total = progressData ?. total ;
29
+ const uploaded = progressData ?. uploaded ;
30
+ const percentageToBeDone = parseInt ( ( total / uploaded ) . toFixed ( 2 ) ) ;
31
+ let percentageDone = 100 - percentageToBeDone ;
32
+ console . log ( "percentageDone" , percentageDone ) ;
19
33
} ;
20
34
21
- const deployImage = async ( ) => {
22
- const progressCallback = ( progressData ) => {
23
- const total = progressData ?. total ;
24
- const uploaded = progressData ?. uploaded ;
25
- const percentageToBeDone = parseInt ( ( total / uploaded ) . toFixed ( 2 ) ) ;
26
- let percentageDone = 100 - percentageToBeDone ;
27
- console . log ( "percentageDone" , percentageDone ) ;
28
- } ;
35
+ const onSubmit = async ( data ) => {
36
+ if ( ! data ) throw Error ( "No file selected" ) ;
37
+ const fileUrl = await uploadData ( ) ;
38
+ console . log ( "fileUrl :>> " , fileUrl ) ;
39
+ } ;
29
40
30
- const output = await lighthouse . upload (
31
- paperFile ,
41
+ const uploadData = async ( ) => {
42
+ // upload encrypted file to IPFS and return the url
43
+ if ( encrypted ) {
44
+ const sig = await encryptionSignature ( ) ;
45
+ const response = await lighthouse . uploadEncrypted (
46
+ data ,
47
+ sig . publicKey ,
48
+ process . env . NEXT_PUBLIC_LIGHTHOUSE_API_KEY ,
49
+ sig . signedMessage ,
50
+ progressCallback
51
+ ) ;
52
+ const url = `https://gateway.lighthouse.storage/ipfs/${ response . data . Hash } ` ;
53
+ console . log ( "Encrypted FileUrl: " , url ) ;
54
+ return url ;
55
+ }
56
+ // upload unencrypted file to IPFS and return the url
57
+ const response = await lighthouse . upload (
58
+ data ,
32
59
process . env . NEXT_PUBLIC_LIGHTHOUSE_API_KEY ,
33
60
progressCallback
34
61
) ;
35
- console . log ( "File Status:" , output ) ;
36
- const url = `Visit at https://gateway.lighthouse.storage/ipfs/ ${ output . data . Hash } ` ;
62
+ const url = `Vhttps://gateway.lighthouse.storage/ipfs/ ${ response . data . Hash } ` ;
63
+ console . log ( "File URL: " , url ) ;
37
64
return url ;
38
65
} ;
39
66
@@ -44,7 +71,7 @@ export function AddPaperForm() {
44
71
// Display response
45
72
console . log ( "ACCESSCON:" , response ) ;
46
73
} ;
47
-
74
+ console . log ( "encrypted :>>" , encrypted ) ;
48
75
return (
49
76
< div className = "px-4 pt-8 mx-auto max-w-7xl sm:px-6 lg:px-8" >
50
77
< div className = "flex flex-col" >
@@ -104,7 +131,33 @@ export function AddPaperForm() {
104
131
/>
105
132
</ div >
106
133
</ div >
134
+ < div className = "sm:col-span-4" >
135
+ < div className = "flex items-start h-" >
136
+ < input
137
+ id = "encrypt"
138
+ aria-describedby = "encrypt-description"
139
+ name = "encrypt"
140
+ type = "checkbox"
141
+ onChange = { ( ) => setEncrypted ( ! encrypted ) }
142
+ className = "w-4 h-4 mt-[0.5px] text-indigo-600 border-gray-300 rounded focus:ring-indigo-500"
143
+ />
107
144
145
+ < div className = "ml-3 text-sm" >
146
+ < label
147
+ htmlFor = "encrypt"
148
+ className = "font-medium text-gray-700"
149
+ >
150
+ Encrypt data
151
+ </ label >
152
+ < p id = "encrypt-description" className = "text-gray-500" >
153
+ The data will be encrypted before being stored on the
154
+ IPFS public network. Meaning it can only be accessed by
155
+ the persons who have the key to decrypt the data once
156
+ downloaded.
157
+ </ p >
158
+ </ div >
159
+ </ div >
160
+ </ div >
108
161
< div className = "sm:col-span-6" >
109
162
< label
110
163
htmlFor = "cover-photo"
@@ -138,7 +191,7 @@ export function AddPaperForm() {
138
191
id = "file-upload"
139
192
name = "file-upload"
140
193
type = "file"
141
- onChange = { ( e ) => setPaperFile ( e ) }
194
+ onChange = { ( e ) => setData ( e ) }
142
195
className = "sr-only"
143
196
/>
144
197
</ label >
@@ -154,18 +207,12 @@ export function AddPaperForm() {
154
207
155
208
< div className = "pt-5" >
156
209
< div className = "flex justify-end" >
157
- < button
158
- type = "button"
159
- className = "px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
160
- >
210
+ < Button type = "button" variant = "white" >
161
211
Clear form
162
- </ button >
163
- < button
164
- type = "submit"
165
- className = "inline-flex justify-center px-4 py-2 ml-3 text-sm font-medium text-white bg-indigo-600 border border-transparent rounded-md shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
166
- >
212
+ </ Button >
213
+ < Button type = "submit" className = "inline-flex justify-center ml-3" >
167
214
Save Paper
168
- </ button >
215
+ </ Button >
169
216
</ div >
170
217
</ div >
171
218
</ form >
0 commit comments