-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3daf31
commit 94fc68c
Showing
7 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |