Skip to content

Commit

Permalink
Merge pull request #297 from snake-eaterr/nip98-fix
Browse files Browse the repository at this point in the history
fix nip98
  • Loading branch information
shocknet-justin committed Sep 18, 2024
2 parents 2034ab5 + 502be54 commit 96ffef5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/Api/tools/nip98.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ export function validateEventUrlTag(event: Event, url: string): boolean {
return urlTag.length > 0 && urlTag[1] === url
}

export function sortObject(obj: Record<string, any>): any {
const allKeys = Object.keys(obj).sort(); // Sort keys
const sortedObj: Record<string, any> = {};

// 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.
Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/State/bridgeMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 96ffef5

Please sign in to comment.