//BitcoinRipeMD160ToAddress takes 20 byte RipeMD160 hash and returns the 25-byte address as well as updates the address representation of the output func BitcoinRipeMD160ToAddress(hash160 []byte, address *block.Address) []byte { ret := append([]byte{0}, hash160[:]...) //append network byte of 0 (main network) as checksum sha2 := sha256.New() sha2.Write(ret) //sha256 on ripemd hash hash3 := sha2.Sum(nil) sha3 := sha256.New() sha3.Write(hash3) // compute second hash to get checksum to store at end of output hash4 := sha3.Sum(nil) ret = append(ret[:], hash4[0:4]...) address.Address = BitcoinToASCII(ret) address.RipeMD160 = hex.EncodeToString(hash160) address.PublicKey = hex.EncodeToString(hash160) return ret }
//BitcoinPublicKeyToAddress takes a 65 byte public key found in parsing addresses //and converts it to the 20 byte form func BitcoinPublicKeyToAddress(pubKey []byte, address *block.Address) ([]byte, []byte, error) { if pubKey[0] != 0x04 { return nil, nil, errors.New("Beginning of 65 byte public key does not match expected format") } sha1 := sha256.New() sha1.Write(pubKey) hash1 := sha1.Sum(nil) ripemd := ripemd160.New() ripemd.Write(hash1) hash160 := ripemd.Sum(nil) ret := BitcoinRipeMD160ToAddress(hash160, address) address.RipeMD160 = hex.EncodeToString(hash160) address.PublicKey = hex.EncodeToString(pubKey) return ret, hash160, nil }
//BitcoinCompressedPublicKeyToAddress takes a compressed ECDSA key and converts it to 25-byte address func BitcoinCompressedPublicKeyToAddress(key []byte, address *block.Address) []byte { if key[0] == 0x02 || key[0] == 0x03 { address.PublicKey = hex.EncodeToString(key) sha1 := sha256.New() sha1.Write(key) hash1 := sha1.Sum(nil) return BitcoinRipeMD160ToAddress(hash1, address) } fmt.Println("Invalid Compressed Public Key") return nil }