func NewAddrFromString(hs string) (a *BtcAddr, e error) { dec := Decodeb58(hs) if dec == nil { e = errors.New("Cannot decode b58 string *" + hs + "*") return } if len(dec) < 25 { e = errors.New("Address too short " + hex.EncodeToString(dec)) return } if len(dec) == 25 { sh := crypto.Sha2Sum(dec[0:21]) if !bytes.Equal(sh[:4], dec[21:25]) { e = errors.New("Address Checksum error") } else { a = new(BtcAddr) a.Version = dec[0] copy(a.Hash160[:], dec[1:21]) a.Checksum = make([]byte, 4) copy(a.Checksum, dec[21:25]) a.Enc58str = hs } } else { // Stealth Addr } return }
// Base58 encoded address func (a *BtcAddr) String() string { if a.Enc58str == "" { var ad [25]byte ad[0] = a.Version copy(ad[1:21], a.Hash160[:]) if a.Checksum == nil { sh := crypto.Sha2Sum(ad[0:21]) a.Checksum = make([]byte, 4) copy(a.Checksum, sh[:4]) } copy(ad[21:25], a.Checksum[:]) a.Enc58str = Encodeb58(ad[:]) } return a.Enc58str }