Skip to content

Commit

Permalink
✨ Add AES encrypt for ebook files
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jan 23, 2025
1 parent 4bff741 commit fbaa086
Show file tree
Hide file tree
Showing 5 changed files with 1,303 additions and 36 deletions.
19 changes: 18 additions & 1 deletion components/IscnUploadForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
</div>
</div>
<FormField v-if="showAddISCNPageOption">
<CheckBox v-model="isEncryptEBookData">{{ $t('UploadForm.label.encryptEBookData') }}</CheckBox>
<CheckBox v-model="isAddISCNPageToEbook">{{ $t('UploadForm.label.insertISCNPage') }}</CheckBox>
</FormField>
<!-- upload field__Submit -->
Expand Down Expand Up @@ -244,6 +245,7 @@ import exifr from 'exifr'
import Hash from 'ipfs-only-hash'
import BigNumber from 'bignumber.js'
import ePub from 'epubjs';
import { encryptDataWithAES } from 'arweavekit/encryption';
import { OfflineSigner } from '@cosmjs/proto-signing'
Expand Down Expand Up @@ -310,6 +312,7 @@ export default class IscnUploadForm extends Vue {
isOpenSignDialog = false
isOpenWarningSnackbar = false
isSizeExceeded = false
isEncryptEBookData = true
isAddISCNPageToEbook = false
uploadSizeLimit: number = UPLOAD_FILESIZE_MAX
Expand Down Expand Up @@ -844,15 +847,29 @@ export default class IscnUploadForm extends Vue {
}
}
let key;
try {
const arrayBuffer = await tempRecord.fileBlob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
let buffer = Buffer.from(arrayBuffer);
if ([
'application/epub+zip',
'application/pdf',
].includes(record.fileType)
&& this.isEncryptEBookData) {
const {
rawEncryptedKeyAsBase64,
combinedArrayBuffer,
} = await encryptDataWithAES({ data: arrayBuffer });
buffer = Buffer.from(combinedArrayBuffer);
key = rawEncryptedKeyAsBase64
}
const { arweaveId, arweaveLink } = await uploadSingleFileToBundlr(buffer, {
fileSize: tempRecord.fileBlob?.size || 0,
ipfsHash: tempRecord.ipfsHash,
fileType: tempRecord.fileType as string,
txHash: tempRecord.transactionHash,
token: this.getToken,
key,
});
if (arweaveId) {
const uploadedData = this.sentArweaveTransactionInfo.get(record.ipfsHash) || {};
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@
"UploadForm.button.size.limit": "(Recommended file size: < 20 MB)",
"UploadForm.button.skip": "Skip Upload",
"UploadForm.button": "Start Upload",
"UploadForm.label.encryptEBookData": "AES encrypt EPUB & PDF",
"UploadForm.label.insertISCNPage": "Add ISCN Page to EPUB & PDF",
"UploadForm.guide.dropFile": "Drop your file here, or",
"UploadForm.guide.selectFile": "Select your file and publish to IPFS",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@walletconnect/client": "^1.6.6",
"@walletconnect/qrcode-modal": "^1.6.6",
"@walletconnect/utils": "^1.6.6",
"arweavekit": "^1.5.1",
"axios": "^1.7.4",
"bignumber.js": "^9.0.1",
"cheerio": "^1.0.0-rc.11",
Expand Down
4 changes: 3 additions & 1 deletion utils/arweave/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export async function uploadSingleFileToBundlr(
ipfsHash,
txHash,
token,
}: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string, token: string },
key,
}: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string, token: string, key?: string },
) {
const bundler = await getBundler({ fileSize, ipfsHash, txHash, token })
const tags = [
Expand All @@ -183,6 +184,7 @@ export async function uploadSingleFileToBundlr(
ipfsHash,
txHash,
arweaveId,
key,
},{
headers: { Authorization: token ? `Bearer ${token}` : '' },
});
Expand Down
Loading

0 comments on commit fbaa086

Please sign in to comment.