Skip to content

Commit

Permalink
Add convertHashIPFStoCID method
Browse files Browse the repository at this point in the history
  • Loading branch information
bonnie57 committed Oct 22, 2024
1 parent 2b61608 commit 73ace66
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/core-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
],
"scripts": {
"build": "pnpm run fix && preconstruct build",
"test": "pnpm run test:unit",
"test:unit": "TS_NODE_PROJECT='./tsconfig.test.json' c8 --all --src ./src mocha -r ts-node/register './test/unit/utils/ipfs.test.ts'",
"test:integration": "TS_NODE_PROJECT='./tsconfig.test.json' mocha -r ts-node/register './test/integration/**/dispute.test.ts' --timeout 240000",
"test": "pnpm run test:unit && pnpm run test:integration",
"test:unit": "TS_NODE_PROJECT='./tsconfig.test.json' c8 --all --src ./src mocha -r ts-node/register './test/unit/**/*.test.ts'",
"test:integration": "TS_NODE_PROJECT='./tsconfig.test.json' mocha -r ts-node/register './test/integration/**/*.test.ts' --timeout 240000",
"fix": "pnpm run format:fix && pnpm run lint:fix",
"format": "prettier --check .",
"format:fix": "prettier --write .",
Expand Down
1 change: 1 addition & 0 deletions packages/core-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ export type {
} from "./abi/generated";

export { getPermissionSignature } from "./utils/sign";
export { convertCIDtoHashIPFS, convertHashIPFStoCID } from "./utils/ipfs";
4 changes: 3 additions & 1 deletion packages/core-sdk/src/resources/dispute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export class DisputeClient {
const req = {
targetIpId: getAddress(request.targetIpId, "request.targetIpId"),
targetTag: stringToHex(request.targetTag, { size: 32 }),
data: request.data || "0x",
data:
request.data ||
"0x00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000278d0000000000000000000000000091f6f05b08c16769d3c85867548615d270c42fc700000000000000000000000000000000000000000000000000000000000000004153534552545f54525554480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a7465737420636c61696d00000000000000000000000000000000000000000000",
disputeEvidenceHash: request.disputeEvidenceHash,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core-sdk/src/types/resources/dispute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type RaiseDisputeRequest = {
targetIpId: Address;
disputeEvidenceHash: Hex;
targetTag: string;
data?: Address;
data?: Hex;
txOptions?: TxOptions;
};

Expand Down
12 changes: 12 additions & 0 deletions packages/core-sdk/src/utils/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CID } from "multiformats/cid";
import bs58 from "bs58";
import { base58btc } from "multiformats/bases/base58";

const v0Prefix = "1220";
export const convertCIDtoHashIPFS = (cid: string): string => {
Expand All @@ -12,3 +13,14 @@ export const convertCIDtoHashIPFS = (cid: string): string => {
.join("");
return "0x" + base16CID.slice(v0Prefix.length);
};

export const convertHashIPFStoCID = (hash: string, version: "v0" | "v1" = "v0"): string => {
const base16CID = v0Prefix + hash.slice(2);
const bytes = new Uint8Array(base16CID.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16)));
const base58CID = bs58.encode(Buffer.from(bytes));
if (version === "v0") {
return base58CID;
} else {
return CID.parse(base58CID, base58btc).toV1().toString();
}
};
4 changes: 2 additions & 2 deletions packages/core-sdk/test/integration/dispute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe("Dispute Functions", () => {
expect(response.txHash).to.be.a("string").and.not.empty;
expect(response.disputeId).to.be.a("bigint");
});

it("should not throw error when cancel a dispute", async () => {
//In the current arbitration policy it is not possible to cancel disputes, so skip this test
it.skip("should not throw error when cancel a dispute", async () => {
const cancelDispute: CancelDisputeRequest = {
disputeId: disputeId,
txOptions: {
Expand Down
9 changes: 4 additions & 5 deletions packages/core-sdk/test/integration/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
licenseTokenAddress,
spgnftBeaconAddress,
} from "../../../src/abi/generated";
export const RPC = "https://story-testnet.aura.network";
export const RPC =
" https://convincing-dark-feather.story-testnet.quiknode.pro/de18afeed566629cd009ba30eca51d6a9f7fb980/";
export const iliadChainId = 1513;

export const mockERC721 = "0x322813fd9a801c5507c9de605d63cea4f2ce6c44";
export const licenseToken =
licenseTokenAddress[Number(iliadChainId) as keyof typeof licenseTokenAddress];
export const spgNftBeacon =
spgnftBeaconAddress[Number(iliadChainId) as keyof typeof spgnftBeaconAddress];
export const licenseToken = licenseTokenAddress[iliadChainId];
export const spgNftBeacon = spgnftBeaconAddress[iliadChainId];

const baseConfig = {
chain: chainStringToViemChain("iliad"),
Expand Down
18 changes: 17 additions & 1 deletion packages/core-sdk/test/unit/utils/ipfs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { convertCIDtoHashIPFS } from "../../../src/utils/ipfs";
import { convertCIDtoHashIPFS, convertHashIPFStoCID } from "../../../src/utils/ipfs";
describe("IPFS", () => {
it("should return hash when call convertCIDtoHashIPFS given CID with v0", async () => {
const result = convertCIDtoHashIPFS("QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR");
Expand All @@ -13,4 +13,20 @@ describe("IPFS", () => {
console.log(result);
expect(result).to.equal("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
});

it("should return CID when call convertHashIPFStoCID given hash with v0", async () => {
const result = convertHashIPFStoCID(
"0xc3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a",
"v0",
);
expect(result).to.equal("QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR");
});

it("should return CID when call convertHashIPFStoCID given hash with v1", async () => {
const result = convertHashIPFStoCID(
"0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"v1",
);
expect(result).to.equal("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku");
});
});
4 changes: 1 addition & 3 deletions packages/react-sdk/test/integration/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ export const walletClient = createWalletClient({
account: privateKeyToAccount(process.env.WALLET_PRIVATE_KEY as Hex),
});

export const getTokenId = async (
nftContract?: Address
): Promise<number | undefined> => {
export const getTokenId = async (): Promise<number | undefined> => {
const { request } = await publicClient.simulateContract({
abi: [
{
Expand Down

0 comments on commit 73ace66

Please sign in to comment.