Skip to content

Commit

Permalink
attempt to fix createMetadataTargetIdPayHash
Browse files Browse the repository at this point in the history
  • Loading branch information
aeolianeth committed May 13, 2024
1 parent a2f48ed commit 9c2b3f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { useEffect, useState } from "react";
import { useJb721TiersHookMetadataIdTarget } from "src/generated/juicebox";
import { Address } from "viem";
import { Address, bytesToString, keccak256, toBytes } from "viem";

/**
* createMetadataTargetIdPayHash returns metadataTargetId hashed with string `"pay"`.
*/
async function createMetadataTargetIdPayHash(metadataTargetId: Address) {
const encoder = new TextEncoder();
const data = encoder.encode(metadataTargetId + "pay");
const hash = await window.crypto.subtle.digest("SHA-256", data);

return Array.from(new Uint8Array(hash))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
async function createMetadataTargetIdPayHash(
metadataTargetId: Address,
purpose: string = "pay"
) {
const targetBytes = toBytes(metadataTargetId);
const purposeBytes = toBytes(purpose);

const targetBytes20 = keccak256(targetBytes).slice(0, 20);
const purposeKeccak = keccak256(purposeBytes);
const purposeBytes20 = purposeKeccak.slice(0, 20);

const xorResult = toBytes(targetBytes20).map(
(byte, i) => byte ^ toBytes(purposeBytes20)[i]
);

return bytesToString(xorResult.slice(0, 4));
}

/**
Expand All @@ -35,7 +43,9 @@ export function use721HookMetadataId({
return;
}

createMetadataTargetIdPayHash(metadataTargetId).then(setHashedMetadataId);
createMetadataTargetIdPayHash(metadataTargetId, "pay").then(
setHashedMetadataId
);
}, [metadataTargetId]);

return hashedMetadataId;
Expand Down
2 changes: 2 additions & 0 deletions packages/juice-sdk-react/src/hooks/usePreparePayMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export function usePreparePayMetadata({
dataHookAddress: jb721Hook?.dataHookAddress,
});

console.log("usePreparePayMetadata::metadataId", metadataId);

if (!jb721Hook || jb721Hook.tierIdsToMint.length == 0 || !metadataId) {
return null;
}
Expand Down

0 comments on commit 9c2b3f0

Please sign in to comment.