Skip to content

Commit

Permalink
feat: support decode from raw spore and cluster data
Browse files Browse the repository at this point in the history
  • Loading branch information
ashuralyk authored and Hanssen0 committed Jan 10, 2025
1 parent e4ea183 commit a1d5626
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-ligers-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-ccc/spore": minor
---

feat(spore): support decoding rendered dob data from spore data and cluster data
17 changes: 17 additions & 0 deletions packages/spore/src/__examples__/decodeDob.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
52 changes: 40 additions & 12 deletions packages/spore/src/dob/api/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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<RenderOutput> {
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);
}
1 change: 1 addition & 0 deletions packages/spore/src/dob/helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./check.js";
export * from "./error.js";
export * from "./object.js";

0 comments on commit a1d5626

Please sign in to comment.