Skip to content

Commit

Permalink
Merge pull request #132 from hackdays-io/issue/123
Browse files Browse the repository at this point in the history
Issue/123
  • Loading branch information
yu23ki14 authored Oct 3, 2024
2 parents 5be8d9c + de45a67 commit cd6588e
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkgs/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
"@hatsprotocol/sdk-v1-subgraph": "^1.0.0",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"pinata-web3": "^0.5.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.2",
"viem": "^2.21.15"
"viem": "^2.21.15",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/commander": "^2.12.2",
Expand Down
1 change: 1 addition & 0 deletions pkgs/cli/src/commands/hats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createHat,
mintHat,
} from "../modules/hatsProtocol";
import { PinataSDK } from "pinata-web3";
import { getAccount } from "../services/wallet";
import { publicClient, rootProgram, walletClient } from "..";
import { Address } from "viem";
Expand Down
111 changes: 111 additions & 0 deletions pkgs/cli/src/commands/pinata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Command } from "commander";
import { getJwt, setJwt } from "../services/pinata";
import { PinataSDK } from "pinata-web3";

export const pinataCommands = new Command();

// ###############################################################
// CLI init setup
// ###############################################################

pinataCommands
.name("pinata")
.description("This is a CLI for pinata")
.version("1.0.0");

/**
* PinataのJWTを設定するコマンド
*/
pinataCommands
.command("setJwt")
.description("Set a jwt of Pinata")
.requiredOption("--jwt <JWT>")
.action(({ jwt }) => {
setJwt(jwt);
});

/**
* Hatのメタデータをipfs上にアップロードするコマンド
*/
interface Responsibility {
label: string;
description?: string;
link?: string;
}
interface Eligibility {
manual: boolean;
criteria: string[];
}
interface Toggle {
manual: boolean;
criteria: string[];
}

pinataCommands
.command("uploadMetadata")
.description("Upload the hat metadata on ipfs.")
.requiredOption("-n, --name <name>", "Hat Name")
.requiredOption("-d, --description <description>", "Hat Details")
.option(
"-r, --responsibilities <label>,<description>,<link>",
"Responsibilities (may be specified multiple times to define multiple responsibilities)",
(value, previous: Responsibility[]) => {
const [label, description, link] = value.split(",");
return previous
? previous.concat([{ label, description, link }])
: [{ label, description, link }];
},
[]
)
.option(
"-a, --authorities <authorities>",
"Authority (may be specified multiple times to define multiple authorities)",
(value, previous: string[]) =>
previous ? previous.concat([value]) : [value],
[]
)
.option(
"-e, --eligibility <manual>,<criteria...>",
"Eligibility (<manual> is a boolean value, <criteria... > can be specified multiple times, separated by commas, to define multiple criteria.)",
(value) => {
const [manual, ...criteria] = value.split(",");
return { manual: manual === "true", criteria } satisfies Eligibility;
}
)
.option(
"-t, --toggle <manual> <criteria...>",
"Toggle (<manual> is a boolean value, <criteria... > can be specified multiple times, separated by spaces, to define multiple criteria.)",
(value) => {
const [manual, ...criteria] = value.split(",");
return { manual: manual === "true", criteria } satisfies Toggle;
}
)
.action(
async ({
name,
description,
responsibilities,
authorities,
eligibility,
toggle,
}) => {
const { jwt } = getJwt();

const pinata = new PinataSDK({ pinataJwt: jwt });

const upload = await pinata.upload.json({
type: "1.0",
data: {
name,
description,
responsibilities,
authorities: authorities,
eligibility,
toggle,
},
});

console.log("CID:", upload.IpfsHash);
console.log("URI:", `ipfs://${upload.IpfsHash}`);
}
);
2 changes: 2 additions & 0 deletions pkgs/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getPublicClient } from "./modules/viem";
import { getWalletClient } from "./services/wallet";
import { skipPreActionCommands } from "./config";
import { bigbangCommands } from "./commands/bigbang";
import { pinataCommands } from "./commands/pinata";

export const rootProgram = new Command();

Expand All @@ -33,5 +34,6 @@ rootProgram
rootProgram.addCommand(bigbangCommands);
rootProgram.addCommand(hatsCommands);
rootProgram.addCommand(walletCommands);
rootProgram.addCommand(pinataCommands);

rootProgram.parse(process.argv);
20 changes: 20 additions & 0 deletions pkgs/cli/src/services/pinata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { existsSync, readFileSync, writeFileSync } from "fs";
import path from "path";
const pinataPath = path.join(__dirname, "pinata.json");

export interface Pinata {
jwt: string
}

export const getJwt = () => {
if (!existsSync(pinataPath)) {
writeFileSync(pinataPath, JSON.stringify({ jwt: "" }));
}

const data = readFileSync(pinataPath, "utf8");
return JSON.parse(data) as Pinata;
};

export const setJwt = (jwt: string) => {
writeFileSync(pinataPath, JSON.stringify({ jwt: jwt }));
}
Loading

0 comments on commit cd6588e

Please sign in to comment.