Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/app/screens/Nostr/ConfirmSignSchnorr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function ConfirmSignSchnorr() {
const { t: tPermissions } = useTranslation("permissions");
const navigate = useNavigate();

const sigHash = navState.args?.sigHash as string;
const sigHash = navState.args?.sigHash as string | undefined;
const plaintext = navState.args?.message as string | undefined;
const origin = navState.origin as OriginData;
const [loading, setLoading] = useState(false);
const [successMessage, setSuccessMessage] = useState("");
Expand Down Expand Up @@ -98,7 +99,7 @@ function ConfirmSignSchnorr() {
publisher: origin.host,
action: tPermissions("nostr.signschnorr.title"),
})}
content={sigHash}
content={plaintext || sigHash || ""}
/>
</div>
<div className="flex flex-col gap-4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ const signSchnorrOrPrompt = async (

const nostr = await state.getState().getNostr();
const sigHash = message.args.sigHash;
const plaintext = message.args.message;

const isMessageMode = message.args.message !== undefined;

try {
if (!sigHash || typeof sigHash !== "string") {
if (isMessageMode) {
if (typeof plaintext !== "string") {
throw new Error("message is missing or not correct");
}
} else if (!sigHash || typeof sigHash !== "string") {
throw new Error("sigHash is missing or not correct");
}

const hasPermission = await hasPermissionFor(
PermissionMethodNostr["NOSTR_SIGNSCHNORR"],
host
);
const permissionMethod = PermissionMethodNostr["NOSTR_SIGNSCHNORR"];

const hasPermission = await hasPermissionFor(permissionMethod, host);

const isBlocked = await isPermissionBlocked(
PermissionMethodNostr["NOSTR_SIGNSCHNORR"],
host
);
const isBlocked = await isPermissionBlocked(permissionMethod, host);

if (isBlocked) {
return { denied: true };
Expand All @@ -55,10 +58,9 @@ const signSchnorrOrPrompt = async (
action: "public/nostr/confirmSignSchnorr",
});

// add permission to db only if user decided to always allow this request
if (promptResponse.data.permissionOption == DONT_ASK_CURRENT) {
await addPermissionFor(
PermissionMethodNostr["NOSTR_SIGNSCHNORR"],
permissionMethod,
host,
promptResponse.data.blocked
);
Expand All @@ -84,7 +86,13 @@ const signSchnorrOrPrompt = async (
}

async function signSchnorr() {
const signedSchnorr = await nostr.signSchnorr(sigHash);
let signedSchnorr: string;

if (isMessageMode) {
signedSchnorr = await nostr.hashAndSignSchnorr(plaintext as string);
} else {
signedSchnorr = await nostr.signSchnorr(sigHash as string);
}

return { data: signedSchnorr };
}
Expand Down
13 changes: 13 additions & 0 deletions src/extension/background-script/nostr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ class Nostr {
return signedHex;
}

async hashAndSignSchnorr(message: string): Promise<string> {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = new Uint8Array(hashBuffer);

const signature = await schnorr.sign(
hashArray,
secp256k1.etc.hexToBytes(this.privateKey)
);
return secp256k1.etc.bytesToHex(signature);
}

nip04Encrypt(pubkey: string, text: string) {
const key = secp256k1.getSharedSecret(this.privateKey, "02" + pubkey);
const normalizedKey = Buffer.from(key.slice(1, 33));
Expand Down
5 changes: 5 additions & 0 deletions src/extension/providers/nostr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export default class NostrProvider extends ProviderBase {
return this.execute("signEventOrPrompt", { event });
}

async hashAndSignSchnorr(message: string) {
await this.enable();
return this.execute("signSchnorrOrPrompt", { message });
}

async signSchnorr(sigHash: string) {
await this.enable();
return this.execute("signSchnorrOrPrompt", { sigHash });
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ export interface MessageSignEvent extends MessageDefault {

export interface MessageSignSchnorr extends MessageDefault {
args: {
sigHash: string;
sigHash?: string;
message?: string;
};
action: "signSchnorr";
}
Expand Down
Loading