Skip to content

Commit

Permalink
using crypto.key as value and adding isLeaf() for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
techlab-lainio committed Nov 10, 2021
1 parent 3f8e8f1 commit a1e458f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 6 additions & 2 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ func (c Chain) Bytes() []byte {
}

func (c Chain) Invite(
invitersKey *crypto.Key,
invitersKey crypto.Key,
inviteesPubKey crypto.PubKey,
position int,
) (nc Chain) {
assert.D.True(invitersKey.PubKeyEqual(c.LeafPubKey()))
assert.D.True(c.isLeaf(invitersKey), "only left can ")

newBlock := Block{
HashToPrev: c.hashToLeaf(),
Expand All @@ -144,6 +144,10 @@ func (c Chain) Invite(
return nc
}

func (c Chain) isLeaf(invitersKey crypto.Key) bool {
return invitersKey.PubKeyEqual(c.LeafPubKey())
}

func (c Chain) LeafPubKey() crypto.PubKey {
assert.D.True(len(c.Blocks) > 0)

Expand Down
14 changes: 7 additions & 7 deletions chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

var (
root, alice, bob struct {
*crypto.Key
crypto.Key
Chain
}
c Chain
rootKey, inviteeKey *crypto.Key
rootKey, inviteeKey crypto.Key
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestInvitation(t *testing.T) {
assert.True(t, bob.Verify())

cecilia := struct {
*crypto.Key
crypto.Key
Chain
}{
Key: crypto.NewKey(),
Expand All @@ -105,7 +105,7 @@ func TestInvitation(t *testing.T) {
func TestCommonInviter(t *testing.T) {
// alice and bod have common root
cecilia := struct {
*crypto.Key
crypto.Key
Chain
}{
Key: crypto.NewKey(),
Expand All @@ -116,7 +116,7 @@ func TestCommonInviter(t *testing.T) {
assert.True(t, cecilia.Verify())

david := struct {
*crypto.Key
crypto.Key
Chain
}{
Key: crypto.NewKey(),
Expand All @@ -127,7 +127,7 @@ func TestCommonInviter(t *testing.T) {
assert.Equal(t, 0, CommonInviter(cecilia.Chain, david.Chain),
"cecilia and david have only common root")
edvin := struct {
*crypto.Key
crypto.Key
Chain
}{
Key: crypto.NewKey(),
Expand All @@ -152,7 +152,7 @@ func TestSameInviter(t *testing.T) {
assert.False(t, SameInviter(c, bob.Chain))

cecilia := struct {
*crypto.Key
crypto.Key
Chain
}{
Key: crypto.NewKey(),
Expand Down
4 changes: 2 additions & 2 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type Key struct {
PubKey
}

func NewKey() *Key {
func NewKey() Key {
pub, priv, err := ed25519.GenerateKey(nil)
err2.Check(err)
return &Key{PrivKey: priv, PubKey: pub}
return Key{PrivKey: priv, PubKey: pub}
}

func (k Key) PubKeyEqual(pubKey PubKey) bool {
Expand Down

0 comments on commit a1e458f

Please sign in to comment.