forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathaddressindex.cpp
More file actions
43 lines (37 loc) · 1.46 KB
/
addressindex.cpp
File metadata and controls
43 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) 2016 BitPay Inc.
// Copyright (c) 2023-2024 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <addressindex.h>
#include <key_io.h>
#include <hash.h>
#include <script/script.h>
template <typename T1>
inline std::vector<uint8_t> TrimScriptP2PKH(const T1& input) {
return std::vector<uint8_t>(input.begin() + 3, input.begin() + 23);
};
template <typename T1>
inline std::vector<uint8_t> TrimScriptP2SH(const T1& input) {
return std::vector<uint8_t>(input.begin() + 2, input.begin() + 22);
};
template <typename T1>
inline std::vector<uint8_t> TrimScriptP2PK(const T1& input) {
return std::vector<uint8_t>(input.begin() + 1, input.end() - 1);
};
bool AddressBytesFromScript(const CScript& script, AddressType& address_type, uint160& address_bytes) {
if (script.IsPayToScriptHash()) {
address_type = AddressType::P2SH;
address_bytes = uint160(TrimScriptP2SH(script));
} else if (script.IsPayToPublicKeyHash()) {
address_type = AddressType::P2PK_OR_P2PKH;
address_bytes = uint160(TrimScriptP2PKH(script));
} else if (script.IsPayToPublicKey()) {
address_type = AddressType::P2PK_OR_P2PKH;
address_bytes = Hash160(TrimScriptP2PK(script));
} else {
address_type = AddressType::UNKNOWN;
address_bytes.SetNull();
return false;
}
return true;
}