Bitcoin ABC  0.22.13
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  CWalletTx prev_wtx1(&m_wallet, prev_tx1);
34  m_wallet.mapWallet.emplace(prev_wtx1.GetId(), std::move(prev_wtx1));
35 
36  CDataStream s_prev_tx2(
37  ParseHex(
38  "0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71"
39  "ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd3013"
40  "5aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f"
41  "0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9"
42  "463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74"
43  "f8a08f81999428185c97b5d852e4063f618765000000"),
45  CTransactionRef prev_tx2;
46  s_prev_tx2 >> prev_tx2;
47  CWalletTx prev_wtx2(&m_wallet, prev_tx2);
48  m_wallet.mapWallet.emplace(prev_wtx2.GetId(), std::move(prev_wtx2));
49 
50  // Add scripts
51  CScript rs1;
52  CDataStream s_rs1(
53  ParseHex("475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c218"
54  "3f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54"
55  "dae4dba2fbfef536d752ae"),
57  s_rs1 >> rs1;
58  spk_man->AddCScript(rs1);
59 
60  CScript rs2;
61  CDataStream s_rs2(
62  ParseHex("47522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1"
63  "b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e8"
64  "6151926860221f0e7352ae"),
66  s_rs2 >> rs2;
67  spk_man->AddCScript(rs2);
68 
69  // Add hd seed
70  // Mainnet and uncompressed form of
71  // cUkG8i1RFfWGWy5ziR11zJ5V4U4W3viSFCfyJmZnvQaUsd1xuF3T
72  CKey key =
73  DecodeSecret("5KSSJQ7UNfFGwVgpCZDSHm5rVNhMFcFtvWM3zQ8mW4qNDEN7LFd");
74  CPubKey master_pub_key = spk_man->DeriveNewSeed(key);
75  spk_man->SetHDSeed(master_pub_key);
76  spk_man->NewKeyPool();
77 
78  // Call FillPSBT
80  CDataStream ssData(
81  ParseHex("70736274ff0100a0020000000258e87a21b56daf0c23be8e7070456c336f7"
82  "cbaa5c8757924f545887bb2abdd750000000000ffffffff6b04ec37326fba"
83  "c8e468a73bf952c8877f84f96c3f9deadeab246455e34fe0cd0100000000f"
84  "fffffff0270aaf008000000001976a914d85c2b71d0060b09c9886aeb815e"
85  "50991dda124d88ac00e1f505000000001976a91400aea9a2e5f0f876a588d"
86  "f5546e8742d1d87008f88ac000000000000000000"),
88  ssData >> psbtx;
89 
90  // FIXME: input 2 hd path is missing.
91  // The path missing comes from the HD masterkey.
92 
93  // Fill transaction with our data
94  bool complete = true;
95  BOOST_REQUIRE_EQUAL(
96  m_wallet.FillPSBT(psbtx, complete, SigHashType(), false, true),
98 
99  // Get the final tx
101  ssTx << psbtx;
102  std::string final_hex = HexStr(ssTx.begin(), ssTx.end());
104  final_hex,
105  "70736274ff0100a0020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c875"
106  "7924f545887bb2abdd750000000000ffffffff6b04ec37326fbac8e468a73bf952c887"
107  "7f84f96c3f9deadeab246455e34fe0cd0100000000ffffffff0270aaf0080000000019"
108  "76a914d85c2b71d0060b09c9886aeb815e50991dda124d88ac00e1f505000000001976"
109  "a91400aea9a2e5f0f876a588df5546e8742d1d87008f88ac000000000001002080f0fa"
110  "020000000017a9140fb9463421696b82c833af241c78c17ddbde493487010447522102"
111  "9583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102da"
112  "b61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206"
113  "029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d9"
114  "0c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b"
115  "18312b5b4e54dae4dba2fbfef536d710d90c6a4f000000800000008001000080000100"
116  "2000c2eb0b0000000017a914f6539307e3a48d1e0136d061f5d1fe19e1a24089870104"
117  "47522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02"
118  "dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e73"
119  "52ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f"
120  "0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af61"
121  "7333db388cead0c231f723379d1b99030b02dc10d90c6a4f0000008000000080020000"
122  "8000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5"
123  "877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02"
124  "c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500"
125  "008000");
126 }
127 
128 BOOST_AUTO_TEST_CASE(parse_hd_keypath) {
129  std::vector<uint32_t> keypath;
130 
132  "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));
133  BOOST_CHECK(!ParseHDKeypath("///////////////////////////", keypath));
134 
136  "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));
137  BOOST_CHECK(!ParseHDKeypath("//////////////////////////'/", keypath));
138 
140  "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));
141  BOOST_CHECK(!ParseHDKeypath("1///////////////////////////", keypath));
142 
144  "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));
145  BOOST_CHECK(!ParseHDKeypath("1/'//////////////////////////", keypath));
146 
147  BOOST_CHECK(ParseHDKeypath("", keypath));
148  BOOST_CHECK(!ParseHDKeypath(" ", keypath));
149 
150  BOOST_CHECK(ParseHDKeypath("0", keypath));
151  BOOST_CHECK(!ParseHDKeypath("O", keypath));
152 
153  BOOST_CHECK(ParseHDKeypath("0000'/0000'/0000'", keypath));
154  BOOST_CHECK(!ParseHDKeypath("0000,/0000,/0000,", keypath));
155 
156  BOOST_CHECK(ParseHDKeypath("01234", keypath));
157  BOOST_CHECK(!ParseHDKeypath("0x1234", keypath));
158 
159  BOOST_CHECK(ParseHDKeypath("1", keypath));
160  BOOST_CHECK(!ParseHDKeypath(" 1", keypath));
161 
162  BOOST_CHECK(ParseHDKeypath("42", keypath));
163  BOOST_CHECK(!ParseHDKeypath("m42", keypath));
164 
165  // 4294967295 == 0xFFFFFFFF (uint32_t max)
166  BOOST_CHECK(ParseHDKeypath("4294967295", keypath));
167  // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
168  BOOST_CHECK(!ParseHDKeypath("4294967296", keypath));
169 
170  BOOST_CHECK(ParseHDKeypath("m", keypath));
171  BOOST_CHECK(!ParseHDKeypath("n", keypath));
172 
173  BOOST_CHECK(ParseHDKeypath("m/", keypath));
174  BOOST_CHECK(!ParseHDKeypath("n/", keypath));
175 
176  BOOST_CHECK(ParseHDKeypath("m/0", keypath));
177  BOOST_CHECK(!ParseHDKeypath("n/0", keypath));
178 
179  BOOST_CHECK(ParseHDKeypath("m/0'", keypath));
180  BOOST_CHECK(!ParseHDKeypath("m/0''", keypath));
181 
182  BOOST_CHECK(ParseHDKeypath("m/0'/0'", keypath));
183  BOOST_CHECK(!ParseHDKeypath("m/'0/0'", keypath));
184 
185  BOOST_CHECK(ParseHDKeypath("m/0/0", keypath));
186  BOOST_CHECK(!ParseHDKeypath("n/0/0", keypath));
187 
188  BOOST_CHECK(ParseHDKeypath("m/0/0/00", keypath));
189  BOOST_CHECK(!ParseHDKeypath("m/0/0/f00", keypath));
190 
191  BOOST_CHECK(ParseHDKeypath("m/0/0/"
192  "00000000000000000000000000000000000000000000000"
193  "0000000000000000000000000000000000000",
194  keypath));
195  BOOST_CHECK(!ParseHDKeypath("m/1/1/"
196  "1111111111111111111111111111111111111111111111"
197  "11111111111111111111111111111111111111",
198  keypath));
199 
200  BOOST_CHECK(ParseHDKeypath("m/0/00/0", keypath));
201  BOOST_CHECK(!ParseHDKeypath("m/0'/00/'0", keypath));
202 
203  BOOST_CHECK(ParseHDKeypath("m/1/", keypath));
204  BOOST_CHECK(!ParseHDKeypath("m/1//", keypath));
205 
206  // 4294967295 == 0xFFFFFFFF (uint32_t max)
207  BOOST_CHECK(ParseHDKeypath("m/0/4294967295", keypath));
208  // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
209  BOOST_CHECK(!ParseHDKeypath("m/0/4294967296", keypath));
210 
211  // 4294967295 == 0xFFFFFFFF (uint32_t max)
212  BOOST_CHECK(ParseHDKeypath("m/4294967295", keypath));
213  // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
214  BOOST_CHECK(!ParseHDKeypath("m/4294967296", keypath));
215 }
216 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
std::shared_ptr< CWallet > m_wallet
Definition: wallet.cpp:464
A version of CTransaction with the PSBT format.
Definition: psbt.h:335
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:196
#define LOCK2(cs1, cs2)
Definition: sync.h:233
BOOST_AUTO_TEST_CASE(psbt_updater_test)
An encapsulated public key.
Definition: pubkey.h:31
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:280
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
Testing setup and teardown for wallet.
bool ParseHDKeypath(const std::string &keypath_str, std::vector< uint32_t > &keypath)
Parse an HD keypaths like "m/7/0&#39;/2000".
Definition: bip32.cpp:13
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
std::vector< uint8_t > ParseHex(const char *psz)
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:132
An encapsulated secp256k1 private key.
Definition: key.h:25
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:80
TxId GetId() const
Definition: wallet.h:593
Signature hash type wrapper class.
Definition: sighashtype.h:37
#define BOOST_CHECK(expr)
Definition: object.cpp:17