Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion blockchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import (
"encoding/gob"
"encoding/hex"
"fmt"
"github.com/SadikSunbul/GO-BlockChain-Simulation/wallet"
"log"
"math/big"
"strings"

"github.com/SadikSunbul/GO-BlockChain-Simulation/wallet"
)

func init() {
gob.Register(elliptic.P256())
}

type Transaction struct {
ID []byte //transectıon hası
Inputs []TxInput //bu transectıondakı ınputlar
Expand Down
10 changes: 8 additions & 2 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package network

import (
"bytes"
"crypto/elliptic"
"encoding/gob"
"encoding/hex"
"fmt"
"github.com/SadikSunbul/GO-BlockChain-Simulation/blockchain"
"github.com/vrecan/death/v3"
"io"
"io/ioutil"
"log"
"net"
"os"
"runtime"
"syscall"

"github.com/SadikSunbul/GO-BlockChain-Simulation/blockchain"
"github.com/vrecan/death/v3"
)

func init() {
gob.Register(elliptic.P256())
}

const (
protocol = "tcp"
version = 1
Expand Down
33 changes: 32 additions & 1 deletion wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"golang.org/x/crypto/ripemd160"
"log"
"math/big"

"golang.org/x/crypto/ripemd160"
)

const (
Expand All @@ -20,6 +22,35 @@ type Wallet struct {
PublicKey []byte
}

// Serialize, cüzdanı serileştirmek için özel bir yöntem
func (w *Wallet) Serialize() []byte {
var content bytes.Buffer

// Private key'in D, X ve Y değerlerini kaydet
content.Write(w.PrivateKey.D.Bytes())
content.Write(w.PrivateKey.PublicKey.X.Bytes())
content.Write(w.PrivateKey.PublicKey.Y.Bytes())
content.Write(w.PublicKey)

return content.Bytes()
}

// DeserializeWallet, serileştirilmiş veriyi Wallet yapısına dönüştürür
func DeserializeWallet(data []byte) *Wallet {
privateKey := new(ecdsa.PrivateKey)
privateKey.Curve = elliptic.P256()

privateKey.D = new(big.Int).SetBytes(data[:32])
privateKey.PublicKey.X = new(big.Int).SetBytes(data[32:64])
privateKey.PublicKey.Y = new(big.Int).SetBytes(data[64:96])
publicKey := data[96:]

return &Wallet{
PrivateKey: *privateKey,
PublicKey: publicKey,
}
}

// NewKeyPair fonksiyonu, bir private ve public key olusturur
func NewKeyPair() (ecdsa.PrivateKey, []byte) {
curve := elliptic.P256() //kullanıcagımız eliptik tipi burdakı sıfreler 256 byte olur
Expand Down
52 changes: 32 additions & 20 deletions wallet/wallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
"os"
)

const walletFile = "./tmp/wallets_%s.data" //Badgerı kullanmıycaz buradakı cuzdsanı saklamak ıcın
const walletFile = "./tmp/wallets_%s.data"

type Wallets struct {
Wallets map[string]*Wallet
}

func init() {
gob.Register(elliptic.P256())
}

// CreateWallets fonksiyonu, bir Wallets nesnesi olusturur
func CreateWallets(nodeId string) (*Wallets, error) {
wallets := Wallets{} // wallet nesnesi olusturulur
Expand Down Expand Up @@ -47,44 +51,52 @@ func (ws *Wallets) GetAllAddress() []string {
return addresses // adreslerin listesi döndürülür
}

// LoadFile fonksiyonu, dosya okunur
// LoadFile fonksiyonu, cüzdanları dosyadan yükler
func (ws *Wallets) LoadFile(nodeId string) error {
walletFile := fmt.Sprintf(walletFile, nodeId)
if _, err := os.Stat(walletFile); os.IsNotExist(err) { // dosya yoksa
return err // hata döndürür
if _, err := os.Stat(walletFile); os.IsNotExist(err) {
return err
}

var wallets Wallets // wallet nesnesi olusturulur

fileContent, err := os.ReadFile(walletFile) // dosya okunur
var walletsData map[string][]byte

fileContent, err := os.ReadFile(walletFile)
if err != nil {
log.Panic(err)
} // hata döndürür
gob.Register(elliptic.P256()) // elliptic nesnesi olusturulur
decoder := gob.NewDecoder(bytes.NewReader(fileContent)) // decoder nesnesi olusturulur
err = decoder.Decode(&wallets) // decoder ile dosya okunur
return err
}

decoder := gob.NewDecoder(bytes.NewReader(fileContent))
err = decoder.Decode(&walletsData)
if err != nil {
log.Panic(err) // hata döndürür
return err
}
ws.Wallets = wallets.Wallets // wallet nesnesi olusturulur

return nil // hata yok
wallets := make(map[string]*Wallet)
for addr, data := range walletsData {
wallets[addr] = DeserializeWallet(data)
}

ws.Wallets = wallets
return nil
}

// SaveFile fonksiyonu, dosya kaydedilir
// SaveFile fonksiyonu, cüzdanları dosyaya kaydeder
func (ws *Wallets) SaveFile(nodeId string) {
var content bytes.Buffer
walletFile := fmt.Sprintf(walletFile, nodeId)

gob.Register(elliptic.P256()) // elliptic nesnesi olusturulur
encoder := gob.NewEncoder(&content) // encoder nesnesi oluşturulur
err := encoder.Encode(ws) // encoder ile dosya kaydedilir
var walletsData = make(map[string][]byte)
for addr, wallet := range ws.Wallets {
walletsData[addr] = wallet.Serialize()
}

encoder := gob.NewEncoder(&content)
err := encoder.Encode(walletsData)
if err != nil {
log.Panic(err)
}
err = os.WriteFile(walletFile, content.Bytes(), 0644) // dosya kaydedilir

err = os.WriteFile(walletFile, content.Bytes(), 0644)
if err != nil {
log.Panic(err)
}
Expand Down
Loading