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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ async window.nostr.signEvent(event): Event // returns the full event object sign
async window.nostr.getRelays(): { [url: string]: RelayPolicy } // returns a map of relays
async window.nostr.nip04.encrypt(pubkey, plaintext): string // returns ciphertext+iv as specified in nip04
async window.nostr.nip04.decrypt(pubkey, ciphertext): string // takes ciphertext+iv as specified in nip04
async window.nostr.nip44.encrypt([[pubkey1, plaintext1], [pubkey2, plaintext2], ...]): string[] // takes array of [pubkey, plaintext] tuples, returns array of ciphertexts as specified in nip-44
async window.nostr.nip44.decrypt([[pubkey1, ciphertext1], [pubkey2, ciphertext2], ...]): string[] // takes array of [pubkey, ciphertext] tuples, returns array of plaintexts as specified in nip-44
```

This extension is Chromium-only. For a maintained Firefox fork, see [nos2x-fox](https://diegogurpegui.com/nos2x-fox/).
Expand Down
53 changes: 40 additions & 13 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import browser from 'webextension-polyfill'
import {
validateEvent,
getSignature,
finalizeEvent,
getEventHash,
getPublicKey,
nip19
} from 'nostr-tools'
import {nip04} from 'nostr-tools'
import {nip04, nip44} from 'nostr-tools'
import {Mutex} from 'async-mutex'
import {LRUCache} from './utils'

import {
NO_PERMISSIONS_REQUIRED,
Expand All @@ -16,11 +17,28 @@ import {
showNotification
} from './common'

const {encrypt, decrypt} = nip04

let openPrompt = null
let promptMutex = new Mutex()
let releasePromptMutex = () => {}
let secretsCache = new LRUCache(100)
let previousSk = null

function getSharedSecret(sk, peer) {
// Detect a key change and erase the cache if they changed their key
if (previousSk != sk) {
secretsCache.clear()
}

let key = secretsCache.get(peer)

if (!key) {
key = nip44.v2.getSharedSecret(sk, peer)

secretsCache.set(peer, key)
}

return key
}

browser.runtime.onInstalled.addListener((_, __, reason) => {
if (reason === 'install') browser.runtime.openOptionsPage()
Expand Down Expand Up @@ -165,22 +183,31 @@ async function handleContentScriptMessage({type, params, host}) {
return results.relays || {}
}
case 'signEvent': {
let {event} = params
const event = finalizeEvent(params.event, sk)

if (!event.pubkey) event.pubkey = getPublicKey(sk)
if (!event.id) event.id = getEventHash(event)
if (!validateEvent(event)) return {error: {message: 'invalid event'}}

event.sig = await getSignature(event, sk)
return event
return validateEvent(event) ? event : {error: {message: 'invalid event'}}
}
case 'nip04.encrypt': {
let {peer, plaintext} = params
return encrypt(sk, peer, plaintext)
return nip04.encrypt(sk, peer, plaintext)
}
case 'nip04.decrypt': {
let {peer, ciphertext} = params
return decrypt(sk, peer, ciphertext)
return nip04.decrypt(sk, peer, ciphertext)
}
case 'nip44.encrypt': {
return params.items.map(([pubkey, plaintext]) => {
const key = getSharedSecret(sk, peer)

return nip44.v2.decrypt(key, plaintext)
})
}
case 'nip44.decrypt': {
return params.items.map(([pubkey, ciphertext]) => {
const key = getSharedSecret(sk, peer)

return nip44.v2.decrypt(key, ciphertext)
})
}
}
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions extension/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const PERMISSION_NAMES = Object.fromEntries([
['signEvent', 'sign events using your private key'],
['nip04.encrypt', 'encrypt messages to peers'],
['nip04.decrypt', 'decrypt messages from peers']
['nip44.encrypt', 'encrypt messages to peers'],
['nip44.decrypt', 'decrypt messages from peers']
])

function matchConditions(conditions, event) {
Expand Down
10 changes: 10 additions & 0 deletions extension/nostr-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ window.nostr = {
}
},

nip44: {
async encrypt(items) {
return window.nostr._call('nip44.encrypt', {items})
},

async decrypt(items) {
return window.nostr._call('nip44.decrypt', {items})
}
},

_call(type, params) {
let id = Math.random().toString().slice(-4)
console.log(
Expand Down
4 changes: 2 additions & 2 deletions extension/options.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import browser from 'webextension-polyfill'
import React, {useState, useCallback, useEffect} from 'react'
import {render} from 'react-dom'
import {generatePrivateKey, nip19} from 'nostr-tools'
import {generateSecretKey, nip19} from 'nostr-tools'
import QRCode from 'react-qr-code'

import {removePermissions} from './common'
Expand Down Expand Up @@ -327,7 +327,7 @@ function Options() {
}

async function generate() {
setPrivKey(nip19.nsecEncode(generatePrivateKey()))
setPrivKey(nip19.nsecEncode(generateSecretKey()))
addUnsavedChanges('private_key')
}

Expand Down
38 changes: 38 additions & 0 deletions extension/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export class LRUCache {
constructor(maxSize) {
this.maxSize = maxSize
this.map = new Map()
this.keys = []
}

clear() {
this.map.clear()
}

has(k) {
return this.map.has(k)
}

get(k) {
const v = this.map.get(k)

if (v !== undefined) {
this.keys.push(k)

if (this.keys.length > this.maxSize * 2) {
this.keys.splice(-this.maxSize)
}
}

return v
}

set(k, v) {
this.map.set(k, v)
this.keys.push(k)

if (this.map.size > this.maxSize) {
this.map.delete(this.keys.shift())
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-react": "^7.28.0",
"events": "^3.3.0",
"nostr-tools": "^1.12.0",
"nostr-tools": "^2.1.0",
"prettier": "^2.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
93 changes: 64 additions & 29 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,63 @@
resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45"
integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==

"@noble/curves@1.0.0", "@noble/curves@~1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.0.0.tgz#e40be8c7daf088aaf291887cbc73f43464a92932"
integrity sha512-2upgEu0iLiDVDZkNLeFV2+ht0BAVgQnEmCk6JsOch9Rp8xfkMCbvbAZlA2pBHQc73dbl+vFOXfqkf4uemdn0bw==
"@noble/ciphers@0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-0.2.0.tgz#a12cda60f3cf1ab5d7c77068c3711d2366649ed7"
integrity sha512-6YBxJDAapHSdd3bLDv6x2wRPwq4QFMUaB3HvljNBUTThDd12eSm7/3F+2lnfzx2jvM+S6Nsy0jEt9QbPqSwqRw==

"@noble/curves@1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
integrity sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==
dependencies:
"@noble/hashes" "1.3.0"
"@noble/hashes" "1.3.2"

"@noble/hashes@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.0.tgz#085fd70f6d7d9d109671090ccae1d3bec62554a1"
integrity sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==
"@noble/curves@~1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.1.0.tgz#f13fc667c89184bc04cccb9b11e8e7bae27d8c3d"
integrity sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==
dependencies:
"@noble/hashes" "1.3.1"

"@noble/hashes@~1.3.0":
"@noble/hashes@1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==

"@scure/base@1.1.1", "@scure/base@~1.1.0":
"@noble/hashes@1.3.2":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==

"@noble/hashes@~1.3.0", "@noble/hashes@~1.3.1":
version "1.3.3"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.3.tgz#39908da56a4adc270147bb07968bf3b16cfe1699"
integrity sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==

"@scure/base@1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938"
integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==

"@scure/bip32@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-1.3.0.tgz#6c8d980ef3f290987736acd0ee2e0f0d50068d87"
integrity sha512-bcKpo1oj54hGholplGLpqPHRbIsnbixFtc06nwuNM5/dwSXOq/AAYoIBRsBmnZJSdfeNW5rnff7NTAz3ZCqR9Q==
"@scure/base@~1.1.0":
version "1.1.5"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.5.tgz#1d85d17269fe97694b9c592552dd9e5e33552157"
integrity sha512-Brj9FiG2W1MRQSTB212YVPRrcbjkv48FoZi/u4l/zds/ieRrqsh7aUf6CLwkAq61oKXr/ZlTzlY66gLIj3TFTQ==

"@scure/bip32@1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-1.3.1.tgz#7248aea723667f98160f593d621c47e208ccbb10"
integrity sha512-osvveYtyzdEVbt3OfwwXFr4P2iVBL5u1Q3q4ONBfDY/UpOuXmOlbgwc1xECEboY8wIays8Yt6onaWMUdUbfl0A==
dependencies:
"@noble/curves" "~1.0.0"
"@noble/hashes" "~1.3.0"
"@noble/curves" "~1.1.0"
"@noble/hashes" "~1.3.1"
"@scure/base" "~1.1.0"

"@scure/bip39@1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.2.0.tgz#a207e2ef96de354de7d0002292ba1503538fc77b"
integrity sha512-SX/uKq52cuxm4YFXWFaVByaSHJh2w3BnokVSeUJVCv6K7WulT9u2BuNRBhuFl8vAuYnzx9bEu9WgpcNYTrYieg==
"@scure/bip39@1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.2.1.tgz#5cee8978656b272a917b7871c981e0541ad6ac2a"
integrity sha512-Z3/Fsz1yr904dduJD0NpiyRHhRYHdcnyh73FZWiV+/qhWi83wNJ3NWolYqCEN+ZWsUz2TWwajJggcRE9r1zUYg==
dependencies:
"@noble/hashes" "~1.3.0"
"@scure/base" "~1.1.0"
Expand Down Expand Up @@ -924,6 +946,11 @@ minimatch@^3.0.4:
dependencies:
brace-expansion "^1.1.7"

mitata@^0.1.6:
version "0.1.6"
resolved "https://registry.yarnpkg.com/mitata/-/mitata-0.1.6.tgz#0d25f0dd105d6c50ec8d9047ee21bed1552fbffd"
integrity sha512-VKQ0r3jriTOU9E2Z+mwbZrUmbg4Li4QyFfi7kfHKl6reZhGzL0AYlu3wE0VPXzIwA5xnFzmEQoBwCcNT8stUkA==

ms@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
Expand All @@ -934,16 +961,24 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=

nostr-tools@^1.12.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.12.0.tgz#ec3618fc2298e029941b7db3bbe95187777c488f"
integrity sha512-fsIXaNJPKaSrO9MxsCEWbhI4tG4pToQK4D4sgLRD0fRDfZ6ocCg8CLlh9lcNx0o8pVErCMLVASxbJ+w4WNK0MA==
nostr-tools@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-2.1.0.tgz#74e655c31fd03457352cd319a7404048ca9bc674"
integrity sha512-LY5y3/BWaqtfySOhG+NSY3mAt+vUw4vQVFjqvawtO6SdEHqvWdNsb2uUqYdyBztFcIKFxoAbGMntbXQ9QO22oA==
dependencies:
"@noble/curves" "1.0.0"
"@noble/hashes" "1.3.0"
"@noble/ciphers" "0.2.0"
"@noble/curves" "1.2.0"
"@noble/hashes" "1.3.1"
"@scure/base" "1.1.1"
"@scure/bip32" "1.3.0"
"@scure/bip39" "1.2.0"
"@scure/bip32" "1.3.1"
"@scure/bip39" "1.2.1"
mitata "^0.1.6"
nostr-wasm v0.1.0

nostr-wasm@v0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/nostr-wasm/-/nostr-wasm-0.1.0.tgz#17af486745feb2b7dd29503fdd81613a24058d94"
integrity sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==

nth-check@^2.0.1:
version "2.1.1"
Expand Down