mirror of
https://github.com/arnaucube/slowlorisdb.git
synced 2026-02-28 05:46:48 +01:00
add initial node
This commit is contained in:
@@ -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
|
||||
|
||||

|
||||
|
||||
@@ -2,8 +2,9 @@ package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slothdb/db"
|
||||
"time"
|
||||
|
||||
"github.com/arnaucube/slowlorisdb/db"
|
||||
)
|
||||
|
||||
type Blockchain struct {
|
||||
|
||||
@@ -2,9 +2,9 @@ package core
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"slothdb/db"
|
||||
"testing"
|
||||
|
||||
"github.com/arnaucube/slowlorisdb/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
||||
48
core/keys.go
48
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
|
||||
}
|
||||
|
||||
@@ -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
go.mod
Normal file
8
go.mod
Normal file
@@ -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
go.sum
Normal file
25
go.sum
Normal file
@@ -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
node/node.go
Normal file
35
node/node.go
Normal file
@@ -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
node/node_test.go
Normal file
38
node/node_test.go
Normal file
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user