I am fetching encrypted buffer data from Arweave. When I log the response data, it appears to be correct, looks like the correct buffer data. When I check typeof for the decrypted buffer data, it shows it is an object. I am attempting to take this decrypted buffer data and send it back as a response so that it can be displayed as an image on my front-end.
My approach is to turn the buffer data into base64, then send the response back with the mimetype and base64 data in an tag.
The problem is that the response is
<img src="data:image/png;base64,[object Promise]" />
Here is my code:
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const crypto = require("crypto");
const Arweave = require("arweave");
const ALGORITHM = "aes-256-gcm";
const arweave = Arweave.init({
host: 'arweave.net',
protocol: 'https'
});
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(),
]);
};
const getData = async (url, key) => {
const response = await arweave.api.get(url, { responseType: "arraybuffer" })
console.log(response.data)
const decrypted_data = decrypt(response.data, key)
console.log(decrypted_data)
return
}
const api = express()
const port = 3000
api.use(cors())
api.use(bodyParser.urlencoded({ extended: false }));
api.use(bodyParser.json());
api.post('/decrypt', (req, res) => {
const key = req.body.key
const url = req.body.url
const sliced_url = url.slice(20);
const decrypted_data = getData(sliced_url, key)
const b64 = decrypted_data.toString('base64');
const mimeType = 'image/png';
res.send(`<img src="data:${mimeType};base64,${b64}" />`);
})
api.listen(port, () => console.log("Server running on port 3000"))
Console.log(data):
<Buffer 10 4a 0e a9 bd b6 ca c9 e1 5d f2 2f 1f 03 3e b2 90 d8 c3 ed 7f 05 47 70 a2 11 c8 26 60 b9 70 33 ca b9 71 3a 56 68 7a b9 1b 37 c3 a2 ad 23 d2 7d 7b 85 ... 41534 more bytes>
Console.log(decrypted_data):
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 00 00 00 01 00 08 06 00 00 00 5c 72 a8 66 00 00 00 06 62 4b 47 44 00 ff 00 ff 00 ff a0 bd a7 ... 41501 more bytes>