/
wallet.go
75 lines (56 loc) · 1.41 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
package wallet
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"log"
"golang.org/x/crypto/ripemd160"
)
const (
checksumLength = 4
//hexadecimal representation of 0
version = byte(0x00)
)
type Wallet struct {
//ecdsa = eliptical curve digital signiture algorithm
PrivateKey ecdsa.PrivateKey
PublicKey []byte
}
func NewKeyPair() (ecdsa.PrivateKey, []byte) {
curve := elliptic.P256()
private, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
log.Panic(err)
}
pub := append(private.PublicKey.X.Bytes(), private.PublicKey.Y.Bytes()...)
return *private, pub
}
func MakeWallet() *Wallet {
privateKey, publicKey := NewKeyPair()
wallet := Wallet{privateKey, publicKey}
return &wallet
}
func PublicKeyHash(publicKey []byte) []byte {
hashedPublicKey := sha256.Sum256(publicKey)
hasher := ripemd160.New()
_, err := hasher.Write(hashedPublicKey[:])
if err != nil {
log.Panic(err)
}
publicRipeMd := hasher.Sum(nil)
return publicRipeMd
}
func Checksum(ripeMdHash []byte) []byte {
firstHash := sha256.Sum256(ripeMdHash)
secondHash := sha256.Sum256(firstHash[:])
return secondHash[:checksumLength]
}
func (w *Wallet) Address() []byte {
pubHash := PublicKeyHash(w.PublicKey)
versionedHash := append([]byte{version}, pubHash...)
checksum := Checksum(versionedHash)
finalHash := append(versionedHash, checksum...)
address := base58Encode(finalHash)
return address
}