-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_test.go
94 lines (78 loc) · 2.65 KB
/
internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2018 The bcext developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
/*
This test file is part of the cashutil package rather than than the
cashutil_test package so it can bridge access to the internals to properly test
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.
*/
package cashutil
import (
"strings"
"github.com/bcext/cashutil/base58"
"github.com/bcext/gcash/btcec"
"github.com/bcext/gcash/chaincfg"
"golang.org/x/crypto/ripemd160"
)
// 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.
func (b *Block) SetBlockBytes(buf []byte) {
b.serializedBlock = buf
}
// TstAppDataDir makes the internal appDataDir function available to the test
// package.
func TstAppDataDir(goos, appName string, roaming bool) string {
return appDataDir(goos, appName, roaming)
}
// TstAddressPubKeyHash makes an AddressPubKeyHash, setting the
// unexported fields with the parameters hash and netID.
func TstAddressPubKeyHash(hash [ripemd160.Size]byte,
param *chaincfg.Params) *AddressPubKeyHash {
return &AddressPubKeyHash{
hash: hash,
net: param,
}
}
// TstAddressScriptHash makes an AddressScriptHash, setting the
// unexported fields with the parameters hash and netID.
func TstAddressScriptHash(hash [ripemd160.Size]byte,
param *chaincfg.Params) *AddressScriptHash {
return &AddressScriptHash{
hash: hash,
net: param,
}
}
// TstAddressPubKey makes an AddressPubKey, setting the unexported fields with
// the parameters.
func TstAddressPubKey(serializedPubKey []byte, pubKeyFormat PubKeyFormat,
param *chaincfg.Params) *AddressPubKey {
pubKey, _ := btcec.ParsePubKey(serializedPubKey, btcec.S256())
return &AddressPubKey{
pubKeyFormat: pubKeyFormat,
pubKey: (*btcec.PublicKey)(pubKey),
net: param,
}
}
var netAddressMapping = map[string]*chaincfg.Params{
"bitcoincash": &chaincfg.MainNetParams,
"bchtest": &chaincfg.TestNet3Params,
"bchreg": &chaincfg.RegressionNetParams,
}
// TstAddressSAddr returns the expected script address bytes for
// P2PKH and P2SH bitcoin addresses.
func TstAddressSAddr(addr string) []byte {
if strings.Contains(addr, ":") {
ret := strings.Split(addr, ":")
net := netAddressMapping[ret[0]]
address, err := DecodeAddress(addr, net)
if err != nil {
return nil
}
return address.ScriptAddress()
}
decoded := base58.Decode(addr)
return decoded[1 : 1+ripemd160.Size]
}