4) AddChunks.js
Now we have successfully parsed through the tx data and got it into a format that is more readable and removed all the unnecessary information. You should see a file titledDigitalArtifactParsed.txt
Now we will take that text output we created and add only the bytes that we need.
Create a new file in your
MyDigitalArtifact
folder titledAddChunks.js
Insert the following code:
const fs = require('fs');
function extractAndSaveFile() {
// Read the opchunks.txt file
const opchunks = fs.readFileSync('DigitalArtifactParsed.txt', 'utf-8').split('\\n');
// Find the index of the OP_PUSHBYTES_17 line
const mimeTypeIndex = opchunks.findIndex(line => line.startsWith('OP_PUSHBYTES_17'));
// Check if MIME type was found
if (mimeTypeIndex === -1) {
console.error('MIME type line not found.');
return;
}
// Extract all the bytes from the line after MIME type till the end
const relevantLines = opchunks.slice(mimeTypeIndex + 1); // Start from the next line after MIME type
const concatenatedDataHex = relevantLines.map(line => line.split(' ')[1]).join('');
if (!concatenatedDataHex) {
console.error('No data found in relevant lines.');
return;
}
const concatenatedDataBytes = Buffer.from(concatenatedDataHex, 'hex');
// Save the data to a file
const outputFileName = `output.glb`;
fs.writeFileSync(outputFileName, concatenatedDataBytes);
console.log(`File has been saved as ${outputFileName}`);
}
extractAndSaveFile();
Run the command
node AddChunks.js
Last updated