Skip to content
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
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ This feature can be enable / disabled in Options.
- getPublicKey()
- signEvent(event)
- getRelays()
- signString()

[NIP-04](https://github.com/nostr-protocol/nips/blob/master/04.md)

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import * as nip44 from "nostr-tools/nip44";
import { Mutex } from "async-mutex";
import { LRUCache } from "./utils";

import {sha256} from "@noble/hashes/sha256";
import {bytesToHex} from "@noble/hashes/utils";
import {schnorr} from "@noble/curves/secp256k1";

import {
NO_PERMISSIONS_REQUIRED,
updatePermission,
Expand Down Expand Up @@ -296,6 +300,24 @@ async function handleContentScriptMessage({ type, params, host, protocol }) {
? event
: { error: { message: "invalid event" } };
}
case "signString": {
if (typeof params.message !== 'string') {
return { error: { message: "message is not a string" } };
}
try {
// Check this is not a stringified event
// trying to bypass permission checks
const obj = JSON.parse(params.message);
if (validateEvent(obj)){
return { error: { message: "use signEvent() to sign events" } };
}
} catch (e) {} // not a JSON string
const utf8Encoder = new TextEncoder();
const hash = bytesToHex(sha256(utf8Encoder.encode(params.message)));
const sig = bytesToHex(schnorr.sign(hash, sk));
const pubkey = bytesToHex(schnorr.getPublicKey(sk));
return {hash: hash, sig: sig, pubkey: pubkey};
}
case "nip04.encrypt": {
let { peer, plaintext } = params;
lib = "nostr-tools/nip04";
Expand Down
1 change: 1 addition & 0 deletions src/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const PERMISSION_NAMES = {
getPublicKey: "read your public key",
getRelays: "read your list of preferred relays",
signEvent: "sign events using your private key",
signString: "sign messages using your private key",
"nip04.encrypt": "encrypt messages to peers",
"nip04.decrypt": "decrypt messages from peers",
"nip44.encrypt": "encrypt messages to peers",
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions src/static/nostr-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ window.nostr = {
return this._call("signEvent", { event });
},

async signString(message) {
return this._call("signString", { message });
},

async getRelays() {
return this._call("getRelays", {});
},
Expand Down