1// Copyright (c) 2013-2017 The btcsuite developers 2// Use of this source code is governed by an ISC 3// license that can be found in the LICENSE file. 4 5/* 6This test file is part of the btcutil package rather than than the 7btcutil_test package so it can bridge access to the internals to properly test 8cases which are either not possible or can't reliably be tested via the public 9interface. The functions are only exported while the tests are being run. 10*/ 11 12package btcutil 13 14import ( 15 "github.com/btcsuite/btcd/btcec" 16 "github.com/btcsuite/btcutil/base58" 17 "github.com/btcsuite/btcutil/bech32" 18 "golang.org/x/crypto/ripemd160" 19) 20 21// SetBlockBytes sets the internal serialized block byte buffer to the passed 22// buffer. It is used to inject errors and is only available to the test 23// package. 24func (b *Block) SetBlockBytes(buf []byte) { 25 b.serializedBlock = buf 26} 27 28// TstAppDataDir makes the internal appDataDir function available to the test 29// package. 30func TstAppDataDir(goos, appName string, roaming bool) string { 31 return appDataDir(goos, appName, roaming) 32} 33 34// TstAddressPubKeyHash makes an AddressPubKeyHash, setting the 35// unexported fields with the parameters hash and netID. 36func TstAddressPubKeyHash(hash [ripemd160.Size]byte, 37 netID byte) *AddressPubKeyHash { 38 39 return &AddressPubKeyHash{ 40 hash: hash, 41 netID: netID, 42 } 43} 44 45// TstAddressScriptHash makes an AddressScriptHash, setting the 46// unexported fields with the parameters hash and netID. 47func TstAddressScriptHash(hash [ripemd160.Size]byte, 48 netID byte) *AddressScriptHash { 49 50 return &AddressScriptHash{ 51 hash: hash, 52 netID: netID, 53 } 54} 55 56// TstAddressWitnessPubKeyHash creates an AddressWitnessPubKeyHash, initiating 57// the fields as given. 58func TstAddressWitnessPubKeyHash(version byte, program [20]byte, 59 hrp string) *AddressWitnessPubKeyHash { 60 61 return &AddressWitnessPubKeyHash{ 62 hrp: hrp, 63 witnessVersion: version, 64 witnessProgram: program, 65 } 66} 67 68// TstAddressWitnessScriptHash creates an AddressWitnessScriptHash, initiating 69// the fields as given. 70func TstAddressWitnessScriptHash(version byte, program [32]byte, 71 hrp string) *AddressWitnessScriptHash { 72 73 return &AddressWitnessScriptHash{ 74 hrp: hrp, 75 witnessVersion: version, 76 witnessProgram: program, 77 } 78} 79 80// TstAddressPubKey makes an AddressPubKey, setting the unexported fields with 81// the parameters. 82func TstAddressPubKey(serializedPubKey []byte, pubKeyFormat PubKeyFormat, 83 netID byte) *AddressPubKey { 84 85 pubKey, _ := btcec.ParsePubKey(serializedPubKey, btcec.S256()) 86 return &AddressPubKey{ 87 pubKeyFormat: pubKeyFormat, 88 pubKey: (*btcec.PublicKey)(pubKey), 89 pubKeyHashID: netID, 90 } 91} 92 93// TstAddressSAddr returns the expected script address bytes for 94// P2PKH and P2SH bitcoin addresses. 95func TstAddressSAddr(addr string) []byte { 96 decoded := base58.Decode(addr) 97 return decoded[1 : 1+ripemd160.Size] 98} 99 100// TstAddressSegwitSAddr returns the expected witness program bytes for 101// bech32 encoded P2WPKH and P2WSH bitcoin addresses. 102func TstAddressSegwitSAddr(addr string) []byte { 103 _, data, err := bech32.Decode(addr) 104 if err != nil { 105 return []byte{} 106 } 107 108 // First byte is version, rest is base 32 encoded data. 109 data, err = bech32.ConvertBits(data[1:], 5, 8, false) 110 if err != nil { 111 return []byte{} 112 } 113 return data 114} 115