2017-02-16 17:07:38 +01:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
2013-05-30 18:50:58 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
2021-09-10 22:30:39 +02:00
|
|
|
This test file is part of the lbcutil package rather than than the
|
|
|
|
lbcutil_test package so it can bridge access to the internals to properly test
|
2013-05-30 18:50:58 +02:00
|
|
|
cases which are either not possible or can't reliably be tested via the public
|
|
|
|
interface. The functions are only exported while the tests are being run.
|
|
|
|
*/
|
|
|
|
|
2021-09-10 22:30:39 +02:00
|
|
|
package lbcutil
|
2013-05-30 18:50:58 +02:00
|
|
|
|
2014-01-03 17:10:25 +01:00
|
|
|
import (
|
2021-09-10 22:30:39 +02:00
|
|
|
"github.com/lbryio/lbcd/btcec"
|
|
|
|
"github.com/lbryio/lbcutil/base58"
|
|
|
|
"github.com/lbryio/lbcutil/bech32"
|
2017-02-16 17:07:38 +01:00
|
|
|
"golang.org/x/crypto/ripemd160"
|
2014-01-03 17:10:25 +01:00
|
|
|
)
|
|
|
|
|
2013-08-05 19:41:31 +02:00
|
|
|
// SetBlockBytes sets the internal serialized block byte buffer to the passed
|
|
|
|
// buffer. It is used to inject errors and is only available to the test
|
|
|
|
// package.
|
2013-05-30 18:50:58 +02:00
|
|
|
func (b *Block) SetBlockBytes(buf []byte) {
|
2013-08-05 19:41:31 +02:00
|
|
|
b.serializedBlock = buf
|
2013-05-30 18:50:58 +02:00
|
|
|
}
|
2013-11-10 19:27:39 +01:00
|
|
|
|
|
|
|
// TstAppDataDir makes the internal appDataDir function available to the test
|
|
|
|
// package.
|
|
|
|
func TstAppDataDir(goos, appName string, roaming bool) string {
|
|
|
|
return appDataDir(goos, appName, roaming)
|
|
|
|
}
|
2014-01-03 17:10:25 +01:00
|
|
|
|
|
|
|
// TstAddressPubKeyHash makes an AddressPubKeyHash, setting the
|
2014-02-26 20:51:49 +01:00
|
|
|
// unexported fields with the parameters hash and netID.
|
2014-01-03 17:10:25 +01:00
|
|
|
func TstAddressPubKeyHash(hash [ripemd160.Size]byte,
|
2014-02-26 20:51:49 +01:00
|
|
|
netID byte) *AddressPubKeyHash {
|
2014-01-03 17:10:25 +01:00
|
|
|
|
|
|
|
return &AddressPubKeyHash{
|
2014-02-26 20:51:49 +01:00
|
|
|
hash: hash,
|
|
|
|
netID: netID,
|
2014-01-03 17:10:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TstAddressScriptHash makes an AddressScriptHash, setting the
|
2014-02-26 20:51:49 +01:00
|
|
|
// unexported fields with the parameters hash and netID.
|
2014-01-03 17:10:25 +01:00
|
|
|
func TstAddressScriptHash(hash [ripemd160.Size]byte,
|
2014-02-26 20:51:49 +01:00
|
|
|
netID byte) *AddressScriptHash {
|
2014-01-03 17:10:25 +01:00
|
|
|
|
|
|
|
return &AddressScriptHash{
|
2014-02-26 20:51:49 +01:00
|
|
|
hash: hash,
|
|
|
|
netID: netID,
|
2014-01-03 17:10:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 07:47:41 +02:00
|
|
|
// TstAddressWitnessPubKeyHash creates an AddressWitnessPubKeyHash, initiating
|
|
|
|
// the fields as given.
|
|
|
|
func TstAddressWitnessPubKeyHash(version byte, program [20]byte,
|
|
|
|
hrp string) *AddressWitnessPubKeyHash {
|
|
|
|
|
|
|
|
return &AddressWitnessPubKeyHash{
|
|
|
|
hrp: hrp,
|
|
|
|
witnessVersion: version,
|
|
|
|
witnessProgram: program,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TstAddressWitnessScriptHash creates an AddressWitnessScriptHash, initiating
|
|
|
|
// the fields as given.
|
|
|
|
func TstAddressWitnessScriptHash(version byte, program [32]byte,
|
|
|
|
hrp string) *AddressWitnessScriptHash {
|
|
|
|
|
|
|
|
return &AddressWitnessScriptHash{
|
|
|
|
hrp: hrp,
|
|
|
|
witnessVersion: version,
|
|
|
|
witnessProgram: program,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 06:05:07 +01:00
|
|
|
// TstAddressPubKey makes an AddressPubKey, setting the unexported fields with
|
|
|
|
// the parameters.
|
|
|
|
func TstAddressPubKey(serializedPubKey []byte, pubKeyFormat PubKeyFormat,
|
2014-02-26 20:51:49 +01:00
|
|
|
netID byte) *AddressPubKey {
|
2014-01-08 06:05:07 +01:00
|
|
|
|
|
|
|
pubKey, _ := btcec.ParsePubKey(serializedPubKey, btcec.S256())
|
|
|
|
return &AddressPubKey{
|
|
|
|
pubKeyFormat: pubKeyFormat,
|
2020-05-13 15:25:32 +02:00
|
|
|
pubKey: pubKey,
|
2014-05-28 00:06:00 +02:00
|
|
|
pubKeyHashID: netID,
|
2014-01-08 06:05:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:10:25 +01:00
|
|
|
// TstAddressSAddr returns the expected script address bytes for
|
|
|
|
// P2PKH and P2SH bitcoin addresses.
|
|
|
|
func TstAddressSAddr(addr string) []byte {
|
2014-12-17 14:07:53 +01:00
|
|
|
decoded := base58.Decode(addr)
|
2014-01-03 17:10:25 +01:00
|
|
|
return decoded[1 : 1+ripemd160.Size]
|
|
|
|
}
|
2017-07-25 07:47:41 +02:00
|
|
|
|
|
|
|
// TstAddressSegwitSAddr returns the expected witness program bytes for
|
|
|
|
// bech32 encoded P2WPKH and P2WSH bitcoin addresses.
|
|
|
|
func TstAddressSegwitSAddr(addr string) []byte {
|
|
|
|
_, data, err := bech32.Decode(addr)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First byte is version, rest is base 32 encoded data.
|
|
|
|
data, err = bech32.ConvertBits(data[1:], 5, 8, false)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|