-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2270 from zeitgeistpm/protected-ipfs-api
Protected IPFS api
- Loading branch information
Showing
9 changed files
with
327 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { u8aToString } from "@polkadot/util"; | ||
import { u8aConcat } from "@polkadot/util/u8a"; | ||
import { | ||
MarketMetadata, | ||
MetadataStorage, | ||
createStorage, | ||
} from "@zeitgeistpm/sdk"; | ||
import { Codec, JsonCodec } from "@zeitgeistpm/utility/dist/codec"; | ||
import * as O from "@zeitgeistpm/utility/dist/option"; | ||
import * as Te from "@zeitgeistpm/utility/dist/taskeither"; | ||
import { Storage, StorageError } from "@zeitgeistpm/web3.storage"; | ||
import * as IPFSHTTPClient from "ipfs-http-client"; | ||
import { CID } from "multiformats/cid"; | ||
|
||
const node = IPFSHTTPClient.create({ | ||
url: process.env.NEXT_PUBLIC_IPFS_NODE_URL, | ||
}); | ||
|
||
export const createMetadataStorage = (): MetadataStorage<MarketMetadata> => { | ||
const createInnerStorage = ( | ||
codec: Codec< | ||
string | Uint8Array, | ||
MarketMetadata | ||
> = JsonCodec<MarketMetadata>(), | ||
): Storage<MarketMetadata, CID> => { | ||
return { | ||
put: Te.from( | ||
async (data) => { | ||
const response = await fetch("/api/ipfs", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
|
||
if (response.status === 400) { | ||
const { message } = await response.json(); | ||
throw new Error(message); | ||
} | ||
|
||
const { cid: cidString } = await response.json(); | ||
const cid = CID.parse(cidString); | ||
|
||
return cid; | ||
}, | ||
(message, error) => new StorageError(message, error), | ||
), | ||
hash: Te.from( | ||
async (data) => { | ||
const response = await fetch("/api/ipfs?only-hash=true", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
|
||
if (response.status === 400) { | ||
const { message } = await response.json(); | ||
throw new Error(message); | ||
} | ||
|
||
const { cid: cidString } = await response.json(); | ||
const cid = CID.parse(cidString); | ||
|
||
return cid; | ||
}, | ||
(message, error) => new StorageError(message, error), | ||
), | ||
get: Te.from( | ||
async (cid) => { | ||
const data = (await read(node, cid).unwrap()) | ||
.map((chunks) => u8aConcat(...chunks)) | ||
.unwrap(); | ||
|
||
if (data) { | ||
const parsed = JSON.parse(u8aToString(data)); | ||
return O.option(O.some(parsed)); | ||
} | ||
|
||
return O.option(O.none()); | ||
}, | ||
(message, error) => new StorageError(message, error), | ||
), | ||
withCodec: <A>() => createInnerStorage(codec) as Storage<A, CID>, | ||
provider: node, | ||
}; | ||
}; | ||
|
||
return createStorage(createInnerStorage()); | ||
}; | ||
|
||
/** | ||
* Read data from a cid and parse it to a string. | ||
*/ | ||
const read = Te.from< | ||
O.IOption<Uint8Array[]>, | ||
Error, | ||
[node: IPFSHTTPClient.IPFSHTTPClient, cid: IPFSHTTPClient.CID] | ||
>(async (node, cid) => { | ||
let content: Uint8Array[] = []; | ||
|
||
for await (const chunk of node.cat(cid)) { | ||
content = [...content, chunk]; | ||
} | ||
|
||
new Blob(content); | ||
|
||
if (content.length === 0) return O.option(O.none()); | ||
|
||
return O.option(O.some(content)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { create as createIPFSClient } from "ipfs-http-client"; | ||
import type { PageConfig } from "next"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { fromZodError } from "zod-validation-error"; | ||
import { IOMarketMetadata } from "./types"; | ||
|
||
export const config: PageConfig = { | ||
runtime: "nodejs", | ||
}; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
if (req.method === "POST") { | ||
return POST(req, res); | ||
} | ||
} | ||
|
||
const POST = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const node = createIPFSClient({ | ||
url: process.env.NEXT_PUBLIC_IPFS_NODE_URL, | ||
headers: { | ||
Authorization: `Basic ${Buffer.from( | ||
process.env.IPFS_NODE_BASIC_AUTH_USERNAME + | ||
":" + | ||
process.env.IPFS_NODE_BASIC_AUTH_PASSWORD, | ||
).toString("base64")}`, | ||
}, | ||
}); | ||
|
||
const parsed = IOMarketMetadata.safeParse(req.body); | ||
|
||
const onlyHash = req.query["only-hash"] === "true" ? true : false; | ||
|
||
if (!parsed.success) { | ||
return res.status(400).json({ | ||
message: fromZodError(parsed.error).toString(), | ||
}); | ||
} | ||
|
||
const metadata = { | ||
__meta: "markets", | ||
...parsed.data, | ||
}; | ||
|
||
const content = JSON.stringify(metadata); | ||
const kbSize = Buffer.byteLength(content) / 1024; | ||
|
||
if (kbSize > 10) { | ||
return res.status(400).json({ | ||
message: "Market metadata is too large. Please keep it under 10kb.", | ||
}); | ||
} | ||
try { | ||
const { cid } = await node.add( | ||
{ content }, | ||
{ | ||
hashAlg: "sha3-384", | ||
pin: !onlyHash, | ||
onlyHash, | ||
}, | ||
); | ||
|
||
// if (!onlyHash) { | ||
// await node.pin.add(cid); | ||
// } | ||
|
||
return res.status(200).json({ | ||
message: `Market metadata ${ | ||
onlyHash ? "hashed" : "pinned" | ||
} successfully.`, | ||
cid: cid.toString(), | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
message: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
// const DELETE = async (req: NextRequest) => { | ||
// const { cid } = JSON.parse(await extractBody(req)); | ||
|
||
// try { | ||
// await node.pin.rm(IPFSHTTPClient.CID.parse(cid)); | ||
|
||
// return new Response( | ||
// JSON.stringify({ | ||
// message: `Market metadata(cid: ${cid}) unpinned successfully.`, | ||
// }), | ||
// { | ||
// status: 200, | ||
// }, | ||
// ); | ||
// } catch (error) { | ||
// return new Response( | ||
// JSON.stringify({ | ||
// message: error.message, | ||
// }), | ||
// { | ||
// status: 500, | ||
// }, | ||
// ); | ||
// } | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as z from "zod"; | ||
|
||
export const IOMarketMetadata = z.object({ | ||
question: z.string(), | ||
description: z.optional(z.string()), | ||
tags: z.optional(z.array(z.string())), | ||
slug: z.optional(z.string()), | ||
categories: z.optional( | ||
z.array( | ||
z.object({ | ||
name: z.string(), | ||
ticker: z.optional(z.string()), | ||
img: z.optional(z.string()), | ||
color: z.optional(z.string()), | ||
}), | ||
), | ||
), | ||
}); |
Oops, something went wrong.