This commit is contained in:
fiatjaf
2022-12-27 07:49:26 -03:00
parent 28663f21f0
commit e4c5dfbebb
3 changed files with 234 additions and 21 deletions

View File

@@ -1,17 +1,28 @@
package nip19
import (
"encoding/hex"
"strings"
"bytes"
)
// TranslatePublicKey turns a hex or bech32 public key into always hex
func TranslatePublicKey(bech32orHexKey string) string {
if strings.HasPrefix(bech32orHexKey, "npub1") {
data, _, _ := Decode(bech32orHexKey)
return hex.EncodeToString(data)
const (
TLVDefault uint8 = 0
TLVRelay uint8 = 1
)
func readTLVEntry(data []byte) (typ uint8, value []byte) {
if len(data) < 2 {
return 0, nil
}
// just return what we got
return bech32orHexKey
typ = data[0]
length := int(data[1])
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)
}