Skip to content

Commit

Permalink
fix(secret): token id conversion after lock and before claim
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadusmanuet committed Dec 11, 2024
1 parent 8c2dbad commit c56ca0c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/handler/chains/secrets/utils/listenForLockEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { EventBuilder } from "../../../event-builder";
import { tryRerunningFailed } from "../../../poller/utils";
import type { LockEventIter, LogInstance } from "../../../types";
import { useMutexAndRelease } from "../../../utils";
import { convertStringToHexToNumb } from "../../../utils/token-id-conversion";
import type { SecretProviderFetch } from "../types";

const CHAIN_IDENT = "SECRET";
Expand Down Expand Up @@ -38,7 +39,7 @@ export default async function listenForLockEvents(
? lastBlock + blockChunks
: latestBlockNumber;

const query = `message.contract_address = '${bridge}' AND tx.height >= ${lastBlock} AND tx.height < ${latestBlock}`;
const query = `message.contract_address = '${bridge}' AND tx.height >= ${lastBlock} AND tx.height <= ${latestBlock}`;
const logs = await useMutexAndRelease(
fetchProvider,
async (c) => await c.query.txsQuery(query),
Expand Down Expand Up @@ -76,10 +77,15 @@ export default async function listenForLockEvents(
token_amount: tokenAmount, // amount of nfts to be transfered ( 1 in 721 case )
nft_type: nftType, // Sigular or multiple ( 721 / 1155)
source_chain: sourceChain, // Source chain of NFT
metadata_uri: metadataUri, // Source chain of NFT
} = parsedLog;
let convertedTokenId = tokenId;
if (sourceChain === "SECRET") {
convertedTokenId = convertStringToHexToNumb(tokenId);
}
await cb(
await builder.nftLocked(
tokenId,
convertedTokenId,
destinationChain,
destinationUserAddress,
sourceNftContractAddress,
Expand All @@ -88,7 +94,7 @@ export default async function listenForLockEvents(
sourceChain,
log.transactionHash,
CHAIN_IDENT,
"",
metadataUri,
),
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/handler/chains/secrets/utils/signClaimData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { sha256 } from "@noble/hashes/sha256";
import * as secp256k1 from "@noble/secp256k1";

import type { TNftTransferDetailsObject } from "../../../types";
import { convertNumbToHexToString } from "../../../utils/token-id-conversion";

export default async function signClaimData(
data: TNftTransferDetailsObject,
privateKey: string,
pubk: string,
) {
let convertedTokenId = data.tokenId;
if (data.sourceChain === "SECRET") {
convertedTokenId = convertNumbToHexToString(data.tokenId);
}
const messageHash = sha256(
[
data.tokenId,
convertedTokenId,
data.sourceChain,
data.destinationChain,
data.destinationUserAddress,
Expand Down
15 changes: 15 additions & 0 deletions src/handler/utils/token-id-conversion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const convertStringToHexToNumb = (str: string) => {
const hex = Array.from(str)
.map((char) => char.charCodeAt(0).toString(16).padStart(2, "0"))
.join("");
const numb = BigInt(`0x${hex}`);
return numb.toString();
};
export const convertNumbToHexToString = (num: string) => {
const numberToBigInt = BigInt(num);
const hex = numberToBigInt.toString(16) as string;
const string = (hex.match(/.{1,2}/g) || [])
.map((byte) => String.fromCharCode(Number.parseInt(byte, 16)))
.join("");
return string;
};

0 comments on commit c56ca0c

Please sign in to comment.