-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
80 lines (64 loc) · 1.97 KB
/
main.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
package main
import (
"blockchain-golang/blockchain"
"fmt"
"strconv"
)
func main() {
chain := blockchain.InitBlockChain()
// Create a wallet for Alice.
aliceWallet, err := blockchain.NewWallet()
if err != nil {
fmt.Println("Error creating Alice's wallet:", err)
return
}
fmt.Println("Alice's wallet created successfully")
// Create a wallet for Bob.
bobWallet, err := blockchain.NewWallet()
if err != nil {
fmt.Println("Error creating Bob's wallet:", err)
return
}
fmt.Println("Bob's wallet created successfully")
// Create a transaction from Alice to Bob.
tx := &blockchain.Transaction{
Sender: aliceWallet.PublicKey.N.String(),
Receiver: bobWallet.PublicKey.N.String(),
Amount: 5.0,
}
fmt.Println("Alice to Bob Transaction created successfully")
// Sign the transaction with Alice’s wallet.
signature, err := aliceWallet.SignTransaction(tx)
if err != nil {
fmt.Println("Error signing the transaction:", err)
return
}
// Verify the transaction using Alice’s wallet, public key, and the signature.
err = blockchain.VerifyTransaction(tx, aliceWallet.PublicKey, signature)
if err != nil {
fmt.Println("Transaction verification failed:", err)
return
}
fmt.Println("Transaction Verified Successfully")
// Add the verified transaction to the blockchain.
chain.AddBlock("Block 1", "Alice", []*blockchain.Transaction{tx})
fmt.Println()
// Print the blockchain.
for _, block := range chain.Blocks {
fmt.Printf("Previous hash: %x\n", block.PrevHash)
fmt.Printf("Data: %s\n", block.Data)
fmt.Printf("Hash: %x\n", block.Hash)
pow := blockchain.NewProofOfWork(block)
fmt.Printf("IsValidPoW: %s\n", strconv.FormatBool(pow.Validate()))
fmt.Println()
fmt.Println("Transactions:")
for _, tx := range block.Transactions {
fmt.Printf("Sender: %s\n", tx.Sender)
fmt.Printf("Receiver: %s\n", tx.Receiver)
fmt.Printf("Amount: %f\n", tx.Amount)
fmt.Printf("Coinbase: %t\n", tx.Coinbase)
fmt.Println()
}
fmt.Println()
}
}