4
1
mirror of https://github.com/Qortal/qortal-ui.git synced 2025-02-23 23:55:47 +00:00
qortal-ui/crypto/api/deps/Base64Message.js
AlphaX-Qortal d915b25b47 Various updates
- Add display private messages from Qortal-Hub
- Re-add accidentally deleted qortal request SAVE_FILE
- Reworked decoding Qortal-Hub messages
- Update languages files
2025-02-11 20:29:22 +01:00

97 lines
2.9 KiB
JavaScript

import {
uint8ArrayToBase64,
base64ToUint8Array,
uint8ArrayToObject,
decryptSingle
} from '../../../plugins/plugins/core/components/GroupEncryption.js'
import {
extensionToPointer,
encodedToChar,
embedToString,
parseQortalLink
} from '../../../plugins/plugins/core/components/qdn-action-constants.js'
const Base64Message = {}
Base64Message.decode = function (string, keys, ref) {
let repliedToStr = ''
let hubSpecialId = ''
let hubMessageStr = ''
let newMessageObject = ''
let messageUseEmbed = {}
let isHubReaction = false
let editStr = false
let embedFileStr = '"images":[""]'
const binaryString = atob(string)
const binaryLength = binaryString.length
const bytes = new Uint8Array(binaryLength)
for (let i = 0; i < binaryLength; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
const decoder = new TextDecoder()
const decodedString = decoder.decode(bytes)
if (decodedString.includes("messageText") || decodedString === "4001") {
if (decodedString === "4001") {
const firstString = 'First group key created.'
const hubString = '{"messageText":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"' + firstString + '"}]}]},"images":[""],"repliedTo":"","version":3}'
return hubString
} else {
return decodedString
}
} else {
const res = decryptSingle(string, keys, false)
if (res === 'noKey' || res === 'decryptionFailed') {
return '{"specialId":"","message":"<p>This message could not be decrypted</p>","repliedTo":"","isEdited":false,"isFromHub":true,"isReaction":false,"version": 3}'
}
const decryptToUnit8Array = base64ToUint8Array(res)
const responseData = uint8ArrayToObject(decryptToUnit8Array)
if (responseData.type === "notification") {
hubMessageStr = responseData.data.message
}
if (ref !== "noref") {
if (responseData.type === "reaction") {
isHubReaction = true
repliedToStr = ref
hubMessageStr = responseData.content
}
}
if (responseData.type === "edit") {
editStr = true
}
if (responseData.repliedTo) {
repliedToStr = responseData.repliedTo
}
if (responseData.specialId) {
hubSpecialId = responseData.specialId
}
if (responseData.message.includes('qortal://use-embed/')) {
const useEmbed1 = extensionToPointer(responseData.message)
const useEmbed2 = /<newpointer>(.*?)<\/newpointer>/g.exec(useEmbed1)
const useEmbed3 = encodedToChar(useEmbed2[1])
messageUseEmbed = parseQortalLink(useEmbed3)
embedFileStr = embedToString(messageUseEmbed)
hubMessageStr = responseData.message.split(useEmbed2[1]).join('')
} else {
hubMessageStr = responseData.message
}
newMessageObject = '{"specialId":"' + hubSpecialId + '","message":"' + hubMessageStr + '",' + embedFileStr + ',"repliedTo":"' + repliedToStr + '","isEdited":' + editStr + ',"isFromHub":true,"isReaction":' + isHubReaction + ',"version": 3}'
return newMessageObject
}
}
export default Base64Message