-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet_address.go
51 lines (38 loc) · 1.19 KB
/
wallet_address.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
package kcoin
import (
"crypto/ecdsa"
"crypto/sha256"
"errors"
"golang.org/x/crypto/ripemd160"
)
func (wallet *Wallet) GetAddress() []byte {
pubKeyHash := hashPubKey(wallet.PrivateKey.PublicKey)
versionedPayload := append([]byte{version}, pubKeyHash...)
checksum := calculateChecksum(versionedPayload)
fullPayload := append(versionedPayload, checksum...)
encoded := Base58Encode(fullPayload)
return encoded
}
func getPubKeyHashFromAddress(address []byte) ([]byte, error) {
decodedAddress := Base58Decode(address)
//fix it later
if len(address) < addressChecksumLen+2 {
return nil, errors.New("Invalid address")
}
return decodedAddress[1 : len(decodedAddress)-addressChecksumLen], nil
}
func hashPubKey(pubKey ecdsa.PublicKey) []byte {
xyAppendedPubKey := getXYAppendedPubKey(pubKey)
pubKeySHA256 := sha256.Sum256(xyAppendedPubKey)
ripemdHasher := ripemd160.New()
_, err := ripemdHasher.Write(pubKeySHA256[:])
panicIfErrNotNil(err)
pubKeyRIPEMD160 := ripemdHasher.Sum(nil)
return pubKeyRIPEMD160
}
func calculateChecksum(payload []byte) []byte {
hashFunc := sha256.Sum256
firstHash := hashFunc(payload)
secondHash := hashFunc(firstHash[:])
return secondHash[:addressChecksumLen]
}