add nip-26 support

This commit is contained in:
pablof7z
2023-03-14 12:16:24 +00:00
committed by fiatjaf_
parent 185ee772e1
commit e96683d235
4 changed files with 18 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ 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.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.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.nip04.decrypt(pubkey, ciphertext): string // takes ciphertext+iv as specified in nip04
async window.nostr.nip26.delegate(delegateePubkey, cond): object // generates a delegation token ({from, to, cond, sig})
``` ```
This extension is Chromium-only. For a maintained Firefox fork, see [nos2x-fox](https://diegogurpegui.com/nos2x-fox/). This extension is Chromium-only. For a maintained Firefox fork, see [nos2x-fox](https://diegogurpegui.com/nos2x-fox/).

View File

@@ -6,7 +6,7 @@ import {
getPublicKey, getPublicKey,
nip19 nip19
} from 'nostr-tools' } from 'nostr-tools'
import {nip04} from 'nostr-tools' import {nip04, nip26} from 'nostr-tools'
import {Mutex} from 'async-mutex' import {Mutex} from 'async-mutex'
import { import {
@@ -142,6 +142,11 @@ async function handleContentScriptMessage({type, params, host}) {
let {peer, ciphertext} = params let {peer, ciphertext} = params
return decrypt(sk, peer, ciphertext) return decrypt(sk, peer, ciphertext)
} }
case 'nip26.delegate': {
let { delegateePubkey, conditionsJson } = params
let parameters = { pubkey: delegateePubkey, ...conditionsJson }
return nip26.createDelegation(sk, parameters)
}
} }
} catch (error) { } catch (error) {
return {error: {message: error.message, stack: error.stack}} return {error: {message: error.message, stack: error.stack}}

View File

@@ -8,14 +8,16 @@ export const PERMISSIONS_REQUIRED = {
getPublicKey: 1, getPublicKey: 1,
getRelays: 5, getRelays: 5,
signEvent: 10, signEvent: 10,
'nip26.delegate': 10,
'nip04.encrypt': 20, 'nip04.encrypt': 20,
'nip04.decrypt': 20 'nip04.decrypt': 20,
} }
const ORDERED_PERMISSIONS = [ const ORDERED_PERMISSIONS = [
[1, ['getPublicKey']], [1, ['getPublicKey']],
[5, ['getRelays']], [5, ['getRelays']],
[10, ['signEvent']], [10, ['signEvent']],
[10, ['nip26.delegate']],
[20, ['nip04.encrypt']], [20, ['nip04.encrypt']],
[20, ['nip04.decrypt']] [20, ['nip04.decrypt']]
] ]
@@ -25,7 +27,8 @@ const PERMISSION_NAMES = {
getRelays: 'read your list of preferred relays', getRelays: 'read your list of preferred relays',
signEvent: 'sign events using your private key', signEvent: 'sign events using your private key',
'nip04.encrypt': 'encrypt messages to peers', 'nip04.encrypt': 'encrypt messages to peers',
'nip04.decrypt': 'decrypt messages from peers' 'nip04.decrypt': 'decrypt messages from peers',
'nip26.delegate': 'create key delegation tokens',
} }
export function getAllowedCapabilities(permission) { export function getAllowedCapabilities(permission) {

View File

@@ -26,6 +26,12 @@ window.nostr = {
} }
}, },
nip26: {
async delegate(delegateePubkey, conditionsJson) {
return window.nostr._call('nip26.delegate', {delegateePubkey, conditionsJson})
}
},
_call(type, params) { _call(type, params) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let id = Math.random().toString().slice(4) let id = Math.random().toString().slice(4)