-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first working version with real crypto
- Loading branch information
1 parent
a491b38
commit b062cf4
Showing
5 changed files
with
504 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.