Skip to content

Commit

Permalink
first working version with real crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
techlab-lainio committed Aug 23, 2021
1 parent a491b38 commit b062cf4
Show file tree
Hide file tree
Showing 5 changed files with 504 additions and 44 deletions.
71 changes: 29 additions & 42 deletions chain/chain.go
Original file line number Diff line number Diff line change
@@ -1,81 +1,68 @@
package chain

import "github.com/lainio/err2/assert"
import (
"bytes"
"crypto/sha256"
"encoding/gob"

"github.com/findy-network/ic/crypto"
"github.com/lainio/err2"
"github.com/lainio/err2/assert"
)

type Block struct {
HashToPrev []byte // check the size later
InviteePubKey PubKey // TODO: check the type later?
InvitersSignature Signature // TODO: check the type
HashToPrev []byte // check the size later
InviteePubKey crypto.PubKey // TODO: check the type later?
InvitersSignature crypto.Signature // TODO: check the type
Position int
}

func (b Block) Hash() []byte {
return nil // TODO:
func (b Block) Bytes() []byte {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err2.Check(enc.Encode(b))
return buf.Bytes()
}

// Chain is the data type for Invitation Chain, it's ID is rootPubKey. TODO:
type Chain struct {
blocks []Block
}

type PubKey = []byte
type Key struct {
PrivKey []byte
PubKey
}

func (k Key) PubKeyEqual( pubKey PubKey) bool {
return byteEqual(k.PubKey, pubKey)
}

func (k Key) Sign(h []byte) Signature {
return nil // TODO:
}

type Signature = []byte

func NewChain(rootPubKey PubKey) Chain {
func NewChain(rootPubKey crypto.PubKey) Chain {
chain := Chain{blocks: make([]Block, 1, 12)}
chain.blocks[0] = Block{
HashToPrev: nil,
InviteePubKey: rootPubKey,
HashToPrev: nil,
InviteePubKey: rootPubKey,
InvitersSignature: nil,
}
return chain
}

func (c *Chain) AddBlock(invitersKey Key, inviteesPubKey PubKey, position int) {
func (c *Chain) AddBlock(invitersKey *crypto.Key, inviteesPubKey crypto.PubKey, position int) {
assert.D.True(invitersKey.PubKeyEqual(c.LeafPubKey()))

newBlock := Block{
HashToPrev: c.HashToLeaf(),
InviteePubKey: inviteesPubKey,
Position: position,
}
h := newBlock.Hash()
newBlock.InvitersSignature = invitersKey.Sign(h)
newBlock.InvitersSignature = invitersKey.Sign(newBlock.Bytes())

c.blocks = append(c.blocks, newBlock)
}

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

return c.blocks[len(c.blocks)-1].InviteePubKey
}

func (c Chain) HashToLeaf() []byte {
return nil // TODO:
}

func byteEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
if c.blocks == nil {
return nil
}
lastBlockBytes := c.blocks[len(c.blocks)-1].Bytes()
ha := sha256.Sum256(lastBlockBytes)
return ha[:]
}
5 changes: 3 additions & 2 deletions chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package chain
import (
"testing"

"github.com/findy-network/ic/crypto"
"github.com/stretchr/testify/assert"
)

func TestNewChain(t *testing.T) {
rootKey := Key{}
rootKey := crypto.NewKey()
c := NewChain(rootKey.PubKey)
inviteeKey := Key{}
inviteeKey := crypto.NewKey()
level := 1
c.AddBlock(rootKey, inviteeKey.PubKey, level)
assert.Len(t, c.blocks, 2)
Expand Down
45 changes: 45 additions & 0 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package crypto

import (
"crypto/ed25519"

"github.com/lainio/err2"
)

type PubKey = []byte
type Key struct {
PrivKey []byte
PubKey
}

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

func (k Key) PubKeyEqual(pubKey PubKey) bool {
return byteEqual(k.PubKey, pubKey)
}

func (k Key) Sign(h []byte) Signature {
return ed25519.Sign(k.PrivKey, h)
}

func (k Key) VerifySign(msg []byte, sig Signature) bool {
return ed25519.Verify(k.PubKey, msg, sig)
}

type Signature = []byte

func byteEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/findy-network/ic
go 1.16

require (
github.com/google/tink/go v1.6.0 // indirect
github.com/lainio/err2 v0.6.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
)
Loading

0 comments on commit b062cf4

Please sign in to comment.