diff --git a/cli/cli.go b/cli/cli.go index 6fefbe2..767e141 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "github.com/SadikSunbul/GO-BlockChain-Simulation/blockchain" + "github.com/SadikSunbul/GO-BlockChain-Simulation/wallet" "log" "os" "runtime" @@ -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. @@ -85,6 +87,21 @@ 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 @@ -92,6 +109,8 @@ func (cli *CommandLine) Run() { // komut satırı işlemleri 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 @@ -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 { @@ -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() + } } diff --git a/go.mod b/go.mod index 56981ed..ed63bb0 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 196695c..59e8d5d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index dfbb1bd..ed14398 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/SadikSunbul/GO-BlockChain-Simulation/wallet" + "github.com/SadikSunbul/GO-BlockChain-Simulation/cli" "os" ) @@ -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() } diff --git a/tmp/wallets.data b/tmp/wallets.data new file mode 100644 index 0000000..2da2382 Binary files /dev/null and b/tmp/wallets.data differ diff --git a/wallet/wallet.go b/wallet/wallet.go index 480317f..93e233b 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -5,7 +5,6 @@ import ( "crypto/elliptic" "crypto/rand" "crypto/sha256" - "fmt" "golang.org/x/crypto/ripemd160" "log" ) @@ -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 } diff --git a/wallet/wallets.go b/wallet/wallets.go new file mode 100644 index 0000000..5c2695c --- /dev/null +++ b/wallet/wallets.go @@ -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) + } +}