Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Oct 3, 2024
1 parent 559caf1 commit de45a67
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 94 deletions.
91 changes: 0 additions & 91 deletions pkgs/cli/src/commands/hats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
mintHat,
} from "../modules/hatsProtocol";
import { PinataSDK } from "pinata-web3";
import { getJwt, setJwt } from "../services/hats";
import { getAccount } from "../services/wallet";
import { publicClient, rootProgram, walletClient } from "..";
import { Address } from "viem";
Expand Down Expand Up @@ -73,96 +72,6 @@ hatsCommands
console.log(wearer);
});

/**
* PinataのJWTを設定するコマンド
*/
hatsCommands
.command("pinata")
.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[];
}
hatsCommands
.command("upload")
.description("Upload the hat metadata on ipfs.")
.requiredOption("-n, --name <name>", "Hat Name")
.option("-d, --description <description>", "Hat Details")
.option(
"-r, --responsibility <label>,<description>,<link>",
"Responsibility (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, --authority <authority>",
"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,
responsibility,
authority,
eligibility,
toggle,
}) => {
const { jwt } = getJwt();

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

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

console.log("CID:", upload.IpfsHash);
});

/**
* ロールを作成
*/
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);
File renamed without changes.
31 changes: 28 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5504,7 +5504,7 @@ streamsearch@^1.1.0:
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-1.1.0.tgz#404dd1e2247ca94af554e841a8ef0eaa238da764"
integrity sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand All @@ -5521,6 +5521,15 @@ string-width@^2.1.1:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"

string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^5.0.1, string-width@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794"
Expand Down Expand Up @@ -5606,7 +5615,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -5620,6 +5629,13 @@ strip-ansi@^4.0.0:
dependencies:
ansi-regex "^3.0.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -6158,7 +6174,16 @@ workerpool@^6.5.1:
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.5.1.tgz#060f73b39d0caf97c6db64da004cd01b4c099544"
integrity sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down

0 comments on commit de45a67

Please sign in to comment.