1 package btcaddr
2 3 import (
4 "crypto/sha256"
5 "encoding/hex"
6 "errors"
7 "golang.org/x/crypto/ripemd160"
8 "hash"
9 10 "github.com/p9c/p9/pkg/base58"
11 "github.com/p9c/p9/pkg/chaincfg"
12 ec "github.com/p9c/p9/pkg/ecc"
13 )
14 15 // //
16 // // // unsupportedWitnessVerError describes an error where a segwit address being
17 // // // decoded has an unsupported witness version.
18 // // type unsupportedWitnessVerError byte
19 //
20 // func (e unsupportedWitnessVerError) Error() string {
21 // return "unsupported witness version: " + string(e)
22 // }
23 //
24 // // unsupportedWitnessProgLenError describes an error where a segwit address
25 // // being decoded has an unsupported witness program length.
26 // type unsupportedWitnessProgLenError int
27 //
28 // func (e unsupportedWitnessProgLenError) Error() string {
29 // return "unsupported witness program length: " + string(rune(e))
30 // }
31 32 var (
33 // ErrChecksumMismatch describes an error where decoding failed due to a bad checksum.
34 ErrChecksumMismatch = errors.New("checksum mismatch")
35 // ErrUnknownAddressType describes an error where an address can not decoded as a specific address type due to the
36 // string encoding beginning with an identifier byte unknown to any standard or registered (via chaincfg.Register)
37 // network.
38 ErrUnknownAddressType = errors.New("unknown address type")
39 // ErrAddressCollision describes an error where an address can not be uniquely determined as either a
40 // pay-to-pubkey-hash or pay-to-script-hash address since the leading identifier is used for describing both address
41 // kinds, but for different networks. Rather than assuming or defaulting to one or the other, this error is returned
42 // and the caller must decide how to decode the address.
43 ErrAddressCollision = errors.New("address collision")
44 )
45 46 // encode returns a human-readable payment address given a ripemd160 hash and netID which encodes the bitcoin
47 // network and address type. It is used in both pay-to-pubkey-hash (P2PKH) and pay-to-script-hash (P2SH) address
48 // encoding.
49 func encode(hash160 []byte, netID byte) string {
50 // Format is 1 byte for a network and address class (i.e. P2PKH vs P2SH), 20 bytes for a RIPEMD160 hash, and 4 bytes of checksum.
51 return base58.CheckEncode(hash160[:ripemd160.Size], netID)
52 }
53 54 // // encodeSegWitAddress creates a bech32 encoded address string representation
55 // // from witness version and witness program.
56 // func encodeSegWitAddress(hrp string, witnessVersion byte, witnessProgram []byte) (string, error) {
57 // // Group the address bytes into 5 bit groups, as this is what is used to encode each character in the address string.
58 // converted, e := bech32.ConvertBits(witnessProgram, 8, 5, true)
59 // if e != nil {
60 // // return "", err
61 // }
62 // // Concatenate the witness version and program, and encode the resulting bytes
63 // // using bech32 encoding.
64 // combined := make([]byte, len(converted)+1)
65 // combined[0] = witnessVersion
66 // copy(combined[1:], converted)
67 // bech, e := bech32.Encode(hrp, combined)
68 // if e != nil {
69 // // return "", err
70 // }
71 // // Chk validity by decoding the created address.
72 // var program []byte
73 // var version byte
74 // version, program, e = decodeSegWitAddress(bech)
75 // if e != nil {
76 // // return "", fmt.Errorf("invalid segwit address: %v", err)
77 // }
78 // if version != witnessVersion || !bytes.Equal(program, witnessProgram) {
79 // return "", fmt.Errorf("invalid segwit address")
80 // }
81 // return bech, nil
82 // }
83 84 // Address is an interface type for any type of destination a transaction output may spend to. This includes
85 // pay-to-pubkey (P2PK), pay-to-pubkey-hash (P2PKH), and pay-to-script-hash (P2SH). Address is designed to be generic
86 // enough that other kinds of addresses may be added in the future without changing the decoding and encoding API.
87 type Address interface {
88 // String returns the string encoding of the transaction output
89 // destination.
90 //
91 // Please note that String differs subtly from EncodeAddress: String
92 // will return the value as a string without any conversion,
93 // while EncodeAddress may convert destination types (for example,
94 // converting pubkeys to P2PKH addresses) before encoding as a
95 // payment address string.
96 String() string
97 // EncodeAddress returns the string encoding of the payment address
98 // associated with the Address value.
99 // See the comment on String for how this method differs from String.
100 EncodeAddress() string
101 // ScriptAddress returns the raw bytes of the address to be used when
102 // inserting the address into a txout's script.
103 ScriptAddress() []byte
104 // IsForNet returns whether or not the address is associated with the
105 // passed bitcoin network.
106 IsForNet(*chaincfg.Params) bool
107 }
108 109 // Decode decodes the string encoding of an address and returns the
110 // Address if addr is a valid encoding for a known address type. The bitcoin
111 // network the address is associated with is extracted if possible. When the
112 // address does not encode the network, such as in the case of a raw public key,
113 // the address will be associated with the passed defaultNet.
114 func Decode(addr string, defaultNet *chaincfg.Params) (Address, error) {
115 // // Bech32 encoded segwit addresses start with a human-readable part (hrp) followed by '1'. For Bitcoin mainnet the
116 // // hrp is "bc", and for testnet it is "tb". If the address string has a prefix that matches one of the prefixes for
117 // // the known networks, we try to decode it as a segwit address.
118 // oneIndex := strings.LastIndexByte(addr, '1')
119 // if oneIndex > 1 {
120 // prefix := addr[:oneIndex+1]
121 // if chaincfg.IsBech32SegwitPrefix(prefix) {
122 // witnessVer, witnessProg, e := decodeSegWitAddress(addr)
123 // if e != nil {
124 // // return nil, e
125 // }
126 // // We currently only support P2WPKH and P2WSH, which is witness version 0.
127 // if witnessVer != 0 {
128 // return nil, unsupportedWitnessVerError(witnessVer)
129 // }
130 // // The HRP is everything before the found '1'.
131 // hrp := prefix[:len(prefix)-1]
132 // switch len(witnessProg) {
133 // case 20:
134 // return newAddressWitnessPubKeyHash(hrp, witnessProg)
135 // case 32:
136 // return newAddressWitnessScriptHash(hrp, witnessProg)
137 // default:
138 // return nil, unsupportedWitnessProgLenError(len(witnessProg))
139 // }
140 // }
141 // }
142 //
143 // Serialized public keys are either 65 bytes (130 hex chars) if
144 // uncompressed/hybrid or 33 bytes (66 hex chars) if compressed.
145 if len(addr) == 130 || len(addr) == 66 {
146 serializedPubKey, e := hex.DecodeString(addr)
147 if e != nil {
148 return nil, e
149 }
150 return NewPubKey(serializedPubKey, defaultNet)
151 }
152 // Switch on decoded length to determine the type.
153 decoded, netID, e := base58.CheckDecode(addr)
154 if e != nil {
155 if e == base58.ErrChecksum {
156 return nil, ErrChecksumMismatch
157 }
158 return nil, errors.New("decoded address is of unknown format")
159 }
160 switch len(decoded) {
161 case ripemd160.Size: // P2PKH or P2SH
162 isP2PKH := chaincfg.IsPubKeyHashAddrID(netID)
163 isP2SH := chaincfg.IsScriptHashAddrID(netID)
164 switch hash160 := decoded; {
165 case isP2PKH && isP2SH:
166 return nil, ErrAddressCollision
167 case isP2PKH:
168 return newPubKeyHash(hash160, netID)
169 case isP2SH:
170 return newScriptHashFromHash(hash160, netID)
171 default:
172 return nil, ErrUnknownAddressType
173 }
174 default:
175 return nil, errors.New("decoded address is of unknown size")
176 }
177 }
178 179 // // decodeSegWitAddress parses a bech32 encoded segwit address string and returns
180 // // the witness version and witness program byte representation.
181 // func decodeSegWitAddress(address string) (byte, []byte, error) {
182 // // Decode the bech32 encoded address.
183 // _, data, e := bech32.Decode(address)
184 // if e != nil {
185 // // return 0, nil, e
186 // }
187 // // The first byte of the decoded address is the witness version, it must exist.
188 // if len(data) < 1 {
189 // return 0, nil, fmt.Errorf("no witness version")
190 // }
191 // // ...and be <= 16.
192 // version := data[0]
193 // if version > 16 {
194 // return 0, nil, fmt.Errorf("invalid witness version: %v", version)
195 // }
196 // // The remaining characters of the address returned are grouped into words of 5
197 // // bits. In order to restore the original witness program bytes, we'll need to
198 // // regroup into 8 bit words.
199 // regrouped, e := bech32.ConvertBits(data[1:], 5, 8, false)
200 // if e != nil {
201 // // return 0, nil, e
202 // }
203 // // The regrouped data must be between 2 and 40 bytes.
204 // if len(regrouped) < 2 || len(regrouped) > 40 {
205 // return 0, nil, fmt.Errorf("invalid data length")
206 // }
207 // // For witness version 0, address MUST be exactly 20 or 32 bytes.
208 // if version == 0 && len(regrouped) != 20 && len(regrouped) != 32 {
209 // return 0, nil, fmt.Errorf("invalid data length for witness "+
210 // "version 0: %v", len(regrouped))
211 // }
212 // return version, regrouped, nil
213 // }
214 215 // PubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH) transaction.
216 type PubKeyHash struct {
217 Hash [ripemd160.Size]byte
218 NetID byte
219 }
220 221 // NewPubKeyHash returns a new PubKeyHash. pkHash must be 20 bytes.
222 func NewPubKeyHash(pkHash []byte, net *chaincfg.Params) (*PubKeyHash, error) {
223 return newPubKeyHash(pkHash, net.PubKeyHashAddrID)
224 }
225 226 // newPubKeyHash is the internal API to create a pubkey hash address with
227 // a known leading identifier byte for a network, rather than looking it up
228 // through its parameters. This is useful when creating a new address structure
229 // from a string encoding where the identifier byte is already known.
230 func newPubKeyHash(pkHash []byte, netID byte) (*PubKeyHash, error) {
231 // Chk for a valid pubkey hash length.
232 if len(pkHash) != ripemd160.Size {
233 return nil, errors.New("pkHash must be 20 bytes")
234 }
235 addr := &PubKeyHash{NetID: netID}
236 copy(addr.Hash[:], pkHash)
237 return addr, nil
238 }
239 240 // EncodeAddress returns the string encoding of a pay-to-pubkey-hash address.
241 // Part of the Address interface.
242 func (a *PubKeyHash) EncodeAddress() string {
243 return encode(a.Hash[:], a.NetID)
244 }
245 246 // ScriptAddress returns the bytes to be included in a txout script to pay to a
247 // pubkey hash. Part of the Address interface.
248 func (a *PubKeyHash) ScriptAddress() []byte {
249 return a.Hash[:]
250 }
251 252 // IsForNet returns whether or not the pay-to-pubkey-hash address is associated
253 // with the passed bitcoin network.
254 func (a *PubKeyHash) IsForNet(net *chaincfg.Params) bool {
255 return a.NetID == net.PubKeyHashAddrID
256 }
257 258 // String returns a human-readable string for the pay-to-pubkey-hash address. This is equivalent to calling
259 // EncodeAddress, but is provided so the type can be used as a fmt.Stringer.
260 func (a *PubKeyHash) String() string {
261 return a.EncodeAddress()
262 }
263 264 // Hash160 returns the underlying array of the pubkey hash. This can be useful when an array is more appropriate than a
265 // slice (for example, when used as map keys).
266 func (a *PubKeyHash) Hash160() *[ripemd160.Size]byte {
267 return &a.Hash
268 }
269 270 // ScriptHash is an Address for a pay-to-script-hash (P2SH) transaction.
271 type ScriptHash struct {
272 Hash [ripemd160.Size]byte
273 NetID byte
274 }
275 276 // NewScriptHash returns a new ScriptHash.
277 func NewScriptHash(serializedScript []byte, net *chaincfg.Params) (*ScriptHash, error) {
278 scriptHash := Hash160(serializedScript)
279 return newScriptHashFromHash(scriptHash, net.ScriptHashAddrID)
280 }
281 282 // NewScriptHashFromHash returns a new ScriptHash. scriptHash must be 20 bytes.
283 func NewScriptHashFromHash(scriptHash []byte, net *chaincfg.Params) (*ScriptHash, error) {
284 return newScriptHashFromHash(scriptHash, net.ScriptHashAddrID)
285 }
286 287 // newScriptHashFromHash is the internal API to create a script hash address with a known leading identifier byte
288 // for a network, rather than looking it up through its parameters. This is useful when creating a new address structure
289 // from a string encoding where the identifer byte is already known.
290 func newScriptHashFromHash(scriptHash []byte, netID byte) (*ScriptHash, error) {
291 // Chk for a valid script hash length.
292 if len(scriptHash) != ripemd160.Size {
293 return nil, errors.New("scriptHash must be 20 bytes")
294 }
295 addr := &ScriptHash{NetID: netID}
296 copy(addr.Hash[:], scriptHash)
297 return addr, nil
298 }
299 300 // EncodeAddress returns the string encoding of a pay-to-script-hash address. Part of the Address interface.
301 func (a *ScriptHash) EncodeAddress() string {
302 return encode(a.Hash[:], a.NetID)
303 }
304 305 // ScriptAddress returns the bytes to be included in a txout script to pay to a script hash. Part of the Address
306 // interface.
307 func (a *ScriptHash) ScriptAddress() []byte {
308 return a.Hash[:]
309 }
310 311 // IsForNet returns whether or not the pay-to-script-hash address is associated with the passed bitcoin network.
312 func (a *ScriptHash) IsForNet(net *chaincfg.Params) bool {
313 return a.NetID == net.ScriptHashAddrID
314 }
315 316 // String returns a human-readable string for the pay-to-script-hash address. This is equivalent to calling
317 // EncodeAddress, but is provided so the type can be used as a fmt.Stringer.
318 func (a *ScriptHash) String() string {
319 return a.EncodeAddress()
320 }
321 322 // Hash160 returns the underlying array of the script hash. This can be useful when an array is more appropriate than a
323 // slice (for example, when used as map keys).
324 func (a *ScriptHash) Hash160() *[ripemd160.Size]byte {
325 return &a.Hash
326 }
327 328 // PubKeyFormat describes what format to use for a pay-to-pubkey address.
329 type PubKeyFormat int
330 331 const (
332 // PKFUncompressed indicates the pay-to-pubkey address format is an uncompressed public key.
333 PKFUncompressed PubKeyFormat = iota
334 // PKFCompressed indicates the pay-to-pubkey address format is a compressed public key.
335 PKFCompressed
336 // PKFHybrid indicates the pay-to-pubkey address format is a hybrid public key.
337 PKFHybrid
338 )
339 340 // PubKey is an Address for a pay-to-pubkey transaction.
341 type PubKey struct {
342 PubKeyFormat PubKeyFormat
343 PublicKey *ec.PublicKey
344 pubKeyHashID byte
345 }
346 347 // NewPubKey returns a new PubKey which represents a pay-to-pubkey address. The serializedPubKey parameter
348 // must be a valid pubkey and can be uncompressed, compressed, or hybrid.
349 func NewPubKey(serializedPubKey []byte, net *chaincfg.Params) (*PubKey, error) {
350 pubKey, e := ec.ParsePubKey(serializedPubKey, ec.S256())
351 if e != nil {
352 return nil, e
353 }
354 // Set the format of the pubkey. This probably should be returned from ec, but do it here to avoid API churn. We
355 // already know the pubkey is valid since it parsed above, so it's safe to simply examine the leading byte to get
356 // the format.
357 pkFormat := PKFUncompressed
358 switch serializedPubKey[0] {
359 case 0x02, 0x03:
360 pkFormat = PKFCompressed
361 case 0x06, 0x07:
362 pkFormat = PKFHybrid
363 }
364 return &PubKey{
365 PubKeyFormat: pkFormat,
366 PublicKey: pubKey,
367 pubKeyHashID: net.PubKeyHashAddrID,
368 },
369 nil
370 }
371 372 // serialize returns the serialization of the public key according to the format associated with the address.
373 func (a *PubKey) serialize() []byte {
374 switch a.PubKeyFormat {
375 default:
376 fallthrough
377 case PKFUncompressed:
378 return a.PublicKey.SerializeUncompressed()
379 case PKFCompressed:
380 return a.PublicKey.SerializeCompressed()
381 case PKFHybrid:
382 return a.PublicKey.SerializeHybrid()
383 }
384 }
385 386 // EncodeAddress returns the string encoding of the public key as a pay-to-pubkey-hash. Note that the public key format (uncompressed, compressed, etc) will change the resulting address. This is expected since pay-to-pubkey-hash is a hash of the serialized public key which obviously differs with the format. At the time of this writing, most Bitcoin addresses are pay-to-pubkey-hash constructed from the uncompressed public key. Part of the Address interface.
387 func (a *PubKey) EncodeAddress() string {
388 return encode(Hash160(a.serialize()), a.pubKeyHashID)
389 }
390 391 // ScriptAddress returns the bytes to be included in a txout script to pay to a public key. Setting the public key format will affect the output of this function accordingly. Part of the Address interface.
392 func (a *PubKey) ScriptAddress() []byte {
393 return a.serialize()
394 }
395 396 // IsForNet returns whether or not the pay-to-pubkey address is associated with the passed bitcoin network.
397 func (a *PubKey) IsForNet(net *chaincfg.Params) bool {
398 return a.pubKeyHashID == net.PubKeyHashAddrID
399 }
400 401 // String returns the hex-encoded human-readable string for the pay-to-pubkey address. This is not the same as calling EncodeAddress.
402 func (a *PubKey) String() string {
403 return hex.EncodeToString(a.serialize())
404 }
405 406 // Format returns the format (uncompressed, compressed, etc) of the pay-to-pubkey address.
407 func (a *PubKey) Format() PubKeyFormat {
408 return a.PubKeyFormat
409 }
410 411 // SetFormat sets the format (uncompressed, compressed, etc) of the pay-to-pubkey address.
412 func (a *PubKey) SetFormat(pkFormat PubKeyFormat) {
413 a.PubKeyFormat = pkFormat
414 }
415 416 // PubKeyHash returns the pay-to-pubkey address converted to a pay-to-pubkey-hash address. Note that the public key format (uncompressed, compressed, etc) will change the resulting address. This is expected since pay-to-pubkey-hash is a hash of the serialized public key which obviously differs with the format. At the time of this writing, most Bitcoin addresses are pay-to-pubkey-hash constructed from the uncompressed public key.
417 func (a *PubKey) PubKeyHash() *PubKeyHash {
418 addr := &PubKeyHash{NetID: a.pubKeyHashID}
419 copy(addr.Hash[:], Hash160(a.serialize()))
420 return addr
421 }
422 423 // PubKey returns the underlying public key for the address.
424 func (a *PubKey) PubKey() *ec.PublicKey {
425 return a.PublicKey
426 }
427 428 //
429 // // AddressWitnessPubKeyHash is an Address for a pay-to-witness-pubkey-hash
430 // // (P2WPKH) output. See BIP 173 for further details regarding native segregated
431 // // witness address encoding:
432 // // https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
433 // type AddressWitnessPubKeyHash struct {
434 // hrp string
435 // witnessVersion byte
436 // witnessProgram [20]byte
437 // }
438 439 // // NewAddressWitnessPubKeyHash returns a new AddressWitnessPubKeyHash.
440 // func NewAddressWitnessPubKeyHash(witnessProg []byte, net *chaincfg.Params) (*AddressWitnessPubKeyHash, error) {
441 // return newAddressWitnessPubKeyHash(net.Bech32HRPSegwit, witnessProg)
442 // }
443 444 // newAddressWitnessPubKeyHash is an internal helper function to create an
445 // // AddressWitnessPubKeyHash with a known human-readable part, rather than
446 // // looking it up through its parameters.
447 // func newAddressWitnessPubKeyHash(hrp string, witnessProg []byte) (*AddressWitnessPubKeyHash, error) {
448 // // Chk for valid program length for witness version 0, which is 20 for P2WPKH.
449 // if len(witnessProg) != 20 {
450 // return nil, errors.New("witness program must be 20 " +
451 // "bytes for p2wpkh")
452 // }
453 // addr := &AddressWitnessPubKeyHash{
454 // hrp: strings.ToLower(hrp),
455 // witnessVersion: 0x00,
456 // }
457 // copy(addr.witnessProgram[:], witnessProg)
458 // return addr, nil
459 // }
460 //
461 // // EncodeAddress returns the bech32 string encoding of an
462 // // AddressWitnessPubKeyHash. Part of the Address interface.
463 // func (a *AddressWitnessPubKeyHash) EncodeAddress() string {
464 // str, e := encodeSegWitAddress(a.hrp, a.witnessVersion,
465 // a.witnessProgram[:])
466 // if e != nil {
467 // // return ""
468 // }
469 // return str
470 // }
471 //
472 // // ScriptAddress returns the witness program for this address. Part of the
473 // // Address interface.
474 // func (a *AddressWitnessPubKeyHash) ScriptAddress() []byte {
475 // return a.witnessProgram[:]
476 // }
477 //
478 // // IsForNet returns whether or not the AddressWitnessPubKeyHash is associated
479 // // with the passed bitcoin network. Part of the Address interface.
480 // func (a *AddressWitnessPubKeyHash) IsForNet(net *chaincfg.Params) bool {
481 // return a.hrp == net.Bech32HRPSegwit
482 // }
483 //
484 // // String returns a human-readable string for the AddressWitnessPubKeyHash. This
485 // // is equivalent to calling EncodeAddress, but is provided so the type can be
486 // // used as a fmt.Stringer. Part of the Address interface.
487 // func (a *AddressWitnessPubKeyHash) String() string {
488 // return a.EncodeAddress()
489 // }
490 //
491 // // Hrp returns the human-readable part of the bech32 encoded
492 // // AddressWitnessPubKeyHash.
493 // func (a *AddressWitnessPubKeyHash) Hrp() string {
494 // return a.hrp
495 // }
496 //
497 // // WitnessVersion returns the witness version of the AddressWitnessPubKeyHash.
498 // func (a *AddressWitnessPubKeyHash) WitnessVersion() byte {
499 // return a.witnessVersion
500 // }
501 //
502 // // WitnessProgram returns the witness program of the AddressWitnessPubKeyHash.
503 // func (a *AddressWitnessPubKeyHash) WitnessProgram() []byte {
504 // return a.witnessProgram[:]
505 // }
506 //
507 // // Hash160 returns the witness program of the AddressWitnessPubKeyHash as a byte
508 // // array.
509 // func (a *AddressWitnessPubKeyHash) Hash160() *[20]byte {
510 // return &a.witnessProgram
511 // }
512 //
513 // // AddressWitnessScriptHash is an Address for a pay-to-witness-script-hash
514 // // (P2WSH) output. See BIP 173 for further details regarding native segregated
515 // // witness address encoding:
516 // // https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
517 // type AddressWitnessScriptHash struct {
518 // hrp string
519 // witnessVersion byte
520 // witnessProgram [32]byte
521 // }
522 //
523 // // NewAddressWitnessScriptHash returns a new AddressWitnessPubKeyHash.
524 // func NewAddressWitnessScriptHash(witnessProg []byte, net *chaincfg.Params) (*AddressWitnessScriptHash, error) {
525 // return newAddressWitnessScriptHash(net.Bech32HRPSegwit, witnessProg)
526 // }
527 //
528 // // newAddressWitnessScriptHash is an internal helper function to create an
529 // // AddressWitnessScriptHash with a known human-readable part, rather than
530 // // looking it up through its parameters.
531 // func newAddressWitnessScriptHash(hrp string, witnessProg []byte) (*AddressWitnessScriptHash, error) {
532 // // Chk for valid program length for witness version 0, which is 32 for P2WSH.
533 // if len(witnessProg) != 32 {
534 // return nil, errors.New("witness program must be 32 " +
535 // "bytes for p2wsh")
536 // }
537 // addr := &AddressWitnessScriptHash{
538 // hrp: strings.ToLower(hrp),
539 // witnessVersion: 0x00,
540 // }
541 // copy(addr.witnessProgram[:], witnessProg)
542 // return addr, nil
543 // }
544 //
545 // // EncodeAddress returns the bech32 string encoding of an
546 // // AddressWitnessScriptHash. Part of the Address interface.
547 // func (a *AddressWitnessScriptHash) EncodeAddress() string {
548 // str, e := encodeSegWitAddress(a.hrp, a.witnessVersion,
549 // a.witnessProgram[:])
550 // if e != nil {
551 // // return ""
552 // }
553 // return str
554 // }
555 //
556 // // ScriptAddress returns the witness program for this address. Part of the
557 // // Address interface.
558 // func (a *AddressWitnessScriptHash) ScriptAddress() []byte {
559 // return a.witnessProgram[:]
560 // }
561 //
562 // // IsForNet returns whether or not the AddressWitnessScriptHash is associated
563 // // with the passed bitcoin network. Part of the Address interface.
564 // func (a *AddressWitnessScriptHash) IsForNet(net *chaincfg.Params) bool {
565 // return a.hrp == net.Bech32HRPSegwit
566 // }
567 //
568 // // String returns a human-readable string for the AddressWitnessScriptHash. This
569 // // is equivalent to calling EncodeAddress, but is provided so the type can be
570 // // used as a fmt.Stringer. Part of the Address interface.
571 // func (a *AddressWitnessScriptHash) String() string {
572 // return a.EncodeAddress()
573 // }
574 //
575 // // Hrp returns the human-readable part of the bech32 encoded
576 // // AddressWitnessScriptHash.
577 // func (a *AddressWitnessScriptHash) Hrp() string {
578 // return a.hrp
579 // }
580 //
581 // // WitnessVersion returns the witness version of the AddressWitnessScriptHash.
582 // func (a *AddressWitnessScriptHash) WitnessVersion() byte {
583 // return a.witnessVersion
584 // }
585 //
586 // // WitnessProgram returns the witness program of the AddressWitnessScriptHash.
587 // func (a *AddressWitnessScriptHash) WitnessProgram() []byte {
588 // return a.witnessProgram[:]
589 // }
590 591 // Hash160 calculates the hash ripemd160(sha256(b)).
592 func Hash160(buf []byte) []byte {
593 return calcHash(calcHash(buf, sha256.New()), ripemd160.New())
594 }
595 596 // Calculate the hash of hasher over buf.
597 func calcHash(buf []byte, hasher hash.Hash) []byte {
598 _, e := hasher.Write(buf)
599 if e != nil {
600 }
601 return hasher.Sum(nil)
602 }
603