mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-27 01:02:44 +02:00
nip19: helper for building nevent from RelayEvent.
This commit is contained in:
parent
1b31dd892e
commit
f19efb4013
34
nip19/helpers.go
Normal file
34
nip19/helpers.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package nip19
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TLVDefault uint8 = 0
|
||||||
|
TLVRelay uint8 = 1
|
||||||
|
TLVAuthor uint8 = 2
|
||||||
|
TLVKind uint8 = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
func readTLVEntry(data []byte) (typ uint8, value []byte) {
|
||||||
|
if len(data) < 2 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
typ = data[0]
|
||||||
|
length := int(data[1])
|
||||||
|
if len(data) < 2+length {
|
||||||
|
return typ, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
value = data[2 : 2+length]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeTLVEntry(buf *bytes.Buffer, typ uint8, value []byte) {
|
||||||
|
length := len(value)
|
||||||
|
buf.WriteByte(typ)
|
||||||
|
buf.WriteByte(uint8(length))
|
||||||
|
buf.Write(value)
|
||||||
|
}
|
@ -1,34 +1,8 @@
|
|||||||
package nip19
|
package nip19
|
||||||
|
|
||||||
import (
|
import "github.com/nbd-wtf/go-nostr"
|
||||||
"bytes"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
func NeventFromRelayEvent(ie nostr.RelayEvent) string {
|
||||||
TLVDefault uint8 = 0
|
v, _ := EncodeEvent(ie.ID, []string{ie.Relay.URL}, ie.PubKey)
|
||||||
TLVRelay uint8 = 1
|
return v
|
||||||
TLVAuthor uint8 = 2
|
|
||||||
TLVKind uint8 = 3
|
|
||||||
)
|
|
||||||
|
|
||||||
func readTLVEntry(data []byte) (typ uint8, value []byte) {
|
|
||||||
if len(data) < 2 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
typ = data[0]
|
|
||||||
length := int(data[1])
|
|
||||||
if len(data) < 2+length {
|
|
||||||
return typ, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
value = data[2 : 2+length]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeTLVEntry(buf *bytes.Buffer, typ uint8, value []byte) {
|
|
||||||
length := len(value)
|
|
||||||
buf.WriteByte(typ)
|
|
||||||
buf.WriteByte(uint8(length))
|
|
||||||
buf.Write(value)
|
|
||||||
}
|
}
|
||||||
|
4
pool.go
4
pool.go
@ -46,9 +46,7 @@ type RelayEvent struct {
|
|||||||
Relay *Relay
|
Relay *Relay
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ie RelayEvent) String() string {
|
func (ie RelayEvent) String() string { return fmt.Sprintf("[%s] >> %s", ie.Relay.URL, ie.Event) }
|
||||||
return fmt.Sprintf("[%s] >> %s", ie.Relay.URL, ie.Event)
|
|
||||||
}
|
|
||||||
|
|
||||||
type PoolOption interface {
|
type PoolOption interface {
|
||||||
ApplyPoolOption(*SimplePool)
|
ApplyPoolOption(*SimplePool)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user