diff --git a/src/Api/tools/nip98.ts b/src/Api/tools/nip98.ts index 009be107..597bdc1a 100644 --- a/src/Api/tools/nip98.ts +++ b/src/Api/tools/nip98.ts @@ -143,6 +143,19 @@ export function validateEventUrlTag(event: Event, url: string): boolean { return urlTag.length > 0 && urlTag[1] === url } +export function sortObject(obj: Record): any { + const allKeys = Object.keys(obj).sort(); // Sort keys + const sortedObj: Record = {}; + + // Build new object with sorted keys + for (const key of allKeys) { + sortedObj[key] = obj[key]; + } + + // Now stringify the object with sorted keys + return sortedObj; +} + /** * Validates if the given event has a method tag that matches the specified method. * @param event - The event to validate. @@ -165,7 +178,7 @@ export function validateEventMethodTag(event: Event, method: string): boolean { * @returns The hash value as a string. */ export function hashPayload(payload: any): string { - const hash = sha256(utf8Encoder.encode(JSON.stringify(payload))) + const hash = sha256(utf8Encoder.encode(JSON.stringify(sortObject(payload)))) return bytesToHex(hash) } diff --git a/src/State/bridgeMiddleware.ts b/src/State/bridgeMiddleware.ts index 7791e3ff..a94223c0 100644 --- a/src/State/bridgeMiddleware.ts +++ b/src/State/bridgeMiddleware.ts @@ -7,7 +7,7 @@ import Bridge from "../Api/bridge"; import { decodeNProfile } from "../custom-nip19"; import { finishEvent } from "../Api/tools"; -import { getToken } from "../Api/tools/nip98"; +import { getToken, sortObject } from "../Api/tools/nip98"; export const upgradeSourcesToNofferBridge = createAction("upgradeSourcesToNofferBridge"); @@ -36,7 +36,7 @@ const enrollToBridge = async (source: PayTo, dispatchCallback: (vanityname: stri const payload = { k1, noffer: userInfoRes.noffer } const nostrHeader = await getToken(`${bridgeUrl}/api/v1/noffer/vanity`, "POST", e => finishEvent(e, source.keys.privateKey), true, payload) const bridgeHandler = new Bridge(bridgeUrl, nostrHeader); - const bridgeRes = await bridgeHandler.GetOrCreateNofferName(payload); + const bridgeRes = await bridgeHandler.GetOrCreateNofferName(sortObject(payload)); if (bridgeRes.status !== "OK") { throw new Error(bridgeRes.reason); }