Skip to content

Commit

Permalink
Add attestation registry for metadata uploads (#58)
Browse files Browse the repository at this point in the history
* Stub manifest creation & registry upload command

* Parameterize upload

---------

Co-authored-by: Seth Feibus <sefeibus@gmail.com>
  • Loading branch information
corydickson and sethfork authored Mar 11, 2024
1 parent b3c5353 commit 472b7bf
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/env", "@babel/preset-react"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build/
node_modules/
.env
dist/
gateway/dist/
10 changes: 10 additions & 0 deletions gateway/config/webpack.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ module.exports = () => {
// // sourceMap: false,
// // },
// },
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
{
test: /\.(scss|css)$/,
use: [
Expand Down
18 changes: 17 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { deploy_app, deploy_data } = require("./deploy");
const {
deploy_app,
deploy_data,
deploy_data_with_metadata,
} = require("./deploy");
const { pull, clone, remove } = require("./repository");
const { version, name, description } = require("../package.json");
const { Command } = require("commander");
Expand Down Expand Up @@ -39,6 +43,14 @@ async function run() {
.action((appName) => {
deployCLI(appName);
});
program
.command("attest")
.description("Deploy the project with metadata")
.argument("[app_name]", "app name")
.argument("[version]", "version")
.action((appName) => {
deployCLIMetadata(appName);
});
program
.command("upload")
.description("Upload data to SocialDB")
Expand Down Expand Up @@ -77,6 +89,10 @@ function deployCLI(appName) {
appSelectorCLI(deploy_app, appName);
}

function deployCLIMetadata(appName, version) {
appSelectorCLI(deploy_data_with_metadata, appName, version);
}

function uploadDataCLI(appName) {
appSelectorCLI(deploy_data, appName);
}
Expand Down
89 changes: 89 additions & 0 deletions lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { read_bos_config } = require("./config");
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const { uploadToIpfs } = require("./ipfs");

const distFolder = process.env.DIST_FOLDER || "build";

Expand Down Expand Up @@ -42,6 +43,93 @@ function deploy_app(appFolder) {
}
}

async function deploy_data_with_metadata(appFolder, version) {
const config = read_bos_config(appFolder);
const appAccount = config.appAccount;

if (!appAccount) {
console.error(
`App account is not defined for ${appFolder}. Skipping data upload.`,
);
return;
}

const dataJSON = fs.readFileSync(
path.join(distFolder, appFolder, "data.json"),
"utf8",
);

let content = {
data: {
[appAccount]: JSON.parse(dataJSON),
},
};

try {
const metadataJSON = fs.readFileSync(
path.join(distFolder, appFolder, "metadata.json"),
"utf8",
);
content.metadata = JSON.parse(metadataJSON);

} catch (error) {
console.log(error.message);
console.log(`Skipping metadata pinning...`);
}


const formattedContent = [content];
const ipfsHash = await uploadToIpfs(formattedContent);
try {
console.log(`Uploaded data for ${appFolder} with ipfs hash: ${ipfsHash}`);
} catch (error) {
console.log(
`Error uploading data for ${appFolder} with ipfs hash: ${ipfsHash}`,
);
console.error(`Error uploading data for ${appFolder}:\n${error.message}`);
}

const manifest = {
package_name: `${appFolder}`,
version: `${version}`,
content_type: "ipfs",
cid: ipfsHash.toString(),
is_contract: true,
};

const argsBase64 = Buffer.from(JSON.stringify(manifest)).toString("base64");
const packageRoot = path.resolve(__dirname, "..");
const nearBinaryPath = path.join(packageRoot, "node_modules", ".bin", "near");
const REGISTRY_ADDRESS = "efficacious-mother.testnet";

const command = [
nearBinaryPath,
"contract",
"call-function",
"as-transaction",
REGISTRY_ADDRESS,
"create_manifest",
"base64-args",
`'${argsBase64}'`,
"prepaid-gas",
"'300.000 TeraGas'",
"sign-as",
appAccount,
"network-config",
"testnet",
].join(" ");

try {
execSync(command, {
cwd: path.join(distFolder, appFolder),
stdio: "inherit",
});
console.log(`Uploaded data for ${appFolder}`);
} catch (error) {
console.error(`Error uploading data for ${appFolder}:\n${error.message}`);
}
}

function deploy_data(appFolder) {
const config = read_bos_config(appFolder);
const appAccount = config.appAccount;
Expand Down Expand Up @@ -101,4 +189,5 @@ function deploy_data(appFolder) {
module.exports = {
deploy_app,
deploy_data,
deploy_data_with_metadata,
};
17 changes: 17 additions & 0 deletions lib/ipfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
async function uploadToIpfs(content) {
const helia = await createHelia();
const heliaInstance = json(helia);

const cid = await heliaInstance.add(content);
try {
console.log("====================================");
console.log("Added file:", cid);
console.log("Added file:", cid.toString());
console.log("====================================");
return cid;
} catch (e) {
throw new Error(`Error uploading content to IPFS: ${e}`);
}
}

module.exports = { uploadToIpfs };

0 comments on commit 472b7bf

Please sign in to comment.