switch base64 to raw

This commit is contained in:
2025-01-31 03:01:15 -01:06
parent ab032cd296
commit d36bcdb243
6 changed files with 67 additions and 58 deletions

View File

@@ -24,6 +24,8 @@ content:\n // literally this word on one line *directly* after the newline of th
The canonical form is exactly this, except for the signature and following linebreak, hashed with Blake2b.
The binary data - Event Ids, Pubkeys and Signatures are encoded in raw base64 URL encoding (without padding), Signatures are 86 characters, Ids and Pubkeys are 43 characters long.
The database stored form of this event should make use of an event ID hash to monotonic collision free serial table and an event table.
Event ID hashes will be encoded in URL-base64 where used in tags or mentioned in content with the prefix `e:`. Public keys must be prefixed with `p:` Tag keys should be intelligible words and a specification for their structure should be defined by users of them and shared with other REALY devs.

View File

@@ -2,13 +2,15 @@ package event
import (
"protocol.realy.lol/pkg/event/types"
"protocol.realy.lol/pkg/pubkey"
"protocol.realy.lol/pkg/signature"
)
type Event struct {
Type types.T
Pubkey []byte
Pubkey pubkey.P
Timestamp int64
Tags [][]byte
Content []byte
Signature []byte
Signature signature.S
}

View File

@@ -7,7 +7,7 @@ import (
"io"
)
const Len = 44
const Len = 43
type P struct{ ed25519.PublicKey }
@@ -33,7 +33,7 @@ func (p *P) Marshal(dst []byte) (result []byte, err error) {
return
}
buf := bytes.NewBuffer(result)
w := base64.NewEncoder(base64.URLEncoding, buf)
w := base64.NewEncoder(base64.RawURLEncoding, buf)
if _, err = w.Write(p.PublicKey); chk.E(err) {
return
}
@@ -62,7 +62,7 @@ func (p *P) Unmarshal(data []byte) (rem []byte, err error) {
return
}
p.PublicKey = make([]byte, ed25519.PublicKeySize)
if _, err = base64.URLEncoding.Decode(p.PublicKey, rem[:i]); chk.E(err) {
if _, err = base64.RawURLEncoding.Decode(p.PublicKey, rem[:i]); chk.E(err) {
return
}
rem = rem[i+1:]

View File

@@ -8,31 +8,33 @@ import (
)
func TestP_Marshal_Unmarshal(t *testing.T) {
pk := make([]byte, ed25519.PublicKeySize)
var err error
if _, err = rand.Read(pk); chk.E(err) {
t.Fatal(err)
}
log.I.S(pk)
var p *P
if p, err = New(pk); chk.E(err) {
t.Fatal(err)
}
var o []byte
if o, err = p.Marshal(nil); chk.E(err) {
t.Fatal(err)
}
log.I.F("%d %s", len(o), o)
p2 := &P{}
var rem []byte
if rem, err = p2.Unmarshal(o); chk.E(err) {
t.Fatal(err)
}
if len(rem) > 0 {
log.I.F("%d %s", len(rem), rem)
}
log.I.S(p2.PublicKey)
if !bytes.Equal(pk, p2.PublicKey) {
t.Fatal("public key did not encode/decode faithfully")
for range 10 {
pk := make([]byte, ed25519.PublicKeySize)
if _, err = rand.Read(pk); chk.E(err) {
t.Fatal(err)
}
log.I.S(pk)
var p *P
if p, err = New(pk); chk.E(err) {
t.Fatal(err)
}
var o []byte
if o, err = p.Marshal(nil); chk.E(err) {
t.Fatal(err)
}
log.I.F("%d %s", len(o), o)
p2 := &P{}
var rem []byte
if rem, err = p2.Unmarshal(o); chk.E(err) {
t.Fatal(err)
}
if len(rem) > 0 {
log.I.F("%d %s", len(rem), rem)
}
log.I.S(p2.PublicKey)
if !bytes.Equal(pk, p2.PublicKey) {
t.Fatal("public key did not encode/decode faithfully")
}
}
}

View File

@@ -7,7 +7,7 @@ import (
"io"
)
const Len = 88
const Len = 86
type S struct{ Signature []byte }
@@ -33,7 +33,7 @@ func (p *S) Marshal(dst []byte) (result []byte, err error) {
return
}
buf := bytes.NewBuffer(result)
w := base64.NewEncoder(base64.URLEncoding, buf)
w := base64.NewEncoder(base64.RawURLEncoding, buf)
if _, err = w.Write(p.Signature); chk.E(err) {
return
}
@@ -62,7 +62,7 @@ func (p *S) Unmarshal(data []byte) (rem []byte, err error) {
return
}
p.Signature = make([]byte, ed25519.SignatureSize)
if _, err = base64.URLEncoding.Decode(p.Signature, rem[:i]); chk.E(err) {
if _, err = base64.RawURLEncoding.Decode(p.Signature, rem[:i]); chk.E(err) {
return
}
rem = rem[i+1:]

View File

@@ -8,31 +8,34 @@ import (
)
func TestS_Marshal_Unmarshal(t *testing.T) {
sig := make([]byte, ed25519.SignatureSize)
var err error
if _, err = rand.Read(sig); chk.E(err) {
t.Fatal(err)
}
log.I.S(sig)
var s *S
if s, err = New(sig); chk.E(err) {
t.Fatal(err)
}
var o []byte
if o, err = s.Marshal(nil); chk.E(err) {
t.Fatal(err)
}
log.I.F("%d %s", len(o), o)
p2 := &S{}
var rem []byte
if rem, err = p2.Unmarshal(o); chk.E(err) {
t.Fatal(err)
}
if len(rem) > 0 {
log.I.F("%d %s", len(rem), rem)
}
log.I.S(p2.Signature)
if !bytes.Equal(sig, p2.Signature) {
t.Fatal("signature did not encode/decode faithfully")
for range 10 {
sig := make([]byte, ed25519.SignatureSize)
if _, err = rand.Read(sig); chk.E(err) {
t.Fatal(err)
}
log.I.S(sig)
var s *S
if s, err = New(sig); chk.E(err) {
t.Fatal(err)
}
var o []byte
if o, err = s.Marshal(nil); chk.E(err) {
t.Fatal(err)
}
log.I.F("%d %s", len(o), o)
p2 := &S{}
var rem []byte
if rem, err = p2.Unmarshal(o); chk.E(err) {
t.Fatal(err)
}
if len(rem) > 0 {
log.I.F("%d %s", len(rem), rem)
}
log.I.S(p2.Signature)
if !bytes.Equal(sig, p2.Signature) {
t.Fatal("signature did not encode/decode faithfully")
}
}
}