-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.go
93 lines (84 loc) · 2.64 KB
/
wallet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr"
)
// Wallet represents a cryptocurrency wallet
type Wallet struct {
PrivateKey *secp256k1.PrivateKey
PublicKey *secp256k1.PublicKey
Balance int
}
// InitializeWallets initializes multiple wallets with random keys
func InitializeWallets(numWallets int) []*Wallet {
var wallets []*Wallet
for i := 0; i < numWallets; i++ {
privateKey, err := secp256k1.GeneratePrivateKey()
if err != nil {
fmt.Println("Error generating private key:", err)
continue
}
wallets = append(wallets, &Wallet{
PrivateKey: privateKey,
PublicKey: privateKey.PubKey(),
Balance: 1000, // Initialize with 1000 coins
})
}
return wallets
}
// CreateWallet creates a new wallet with a random key and initial balance
func CreateWallet() (*Wallet, error) {
privateKey, err := secp256k1.GeneratePrivateKey()
if err != nil {
return nil, fmt.Errorf("error generating private key: %w", err)
}
return &Wallet{
PrivateKey: privateKey,
PublicKey: privateKey.PubKey(),
Balance: 1000, // Initialize with 1000 coins
}, nil
}
// CalculateTransactionHash calculates the hash of the transaction
func CalculateTransactionHash(transaction Transaction) [32]byte {
record := transaction.Sender + transaction.Receiver + fmt.Sprint(transaction.Amount) + transaction.Timestamp
h := sha256.New()
h.Write([]byte(record))
var hash [32]byte
copy(hash[:], h.Sum(nil))
return hash
}
// SignTransaction signs a transaction with the wallet's private key
func SignTransaction(privateKey *secp256k1.PrivateKey, transaction Transaction) (string, error) {
hash := CalculateTransactionHash(transaction)
sig, err := schnorr.Sign(privateKey, hash[:]) // Hash needs to be a 32-byte array
if err != nil {
return "", fmt.Errorf("signing failed: %w", err)
}
return hex.EncodeToString(sig.Serialize()), nil
}
// VerifyTransaction verifies the transaction signature
func VerifyTransaction(publicKey *secp256k1.PublicKey, transaction Transaction) (bool, error) {
hash := CalculateTransactionHash(transaction)
signatureBytes, err := hex.DecodeString(transaction.Signature)
if err != nil {
return false, err
}
sig, err := schnorr.ParseSignature(signatureBytes)
if err != nil {
return false, err
}
return sig.Verify(hash[:], publicKey), nil
}
// ProcessTransfer processes the transfer between sender and receiver
func ProcessTransfer(sender, receiver *Wallet, amount int) error {
if sender.Balance < amount {
return errors.New("insufficient funds")
}
sender.Balance -= amount
receiver.Balance += amount
return nil
}