-
Notifications
You must be signed in to change notification settings - Fork 31
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 all 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 @@ | ||
--- | ||
"@ckb-ccc/dob-render": patch | ||
"@ckb-ccc/spore": patch | ||
--- | ||
|
||
Fix decoder responsed value type mismatch and migrate dob-render-sdk to ccc | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,8 @@ | |
"@ckb-ccc/shell": "workspace:*", | ||
"@ckb-ccc/uni-sat": "workspace:*", | ||
"@ckb-ccc/utxo-global": "workspace:*", | ||
"@ckb-ccc/xverse": "workspace:*" | ||
"@ckb-ccc/xverse": "workspace:*", | ||
"@ckb-ccc/dob-render": "workspace:*" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# @ckb-ccc/dob-render | ||
|
||
CCC - CKBer's Codebase. Common Chains Connector's render SDK for DOB protocol. | ||
|
||
This package provides rendering capabilities for DOB (Decentralized Object) protocol, allowing you to render DOB tokens as SVG images. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @ckb-ccc/dob-render | ||
``` | ||
|
||
## Usage | ||
|
||
```typescript | ||
import { | ||
renderByTokenKey, | ||
renderByDobDecodeResponse, | ||
} from "@ckb-ccc/dob-render"; | ||
|
||
// Render by token key | ||
const svg = await renderByTokenKey("your-token-key"); | ||
|
||
// Render by DOB decode response | ||
const svg = renderByDobDecodeResponse(renderOutput); | ||
``` | ||
|
||
## API | ||
|
||
### `renderByTokenKey(tokenKey: string, options?: RenderOptions)` | ||
|
||
Renders a DOB token by its key. | ||
|
||
### `renderByDobDecodeResponse(renderOutput: RenderOutput, props?: RenderProps)` | ||
|
||
Renders a DOB token from a decoded response. | ||
|
||
## Dependencies | ||
|
||
- `satori` - SVG to image conversion | ||
- `svgson` - SVG parsing | ||
- `axios` - HTTP client for API calls |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// @ts-check | ||
|
||
import eslint from "@eslint/js"; | ||
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"; | ||
import tseslint from "typescript-eslint"; | ||
|
||
import { dirname } from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
export default [ | ||
...tseslint.config({ | ||
files: ["**/*.ts"], | ||
extends: [ | ||
eslint.configs.recommended, | ||
...tseslint.configs.recommendedTypeChecked, | ||
], | ||
rules: { | ||
"@typescript-eslint/no-unused-vars": [ | ||
"error", | ||
{ | ||
args: "all", | ||
argsIgnorePattern: "^_", | ||
caughtErrors: "all", | ||
caughtErrorsIgnorePattern: "^_", | ||
destructuredArrayIgnorePattern: "^_", | ||
varsIgnorePattern: "^_", | ||
ignoreRestSiblings: true, | ||
}, | ||
], | ||
"@typescript-eslint/unbound-method": ["error", { ignoreStatic: true }], | ||
"@typescript-eslint/no-unsafe-member-access": "off", | ||
"@typescript-eslint/require-await": "off", | ||
"@typescript-eslint/only-throw-error": [ | ||
"error", | ||
{ | ||
allowThrowingAny: true, | ||
allowThrowingUnknown: true, | ||
allowRethrowing: true, | ||
}, | ||
], | ||
"@typescript-eslint/prefer-promise-reject-errors": [ | ||
"error", | ||
{ | ||
allowThrowingAny: true, | ||
allowThrowingUnknown: true, | ||
}, | ||
], | ||
"no-empty": "off", | ||
"prefer-const": [ | ||
"error", | ||
{ ignoreReadBeforeAssign: true, destructuring: "all" }, | ||
], | ||
}, | ||
languageOptions: { | ||
parserOptions: { | ||
project: true, | ||
tsconfigRootDir: dirname(fileURLToPath(import.meta.url)), | ||
}, | ||
}, | ||
}), | ||
eslintPluginPrettierRecommended, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "@ckb-ccc/dob-render", | ||
"version": "1.0.1", | ||
"description": "CCC - CKBer's Codebase. Common Chains Connector's render SDK for DOB protocol", | ||
"author": "ashuralyk <[email protected]>", | ||
"license": "MIT", | ||
"private": false, | ||
"homepage": "https://github.com/ckb-devrel/ccc", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/ckb-devrel/ccc.git" | ||
}, | ||
"sideEffects": false, | ||
"main": "dist.commonjs/index.js", | ||
"module": "dist/index.js", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"require": "./dist.commonjs/index.js", | ||
"default": "./dist.commonjs/index.js" | ||
}, | ||
"./barrel": { | ||
"import": "./dist/barrel.js", | ||
"require": "./dist.commonjs/barrel.js", | ||
"default": "./dist.commonjs/barrel.js" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "rimraf ./dist && rimraf ./dist.commonjs && tsc && tsc --project tsconfig.commonjs.json", | ||
"lint": "eslint ./src", | ||
"format": "prettier --write . && eslint --fix ./src" | ||
}, | ||
"devDependencies": { | ||
"@eslint/js": "^9.34.0", | ||
"@types/node": "^24.3.0", | ||
"eslint": "^9.34.0", | ||
"eslint-config-prettier": "^10.1.8", | ||
"eslint-plugin-prettier": "^5.5.4", | ||
"prettier": "^3.6.2", | ||
"prettier-plugin-organize-imports": "^4.2.0", | ||
"rimraf": "^6.0.1", | ||
"typescript": "^5.9.2", | ||
"typescript-eslint": "^8.41.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@ckb-ccc/spore": "workspace:*", | ||
"axios": "^1.11.0", | ||
"satori": "^0.10.13", | ||
"svgson": "^5.3.1" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @see https://prettier.io/docs/configuration | ||
* @type {import("prettier").Config} | ||
*/ | ||
const config = { | ||
singleQuote: false, | ||
trailingComma: "all", | ||
plugins: [require.resolve("prettier-plugin-organize-imports")], | ||
}; | ||
|
||
module.exports = config; |
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,37 @@ | ||
import { dob } from "@ckb-ccc/spore"; | ||
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 { renderTextSvg } from "../core/renderers/textRender.js"; | ||
import type { RenderOptions } from "../types/query.js"; | ||
import { | ||
defaultQueryBtcFsFn, | ||
defaultQueryCkbFsFn, | ||
defaultQueryIpfsFn, | ||
defaultQueryUrlFn, | ||
} from "../types/query.js"; | ||
|
||
export function renderByDobDecodeResponse( | ||
renderOutput: dob.RenderOutput, | ||
props?: RenderOptions, | ||
) { | ||
const { traits, indexVarRegister } = traitsParser(renderOutput); | ||
for (const trait of traits) { | ||
if (trait.name === String(Key.Type) && trait.value === "image") { | ||
return renderImageSvg(traits, { | ||
queryBtcFsFn: props?.queryBtcFsFn || defaultQueryBtcFsFn, | ||
queryIpfsFn: props?.queryIpfsFn || defaultQueryIpfsFn, | ||
queryCkbFsFn: props?.queryCkbFsFn || defaultQueryCkbFsFn, | ||
queryUrlFn: props?.queryUrlFn || defaultQueryUrlFn, | ||
}); | ||
} | ||
// TODO: multiple images | ||
if (trait.name === String(Key.Image) && trait.value instanceof Promise) { | ||
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. |
||
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,14 @@ | ||
import { dob } from "@ckb-ccc/spore"; | ||
import type { RenderOptions } from "../types/query.js"; | ||
import { renderByDobDecodeResponse } from "./renderDobDecode.js"; | ||
|
||
export async function renderByTokenKey( | ||
tokenKey: string, | ||
options?: RenderOptions & { dobDecodeServerURL?: string }, | ||
) { | ||
const serverURL = | ||
options?.dobDecodeServerURL || "https://dob-decoder.ckbccc.com"; | ||
const renderOutput = await dob.decodeDobBySporeId(tokenKey, serverURL); | ||
|
||
return renderByDobDecodeResponse(renderOutput, options); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./api/index.js"; | ||
export * from "./config/index.js"; | ||
export * from "./core/index.js"; | ||
export * from "./types/index.js"; | ||
export * from "./utils/index.js"; |
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,2 @@ | ||
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)); | ||||||
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. The
Suggested change
|
||||||
} | ||||||
|
||||||
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.
The
String()
wrapper aroundKey.Type
is redundant because TypeScript enum members with string initializers are already strings. You can comparetrait.name
directly withKey.Type
. This improves code clarity and removes an unnecessary function call.