[英]Fetching Uint8Array to use as ArrayBuffer
I am attempting to fetch encrypted raw buffer data, pass to a decrypt function and use this to display an image.我正在尝试获取加密的原始缓冲区数据,传递给解密 function 并使用它来显示图像。
What I have so far returns a Uint8Array.到目前为止我所拥有的返回一个 Uint8Array。 I try to pass this to the decrypt function and it gives the following error:
我尝试将此传递给解密 function 并给出以下错误:
Unhandled Rejection (TypeError): First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
Error is given from the following code:以下代码给出了错误:
const fetched_data = await arweave.transactions.getData('transaction', {decode: true})
const decrypted_data = decrypt(fetched_data.buffer, key)
console.log(decrypted_data)
So I figured I need to change it to an ArrayBuffer.所以我想我需要将其更改为 ArrayBuffer。 I followed the answer here: Uint8Array to ArrayBuffer
我在这里遵循了答案: Uint8Array to ArrayBuffer
My code now:我现在的代码:
const fetched_data = await arweave.transactions.getData('transaction', {decode: true})
const buffer_data = fetched_data.buffer.slice(fetched_data.byteOffset, fetched_data.byteLength + fetched_data.byteOffset)
const decrypted_data = decrypt(buffer_data, key)
console.log(decrypted_data)
This returns an ArrayBuffer.这将返回一个 ArrayBuffer。 However, passing it to my decrypt function yields the same error.
但是,将它传递给我的解密 function 会产生相同的错误。 Here is my decrypt function.
这是我的解密 function。
export const decrypt = (dataBuffer, key) => {
// Create cipherKey
const cipherKey = Buffer.from(key);
// Get iv and its size
const ivSize = dataBuffer.readUInt8(0);
const iv = dataBuffer.slice(1, ivSize + 1);
// Get authTag - is default 16 bytes in AES-GCM
const authTag = dataBuffer.slice(ivSize + 1, ivSize + 17);
// Create decipher
const decipher = crypto.createDecipheriv(ALGORITHM, cipherKey, iv);
decipher.setAuthTag(authTag);
return Buffer.concat([
decipher.update(dataBuffer.slice(ivSize + 17)),
decipher.final(),
]);
};
The decrypt function is working as expected in NodeJS on my local machine, but now I am trying to fetch the encrypted data from Arweave and I run into this issue.解密 function 在我的本地机器上的 NodeJS 中按预期工作,但现在我试图从 Arweave 获取加密数据,我遇到了这个问题。 Any help is appreciated.
任何帮助表示赞赏。
Update更新
I also tried to get it to work with a regular fetch.我还试图让它与常规提取一起工作。
const getData = async (data, key) => {
const response = get('URL HERE')
response.then(res => console.log(decrypt(res, key)))
return
}
Response returns a Promise with ArrayBuffer.响应返回带有 ArrayBuffer 的 Promise。 When I pass the response to decrypt function, it shows the same error.
当我传递响应以解密 function 时,它显示相同的错误。
Please try reducing your problem set to show a minimum viable example.请尝试减少您的问题集以显示一个最小的可行示例。
(async () => {
const url = 'https://fetch-progress.anthum.com/images/sunrise-baseline.jpg'
const imageMimeType = 'image/jpeg'
const buffer = await (await fetch(url)).arrayBuffer()
// Decrypt your buffer before passing it to addImage()
const decryptedBuffer = await decrypt(buffer)
const img = document.createElement('img')
img.src = URL.createObjectURL(
new Blob([ decryptedBuffer ], { type: imageMimeType })
)
document.body.append(img)
})()
// returns decrypted ArrayBuffer.
async function decrypt(encryptedBuffer) {
// Implement your decryption here
return encryptedBuffer
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.