Bitcoin ABC  0.24.11
P2P Digital Currency
psbt_wallet_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <key_io.h>
6 #include <util/bip32.h>
7 #include <util/error.h>
8 #include <util/strencodings.h>
10 #include <wallet/wallet.h>
11 
12 #include <test/util/setup_common.h>
13 
14 #include <boost/test/unit_test.hpp>
15 
17 
18 BOOST_AUTO_TEST_CASE(psbt_updater_test) {
19  auto spk_man = m_wallet.GetOrCreateLegacyScriptPubKeyMan();
20  LOCK2(m_wallet.cs_wallet, spk_man->cs_KeyStore);
21 
22  // Create prevtxs and add to wallet
23  CDataStream s_prev_tx1(
24  ParseHex("020000000158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f54"
25  "5887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c"
26  "68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608"
27  "eb9d64d2b260db4f1118700c2eb0b0000000017a914f6539307e3a48d1e01"
28  "36d061f5d1fe19e1a240898765000000"),
30 
31  CTransactionRef prev_tx1;
32  s_prev_tx1 >> prev_tx1;
33  m_wallet.mapWallet.emplace(std::piecewise_construct,
34  std::forward_as_tuple(prev_tx1->GetId()),
35  std::forward_as_tuple(&m_wallet, prev_tx1));
36 
37  CDataStream s_prev_tx2(
38  ParseHex(
39  "0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71"
40  "ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd3013"
41  "5aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f"
42  "0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9"
43  "463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74"
44  "f8a08f81999428185c97b5d852e4063f618765000000"),
46  CTransactionRef prev_tx2;
47  s_prev_tx2 >> prev_tx2;
48  m_wallet.mapWallet.emplace(std::piecewise_construct,
49  std::forward_as_tuple(prev_tx2->GetId()),
50  std::forward_as_tuple(&m_wallet, prev_tx2));
51 
52  // Add scripts
53  CScript rs1;
54  CDataStream s_rs1(
55  ParseHex("475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c218"
56  "3f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54"
57  "dae4dba2fbfef536d752ae"),
59  s_rs1 >> rs1;
60  spk_man->AddCScript(rs1);
61 
62  CScript rs2;
63  CDataStream s_rs2(
64  ParseHex("47522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1"
65  "b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e8"
66  "6151926860221f0e7352ae"),
68  s_rs2 >> rs2;
69  spk_man->AddCScript(rs2);
70 
71  // Add hd seed
72  // Mainnet and uncompressed form of
73  // cUkG8i1RFfWGWy5ziR11zJ5V4U4W3viSFCfyJmZnvQaUsd1xuF3T
74  CKey key =
75  DecodeSecret("5KSSJQ7UNfFGwVgpCZDSHm5rVNhMFcFtvWM3zQ8mW4qNDEN7LFd");
76  CPubKey master_pub_key = spk_man->DeriveNewSeed(key);
77  spk_man->SetHDSeed(master_pub_key);
78  spk_man->NewKeyPool();
79 
80  // Call FillPSBT
82  CDataStream ssData(
83  ParseHex("70736274ff0100a0020000000258e87a21b56daf0c23be8e7070456c336f7"
84  "cbaa5c8757924f545887bb2abdd750000000000ffffffff6b04ec37326fba"
85  "c8e468a73bf952c8877f84f96c3f9deadeab246455e34fe0cd0100000000f"
86  "fffffff0270aaf008000000001976a914d85c2b71d0060b09c9886aeb815e"
87  "50991dda124d88ac00e1f505000000001976a91400aea9a2e5f0f876a588d"
88  "f5546e8742d1d87008f88ac000000000000000000"),
90  ssData >> psbtx;
91 
92  // FIXME: input 2 hd path is missing.
93  // The path missing comes from the HD masterkey.
94 
95  // Fill transaction with our data
96  bool complete = true;
97  BOOST_REQUIRE_EQUAL(
98  m_wallet.FillPSBT(psbtx, complete, SigHashType(), false, true),
100 
101  // Get the final tx
103  ssTx << psbtx;
104  std::string final_hex = HexStr(ssTx);
106  final_hex,
107  "70736274ff0100a0020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c875"
108  "7924f545887bb2abdd750000000000ffffffff6b04ec37326fbac8e468a73bf952c887"
109  "7f84f96c3f9deadeab246455e34fe0cd0100000000ffffffff0270aaf0080000000019"
110  "76a914d85c2b71d0060b09c9886aeb815e50991dda124d88ac00e1f505000000001976"
111  "a91400aea9a2e5f0f876a588df5546e8742d1d87008f88ac000000000001002080f0fa"
112  "020000000017a9140fb9463421696b82c833af241c78c17ddbde493487010447522102"
113  "9583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102da"
114  "b61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206"
115  "029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d9"
116  "0c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b"
117  "18312b5b4e54dae4dba2fbfef536d710d90c6a4f000000800000008001000080000100"
118  "2000c2eb0b0000000017a914f6539307e3a48d1e0136d061f5d1fe19e1a24089870104"
119  "47522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02"
120  "dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e73"
121  "52ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f"
122  "0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af61"
123  "7333db388cead0c231f723379d1b99030b02dc10d90c6a4f0000008000000080020000"
124  "8000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5"
125  "877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02"
126  "c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500"
127  "008000");
128 }
129 
130 BOOST_AUTO_TEST_CASE(parse_hd_keypath) {
131  std::vector<uint32_t> keypath;
132 
134  "1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1", keypath));
135  BOOST_CHECK(!ParseHDKeypath("///////////////////////////", keypath));
136 
138  "1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1'/1", keypath));
139  BOOST_CHECK(!ParseHDKeypath("//////////////////////////'/", keypath));
140 
142  "1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/", keypath));
143  BOOST_CHECK(!ParseHDKeypath("1///////////////////////////", keypath));
144 
146  "1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1'/", keypath));
147  BOOST_CHECK(!ParseHDKeypath("1/'//////////////////////////", keypath));
148 
149  BOOST_CHECK(ParseHDKeypath("", keypath));
150  BOOST_CHECK(!ParseHDKeypath(" ", keypath));
151 
152  BOOST_CHECK(ParseHDKeypath("0", keypath));
153  BOOST_CHECK(!ParseHDKeypath("O", keypath));
154 
155  BOOST_CHECK(ParseHDKeypath("0000'/0000'/0000'", keypath));
156  BOOST_CHECK(!ParseHDKeypath("0000,/0000,/0000,", keypath));
157 
158  BOOST_CHECK(ParseHDKeypath("01234", keypath));
159  BOOST_CHECK(!ParseHDKeypath("0x1234", keypath));
160 
161  BOOST_CHECK(ParseHDKeypath("1", keypath));
162  BOOST_CHECK(!ParseHDKeypath(" 1", keypath));
163 
164  BOOST_CHECK(ParseHDKeypath("42", keypath));
165  BOOST_CHECK(!ParseHDKeypath("m42", keypath));
166 
167  // 4294967295 == 0xFFFFFFFF (uint32_t max)
168  BOOST_CHECK(ParseHDKeypath("4294967295", keypath));
169  // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
170  BOOST_CHECK(!ParseHDKeypath("4294967296", keypath));
171 
172  BOOST_CHECK(ParseHDKeypath("m", keypath));
173  BOOST_CHECK(!ParseHDKeypath("n", keypath));
174 
175  BOOST_CHECK(ParseHDKeypath("m/", keypath));
176  BOOST_CHECK(!ParseHDKeypath("n/", keypath));
177 
178  BOOST_CHECK(ParseHDKeypath("m/0", keypath));
179  BOOST_CHECK(!ParseHDKeypath("n/0", keypath));
180 
181  BOOST_CHECK(ParseHDKeypath("m/0'", keypath));
182  BOOST_CHECK(!ParseHDKeypath("m/0''", keypath));
183 
184  BOOST_CHECK(ParseHDKeypath("m/0'/0'", keypath));
185  BOOST_CHECK(!ParseHDKeypath("m/'0/0'", keypath));
186 
187  BOOST_CHECK(ParseHDKeypath("m/0/0", keypath));
188  BOOST_CHECK(!ParseHDKeypath("n/0/0", keypath));
189 
190  BOOST_CHECK(ParseHDKeypath("m/0/0/00", keypath));
191  BOOST_CHECK(!ParseHDKeypath("m/0/0/f00", keypath));
192 
193  BOOST_CHECK(ParseHDKeypath("m/0/0/"
194  "00000000000000000000000000000000000000000000000"
195  "0000000000000000000000000000000000000",
196  keypath));
197  BOOST_CHECK(!ParseHDKeypath("m/1/1/"
198  "1111111111111111111111111111111111111111111111"
199  "11111111111111111111111111111111111111",
200  keypath));
201 
202  BOOST_CHECK(ParseHDKeypath("m/0/00/0", keypath));
203  BOOST_CHECK(!ParseHDKeypath("m/0'/00/'0", keypath));
204 
205  BOOST_CHECK(ParseHDKeypath("m/1/", keypath));
206  BOOST_CHECK(!ParseHDKeypath("m/1//", keypath));
207 
208  // 4294967295 == 0xFFFFFFFF (uint32_t max)
209  BOOST_CHECK(ParseHDKeypath("m/0/4294967295", keypath));
210  // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
211  BOOST_CHECK(!ParseHDKeypath("m/0/4294967296", keypath));
212 
213  // 4294967295 == 0xFFFFFFFF (uint32_t max)
214  BOOST_CHECK(ParseHDKeypath("m/4294967295", keypath));
215  // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
216  BOOST_CHECK(!ParseHDKeypath("m/4294967296", keypath));
217 }
218 
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:244
bip32.h
m_wallet
std::shared_ptr< CWallet > m_wallet
Definition: wallet.cpp:462
ParseHex
std::vector< uint8_t > ParseHex(const char *psz)
Definition: strencodings.cpp:87
wallet.h
key_io.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(psbt_updater_test)
Definition: psbt_wallet_tests.cpp:18
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:165
strencodings.h
SigHashType
Signature hash type wrapper class.
Definition: sighashtype.h:37
error.h
DecodeSecret
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:80
WalletTestingSetup
Testing setup and teardown for wallet.
Definition: wallet_test_fixture.h:21
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
wallet_test_fixture.h
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
CKey
An encapsulated secp256k1 private key.
Definition: key.h:28
PartiallySignedTransaction
A version of CTransaction with the PSBT format.
Definition: psbt.h:335
TransactionError::OK
@ OK
No error.
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
ParseHDKeypath
bool ParseHDKeypath(const std::string &keypath_str, std::vector< uint32_t > &keypath)
Parse an HD keypaths like "m/7/0'/2000".
Definition: bip32.cpp:13
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:656
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_AUTO_TEST_SUITE_END
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11