From af1f7b72d3b1d380233d7a78306a98207ec09d85 Mon Sep 17 00:00:00 2001 From: aowheel Date: Thu, 3 Oct 2024 21:08:32 +0900 Subject: [PATCH] =?UTF-8?q?hats=20pinata=20--jwt=20=E3=81=A7pinata?= =?UTF-8?q?=E3=81=AEJWT=E3=82=92=E5=90=84=E8=87=AA=E3=81=A7=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=81=A7=E3=81=8D=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/cli/src/commands/hats.ts | 19 +++++++++++++++---- pkgs/cli/src/services/hats.ts | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 pkgs/cli/src/services/hats.ts diff --git a/pkgs/cli/src/commands/hats.ts b/pkgs/cli/src/commands/hats.ts index 1066d87..2f2cbeb 100644 --- a/pkgs/cli/src/commands/hats.ts +++ b/pkgs/cli/src/commands/hats.ts @@ -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(); @@ -66,6 +64,17 @@ hatsCommands console.log(wearer); }); +/** + * PinataのJWTを設定するコマンド + */ +hatsCommands + .command("pinata") + .description("Set a jwt of Pinata") + .requiredOption("--jwt ") + .action(({ jwt }) => { + setJwt(jwt); + }); + /** * Hatのメタデータをipfs上にアップロードするコマンド */ @@ -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", diff --git a/pkgs/cli/src/services/hats.ts b/pkgs/cli/src/services/hats.ts new file mode 100644 index 0000000..5a90f41 --- /dev/null +++ b/pkgs/cli/src/services/hats.ts @@ -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 })); +}