Initial commit
This commit is contained in:
79
chain/block.go
Normal file
79
chain/block.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package chain
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/sha3"
|
||||
"log"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type block struct {
|
||||
Transactions []transaction
|
||||
HashPrevious []byte
|
||||
Nonce []byte
|
||||
}
|
||||
|
||||
func NewBlock(hashPrevious []byte) block {
|
||||
return block{
|
||||
Transactions: nil,
|
||||
HashPrevious: hashPrevious,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *block) AddTransaction(t transaction) {
|
||||
b.Transactions = append(b.Transactions, t)
|
||||
}
|
||||
|
||||
func (b block) GenerateHash(difficulty int) []byte {
|
||||
found := false
|
||||
var compBytes []byte
|
||||
for i := 0; i < difficulty; i++ {
|
||||
compBytes = append(compBytes, []byte("0x00")...)
|
||||
}
|
||||
for !found {
|
||||
random := make([]byte, 64)
|
||||
rand.Read(random)
|
||||
b.Nonce = random
|
||||
var buf bytes.Buffer
|
||||
err := gob.NewEncoder(&buf).Encode(b.Nonce)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
hasher := sha3.New512()
|
||||
hasher.Write(b.ToBytes())
|
||||
hash := hasher.Sum(nil)
|
||||
fmt.Printf("\033[2K\r%x\n", hash)
|
||||
if checkAllZero(XorSlice(hash[:difficulty], compBytes)) {
|
||||
return hash
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b block) ToBytes() []byte {
|
||||
var bytes []byte
|
||||
for i := 0; i < len(b.Transactions); i++ {
|
||||
bytes = append(bytes, b.Transactions[i].ToBytes()...)
|
||||
}
|
||||
bytes = append(bytes, b.Nonce...)
|
||||
return bytes
|
||||
}
|
||||
|
||||
func XorSlice(a []byte, b []byte) []byte {
|
||||
for i, _ := range a {
|
||||
a[i] = a[i] ^ b[i]
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func checkAllZero(a []byte) bool {
|
||||
fmt.Printf("Checking if all zero: %x\n", a)
|
||||
for i, _ := range a {
|
||||
if a[i] != 0x00 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
1
chain/chain.go
Normal file
1
chain/chain.go
Normal file
@@ -0,0 +1 @@
|
||||
package chain
|
||||
43
chain/transaction.go
Normal file
43
chain/transaction.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package chain
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"encoding/gob"
|
||||
"time"
|
||||
)
|
||||
|
||||
type transaction struct {
|
||||
Src string
|
||||
Dst string
|
||||
Amount int
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
func NewTransaction(source string, dst string, amount int) transaction {
|
||||
return transaction{Src: source, Dst: dst, Amount: amount, Timestamp: time.Now()}
|
||||
}
|
||||
|
||||
func (ta transaction) SignTransaction(privKey *rsa.PrivateKey, d int) ([]byte, error) {
|
||||
|
||||
return rsa.SignPKCS1v15(rand.Reader, privKey, crypto.SHA256, ta.HashTransaction())
|
||||
}
|
||||
|
||||
func (ta transaction) HashTransaction() []byte {
|
||||
var buf bytes.Buffer
|
||||
gob.NewEncoder(&buf).Encode(ta)
|
||||
hash := sha256.New().Sum(buf.Bytes())
|
||||
return hash
|
||||
}
|
||||
|
||||
func (ta transaction) ToBytes() []byte {
|
||||
bytes := []byte(ta.Src)
|
||||
bytes = append(bytes, []byte(ta.Dst)...)
|
||||
//fmt.Printf("%s", ta.Dst)
|
||||
bytes = append(bytes, byte(ta.Amount))
|
||||
bytes = append(bytes, []byte(ta.Timestamp.String())...)
|
||||
return bytes
|
||||
}
|
||||
Reference in New Issue
Block a user