-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
62 lines (55 loc) · 1.74 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
package doge
import (
"errors"
)
func Hash160(bytes []byte) []byte {
return RIPEMD160(Sha256(bytes))
}
func Hash160toAddress(hash []byte, prefix byte) Address {
ver_hash := [1 + 20 + 4]byte{}
ver_hash[0] = prefix
if copy(ver_hash[1:], hash) != 20 {
panic("Hash160toAddress: wrong RIPEMD-160 length")
}
return Address(Base58EncodeCheck(ver_hash[0:21]))
}
// PubKeyToP2PKH converst an ECPubKeyCompressed or ECPubKeyUncompressed to a Dogecoin P2PKH Address.
func PubKeyToP2PKH(key []byte, chain *ChainParams) (Address, error) {
if !((len(key) == ECPubKeyUncompressedLen && key[0] == 0x04) ||
(len(key) == ECPubKeyCompressedLen && (key[0] == 0x02 || key[0] == 0x03))) {
return "", errors.New("PubKeyToAddress: invalid pubkey")
}
payload := Hash160(key[:])
ver_hash := [1 + 20 + 4]byte{}
ver_hash[0] = chain.P2PKH_Address_Prefix
if copy(ver_hash[1:], payload) != 20 {
panic("PubKeyToAddress: wrong RIPEMD-160 length")
}
return Address(Base58EncodeCheck(ver_hash[0:21])), nil
}
func ScriptToP2SH(redeemScript []byte, chain *ChainParams) Address {
if len(redeemScript) < 1 {
panic("ScriptToP2SH: bad script length")
}
payload := Hash160(redeemScript)
ver_hash := [1 + 20 + 4]byte{}
ver_hash[0] = chain.P2SH_Address_Prefix
if copy(ver_hash[1:], payload) != 20 {
panic("ScriptToP2SH: wrong RIPEMD-160 length")
}
return Address(Base58EncodeCheck(ver_hash[0:21]))
}
func ValidateP2PKH(address Address, chain *ChainParams) bool {
key, err := Base58DecodeCheck(string(address))
if err != nil {
return false
}
return key[0] == chain.P2PKH_Address_Prefix
}
func ValidateP2SH(address Address, chain *ChainParams) bool {
key, err := Base58DecodeCheck(string(address))
if err != nil {
return false
}
return key[0] == chain.P2SH_Address_Prefix
}