Skip to content

Commit 70a0201

Browse files
committed
✨ Add AES encrypt for ebook files
1 parent 4bff741 commit 70a0201

File tree

5 files changed

+1312
-39
lines changed

5 files changed

+1312
-39
lines changed

components/IscnUploadForm.vue

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
</table>
106106
</div>
107107
</div>
108+
<FormField v-if="hasEbookInRecords">
109+
<CheckBox v-model="isEncryptEBookData">{{ $t('UploadForm.label.encryptEBookData') }}</CheckBox>
110+
</FormField>
108111
<FormField v-if="showAddISCNPageOption">
109112
<CheckBox v-model="isAddISCNPageToEbook">{{ $t('UploadForm.label.insertISCNPage') }}</CheckBox>
110113
</FormField>
@@ -244,6 +247,7 @@ import exifr from 'exifr'
244247
import Hash from 'ipfs-only-hash'
245248
import BigNumber from 'bignumber.js'
246249
import ePub from 'epubjs';
250+
import { encryptDataWithAES } from 'arweavekit/dist/lib/encryption';
247251
248252
import { OfflineSigner } from '@cosmjs/proto-signing'
249253
@@ -310,6 +314,7 @@ export default class IscnUploadForm extends Vue {
310314
isOpenSignDialog = false
311315
isOpenWarningSnackbar = false
312316
isSizeExceeded = false
317+
isEncryptEBookData = true
313318
isAddISCNPageToEbook = false
314319
315320
uploadSizeLimit: number = UPLOAD_FILESIZE_MAX
@@ -402,14 +407,18 @@ export default class IscnUploadForm extends Vue {
402407
}
403408
}
404409
405-
get showAddISCNPageOption() {
406-
return this.mode === MODE.EDIT
407-
&& this.fileRecords.some(file => [
410+
get hasEbookInRecords() {
411+
return this.fileRecords.some(file => [
408412
'application/epub+zip',
409413
'application/pdf',
410414
].includes(file.fileType))
411415
}
412416
417+
get showAddISCNPageOption() {
418+
return this.mode === MODE.EDIT
419+
&& this.hasEbookInRecords
420+
}
421+
413422
get modifiedFileRecords() {
414423
if (!this.isAddISCNPageToEbook || this.mode !== MODE.EDIT) return this.fileRecords
415424
return this.fileRecords.map((record) => {
@@ -844,15 +853,29 @@ export default class IscnUploadForm extends Vue {
844853
}
845854
}
846855
856+
let key;
847857
try {
848858
const arrayBuffer = await tempRecord.fileBlob.arrayBuffer();
849-
const buffer = Buffer.from(arrayBuffer);
859+
let buffer = Buffer.from(arrayBuffer);
860+
if ([
861+
'application/epub+zip',
862+
'application/pdf',
863+
].includes(record.fileType)
864+
&& this.isEncryptEBookData) {
865+
const {
866+
rawEncryptedKeyAsBase64,
867+
combinedArrayBuffer,
868+
} = await encryptDataWithAES({ data: arrayBuffer });
869+
buffer = Buffer.from(combinedArrayBuffer);
870+
key = rawEncryptedKeyAsBase64
871+
}
850872
const { arweaveId, arweaveLink } = await uploadSingleFileToBundlr(buffer, {
851873
fileSize: tempRecord.fileBlob?.size || 0,
852874
ipfsHash: tempRecord.ipfsHash,
853875
fileType: tempRecord.fileType as string,
854876
txHash: tempRecord.transactionHash,
855877
token: this.getToken,
878+
key,
856879
});
857880
if (arweaveId) {
858881
const uploadedData = this.sentArweaveTransactionInfo.get(record.ipfsHash) || {};

locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@
385385
"UploadForm.button.size.limit": "(Recommended file size: < 20 MB)",
386386
"UploadForm.button.skip": "Skip Upload",
387387
"UploadForm.button": "Start Upload",
388+
"UploadForm.label.encryptEBookData": "Encrypt EPUB & PDF on upload",
388389
"UploadForm.label.insertISCNPage": "Add ISCN Page to EPUB & PDF",
389390
"UploadForm.guide.dropFile": "Drop your file here, or",
390391
"UploadForm.guide.selectFile": "Select your file and publish to IPFS",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@walletconnect/client": "^1.6.6",
3535
"@walletconnect/qrcode-modal": "^1.6.6",
3636
"@walletconnect/utils": "^1.6.6",
37+
"arweavekit": "^1.5.1",
3738
"axios": "^1.7.4",
3839
"bignumber.js": "^9.0.1",
3940
"cheerio": "^1.0.0-rc.11",

utils/arweave/v2.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ export async function uploadSingleFileToBundlr(
163163
ipfsHash,
164164
txHash,
165165
token,
166-
}: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string, token: string },
166+
key,
167+
}: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string, token: string, key?: string },
167168
) {
168169
const bundler = await getBundler({ fileSize, ipfsHash, txHash, token })
169170
const tags = [
@@ -183,6 +184,7 @@ export async function uploadSingleFileToBundlr(
183184
ipfsHash,
184185
txHash,
185186
arweaveId,
187+
key,
186188
},{
187189
headers: { Authorization: token ? `Bearer ${token}` : '' },
188190
});

0 commit comments

Comments
 (0)