Skip to content

Commit b7ef8c2

Browse files
committed
up
1 parent 66988c4 commit b7ef8c2

File tree

4 files changed

+78
-24
lines changed

4 files changed

+78
-24
lines changed

blockchain/transaction.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ import (
99
"encoding/gob"
1010
"encoding/hex"
1111
"fmt"
12-
"github.com/SadikSunbul/GO-BlockChain-Simulation/wallet"
1312
"log"
1413
"math/big"
1514
"strings"
15+
16+
"github.com/SadikSunbul/GO-BlockChain-Simulation/wallet"
1617
)
1718

19+
func init() {
20+
gob.Register(elliptic.P256())
21+
}
22+
1823
type Transaction struct {
1924
ID []byte //transectıon hası
2025
Inputs []TxInput //bu transectıondakı ınputlar

network/network.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ package network
22

33
import (
44
"bytes"
5+
"crypto/elliptic"
56
"encoding/gob"
67
"encoding/hex"
78
"fmt"
8-
"github.com/SadikSunbul/GO-BlockChain-Simulation/blockchain"
9-
"github.com/vrecan/death/v3"
109
"io"
1110
"io/ioutil"
1211
"log"
1312
"net"
1413
"os"
1514
"runtime"
1615
"syscall"
16+
17+
"github.com/SadikSunbul/GO-BlockChain-Simulation/blockchain"
18+
"github.com/vrecan/death/v3"
1719
)
1820

21+
func init() {
22+
gob.Register(elliptic.P256())
23+
}
24+
1925
const (
2026
protocol = "tcp"
2127
version = 1

wallet/wallet.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"crypto/elliptic"
77
"crypto/rand"
88
"crypto/sha256"
9-
"golang.org/x/crypto/ripemd160"
109
"log"
10+
"math/big"
11+
12+
"golang.org/x/crypto/ripemd160"
1113
)
1214

1315
const (
@@ -20,6 +22,35 @@ type Wallet struct {
2022
PublicKey []byte
2123
}
2224

25+
// Serialize, cüzdanı serileştirmek için özel bir yöntem
26+
func (w *Wallet) Serialize() []byte {
27+
var content bytes.Buffer
28+
29+
// Private key'in D, X ve Y değerlerini kaydet
30+
content.Write(w.PrivateKey.D.Bytes())
31+
content.Write(w.PrivateKey.PublicKey.X.Bytes())
32+
content.Write(w.PrivateKey.PublicKey.Y.Bytes())
33+
content.Write(w.PublicKey)
34+
35+
return content.Bytes()
36+
}
37+
38+
// DeserializeWallet, serileştirilmiş veriyi Wallet yapısına dönüştürür
39+
func DeserializeWallet(data []byte) *Wallet {
40+
privateKey := new(ecdsa.PrivateKey)
41+
privateKey.Curve = elliptic.P256()
42+
43+
privateKey.D = new(big.Int).SetBytes(data[:32])
44+
privateKey.PublicKey.X = new(big.Int).SetBytes(data[32:64])
45+
privateKey.PublicKey.Y = new(big.Int).SetBytes(data[64:96])
46+
publicKey := data[96:]
47+
48+
return &Wallet{
49+
PrivateKey: *privateKey,
50+
PublicKey: publicKey,
51+
}
52+
}
53+
2354
// NewKeyPair fonksiyonu, bir private ve public key olusturur
2455
func NewKeyPair() (ecdsa.PrivateKey, []byte) {
2556
curve := elliptic.P256() //kullanıcagımız eliptik tipi burdakı sıfreler 256 byte olur

wallet/wallets.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import (
99
"os"
1010
)
1111

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

1414
type Wallets struct {
1515
Wallets map[string]*Wallet
1616
}
1717

18+
func init() {
19+
gob.Register(elliptic.P256())
20+
}
21+
1822
// CreateWallets fonksiyonu, bir Wallets nesnesi olusturur
1923
func CreateWallets(nodeId string) (*Wallets, error) {
2024
wallets := Wallets{} // wallet nesnesi olusturulur
@@ -47,44 +51,52 @@ func (ws *Wallets) GetAllAddress() []string {
4751
return addresses // adreslerin listesi döndürülür
4852
}
4953

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

57-
var wallets Wallets // wallet nesnesi olusturulur
58-
59-
fileContent, err := os.ReadFile(walletFile) // dosya okunur
61+
var walletsData map[string][]byte
6062

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

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

73-
return nil // hata yok
74+
wallets := make(map[string]*Wallet)
75+
for addr, data := range walletsData {
76+
wallets[addr] = DeserializeWallet(data)
77+
}
78+
79+
ws.Wallets = wallets
80+
return nil
7481
}
7582

76-
// SaveFile fonksiyonu, dosya kaydedilir
83+
// SaveFile fonksiyonu, cüzdanları dosyaya kaydeder
7784
func (ws *Wallets) SaveFile(nodeId string) {
7885
var content bytes.Buffer
7986
walletFile := fmt.Sprintf(walletFile, nodeId)
8087

81-
gob.Register(elliptic.P256()) // elliptic nesnesi olusturulur
82-
encoder := gob.NewEncoder(&content) // encoder nesnesi oluşturulur
83-
err := encoder.Encode(ws) // encoder ile dosya kaydedilir
88+
var walletsData = make(map[string][]byte)
89+
for addr, wallet := range ws.Wallets {
90+
walletsData[addr] = wallet.Serialize()
91+
}
92+
93+
encoder := gob.NewEncoder(&content)
94+
err := encoder.Encode(walletsData)
8495
if err != nil {
8596
log.Panic(err)
8697
}
87-
err = os.WriteFile(walletFile, content.Bytes(), 0644) // dosya kaydedilir
98+
99+
err = os.WriteFile(walletFile, content.Bytes(), 0644)
88100
if err != nil {
89101
log.Panic(err)
90102
}

0 commit comments

Comments
 (0)