BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
psbt_wallet_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 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 <script/sign.h>
7 #include <utilstrencodings.h>
8 #include <wallet/rpcwallet.h>
9 #include <wallet/wallet.h>
10 #include <univalue.h>
11 
12 #include <boost/test/unit_test.hpp>
13 #include <test/test_bitcoin.h>
15 
17 
18 BOOST_AUTO_TEST_CASE(psbt_updater_test)
19 {
21 
22  // Create prevtxs and add to wallet
23  CDataStream s_prev_tx1(ParseHex("0200000000010158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88702483045022100a22edcc6e5bc511af4cc4ae0de0fcd75c7e04d8c1c3a8aa9d820ed4b967384ec02200642963597b9b1bc22c75e9f3e117284a962188bf5e8a74c895089046a20ad770121035509a48eb623e10aace8bfd0212fdb8a8e5af3c94b0b133b95e114cab89e4f7965000000"), SER_NETWORK, PROTOCOL_VERSION);
24  CTransactionRef prev_tx1;
25  s_prev_tx1 >> prev_tx1;
26  CWalletTx prev_wtx1(&m_wallet, prev_tx1);
27  m_wallet.mapWallet.emplace(prev_wtx1.GetHash(), std::move(prev_wtx1));
28 
29  CDataStream s_prev_tx2(ParseHex("0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000"), SER_NETWORK, PROTOCOL_VERSION);
30  CTransactionRef prev_tx2;
31  s_prev_tx2 >> prev_tx2;
32  CWalletTx prev_wtx2(&m_wallet, prev_tx2);
33  m_wallet.mapWallet.emplace(prev_wtx2.GetHash(), std::move(prev_wtx2));
34 
35  // Add scripts
36  CScript rs1;
37  CDataStream s_rs1(ParseHex("475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae"), SER_NETWORK, PROTOCOL_VERSION);
38  s_rs1 >> rs1;
39  m_wallet.AddCScript(rs1);
40 
41  CScript rs2;
42  CDataStream s_rs2(ParseHex("2200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903"), SER_NETWORK, PROTOCOL_VERSION);
43  s_rs2 >> rs2;
44  m_wallet.AddCScript(rs2);
45 
46  CScript ws1;
47  CDataStream s_ws1(ParseHex("47522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae"), SER_NETWORK, PROTOCOL_VERSION);
48  s_ws1 >> ws1;
49  m_wallet.AddCScript(ws1);
50 
51  // Add hd seed
52  CKey key = DecodeSecret("5KSSJQ7UNfFGwVgpCZDSHm5rVNhMFcFtvWM3zQ8mW4qNDEN7LFd"); // Mainnet and uncompressed form of cUkG8i1RFfWGWy5ziR11zJ5V4U4W3viSFCfyJmZnvQaUsd1xuF3T
53  CPubKey master_pub_key = m_wallet.DeriveNewSeed(key);
54  m_wallet.SetHDSeed(master_pub_key);
56 
57  // Call FillPSBT
59  CDataStream ssData(ParseHex("70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000000000000000000"), SER_NETWORK, PROTOCOL_VERSION);
60  ssData >> psbtx;
61 
62  // Use CTransaction for the constant parts of the
63  // transaction to avoid rehashing.
64  const CTransaction txConst(*psbtx.tx);
65 
66  // Fill transaction with our data
67  FillPSBT(&m_wallet, psbtx, &txConst, 1, false, true);
68 
69  // Get the final tx
70  CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
71  ssTx << psbtx;
72  std::string final_hex = HexStr(ssTx.begin(), ssTx.end());
73  BOOST_CHECK_EQUAL(final_hex, "70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f6187650000000104475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d90c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d710d90c6a4f0000008000000080010000800001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88701042200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903010547522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc10d90c6a4f00000080000000800200008000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000");
74 }
75 
76 BOOST_AUTO_TEST_CASE(parse_hd_keypath)
77 {
78  std::vector<uint32_t> keypath;
79 
80  BOOST_CHECK(ParseHDKeypath("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));
81  BOOST_CHECK(!ParseHDKeypath("///////////////////////////", keypath));
82 
83  BOOST_CHECK(ParseHDKeypath("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));
84  BOOST_CHECK(!ParseHDKeypath("//////////////////////////'/", keypath));
85 
86  BOOST_CHECK(ParseHDKeypath("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));
87  BOOST_CHECK(!ParseHDKeypath("1///////////////////////////", keypath));
88 
89  BOOST_CHECK(ParseHDKeypath("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));
90  BOOST_CHECK(!ParseHDKeypath("1/'//////////////////////////", keypath));
91 
92  BOOST_CHECK(ParseHDKeypath("", keypath));
93  BOOST_CHECK(!ParseHDKeypath(" ", keypath));
94 
95  BOOST_CHECK(ParseHDKeypath("0", keypath));
96  BOOST_CHECK(!ParseHDKeypath("O", keypath));
97 
98  BOOST_CHECK(ParseHDKeypath("0000'/0000'/0000'", keypath));
99  BOOST_CHECK(!ParseHDKeypath("0000,/0000,/0000,", keypath));
100 
101  BOOST_CHECK(ParseHDKeypath("01234", keypath));
102  BOOST_CHECK(!ParseHDKeypath("0x1234", keypath));
103 
104  BOOST_CHECK(ParseHDKeypath("1", keypath));
105  BOOST_CHECK(!ParseHDKeypath(" 1", keypath));
106 
107  BOOST_CHECK(ParseHDKeypath("42", keypath));
108  BOOST_CHECK(!ParseHDKeypath("m42", keypath));
109 
110  BOOST_CHECK(ParseHDKeypath("4294967295", keypath)); // 4294967295 == 0xFFFFFFFF (uint32_t max)
111  BOOST_CHECK(!ParseHDKeypath("4294967296", keypath)); // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
112 
113  BOOST_CHECK(ParseHDKeypath("m", keypath));
114  BOOST_CHECK(!ParseHDKeypath("n", keypath));
115 
116  BOOST_CHECK(ParseHDKeypath("m/", keypath));
117  BOOST_CHECK(!ParseHDKeypath("n/", keypath));
118 
119  BOOST_CHECK(ParseHDKeypath("m/0", keypath));
120  BOOST_CHECK(!ParseHDKeypath("n/0", keypath));
121 
122  BOOST_CHECK(ParseHDKeypath("m/0'", keypath));
123  BOOST_CHECK(!ParseHDKeypath("m/0''", keypath));
124 
125  BOOST_CHECK(ParseHDKeypath("m/0'/0'", keypath));
126  BOOST_CHECK(!ParseHDKeypath("m/'0/0'", keypath));
127 
128  BOOST_CHECK(ParseHDKeypath("m/0/0", keypath));
129  BOOST_CHECK(!ParseHDKeypath("n/0/0", keypath));
130 
131  BOOST_CHECK(ParseHDKeypath("m/0/0/00", keypath));
132  BOOST_CHECK(!ParseHDKeypath("m/0/0/f00", keypath));
133 
134  BOOST_CHECK(ParseHDKeypath("m/0/0/000000000000000000000000000000000000000000000000000000000000000000000000000000000000", keypath));
135  BOOST_CHECK(!ParseHDKeypath("m/1/1/111111111111111111111111111111111111111111111111111111111111111111111111111111111111", keypath));
136 
137  BOOST_CHECK(ParseHDKeypath("m/0/00/0", keypath));
138  BOOST_CHECK(!ParseHDKeypath("m/0'/00/'0", keypath));
139 
140  BOOST_CHECK(ParseHDKeypath("m/1/", keypath));
141  BOOST_CHECK(!ParseHDKeypath("m/1//", keypath));
142 
143  BOOST_CHECK(ParseHDKeypath("m/0/4294967295", keypath)); // 4294967295 == 0xFFFFFFFF (uint32_t max)
144  BOOST_CHECK(!ParseHDKeypath("m/0/4294967296", keypath)); // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
145 
146  BOOST_CHECK(ParseHDKeypath("m/4294967295", keypath)); // 4294967295 == 0xFFFFFFFF (uint32_t max)
147  BOOST_CHECK(!ParseHDKeypath("m/4294967296", keypath)); // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
148 }
149 
const uint256 & GetHash() const
Definition: wallet.h:283
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
A version of CTransaction with the PSBT format.
Definition: sign.h:557
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:221
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:402
BOOST_AUTO_TEST_CASE(psbt_updater_test)
bool ParseHDKeypath(const std::string &keypath_str, std::vector< uint32_t > &keypath)
Parse an HD keypaths like "m/7/0&#39;/2000".
bool NewKeyPool()
Mark old keypool keys as used, and generate all new keys.
Definition: wallet.cpp:3149
bool AddCScript(const CScript &redeemScript) override
Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki.
Definition: wallet.cpp:355
#define LOCK(cs)
Definition: sync.h:181
An encapsulated public key.
Definition: pubkey.h:30
CCriticalSection cs_wallet
Definition: wallet.h:709
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:295
bool FillPSBT(const CWallet *pwallet, PartiallySignedTransaction &psbtx, const CTransaction *txConst, int sighash_type, bool sign, bool bip32derivs)
Definition: rpcwallet.cpp:3738
CPubKey DeriveNewSeed(const CKey &key)
Definition: wallet.cpp:1370
const_iterator end() const
Definition: streams.h:310
const_iterator begin() const
Definition: streams.h:308
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
Testing setup and teardown for wallet.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
void SetHDSeed(const CPubKey &key)
Definition: wallet.cpp:1397
CWallet & m_wallet
Definition: wallet.cpp:54
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
An encapsulated private key.
Definition: key.h:27
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:264
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:133
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::vector< unsigned char > ParseHex(const char *psz)