Skip to content

Commit

Permalink
hats pinata --jwt <JWT>でpinataのJWTを各自で設定できる機能を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
aowheel committed Oct 3, 2024
1 parent 8b23938 commit af1f7b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkgs/cli/src/commands/hats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import {
getWearersInfo,
} from "../modules/hatsProtocol";
import { PinataSDK } from "pinata-web3";
import { config } from "dotenv";

config();
import { getJwt, setJwt } from "../services/hats";

export const hatsCommands = new Command();

Expand Down Expand Up @@ -66,6 +64,17 @@ hatsCommands
console.log(wearer);
});

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

/**
* Hatのメタデータをipfs上にアップロードするコマンド
*/
Expand Down Expand Up @@ -126,7 +135,9 @@ hatsCommands
eligibility,
toggle,
}) => {
const pinata = new PinataSDK({ pinataJwt: process.env.PINATA_JWT });
const { jwt } = getJwt();

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

const upload = await pinata.upload.json({
"type": "1.0",
Expand Down
20 changes: 20 additions & 0 deletions pkgs/cli/src/services/hats.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 }));
}

0 comments on commit af1f7b7

Please sign in to comment.