Skip to content

Commit

Permalink
sürüm değişikliğinden Önceki comit
Browse files Browse the repository at this point in the history
  • Loading branch information
SadikSunbul committed Jun 20, 2024
1 parent c3daf31 commit 94fc68c
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 11 deletions.
38 changes: 37 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"github.com/SadikSunbul/GO-BlockChain-Simulation/blockchain"
"github.com/SadikSunbul/GO-BlockChain-Simulation/wallet"
"log"
"os"
"runtime"
Expand All @@ -21,7 +22,8 @@ func (cli *CommandLine) printUsage() {
fmt.Printf(" %-40s : %s\n", "createblockchain -address ADDRESS", "Yeni bir blok zinciri oluşturur ve belirtilen adrese oluşum ödülünü gönderir")
fmt.Printf(" %-40s : %s\n", "printchain", "Blok zincirindeki tüm blokları yazdırır")
fmt.Printf(" %-40s : %s\n", "send -from FROM -to TO -amount AMOUNT", "Belirtilen miktarı belirtilen adresten diğer bir adrese gönderir")

fmt.Printf(" %-40s : %s\n", "createwallet", "-Yeni bir cüzdan oluşturur")
fmt.Printf(" %-40s : %s\n", "listaddresses", "Cüzdan dosyamızdaki adresleri listeleyin\n")
}

// validateArgs fonksiyonu, komut satırı argümanlarını doğrular.
Expand Down Expand Up @@ -85,13 +87,30 @@ func (cli *CommandLine) send(from, to string, amount int) { // para göndermek
fmt.Println("Success!")
}

func (cli *CommandLine) listAddresses() {
wallets, _ := wallet.CreateWallets()
addresses := wallets.GetAllAddress()
for _, address := range addresses {
fmt.Println(address)
}
}

func (cli *CommandLine) CreateWallet() {
wallets, _ := wallet.CreateWallets()
address := wallets.AddWallet()
wallets.SaveFile()
fmt.Printf("New address is : %s\n", address)
}

func (cli *CommandLine) Run() { // komut satırı işlemleri
cli.validateArgs() // komut satırı argümanlarını dogrular

getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError) // getbalance komutunu tanımla
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError) // createblockchain komutunu tanımla
sendCmd := flag.NewFlagSet("send", flag.ExitOnError) // send komutunu tanımla
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError) // printchain komutunu tanımla
createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError)
listAddressesCmd := flag.NewFlagSet("listaddresses", flag.ExitOnError)

getBalanceAddress := getBalanceCmd.String("address", "", "Bakiye almanın adresi") // getbalance komutundaki adres bilgisini tanımla
createBlockchainAddress := createBlockchainCmd.String("address", "", "Genesis blok ödülünün gönderileceği adres") // createblockchain komutundaki adres bilgisini tanımla
Expand All @@ -115,6 +134,16 @@ func (cli *CommandLine) Run() { // komut satırı işlemleri
if err != nil {
log.Panic(err)
}
case "listaddresses":
err := listAddressesCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "createwallet":
err := createWalletCmd.Parse(os.Args[2:])
if err != nil {
log.Panic(err)
}
case "send":
err := sendCmd.Parse(os.Args[2:]) // send komutunu çalıştır
if err != nil {
Expand Down Expand Up @@ -153,4 +182,11 @@ func (cli *CommandLine) Run() { // komut satırı işlemleri

cli.send(*sendFrom, *sendTo, *sendAmount) // send komutunu çalıştır
}

if createWalletCmd.Parsed() {
cli.CreateWallet()
}
if listAddressesCmd.Parsed() {
cli.listAddresses()
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ require (
require (
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/dgraph-io/ristretto v0.0.2 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/golang/protobuf v1.3.5 // indirect
github.com/pkg/errors v0.8.1 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ github.com/dgraph-io/badger v1.6.2 h1:mNw0qs90GVgGGWylh0umH5iag1j6n/PeJtNvL6KY/x
github.com/dgraph-io/badger v1.6.2/go.mod h1:JW2yswe3V058sS0kZ2h/AXeDSqFjxnZcRrVH//y2UQE=
github.com/dgraph-io/ristretto v0.0.2 h1:a5WaUrDa0qm0YrAAS1tUykT5El3kt62KNZZeMxQn3po=
github.com/dgraph-io/ristretto v0.0.2/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgraph-io/ristretto v0.0.3 h1:jh22xisGBjrEVnRZ1DVTpBVQm0Xndu8sMl0CWDzSIBI=
github.com/dgraph-io/ristretto v0.0.3/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/SadikSunbul/GO-BlockChain-Simulation/wallet"
"github.com/SadikSunbul/GO-BlockChain-Simulation/cli"
"os"
)

Expand All @@ -10,6 +10,9 @@ func main() {
//cli := cli.CommandLine{} // Komut satırı işlemleri için kullanılan yapıyı temsil eder.
//cli.Run() // Komut satırı işlemlerini başlatır

w := wallet.MakeWallet()
w.Address()
//w := wallet.MakeWallet()
//w.Address()

cli := cli.CommandLine{}
cli.CreateWallet()
}
Binary file added tmp/wallets.data
Binary file not shown.
4 changes: 0 additions & 4 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"fmt"
"golang.org/x/crypto/ripemd160"
"log"
)
Expand Down Expand Up @@ -64,9 +63,6 @@ func (w Wallet) Address() []byte {
checksum := Checksum(versionedHash) // checksum kodu olusturulur
fullHash := append(versionedHash, checksum...) // versionedHash ve checksum kodu birleştirilir
address := Base58Encode(fullHash) // adres olusturulur
fmt.Printf("pub key: %x\n", w.PublickKey)
fmt.Printf("pub has: %x\n", pubHash)
fmt.Printf("address: %x\n", address)

return address
}
84 changes: 84 additions & 0 deletions wallet/wallets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package wallet

import (
"bytes"
"crypto/elliptic"
"encoding/gob"
"fmt"
"log"
"os"
)

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

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

func CreateWallets() (*Wallets, error) {
wallets := Wallets{}
wallets.Wallets = make(map[string]*Wallet)

err := wallets.LoadFile()

return &wallets, err
}

func (ws *Wallets) GetWallet(address string) Wallet {
return *ws.Wallets[address]
}

func (ws *Wallets) AddWallet() string {
wallet := MakeWallet()
address := fmt.Sprintf("%s", wallet.Address())
ws.Wallets[address] = wallet
return address
}

func (ws *Wallets) GetAllAddress() []string {
var addresses []string
for address := range ws.Wallets {
addresses = append(addresses, address)
}
return addresses
}

func (ws *Wallets) LoadFile() error {
if _, err := os.Stat(walletFile); os.IsNotExist(err) { //dosya varmı
return err
}

var wallets Wallets

fileContent, err := os.ReadFile(walletFile)

if err != nil {
log.Panic(err)
}
gob.Register(elliptic.P256())
decoder := gob.NewDecoder(bytes.NewReader(fileContent))
err = decoder.Decode(&wallets)

if err != nil {
fmt.Println("hataa decode de")
log.Panic(err)
}
ws.Wallets = wallets.Wallets

return nil
}

func (ws *Wallets) SaveFile() {
var content bytes.Buffer

gob.Register(elliptic.P256())
encoder := gob.NewEncoder(&content)
err := encoder.Encode(ws)
if err != nil {
log.Panic(err)
}
err = os.WriteFile(walletFile, content.Bytes(), 0644)
if err != nil {
log.Panic(err)
}
}

0 comments on commit 94fc68c

Please sign in to comment.