_infernal_test.go raw
1 package util
2
3 import (
4 "github.com/p9c/p9/pkg/btcaddr"
5 "golang.org/x/crypto/ripemd160"
6
7 "github.com/p9c/p9/pkg/appdata"
8 "github.com/p9c/p9/pkg/base58"
9 "github.com/p9c/p9/pkg/bech32"
10 ec "github.com/p9c/p9/pkg/ecc"
11 )
12
13 // TstAppDataDir makes the internal appDataDir function available to the test package.
14 func TstAppDataDir(goos, appName string, roaming bool) string {
15 return appdata.GetDataDir(goos, appName, roaming)
16 }
17
18 // TstAddressPubKeyHash makes an PubKeyHash, setting the unexported fields with the parameters hash and netID.
19 func TstAddressPubKeyHash(hash [ripemd160.Size]byte,
20 netID byte,
21 ) *btcaddr.PubKeyHash {
22 return &btcaddr.PubKeyHash{
23 Hash: hash,
24 NetID: netID,
25 }
26 }
27
28 // TstAddressScriptHash makes an ScriptHash, setting the unexported fields with the parameters hash and netID.
29 func TstAddressScriptHash(hash [ripemd160.Size]byte,
30 netID byte,
31 ) *btcaddr.ScriptHash {
32 return &btcaddr.ScriptHash{
33 Hash: hash,
34 NetID: netID,
35 }
36 }
37
38 // // TstAddressWitnessPubKeyHash creates an AddressWitnessPubKeyHash, initiating
39 // // the fields as given.
40 // func TstAddressWitnessPubKeyHash(version byte, program [20]byte,
41 // hrp string) *AddressWitnessPubKeyHash {
42 // return &AddressWitnessPubKeyHash{
43 // hrp: hrp,
44 // witnessVersion: version,
45 // witnessProgram: program,
46 // }
47 // }
48 //
49 // // TstAddressWitnessScriptHash creates an AddressWitnessScriptHash, initiating
50 // // the fields as given.
51 // func TstAddressWitnessScriptHash(version byte, program [32]byte,
52 // hrp string) *AddressWitnessScriptHash {
53 // return &AddressWitnessScriptHash{
54 // hrp: hrp,
55 // witnessVersion: version,
56 // witnessProgram: program,
57 // }
58 // }
59
60 // TstAddressPubKey makes an PubKey, setting the unexported fields with the parameters.
61 func TstAddressPubKey(serializedPubKey []byte, pubKeyFormat btcaddr.PubKeyFormat,
62 netID byte,
63 ) *btcaddr.PubKey {
64 pubKey, _ := ec.ParsePubKey(serializedPubKey, ec.S256())
65 return &btcaddr.PubKey{
66 PubKeyFormat: pubKeyFormat,
67 pubKey: pubKey,
68 pubKeyHashID: netID,
69 }
70 }
71
72 // TstAddressSAddr returns the expected script address bytes for P2PKH and P2SH bitcoin addresses.
73 func TstAddressSAddr(addr string) []byte {
74 decoded := base58.Decode(addr)
75 return decoded[1 : 1+ripemd160.Size]
76 }
77
78 // TstAddressSegwitSAddr returns the expected witness program bytes for bech32
79 // encoded P2WPKH and P2WSH bitcoin addresses.
80 func TstAddressSegwitSAddr(addr string) []byte {
81 _, data, e := bech32.Decode(addr)
82 if e != nil {
83 return []byte{}
84 }
85 // First byte is version, rest is base 32 encoded data.
86 data, e = bech32.ConvertBits(data[1:], 5, 8, false)
87 if e != nil {
88 return []byte{}
89 }
90 return data
91 }
92