Skip to content

Commit

Permalink
Challenge function added
Browse files Browse the repository at this point in the history
  • Loading branch information
techlab-lainio committed Sep 14, 2021
1 parent ab3221e commit 32f46ef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 19 additions & 1 deletion chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ type Block struct {
Position int
}

func NewVerifyBlock() Block {
return Block{
HashToPrev: crypto.RandSlice(32),
InviteePubKey: crypto.RandSlice(32),
}
}

func (b Block) Bytes() []byte {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
Expand All @@ -41,7 +48,11 @@ func EqualBlocks(b1, b2 Block) bool {
}

func (b Block) VerifySign(invitersPubKey crypto.PubKey) bool {
return crypto.VerifySign(invitersPubKey, b.NoSign().Bytes(), b.InvitersSignature)
return crypto.VerifySign(
invitersPubKey,
b.NoSign().Bytes(),
b.InvitersSignature,
)
}

// Chain is the data type for Invitation Chain, it's ID is rootPubKey
Expand Down Expand Up @@ -186,6 +197,13 @@ func (c Chain) IsInvitee(invitee Chain) bool {
)
}

func (c Chain) Callenge(f func(d []byte) crypto.Signature) bool {
pubKey := c.lastBlock().InviteePubKey
cb := NewVerifyBlock().Bytes()
sig := f(cb)
return crypto.VerifySign(pubKey, cb, sig)
}

func (c Chain) firstBlock() Block {
return c.Blocks[0]
}
Expand Down
15 changes: 15 additions & 0 deletions chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,19 @@ func TestSameInviter(t *testing.T) {
func TestChallengeInvitee(t *testing.T) {
// chain leaf is the only part who has the prive key for the leaf, so
// it can response the challenge properly.
assert.True(t, alice.Chain.Callenge(
func (d []byte) crypto.Signature {
return alice.Sign(d)
},
))
assert.False(t, bob.Chain.Callenge(
func (d []byte) crypto.Signature {
return alice.Sign(d)
},
))
assert.True(t, bob.Chain.Callenge(
func (d []byte) crypto.Signature {
return bob.Sign(d)
},
))
}

0 comments on commit 32f46ef

Please sign in to comment.