Browse Source

add initial node

master
arnaucube 5 years ago
parent
commit
67e7f532e4
9 changed files with 140 additions and 25 deletions
  1. +3
    -1
      README.md
  2. +2
    -1
      core/blockchain.go
  3. +1
    -1
      core/blockchain_test.go
  4. +27
    -21
      core/keys.go
  5. +1
    -1
      core/keys_test.go
  6. +8
    -0
      go.mod
  7. +25
    -0
      go.sum
  8. +35
    -0
      node/node.go
  9. +38
    -0
      node/node_test.go

+ 3
- 1
README.md

@ -2,6 +2,8 @@
Slow, decentralized and cryptographically consistent database
Basically this repo is a blockchain written from scratch, that allows to launch multiple simultaneous blockchains. The motivation of this project is to fill the empty hours during a travel.
Basically this repo is a blockchain written from scratch, that allows to launch multiple simultaneous blockchains.
Watch the blockchain in action: http://www.youtubemultiplier.com/5ca9c1a540b31-slowlorisdb-visual-representation.php
![slowloris](https://04019a5a-a-62cb3a1a-s-sites.googlegroups.com/site/jchristensensdigitalportfolio/slow-loris/IO-moth-eating-frozen-apple-sauce.jpg "slowloris")

+ 2
- 1
core/blockchain.go

@ -2,8 +2,9 @@ package core
import (
"errors"
"slothdb/db"
"time"
"github.com/arnaucube/slowlorisdb/db"
)
type Blockchain struct {

+ 1
- 1
core/blockchain_test.go

@ -2,9 +2,9 @@ package core
import (
"io/ioutil"
"slothdb/db"
"testing"
"github.com/arnaucube/slowlorisdb/db"
"github.com/stretchr/testify/assert"
)

+ 27
- 21
core/keys.go

@ -45,26 +45,35 @@ func AddressFromPrivK(privK *ecdsa.PrivateKey) Address {
return Address(h)
}
func PackSignature(r, s *big.Int) []byte {
sig := r.Bytes()
sig = append(sig, s.Bytes()...)
return sig
func (sig *Signature) Bytes(r, s *big.Int) []byte {
b := r.Bytes()
b = append(b, s.Bytes()...)
return b
}
func UnpackSignature(sig []byte) (*big.Int, *big.Int, error) {
if len(sig) != 64 {
return nil, nil, errors.New("Invalid signature")
func SignatureFromBytes(b []byte) (*Signature, error) {
if len(b) != 64 {
return nil, errors.New("Invalid signature")
}
rBytes := sig[:32]
sBytes := sig[32:]
rBytes := b[:32]
sBytes := b[32:]
r := new(big.Int).SetBytes(rBytes)
s := new(big.Int).SetBytes(sBytes)
return r, s, nil
sig := &Signature{
R: r,
S: s,
}
return sig, nil
}
func Sign(privK *ecdsa.PrivateKey, m []byte) ([]byte, error) {
type Signature struct {
R *big.Int
S *big.Int
}
func Sign(privK *ecdsa.PrivateKey, m []byte) (*Signature, error) {
r := big.NewInt(0)
s := big.NewInt(0)
@ -72,23 +81,20 @@ func Sign(privK *ecdsa.PrivateKey, m []byte) ([]byte, error) {
r, s, err := ecdsa.Sign(rand.Reader, privK, hashMsg[:])
if err != nil {
return []byte{}, err
return nil, err
}
sig := &Signature{
R: r,
S: s,
}
sig := PackSignature(r, s)
return sig, nil
}
func VerifySignature(pubK *ecdsa.PublicKey, m []byte, sig []byte) bool {
func VerifySignature(pubK *ecdsa.PublicKey, m []byte, sig Signature) bool {
hashMsg := HashBytes(m)
r, s, err := UnpackSignature(sig)
if err != nil {
return false
}
verified := ecdsa.Verify(pubK, hashMsg[:], r, s)
verified := ecdsa.Verify(pubK, hashMsg[:], sig.R, sig.S)
return verified
}

+ 1
- 1
core/keys_test.go

@ -29,6 +29,6 @@ func TestSignAndVerify(t *testing.T) {
assert.Nil(t, err)
// Verify
verified := VerifySignature(&privK.PublicKey, m, sig)
verified := VerifySignature(&privK.PublicKey, m, *sig)
assert.True(t, verified)
}

+ 8
- 0
go.mod

@ -0,0 +1,8 @@
module github.com/arnaucube/slowlorisdb
go 1.12
require (
github.com/stretchr/testify v1.3.0
github.com/syndtr/goleveldb v1.0.0
)

+ 25
- 0
go.sum

@ -0,0 +1,25 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 35
- 0
node/node.go

@ -0,0 +1,35 @@
package node
import (
"crypto/ecdsa"
"github.com/arnaucube/slowlorisdb/core"
"github.com/arnaucube/slowlorisdb/db"
)
type Node struct {
PrivK *ecdsa.PrivateKey
Addr core.Address
Bc *core.Blockchain
}
func NewNode(db *db.Db) (*Node, error) {
privK, err := core.NewKey()
if err != nil {
return nil, err
}
addr := core.AddressFromPrivK(privK)
bc := core.NewBlockchain(db)
node := &Node{
PrivK: privK,
Addr: addr,
Bc: bc,
}
return node, nil
}
func (node *Node) Sign(m []byte) (*core.Signature, error) {
return core.Sign(node.PrivK, m)
}

+ 38
- 0
node/node_test.go

@ -0,0 +1,38 @@
package node
import (
"io/ioutil"
"testing"
"github.com/arnaucube/slowlorisdb/core"
"github.com/arnaucube/slowlorisdb/db"
"github.com/stretchr/testify/assert"
)
func TestNode(t *testing.T) {
dir, err := ioutil.TempDir("", "db")
assert.Nil(t, err)
db, err := db.New(dir)
assert.Nil(t, err)
node, err := NewNode(db)
assert.Nil(t, err)
assert.Equal(t, node.Addr, core.AddressFromPrivK(node.PrivK))
}
func TestNodeSignature(t *testing.T) {
dir, err := ioutil.TempDir("", "db")
assert.Nil(t, err)
db, err := db.New(dir)
assert.Nil(t, err)
node, err := NewNode(db)
assert.Nil(t, err)
m := []byte("test")
sig, err := node.Sign(m)
assert.Nil(t, err)
pubK := node.PrivK.PublicKey
assert.True(t, core.VerifySignature(&pubK, m, *sig))
}

Loading…
Cancel
Save