just format

This commit is contained in:
Alex Gleason
2023-08-31 13:42:15 -05:00
parent 2a70bb18ff
commit 718032022c
55 changed files with 930 additions and 1488 deletions

View File

@@ -1,11 +1,11 @@
import {schnorr} from '@noble/curves/secp256k1'
import {bytesToHex} from '@noble/hashes/utils'
import {sha256} from '@noble/hashes/sha256'
import { schnorr } from '@noble/curves/secp256k1'
import { bytesToHex } from '@noble/hashes/utils'
import { sha256 } from '@noble/hashes/sha256'
import {utf8Encoder} from './utils.ts'
import {getPublicKey} from './keys.ts'
import { utf8Encoder } from './utils.ts'
import { getPublicKey } from './keys.ts'
import type {Event} from './event.ts'
import type { Event } from './event.ts'
export type Parameters = {
pubkey: string // the key to whom the delegation will be given
@@ -21,32 +21,24 @@ export type Delegation = {
sig: string
}
export function createDelegation(
privateKey: string,
parameters: Parameters
): Delegation {
export function createDelegation(privateKey: string, parameters: Parameters): Delegation {
let conditions = []
if ((parameters.kind || -1) >= 0) conditions.push(`kind=${parameters.kind}`)
if (parameters.until) conditions.push(`created_at<${parameters.until}`)
if (parameters.since) conditions.push(`created_at>${parameters.since}`)
let cond = conditions.join('&')
if (cond === '')
throw new Error('refusing to create a delegation without any conditions')
if (cond === '') throw new Error('refusing to create a delegation without any conditions')
let sighash = sha256(
utf8Encoder.encode(`nostr:delegation:${parameters.pubkey}:${cond}`)
)
let sighash = sha256(utf8Encoder.encode(`nostr:delegation:${parameters.pubkey}:${cond}`))
let sig = bytesToHex(
schnorr.sign(sighash, privateKey)
)
let sig = bytesToHex(schnorr.sign(sighash, privateKey))
return {
from: getPublicKey(privateKey),
to: parameters.pubkey,
cond,
sig
sig,
}
}
@@ -65,27 +57,14 @@ export function getDelegator(event: Event<number>): string | null {
let [key, operator, value] = conditions[i].split(/\b/)
// the supported conditions are just 'kind' and 'created_at' for now
if (key === 'kind' && operator === '=' && event.kind === parseInt(value))
continue
else if (
key === 'created_at' &&
operator === '<' &&
event.created_at < parseInt(value)
)
continue
else if (
key === 'created_at' &&
operator === '>' &&
event.created_at > parseInt(value)
)
continue
if (key === 'kind' && operator === '=' && event.kind === parseInt(value)) continue
else if (key === 'created_at' && operator === '<' && event.created_at < parseInt(value)) continue
else if (key === 'created_at' && operator === '>' && event.created_at > parseInt(value)) continue
else return null // invalid condition
}
// check signature
let sighash = sha256(
utf8Encoder.encode(`nostr:delegation:${event.pubkey}:${cond}`)
)
let sighash = sha256(utf8Encoder.encode(`nostr:delegation:${event.pubkey}:${cond}`))
if (!schnorr.verify(sig, sighash, pubkey)) return null
return pubkey