-
Notifications
You must be signed in to change notification settings - Fork 35
feat(spore): dob-render-sdk migration #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 10 commits
def2273
600a36e
a077bdd
694ae83
e2fba3d
2204c4f
4bb1040
19962f3
4d0934d
04a9592
cc3f59a
7c4e4f0
5260933
f3a303b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@ckb-ccc/spore": minor | ||
| --- | ||
|
|
||
| Migrate dob-render-sdk directly into spore module | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { spore } from "@ckb-ccc/ccc"; | ||
|
|
||
| const sporeId = | ||
| "dc19e68af1793924845e2a4dbc23598ed919dcfe44d3f9cd90964fe590efb0e4"; | ||
|
|
||
| const dobRender = await spore.dob.renderByTokenKey(sporeId); | ||
| console.log(dobRender); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,12 @@ | |
| }, | ||
| "dependencies": { | ||
| "@ckb-ccc/core": "workspace:*", | ||
| "axios": "^1.11.0" | ||
| "axios": "^1.11.0", | ||
| "satori": "^0.10.13", | ||
| "svgson": "^5.3.1" | ||
| }, | ||
| "peerDependencies": { | ||
| "satori": "^0.10.13" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }, | ||
|
Comment on lines
+65
to
67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "packageManager": "pnpm@10.8.1" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { describe, it } from "vitest"; | ||
| import { renderByTokenKey } from "../dob/index.js"; | ||
|
|
||
| describe("decodeDob [testnet]", () => { | ||
| it("should respose a decoded dob render data from a spore id", async () => { | ||
| // The spore id that you want to decode (must be a valid spore dob) | ||
| const sporeId = | ||
| "dc19e68af1793924845e2a4dbc23598ed919dcfe44d3f9cd90964fe590efb0e4"; | ||
|
|
||
| // Decode from spore id | ||
| const dob = await renderByTokenKey(sporeId); | ||
| console.log(dob); | ||
| }, 60000); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case lacks assertions. A test should verify that the code behaves as expected, but this one only logs the result to the console. It will pass as long as no error is thrown. Please add
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case uses Additionally, the test description "should respose" contains a typo and should be "should respond". it("should respond with decoded DOB render data from a spore id", async () => {
// The spore id that you want to decode (must be a valid spore dob)
const sporeId =
"dc19e68af1793924845e2a4dbc23598ed919dcfe44d3f9cd90964fe590efb0e4";
// Decode from spore id
const dob = await renderByTokenKey(sporeId);
expect(dob).toBeDefined();
// TODO: Add more specific assertions about the 'dob' content to make this a more effective test.
}, 60000);
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is missing assertions to verify the output. It currently only checks that it("should respond with a decoded dob render data from a spore id", async () => {
// The spore id that you want to decode (must be a valid spore dob)
const sporeId =
"dc19e68af1793924845e2a4dbc23598ed919dcfe44d3f9cd90964fe590efb0e4";
// Decode from spore id
const dob = await renderByTokenKey(sporeId);
console.log(dob);
// TODO: Add assertions to validate the `dob` content.
// For example: expect(dob).toContain('<svg');
}, 60000); |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./api/index.js"; | ||
| export * from "./config/index.js"; | ||
| export * from "./helper/index.js"; | ||
| export * from "./render/index.js"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from "./renderDobDecode.js"; | ||
| export * from "./renderToken.js"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { RenderOutput } from "../../helper/object.js"; | ||
| import { Key } from "../config/constants.js"; | ||
| import { renderTextParamsParser } from "../core/parsers/textParamsParser.js"; | ||
| import { traitsParser } from "../core/parsers/traitsParser.js"; | ||
| import { renderDob1Svg } from "../core/renderers/dob1Render.js"; | ||
| import { renderImageSvg } from "../core/renderers/imageRender.js"; | ||
| import type { RenderProps } from "../core/renderers/textRender.js"; | ||
| import { renderTextSvg } from "../core/renderers/textRender.js"; | ||
|
|
||
| export function renderByDobDecodeResponse( | ||
| renderOutput: RenderOutput | string, | ||
| props?: Pick<RenderProps, "font"> & { | ||
| outputType?: "svg"; | ||
| }, | ||
| ) { | ||
| let renderData: RenderOutput; | ||
| if (typeof renderOutput === "string") { | ||
| renderData = JSON.parse(renderOutput) as RenderOutput; | ||
| } else { | ||
| renderData = renderOutput; | ||
| } | ||
|
|
||
| const { traits, indexVarRegister } = traitsParser(renderData); | ||
| for (const trait of traits) { | ||
| if (trait.name === String(Key.Type) && trait.value === "image") { | ||
| return renderImageSvg(traits); | ||
| } | ||
| // TODO: multiple images | ||
| if (trait.name === String(Key.Image) && trait.value instanceof Promise) { | ||
| return renderDob1Svg(trait.value); | ||
| } | ||
| } | ||
| const renderOptions = renderTextParamsParser(traits, indexVarRegister); | ||
| return renderTextSvg({ ...renderOptions, font: props?.font }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { decodeDobBySporeId } from "../../api/decode.js"; | ||
| import { config } from "../config.js"; | ||
| import type { RenderProps } from "../core/renderers/textRender.js"; | ||
| import { renderByDobDecodeResponse } from "./renderDobDecode.js"; | ||
|
|
||
| export async function renderByTokenKey( | ||
| tokenKey: string, | ||
| options?: Pick<RenderProps, "font"> & { | ||
| outputType?: "svg"; | ||
| }, | ||
| ) { | ||
| const renderOutput = await decodeDobBySporeId( | ||
| tokenKey, | ||
| config.dobDecodeServerURL, | ||
| ); | ||
|
|
||
| return renderByDobDecodeResponse(renderOutput, options); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| export type FileServerResult = | ||
| | string | ||
| | { | ||
| content: string; | ||
| content_type: string; | ||
| }; | ||
|
|
||
| export type BtcFsResult = FileServerResult; | ||
| export type IpfsResult = FileServerResult; | ||
|
|
||
| export type BtcFsURI = `btcfs://${string}`; | ||
| export type IpfsURI = `ipfs://${string}`; | ||
|
|
||
| export type QueryBtcFsFn = (uri: BtcFsURI) => Promise<BtcFsResult>; | ||
| export type QueryIpfsFn = (uri: IpfsURI) => Promise<IpfsResult>; | ||
| export type QueryUrlFn = (uri: string) => Promise<FileServerResult>; | ||
|
|
||
| export class Config { | ||
| private _dobDecodeServerURL = "https://dob-decoder.ckbccc.com"; | ||
| private _queryBtcFsFn: QueryBtcFsFn = async (uri) => { | ||
| console.log("dob-render-sdk requiring", uri); | ||
| const response = await fetch( | ||
| `https://dob-decoder.ckbccc.com/restful/dob_extract_image?uri=${uri}&encode=base64`, | ||
| ); | ||
| const text = await response.text(); | ||
| return { | ||
| content: text, | ||
| content_type: "", | ||
| }; | ||
| }; | ||
|
|
||
| private _queryUrlFn = async (url: string) => { | ||
| console.log("dob-render-sdk requiring", url); | ||
| const response = await fetch(url); | ||
| const blob = await response.blob(); | ||
| return new Promise<IpfsResult>((resolve, reject) => { | ||
| const reader = new FileReader(); | ||
|
|
||
| reader.onload = function () { | ||
| const base64 = this.result as string; | ||
| resolve(base64); | ||
| }; | ||
| reader.onerror = (error) => { | ||
| reject(new Error(`FileReader error: ${error.type}`)); | ||
| }; | ||
| reader.readAsDataURL(blob); | ||
| }); | ||
| }; | ||
|
|
||
| private _queryIpfsFn = async (uri: IpfsURI) => { | ||
| const key = uri.substring("ipfs://".length); | ||
| const url = `https://ipfs.io/ipfs/${key}`; | ||
| return this._queryUrlFn(url); | ||
| }; | ||
|
|
||
| get dobDecodeServerURL() { | ||
| return this._dobDecodeServerURL; | ||
| } | ||
|
|
||
| setDobDecodeServerURL(dobDecodeServerURL: string): void { | ||
| this._dobDecodeServerURL = dobDecodeServerURL; | ||
| } | ||
|
|
||
| setQueryBtcFsFn(fn: QueryBtcFsFn): void { | ||
| this._queryBtcFsFn = fn; | ||
| } | ||
|
|
||
| setQueryIpfsFn(fn: QueryIpfsFn): void { | ||
| this._queryIpfsFn = fn; | ||
| } | ||
|
|
||
| get queryBtcFsFn(): QueryBtcFsFn { | ||
| return this._queryBtcFsFn; | ||
| } | ||
|
|
||
| get queryIpfsFn(): QueryIpfsFn { | ||
| return this._queryIpfsFn; | ||
| } | ||
|
|
||
| get queryUrlFn(): QueryUrlFn { | ||
| return this._queryUrlFn; | ||
| } | ||
| } | ||
|
|
||
| export const config = new Config(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export enum Key { | ||
| Bg = "prev.bg", | ||
| Type = "prev.type", | ||
| BgColor = "prev.bgcolor", | ||
| Prev = "prev", | ||
| Image = "IMAGE", | ||
| } | ||
|
|
||
| export const ARRAY_REG = /\(%(.*?)\):(\[.*?\])/; | ||
| export const ARRAY_INDEX_REG = /(\d+)<_>$/; | ||
| export const GLOBAL_TEMPLATE_REG = /^prev<(.*?)>/; | ||
| export const TEMPLATE_REG = /^(.*?)<(.*?)>/; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import SpaceGroteskBoldBase64 from "../fonts/spaceGroteskBold.base64.js"; | ||
| import TurretRoadBoldBase64 from "../fonts/turretRoadBold.base64.js"; | ||
| import TurretRoadMediumBase64 from "../fonts/turretRoadMedium.base64.js"; | ||
|
|
||
| export const FONTS = { | ||
| SpaceGroteskBold: SpaceGroteskBoldBase64, | ||
| TurretRoadBold: TurretRoadBoldBase64, | ||
| TurretRoadMedium: TurretRoadMediumBase64, | ||
| } as const; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { config } from "../config.js"; | ||
| export * from "./constants.js"; | ||
| export * from "./fonts.js"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from "./parsers/index.js"; | ||
| export * from "./renderers/index.js"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Key } from "../../config/constants.js"; | ||
| import type { ParsedTrait } from "../../types/core.js"; | ||
|
|
||
| export function getBackgroundColorByTraits( | ||
| traits: ParsedTrait[], | ||
| ): ParsedTrait | undefined { | ||
| return traits.find((trait) => trait.name === String(Key.BgColor)); | ||
| } | ||
|
|
||
| export function backgroundColorParser( | ||
| traits: ParsedTrait[], | ||
| options?: { | ||
| defaultColor?: string; | ||
| }, | ||
| ): string { | ||
| const bgColorTrait = getBackgroundColorByTraits(traits); | ||
| if (bgColorTrait) { | ||
| if (typeof bgColorTrait.value === "string") { | ||
| if ( | ||
| bgColorTrait.value.startsWith("#(") && | ||
| bgColorTrait.value.endsWith(")") | ||
| ) { | ||
| return bgColorTrait.value.replace("#(", "linear-gradient("); | ||
| } | ||
| return bgColorTrait.value; | ||
| } | ||
| } | ||
| return options?.defaultColor || "#000"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export * from "./backgroundColorParser.js"; | ||
| export * from "./styleParser.js"; | ||
| export * from "./textParamsParser.js"; | ||
| export * from "./traitsParser.js"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is an example file, using top-level
awaitcan limit its reusability in environments that don't support it. It would be more robust to wrap this logic in anasyncfunction.