From a1d56266da68602495054ad6b627b998ef27f04d Mon Sep 17 00:00:00 2001 From: ashuralyk Date: Fri, 10 Jan 2025 20:44:18 +0800 Subject: [PATCH] feat: support decode from raw spore and cluster data --- .changeset/lemon-ligers-fail.md | 5 ++ .../spore/src/__examples__/decodeDob.test.ts | 17 ++++++ packages/spore/src/dob/api/decode.ts | 52 ++++++++++++++----- packages/spore/src/dob/helper/index.ts | 1 + 4 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 .changeset/lemon-ligers-fail.md create mode 100644 packages/spore/src/__examples__/decodeDob.test.ts diff --git a/.changeset/lemon-ligers-fail.md b/.changeset/lemon-ligers-fail.md new file mode 100644 index 00000000..8fd8fd9d --- /dev/null +++ b/.changeset/lemon-ligers-fail.md @@ -0,0 +1,5 @@ +--- +"@ckb-ccc/spore": minor +--- + +feat(spore): support decoding rendered dob data from spore data and cluster data diff --git a/packages/spore/src/__examples__/decodeDob.test.ts b/packages/spore/src/__examples__/decodeDob.test.ts new file mode 100644 index 00000000..75e6582d --- /dev/null +++ b/packages/spore/src/__examples__/decodeDob.test.ts @@ -0,0 +1,17 @@ +import "dotenv/config"; +import { decodeDobBySporeId } from "../dob/index.js"; + +describe("decodeDob [testnet]", () => { + it("should respose a decoded dob render data from a spore id", async () => { + // The address that https://github.com/sporeprotocol/dob-decoder-standalone-server running at + const decoderServerUrl = "http://127.0.0.1:8090"; + + // The spore id that you want to decode (must be a valid spore dob) + const sporeId = + "0x29e4cfd388b9a01f7a853d476feb8e33af38565a1e751d55c9423bf7aa4b480b"; + + // Decode from spore id + const dob = await decodeDobBySporeId(sporeId, decoderServerUrl); + console.log(dob); + }, 60000); +}); diff --git a/packages/spore/src/dob/api/decode.ts b/packages/spore/src/dob/api/decode.ts index af19055c..a36a4ad0 100644 --- a/packages/spore/src/dob/api/decode.ts +++ b/packages/spore/src/dob/api/decode.ts @@ -3,6 +3,21 @@ import { Axios } from "axios"; import { getErrorByCode } from "../helper/error.js"; import { RenderOutput } from "../helper/object.js"; +export function extractDobFromDecoderResult(result: string): RenderOutput { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const decoderResult = JSON.parse(result); + if ("error" in decoderResult) { + const serverError = getErrorByCode(decoderResult.error.code as number); + throw new Error(`Decode DOB failed: ${serverError}`); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument + const renderResult = JSON.parse(decoderResult.result); + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument + const renderOutput = JSON.parse(renderResult.render_output); + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return renderOutput; +} + export async function decodeDobBySporeId( sporeId: ccc.HexLike, dobServerUrl: string, @@ -23,18 +38,7 @@ export async function decodeDobBySporeId( params: [ccc.hexFrom(sporeId).replace(/^0x/, "")], }), ); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument - const decoderResult = JSON.parse(result.data); - if ("error" in decoderResult) { - const serverError = getErrorByCode(decoderResult.error.code as number); - throw new Error(`Decode DOB failed: ${serverError}`); - } - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument - const renderResult = JSON.parse(decoderResult.result); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument - const renderOutput = JSON.parse(renderResult.render_output); - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return renderOutput; + return extractDobFromDecoderResult(result.data as string); } export async function decodeDobBySporeCell( @@ -59,3 +63,27 @@ export async function decodeDobBySporeOutpoint( } return decodeDobBySporeCell(liveCell.cellOutput, dobServerUrl); } + +export async function decodeDobByRawData( + sporeCellData: ccc.HexLike, + clusterCellData: ccc.HexLike, + dobServerUrl: string, +): Promise { + const axios = new Axios({ + baseURL: dobServerUrl, + method: "POST", + headers: { + "Content-Type": "application/json", + }, + }); + const result = await axios.post( + "/", + JSON.stringify({ + id: 0, + jsonrpc: "2.0", + method: "dob_raw_decode", + params: [ccc.hexFrom(sporeCellData), ccc.hexFrom(clusterCellData)], + }), + ); + return extractDobFromDecoderResult(result.data as string); +} diff --git a/packages/spore/src/dob/helper/index.ts b/packages/spore/src/dob/helper/index.ts index ac718b24..98d78216 100644 --- a/packages/spore/src/dob/helper/index.ts +++ b/packages/spore/src/dob/helper/index.ts @@ -1,2 +1,3 @@ export * from "./check.js"; +export * from "./error.js"; export * from "./object.js";