Skip to content

fix a bug relating non hex string #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/mesh-common/src/data/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export const hexToBytes = (hex: string) => Buffer.from(hex, "hex");
export const stringToHex = (str: string) =>
Buffer.from(str, "utf8").toString("hex");

/**
* Checking if the string is a hex string
* @param hex The string to be checked
* @returns True if the string is a hex string, false otherwise
*/
export const isHexString = (hex: string) => /^[0-9A-F]*$/i.test(hex);

/**
* Converting hex string to utf8 string
* @param hex The hex string to be converted
Expand Down
6 changes: 3 additions & 3 deletions packages/mesh-core-cst/src/message-signing/check-signature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ready } from "@cardano-sdk/crypto";

import { DataSignature } from "@meshsdk/common";
import { DataSignature, isHexString, stringToHex } from "@meshsdk/common";

import {
Address,
Expand Down Expand Up @@ -106,8 +106,8 @@ export const checkSignature = async (
if (builder.getPayload() === null) {
return false;
}

if (Buffer.from(data, "hex").compare(builder.getPayload()!) !== 0) {
const hexData = isHexString(data) ? data : stringToHex(data);
if (Buffer.from(hexData, "hex").compare(builder.getPayload()!) !== 0) {
return false;
}

Expand Down
7 changes: 5 additions & 2 deletions packages/mesh-core-cst/src/message-signing/sign-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import {
CborUInt,
} from "@harmoniclabs/cbor";

import { DataSignature } from "@meshsdk/common";
import { DataSignature, stringToHex, isHexString} from "@meshsdk/common";

import { HexBlob, Signer } from "../types";
import { CoseSign1, getCoseKeyFromPublicKey } from "./cose-sign1";



/**
* Sign the data string using the provided signer
* @param data The data string to sign
* @param signer The signer object containing the key and address
* @returns DataSignature for verification
*/
export const signData = (data: string, signer: Signer): DataSignature => {
const payload = Buffer.from(data, "hex");
const hexData = isHexString(data) ? data : stringToHex(data);
const payload = Buffer.from(hexData, "hex");
const publicKey = Buffer.from(signer.key.toPublic().bytes());

const protectedMap: CborMapEntry[] = [];
Expand Down
7 changes: 6 additions & 1 deletion packages/mesh-core-cst/test/message-signing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const config = {
"addr_test1qqmrzjhtanauj20wg37uk58adyrqfm82a9qr52vdnv0e54r42v0mu8ngky0f5yxmh3wl3z0da2fryk59kavth0u8xhvsufgmc8",
nonce:
"5369676e20746f2076657269667920746865206164647265737320666f723a207374616b655f74657374317570363478386137726535747a3835367a72646d6368306333386b373479336a74327a6d776b396d6837726e746b6773367a786a70315a424e474e79506d3975565677514a53385837724663476551424843657433",
nonceInUtf8:
"Sign to verify the address for: stake_test1up64x8a7re5tz856zrdmch0c38k74y3jt2zmwk9mh7rntkgs6zxjp1ZBNGNyPm9uVVwQJS8X7rFcGeQBHCet3",
rewardAddress:
"stake_test1up64x8a7re5tz856zrdmch0c38k74y3jt2zmwk9mh7rntkgs6zxjp",
invalidAddress:
Expand All @@ -23,7 +25,10 @@ describe("MessageSigning", () => {
const result = await checkSignature(config.nonce, config.signature);
expect(result).toBe(true);
});

it("checkSignature with non hex string", async () => {
const result = await checkSignature(config.nonceInUtf8, config.signature);
expect(result).toBe(true);
});
it("checkSignature validates signature's address against provided rewardAddress", async () => {
const result = await checkSignature(
config.nonce,
Expand Down