Skip to content

Instantly share code, notes, and snippets.

@fiatjaf
Created October 17, 2023 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiatjaf/586b55ca99d61e0655478f1abad1886e to your computer and use it in GitHub Desktop.
Save fiatjaf/586b55ca99d61e0655478f1abad1886e to your computer and use it in GitHub Desktop.
adding relay url hashes to nostr keys
original keypair:
a987223eeae5151a288fa29e6385b31db9c71eb3fcd50d6e2e1950c1c308971b =>
02627895b1b0feab4445fe3a35013d7865ec9fba4d671474c2499baa83c4689532
hash:
7b539d3ad7d7213c8fdc4ab60cbe4ae54303cc0b99d685772c378efb4d1e16df =>
02f5bb62900b0a777957697904e3aff10db89456274fc0fba5e91b2fd1b5ba7511
derived keypair:
24dabf79c2bc3656b86bed547043fe04421c0dd8e762f2a99a7e81303ff06cb9 =>
02b50553723ea4765da9b24f589217860f44d425da6f335dc97e4e1b74875b0eeb
result of adding hash point to original pubkey:
02b50553723ea4765da9b24f589217860f44d425da6f335dc97e4e1b74875b0eeb
result of subtracting hash point from derived pubkey:
02627895b1b0feab4445fe3a35013d7865ec9fba4d671474c2499baa83c4689532
import {secp256k1} from '@noble/curves/secp256k1'
import {
bytesToHex,
bytesToNumberBE,
numberToBytesBE
} from '@noble/curves/abstract/utils'
import {sha256} from '@noble/hashes/sha256' // ECMAScript modules (ESM) and Common.js
const originalPriv = secp256k1.utils.randomPrivateKey()
const originalPub = secp256k1.getPublicKey(originalPriv)
console.log(
'original keypair:\n ',
bytesToHex(originalPriv),
'=>\n ',
bytesToHex(originalPub)
)
const relay = 'wss://inbox.nostr.wine'
const hashPriv = sha256(relay)
const hashPub = secp256k1.getPublicKey(hashPriv)
console.log('hash:\n ', bytesToHex(hashPriv), '=>\n ', bytesToHex(hashPub))
const derivedPriv = numberToBytesBE(
(bytesToNumberBE(originalPriv) + bytesToNumberBE(hashPriv)) %
secp256k1.CURVE.n,
32
)
const derivedPub = secp256k1.getPublicKey(derivedPriv)
console.log(
'derived keypair:\n ',
bytesToHex(derivedPriv),
'=>\n ',
bytesToHex(derivedPub)
)
const originalPoint = secp256k1.ProjectivePoint.fromHex(originalPub)
const hashPoint = secp256k1.ProjectivePoint.fromHex(hashPub)
const result = originalPoint.add(hashPoint)
console.log(
'result of adding hash point to original pubkey:\n ',
result.toHex()
)
const derivedPoint = secp256k1.ProjectivePoint.fromHex(derivedPub)
const result2 = derivedPoint.subtract(hashPoint)
console.log(
'result of subtracting hash point from derived pubkey:\n ',
result2.toHex()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment