Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Mem struct {
values map[string][]byte
metadata map[string]*metadata
nowFn func() time.Time
inc int64
inc map[string]int64
}

type metadata struct {
Expand All @@ -38,6 +38,7 @@ func NewMem() *Mem {
paths: NewStringSet(),
values: map[string][]byte{},
metadata: map[string]*metadata{},
inc: map[string]int64{},
nowFn: time.Now,
}
}
Expand Down Expand Up @@ -293,11 +294,13 @@ func randBytes(length int) []byte {
func (m *Mem) EventsAdd(ctx context.Context, path string, data [][]byte) ([]*events.Event, error) {
out := make([]*events.Event, 0, len(data))
for _, b := range data {
m.inc++
inc := m.inc[path]
inc++
m.inc[path] = inc
id := encoding.MustEncode(randBytes(32), encoding.Base62)
event := &events.Event{
Data: b,
Index: m.inc,
Index: inc,
Timestamp: m.nowFn(),
}
b, err := json.Marshal(event)
Expand All @@ -313,7 +316,7 @@ func (m *Mem) EventsAdd(ctx context.Context, path string, data [][]byte) ([]*eve
return out, nil
}

// EventsDelete removes events at path.
// EventsDelete removes all events at path.
func (m *Mem) EventsDelete(ctx context.Context, path string) (bool, error) {
ok, err := m.Delete(ctx, path)
if err != nil {
Expand Down
13 changes: 11 additions & 2 deletions edx25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func NewEdX25519KeyFromPrivateKey(privateKey *[ed25519.PrivateKeySize]byte) *EdX
}

func (k *EdX25519Key) setPrivateKey(b []byte) error {
if len(b) != ed25519.PrivateKeySize {
return errors.Errorf("invalid private key length %d", len(b))
}
// Derive public key from private key
edpk := ed25519.PrivateKey(b)
publicKey := edpk.Public().(ed25519.PublicKey)
Expand Down Expand Up @@ -94,7 +97,7 @@ func (k *EdX25519Key) Signer() crypto.Signer {

// MarshalText for encoding.TextMarshaler interface.
func (k *EdX25519Key) MarshalText() ([]byte, error) {
return []byte(encoding.MustEncode(k.Bytes(), encoding.Base64)), nil
return []byte(encoding.MustEncode(k.Seed()[:], encoding.Base64)), nil
}

// UnmarshalText for encoding.TextUnmarshaler interface.
Expand All @@ -103,7 +106,13 @@ func (k *EdX25519Key) UnmarshalText(s []byte) error {
if err != nil {
return err
}
if err := k.setPrivateKey(b); err != nil {
var privateKey []byte
if len(b) == 32 {
privateKey = ed25519.NewKeyFromSeed(b)
} else {
privateKey = b
}
if err := k.setPrivateKey(privateKey); err != nil {
return err
}
return nil
Expand Down
11 changes: 10 additions & 1 deletion edx25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,27 @@ func TestSign(t *testing.T) {
}

func TestEdX25519JSON(t *testing.T) {
key := keys.GenerateEdX25519Key()
seed := keys.Bytes32(bytes.Repeat([]byte{0x01}, 32))

key := keys.NewEdX25519KeyFromSeed(seed)

type test struct {
Key *keys.EdX25519Key `json:"key"`
}

b, err := json.Marshal(test{Key: key})
require.NoError(t, err)
expected := `{"key":"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="}`

require.Equal(t, expected, string(b))
var out test
err = json.Unmarshal(b, &out)
require.NoError(t, err)
require.Equal(t, key.Bytes(), out.Key.Bytes())

// 64 byte private key
old := `{"key":"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQGKiOPddAnxlf1S2y08ul1yymcJvx2UEhvzdIgBtA9vXA=="}`
err = json.Unmarshal([]byte(old), &out)
require.NoError(t, err)
require.Equal(t, key.Bytes(), out.Key.Bytes())
}