Created
August 18, 2021 10:14
-
-
Save samcamwilliams/63a56d6cffc8a5ac67bfd668b31f142f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Arweave = require('arweave'); | |
const arweave = Arweave.init({ host: 'arweave.net', protocol: 'https', port: 443 }); | |
const fs = require('fs'); | |
const deepHash = require('arweave/node/lib/deepHash'); | |
const ArweaveBundles = require('arweave-bundles'); | |
const deps = { | |
utils: Arweave.utils, | |
crypto: Arweave.crypto, | |
deepHash: deepHash.default, | |
} | |
const arBundles = ArweaveBundles.default(deps); | |
const directory = './data/'; | |
const walletLoc = './key.json'; | |
let index = []; | |
let items = []; | |
let wallet = JSON.parse(fs.readFileSync(walletLoc)); | |
async function fileToDataEntry(fileLoc) { | |
console.log("Processing file: " + fileLoc); | |
let file = fs.readFileSync(directory + "/" + fileLoc); | |
let item = | |
await arBundles.createData( | |
{ data: file, tags: [{ name: 'Content-Type', value: "text/plain" }] }, | |
wallet | |
); | |
let signedItem = await arBundles.sign(item, wallet); | |
return signedItem; | |
} | |
async function generateBundle() { | |
let files = fs.readdirSync(directory); | |
for (const fileLoc of files) { | |
let entry = await fileToDataEntry(fileLoc); | |
items.push(entry); | |
index.push({file: fileLoc, id: entry.id}); | |
} | |
console.log("Generated " + items.length + " data entries!"); | |
console.log("Bundling and verifying..."); | |
const bundle = await arBundles.bundleData(items); | |
console.log("...bundled!"); | |
const bundleData = JSON.stringify(bundle); | |
console.log("Writing index..."); | |
await fs.writeFileSync("index.json", JSON.stringify(index)); | |
const tx = await arweave.createTransaction({ data: bundleData }, wallet); | |
tx.addTag('Bundle-Format', 'json'); | |
tx.addTag('Bundle-Version', '1.0.0'); | |
tx.addTag('Content-Type', 'application/json'); | |
console.log("Posting bundle to the network..."); | |
await arweave.transactions.sign(tx, wallet); | |
console.log(tx); | |
let uploader = await arweave.transactions.getUploader(tx); | |
while (!uploader.isComplete) { | |
await uploader.uploadChunk(); | |
console.log(`${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`); | |
} | |
} | |
generateBundle(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment