Branch data Line data Source code
1 : : // Copyright (c) 2017-2022 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 <node/types.h>
7 : : #include <util/bip32.h>
8 : : #include <util/strencodings.h>
9 : : #include <wallet/wallet.h>
10 : :
11 : : #include <boost/test/unit_test.hpp>
12 : : #include <test/util/setup_common.h>
13 : : #include <wallet/test/wallet_test_fixture.h>
14 : :
15 : : using namespace util::hex_literals;
16 : :
17 : : namespace wallet {
18 : : BOOST_FIXTURE_TEST_SUITE(psbt_wallet_tests, WalletTestingSetup)
19 : :
20 : 3 : static void import_descriptor(CWallet& wallet, const std::string& descriptor)
21 : : EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
22 : : {
23 : 3 : AssertLockHeld(wallet.cs_wallet);
24 : 3 : FlatSigningProvider provider;
25 [ + - ]: 3 : std::string error;
26 [ + - ]: 3 : auto descs = Parse(descriptor, provider, error, /* require_checksum=*/ false);
27 [ - + ]: 3 : assert(descs.size() == 1);
28 [ + - ]: 3 : auto& desc = descs.at(0);
29 [ + - + - ]: 3 : WalletDescriptor w_desc(std::move(desc), 0, 0, 10, 0);
30 [ + - + - ]: 3 : wallet.AddWalletDescriptor(w_desc, provider, "", false);
31 : 3 : }
32 : :
33 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(psbt_updater_test)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
34 : : {
35 : 1 : LOCK(m_wallet.cs_wallet);
36 [ + - ]: 1 : m_wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
37 : :
38 : : // Create prevtxs and add to wallet
39 : 1 : DataStream s_prev_tx1{
40 [ + - ]: 1 : "0200000000010158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88702483045022100a22edcc6e5bc511af4cc4ae0de0fcd75c7e04d8c1c3a8aa9d820ed4b967384ec02200642963597b9b1bc22c75e9f3e117284a962188bf5e8a74c895089046a20ad770121035509a48eb623e10aace8bfd0212fdb8a8e5af3c94b0b133b95e114cab89e4f7965000000"_hex,
41 [ + - ]: 1 : };
42 : 1 : CTransactionRef prev_tx1;
43 [ + - ]: 1 : s_prev_tx1 >> TX_WITH_WITNESS(prev_tx1);
44 [ + - ]: 1 : m_wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(prev_tx1->GetHash()), std::forward_as_tuple(prev_tx1, TxStateInactive{}));
45 : :
46 : 1 : DataStream s_prev_tx2{
47 [ + - ]: 1 : "0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000"_hex,
48 [ + - ]: 1 : };
49 : 1 : CTransactionRef prev_tx2;
50 [ + - ]: 1 : s_prev_tx2 >> TX_WITH_WITNESS(prev_tx2);
51 [ + - ]: 1 : m_wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(prev_tx2->GetHash()), std::forward_as_tuple(prev_tx2, TxStateInactive{}));
52 : :
53 : : // Import descriptors for keys and scripts
54 [ + - + - ]: 1 : import_descriptor(m_wallet, "sh(multi(2,xprv9s21ZrQH143K2LE7W4Xf3jATf9jECxSb7wj91ZnmY4qEJrS66Qru9RFqq8xbkgT32ya6HqYJweFdJUEDf5Q6JFV7jMiUws7kQfe6Tv4RbfN/0h/0h/0h,xprv9s21ZrQH143K2LE7W4Xf3jATf9jECxSb7wj91ZnmY4qEJrS66Qru9RFqq8xbkgT32ya6HqYJweFdJUEDf5Q6JFV7jMiUws7kQfe6Tv4RbfN/0h/0h/1h))");
55 [ + - + - ]: 1 : import_descriptor(m_wallet, "sh(wsh(multi(2,xprv9s21ZrQH143K2LE7W4Xf3jATf9jECxSb7wj91ZnmY4qEJrS66Qru9RFqq8xbkgT32ya6HqYJweFdJUEDf5Q6JFV7jMiUws7kQfe6Tv4RbfN/0h/0h/2h,xprv9s21ZrQH143K2LE7W4Xf3jATf9jECxSb7wj91ZnmY4qEJrS66Qru9RFqq8xbkgT32ya6HqYJweFdJUEDf5Q6JFV7jMiUws7kQfe6Tv4RbfN/0h/0h/3h)))");
56 [ + - + - ]: 1 : import_descriptor(m_wallet, "wpkh(xprv9s21ZrQH143K2LE7W4Xf3jATf9jECxSb7wj91ZnmY4qEJrS66Qru9RFqq8xbkgT32ya6HqYJweFdJUEDf5Q6JFV7jMiUws7kQfe6Tv4RbfN/0h/0h/*h)");
57 : :
58 : : // Call FillPSBT
59 : 1 : PartiallySignedTransaction psbtx;
60 : 1 : DataStream ssData{
61 [ + - ]: 1 : "70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000000000000000000"_hex,
62 [ + - ]: 1 : };
63 [ + - ]: 1 : ssData >> psbtx;
64 : :
65 : : // Fill transaction with our data
66 : 1 : bool complete = true;
67 [ + - + - : 2 : BOOST_REQUIRE(!m_wallet.FillPSBT(psbtx, complete, SIGHASH_ALL, false, true));
+ - + - ]
68 : :
69 : : // Get the final tx
70 : 1 : DataStream ssTx{};
71 [ + - ]: 1 : ssTx << psbtx;
72 [ + - ]: 1 : std::string final_hex = HexStr(ssTx);
73 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(final_hex, "70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f6187650000000104475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d90c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d710d90c6a4f0000008000000080010000800001008a020000000158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8876500000001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88701042200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903010547522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc10d90c6a4f00000080000000800200008000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000");
74 : :
75 : : // Mutate the transaction so that one of the inputs is invalid
76 : 1 : psbtx.tx->vin[0].prevout.n = 2;
77 : :
78 : : // Try to sign the mutated input
79 : 1 : SignatureData sigdata;
80 [ + - + - : 2 : BOOST_CHECK(m_wallet.FillPSBT(psbtx, complete, SIGHASH_ALL, true, true));
+ - ]
81 [ + - + - : 4 : }
+ - ]
82 : :
83 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(parse_hd_keypath)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
84 : : {
85 : 1 : std::vector<uint32_t> keypath;
86 : :
87 [ + - + - : 2 : 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));
+ - + - +
- ]
88 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("///////////////////////////", keypath));
+ - + - +
- ]
89 : :
90 [ + - + - : 2 : 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));
+ - + - +
- ]
91 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("//////////////////////////'/", keypath));
+ - + - +
- ]
92 : :
93 [ + - + - : 2 : 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));
+ - + - +
- ]
94 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("1///////////////////////////", keypath));
+ - + - +
- ]
95 : :
96 [ + - + - : 2 : 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));
+ - + - +
- ]
97 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("1/'//////////////////////////", keypath));
+ - + - +
- ]
98 : :
99 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("", keypath));
+ - + - +
- ]
100 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath(" ", keypath));
+ - + - +
- ]
101 : :
102 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("0", keypath));
+ - + - +
- ]
103 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("O", keypath));
+ - + - +
- ]
104 : :
105 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("0000'/0000'/0000'", keypath));
+ - + - +
- ]
106 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("0000,/0000,/0000,", keypath));
+ - + - +
- ]
107 : :
108 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("01234", keypath));
+ - + - +
- ]
109 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("0x1234", keypath));
+ - + - +
- ]
110 : :
111 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("1", keypath));
+ - + - +
- ]
112 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath(" 1", keypath));
+ - + - +
- ]
113 : :
114 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("42", keypath));
+ - + - +
- ]
115 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m42", keypath));
+ - + - +
- ]
116 : :
117 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("4294967295", keypath)); // 4294967295 == 0xFFFFFFFF (uint32_t max)
+ - + - +
- ]
118 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("4294967296", keypath)); // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
+ - + - +
- ]
119 : :
120 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m", keypath));
+ - + - +
- ]
121 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("n", keypath));
+ - + - +
- ]
122 : :
123 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/", keypath));
+ - + - +
- ]
124 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("n/", keypath));
+ - + - +
- ]
125 : :
126 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0", keypath));
+ - + - +
- ]
127 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("n/0", keypath));
+ - + - +
- ]
128 : :
129 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0'", keypath));
+ - + - +
- ]
130 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/0''", keypath));
+ - + - +
- ]
131 : :
132 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0'/0'", keypath));
+ - + - +
- ]
133 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/'0/0'", keypath));
+ - + - +
- ]
134 : :
135 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0/0", keypath));
+ - + - +
- ]
136 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("n/0/0", keypath));
+ - + - +
- ]
137 : :
138 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0/0/00", keypath));
+ - + - +
- ]
139 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/0/0/f00", keypath));
+ - + - +
- ]
140 : :
141 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0/0/000000000000000000000000000000000000000000000000000000000000000000000000000000000000", keypath));
+ - + - +
- ]
142 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/1/1/111111111111111111111111111111111111111111111111111111111111111111111111111111111111", keypath));
+ - + - +
- ]
143 : :
144 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0/00/0", keypath));
+ - + - +
- ]
145 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/0'/00/'0", keypath));
+ - + - +
- ]
146 : :
147 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/1/", keypath));
+ - + - +
- ]
148 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/1//", keypath));
+ - + - +
- ]
149 : :
150 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/0/4294967295", keypath)); // 4294967295 == 0xFFFFFFFF (uint32_t max)
+ - + - +
- ]
151 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/0/4294967296", keypath)); // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
+ - + - +
- ]
152 : :
153 [ + - + - : 2 : BOOST_CHECK(ParseHDKeypath("m/4294967295", keypath)); // 4294967295 == 0xFFFFFFFF (uint32_t max)
+ - + - +
- ]
154 [ + - + - : 2 : BOOST_CHECK(!ParseHDKeypath("m/4294967296", keypath)); // 4294967296 == 0xFFFFFFFF (uint32_t max) + 1
+ - + - ]
155 : 1 : }
156 : :
157 : : BOOST_AUTO_TEST_SUITE_END()
158 : : } // namespace wallet
|