-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
112 lines (96 loc) · 2.36 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"github.com/btcsuite/btcutil/base58"
"math/big"
)
type Wallet struct {
KeyPairs map[string] /*Addr*/ KeyPair
}
type KeyPair struct {
PrivKey *ecdsa.PrivateKey
PubKey []byte
}
var WalletObj Wallet = Wallet{
KeyPairs: make(map[string]KeyPair, 5),
}
func (w *Wallet) ListAllAddr() []string {
result := []string{}
for addr, _ := range w.KeyPairs {
fmt.Println("Addr: ", addr)
result = append(result, addr)
}
return result
}
func (w *Wallet) Send(fromAddr, targetAddr string, amount uint64, chain *BlockChain, minerAddr string) error {
tx, err := NewTx(fromAddr, targetAddr, amount, chain, w)
if err != nil {
return err
}
return chain.AddBlock([]*Transaction{tx}, minerAddr, "矿工挖矿")
}
func GenKeyPair() error {
p256 := elliptic.P256()
privateKey, err := ecdsa.GenerateKey(p256, rand.Reader)
if err != nil {
return err
}
pubKeyRaw := privateKey.PublicKey
pubKey := append(pubKeyRaw.X.Bytes(), pubKeyRaw.Y.Bytes()...) //简单起见使用非压缩的公钥
addr := PubKey2Addr(pubKey)
WalletObj.KeyPairs[addr] = KeyPair{
PrivKey: privateKey,
PubKey: pubKey,
}
fmt.Println("new Addr:", addr)
return nil
}
func CheckAddr(addr string) bool {
decode := base58.Decode(addr)
if len(decode) != 25 {
return false
}
payload := decode[:len(decode)-4]
sum := checkSum(payload)
return bytes.Equal(sum, decode[len(decode)-4:])
}
func PubKeyBytes2PubKey(pubkey []byte) (*ecdsa.PublicKey, error) {
if len(pubkey) != 64 {
return nil, fmt.Errorf("pubkey len is not 512")
}
var x, y big.Int
x.SetBytes(pubkey[:len(pubkey)/2])
y.SetBytes(pubkey[len(pubkey)/2:])
p256 := elliptic.P256()
return &ecdsa.PublicKey{
Curve: p256,
X: &x,
Y: &y,
}, nil
}
func Addr2PubKeyHash(addr string) (pubKeyHash []byte, err error) {
if !CheckAddr(addr) {
err = fmt.Errorf("地址格式不对:", addr)
return
}
decode := base58.Decode(addr)
pubKeyHash = decode[1 : len(decode)-4]
return
}
func PubKeyHash2Addr(pubKeyHash []byte) string {
payload := append([]byte{0x00}, pubKeyHash...)
checksum := checkSum(payload)
return base58.Encode(append(payload, checksum...))
}
func PubKey2Addr(pubKey []byte) string {
pubKeyHash := Hash160(pubKey)
return PubKeyHash2Addr(pubKeyHash)
}
func checkSum(payload []byte) []byte {
dHash := Hash256(payload)
return dHash[:4]
}