mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 16:19:07 -05:00
parse hex and encode to npub/nprofile/etc.
This commit is contained in:
parent
18524fe613
commit
268022800d
|
@ -9,12 +9,55 @@ import io.circe.syntax.*
|
||||||
import calico.*
|
import calico.*
|
||||||
import calico.html.io.{*, given}
|
import calico.html.io.{*, given}
|
||||||
import calico.syntax.*
|
import calico.syntax.*
|
||||||
|
import scodec.bits.ByteVector
|
||||||
import scoin.*
|
import scoin.*
|
||||||
import snow.*
|
import snow.*
|
||||||
|
|
||||||
import Utils.*
|
import Utils.*
|
||||||
|
|
||||||
object Components {
|
object Components {
|
||||||
|
def render32Bytes(bytes32: ByteVector32): Resource[IO, HtmlDivElement[IO]] =
|
||||||
|
div(
|
||||||
|
cls := "text-md",
|
||||||
|
entry("canonical hex", bytes32.toHex),
|
||||||
|
"if this is a public key:",
|
||||||
|
div(
|
||||||
|
cls := "pl-2 mb-2",
|
||||||
|
entry(
|
||||||
|
"npub",
|
||||||
|
NIP19.encode(XOnlyPublicKey(bytes32))
|
||||||
|
),
|
||||||
|
entry(
|
||||||
|
"nprofile",
|
||||||
|
NIP19.encode(ProfilePointer(XOnlyPublicKey(bytes32)))
|
||||||
|
)
|
||||||
|
),
|
||||||
|
"if this is a private key:",
|
||||||
|
div(
|
||||||
|
cls := "pl-2 mb-2",
|
||||||
|
entry(
|
||||||
|
"nsec",
|
||||||
|
NIP19.encode(PrivateKey(bytes32))
|
||||||
|
),
|
||||||
|
entry(
|
||||||
|
"npub",
|
||||||
|
NIP19.encode(XOnlyPublicKey(bytes32))
|
||||||
|
),
|
||||||
|
entry(
|
||||||
|
"nprofile",
|
||||||
|
NIP19.encode(ProfilePointer(XOnlyPublicKey(bytes32)))
|
||||||
|
)
|
||||||
|
),
|
||||||
|
"if this is an event id:",
|
||||||
|
div(
|
||||||
|
cls := "pl-2 mb-2",
|
||||||
|
entry(
|
||||||
|
"nevent",
|
||||||
|
NIP19.encode(EventPointer(bytes32.toHex))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def renderEventPointer(
|
def renderEventPointer(
|
||||||
evp: snow.EventPointer
|
evp: snow.EventPointer
|
||||||
): Resource[IO, HtmlDivElement[IO]] =
|
): Resource[IO, HtmlDivElement[IO]] =
|
||||||
|
|
|
@ -90,10 +90,11 @@ object Main extends IOWebApp {
|
||||||
div(
|
div(
|
||||||
cls := "w-full flex my-5",
|
cls := "w-full flex my-5",
|
||||||
store.result.map {
|
store.result.map {
|
||||||
case Left(msg) => div(msg)
|
case Left(msg) => div(msg)
|
||||||
case Right(event: Event) => renderEvent(event, store)
|
case Right(bytes: ByteVector32) => render32Bytes(bytes)
|
||||||
case Right(pp: ProfilePointer) => renderProfilePointer(pp)
|
case Right(event: Event) => renderEvent(event, store)
|
||||||
case Right(evp: EventPointer) => renderEventPointer(evp)
|
case Right(pp: ProfilePointer) => renderProfilePointer(pp)
|
||||||
|
case Right(evp: EventPointer) => renderEventPointer(evp)
|
||||||
case Right(sk: PrivateKey) =>
|
case Right(sk: PrivateKey) =>
|
||||||
renderProfilePointer(
|
renderProfilePointer(
|
||||||
ProfilePointer(pubkey = sk.publicKey.xonly),
|
ProfilePointer(pubkey = sk.publicKey.xonly),
|
||||||
|
|
|
@ -1,33 +1,41 @@
|
||||||
|
import scala.util.Try
|
||||||
import io.circe.parser.*
|
import io.circe.parser.*
|
||||||
import cats.syntax.all.*
|
import cats.syntax.all.*
|
||||||
|
import scodec.bits.ByteVector
|
||||||
import scoin.*
|
import scoin.*
|
||||||
import snow.*
|
import snow.*
|
||||||
|
|
||||||
type Result = Either[
|
type Result = Either[
|
||||||
String,
|
String,
|
||||||
Event | PrivateKey | AddressPointer | EventPointer | ProfilePointer
|
Event | PrivateKey | AddressPointer | EventPointer | ProfilePointer |
|
||||||
|
ByteVector32
|
||||||
]
|
]
|
||||||
|
|
||||||
object Parser {
|
object Parser {
|
||||||
def parseInput(input: String): Result =
|
def parseInput(input: String): Result =
|
||||||
NIP19.decode(input) match {
|
ByteVector
|
||||||
case Right(pp: ProfilePointer) => Right(pp)
|
.fromHex(input)
|
||||||
case Right(evp: EventPointer) => Right(evp)
|
.flatMap(b => Try(Right(ByteVector32(b))).toOption)
|
||||||
case Right(sk: PrivateKey) => Right(sk)
|
.getOrElse(
|
||||||
case Right(addr: AddressPointer) => Right(addr)
|
NIP19.decode(input) match {
|
||||||
case Left(_) =>
|
case Right(pp: ProfilePointer) => Right(pp)
|
||||||
parse(input) match {
|
case Right(evp: EventPointer) => Right(evp)
|
||||||
case Left(err: io.circe.ParsingFailure) =>
|
case Right(sk: PrivateKey) => Right(sk)
|
||||||
Left("not valid JSON or NIP-19 code")
|
case Right(addr: AddressPointer) => Right(addr)
|
||||||
case Right(json) =>
|
case Left(_) =>
|
||||||
json
|
parse(input) match {
|
||||||
.as[Event]
|
case Left(err: io.circe.ParsingFailure) =>
|
||||||
.leftMap { err =>
|
Left("not valid JSON or NIP-19 code")
|
||||||
err.pathToRootString match {
|
case Right(json) =>
|
||||||
case None => s"decoding ${err.pathToRootString}"
|
json
|
||||||
case Some(path) => s"field $path is missing or wrong"
|
.as[Event]
|
||||||
}
|
.leftMap { err =>
|
||||||
}
|
err.pathToRootString match {
|
||||||
|
case None => s"decoding ${err.pathToRootString}"
|
||||||
|
case Some(path) => s"field $path is missing or wrong"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ object Store {
|
||||||
|
|
||||||
_ <- input.discrete
|
_ <- input.discrete
|
||||||
.evalTap(input => IO.cede *> window.localStorage.setItem(key, input))
|
.evalTap(input => IO.cede *> window.localStorage.setItem(key, input))
|
||||||
.evalTap(input => result.set(Parser.parseInput(input)))
|
.evalTap(input => result.set(Parser.parseInput(input.trim())))
|
||||||
.compile
|
.compile
|
||||||
.drain
|
.drain
|
||||||
.background
|
.background
|
||||||
|
|
Loading…
Reference in New Issue
Block a user