Skip to content

Instantly share code, notes, and snippets.

@rcaetano
Last active October 7, 2022 11:29
Show Gist options
  • Save rcaetano/20edba79458de95e96850a3d06ae9963 to your computer and use it in GitHub Desktop.
Save rcaetano/20edba79458de95e96850a3d06ae9963 to your computer and use it in GitHub Desktop.
vault2ipfs - script to push your Akord Stacks to IPFS
{
"name": "vault-dump",
"version": "0.1.0",
"private": true,
"type": "module",
"dependencies": {
"@akord/akord-js": "^1.4.1",
"dotenv": "^16.0.3",
"ipfs-http-client": "^58.0.1"
},
"scripts": {
"go": "node vault2ipfs.js",
"clean": "rm -rf node_modules yarn-error.log yarn.lock"
}
}
import { Akord } from "@akord/akord-js";
import { create } from "ipfs-http-client";
import * as dotenv from "dotenv";
dotenv.config();
// need to be running a local IPFS API
// > ipfs daemon
const ipfs = create();
// now we'll iterate the stacks in the vault and push the files to IPFS
(async () => {
const uploads = [];
try {
const { akord } = await Akord.auth.signIn(
process.env.EMAIL,
process.env.PASS
);
const stacks = await akord.stack.list(process.env.VAULTID);
// push stacks to ipfs
for (var i in stacks) {
const stack = stacks[i];
const { data: fileBuffer, name: fileName } = await akord.stack.getFile(
stack.id
);
const { cid } = await ipfs.add(fileBuffer);
uploads.push({
stack,
cid: cid.toString(),
});
console.log(fileName, "->", cid);
}
console.log(
JSON.stringify(
{
vaultId: process.env.VAULTID,
stacks: uploads,
},
null,
2
)
);
} catch (e) {
console.error(e);
}
})();

vault2ipfs.js

A simple script to push the stacks in your Akord Vault to IPFS.

IPFS Node

You'll need to run a local IPFS node to use this script.

brew install ipfs
ipfs daemon

Setup the Script

The script reads your email/password and vault id from a .env config file.

NOTE: Make sure you have an account with http://v2.akord.com

touch .env
echo "EMAIL='-- email --'" >> .env
echo "PASS='-- password --'" >> .env
echo "VAULTID='-- vault id --'" >> .env

Then install the npm modules:

yarn install

Run the script

You should be ready to run the script now:

yarn go

Access your files from IPFS

Once the script completes, it will dump a JSON file to console with the IFPS CID's attached to each stack:

{
  "vaultId": "ebKjhDPQ7VZy5bK8W8Qy0BKmRJHoZFLfD_DsHlJHK0I",
  "stacks": [
    {
      "stack": {
        "id": "69fd2d6d-f53e-4215-b667-fee6964c807f",
        "createdAt": "2022-10-05T14:23:09.515Z",
        "status": "ACTIVE",
        "title": "my-photo.jpg",
        "description": null,
        "resourceVersion": 1,
        "size": null,
        "files": [
          {
            "title": "my-photo.jpg",
            "resourceUrl": "public/a99b2501-d24f-4d1d-b883-dae92c790b20",
            "thumbnailUrl": "public/da831fec-d2c6-4d0f-9c29-49d12363f435",
            "postedAt": null,
            "fileType": "image/jpeg",
            "size": 42192,
            "numberOfChunks": null,
            "chunkSize": null,
            "hash": "3cb1fbcb7035-54a5-4aa0-9526-3cbcb7035",
            "resourceTx": "Mr_6ozOs6TwtFRRlPlG1YJwikBdIaG89387x9-Zeikg",
            "thumbnailTx": null
          }
        ]
      },
      "cid": "Qmp6jVm5mYstmd4ZjYk3UC4FQy5REQ19uAwkbo4617iD94"
    }
  ]
}

Next you can test by opening the file:

ipfs cat Qmp6jVm5mYstmd4ZjYk3UC4FQy5REQ19uAwkbo4617iD94 | open -a Preview.app -f

Feedback

We'd love to hear from you : https://discord.com/channels/890229689019432981/982206047227871262

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment