forked from bitcoinsv/bsvutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_test.go
106 lines (91 loc) · 3.21 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
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2013-2017 The btcsuite 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 bsvutil package rather than than the
bsvutil_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 bsvutil
import (
"github.com/bitcoinsv/bsvd/bsvec"
"github.com/bitcoinsv/bsvd/chaincfg"
"github.com/bitcoinsv/bsvutil/base58"
"golang.org/x/crypto/ripemd160"
"strings"
)
// 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 a AddressPubKeyHash, setting the
// unexported fields with the parameters hash and netID.
func TstAddressPubKeyHash(hash [ripemd160.Size]byte,
params *chaincfg.Params) *AddressPubKeyHash {
return &AddressPubKeyHash{
hash: hash,
prefix: params.CashAddressPrefix,
}
}
// TstAddressScriptHash makes a AddressScriptHash, setting the
// unexported fields with the parameters hash and netID.
func TstAddressScriptHash(hash [ripemd160.Size]byte,
params *chaincfg.Params) *AddressScriptHash {
return &AddressScriptHash{
hash: hash,
prefix: params.CashAddressPrefix,
}
}
// TstLegacyAddressPubKeyHash makes a LegacyAddressPubKeyHash, setting the
// unexported fields with the parameters hash and netID.
func TstLegacyAddressPubKeyHash(hash [ripemd160.Size]byte,
netID byte) *LegacyAddressPubKeyHash {
return &LegacyAddressPubKeyHash{
hash: hash,
netID: netID,
}
}
// TstLegacyAddressScriptHash makes a LegacyAddressScriptHash, setting the
// unexported fields with the parameters hash and netID.
func TstLegacyAddressScriptHash(hash [ripemd160.Size]byte,
netID byte) *LegacyAddressScriptHash {
return &LegacyAddressScriptHash{
hash: hash,
netID: netID,
}
}
// TstAddressPubKey makes an AddressPubKey, setting the unexported fields with
// the parameters.
func TstAddressPubKey(serializedPubKey []byte, pubKeyFormat PubKeyFormat,
netID byte) *AddressPubKey {
pubKey, _ := bsvec.ParsePubKey(serializedPubKey, bsvec.S256())
return &AddressPubKey{
pubKeyFormat: pubKeyFormat,
pubKey: (*bsvec.PublicKey)(pubKey),
pubKeyHashID: netID,
}
}
// TstLegacyAddressSAddr returns the expected script address bytes for
// P2PKH and P2SH legacy addresses.
func TstLegacyAddressSAddr(addr string) []byte {
decoded := base58.Decode(addr)
return decoded[1 : 1+ripemd160.Size]
}
// TstAddressSAddr returns the expected script address bytes for
// P2PKH and P2SH cashaddr addresses.
func TstAddressSAddr(addr string, params *chaincfg.Params) []byte {
prefix := params.CashAddressPrefix
if !strings.HasPrefix(addr, prefix) {
addr = prefix + ":" + addr
}
decoded, _, _, _ := checkDecodeCashAddress(addr)
return decoded[:ripemd160.Size]
}