MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ParsePubkeyInner

Function ParsePubkeyInner

src/script/descriptor.cpp:1938–2014  ·  view source on GitHub ↗

Parse a public key that excludes origin information. */

Source from the content-addressed store, hash-verified

1936
1937/** Parse a public key that excludes origin information. */
1938std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t& key_exp_index, const std::span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, bool& apostrophe, std::string& error)
1939{
1940 std::vector<std::unique_ptr<PubkeyProvider>> ret;
1941 bool permit_uncompressed = ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH;
1942 auto split = Split(sp, '/');
1943 std::string str(split[0].begin(), split[0].end());
1944 if (str.size() == 0) {
1945 error = "No key provided";
1946 return {};
1947 }
1948 if (IsSpace(str.front()) || IsSpace(str.back())) {
1949 error = strprintf("Key '%s' is invalid due to whitespace", str);
1950 return {};
1951 }
1952 if (split.size() == 1) {
1953 if (IsHex(str)) {
1954 std::vector<unsigned char> data = ParseHex(str);
1955 CPubKey pubkey(data);
1956 if (pubkey.IsValid() && !pubkey.IsValidNonHybrid()) {
1957 error = "Hybrid public keys are not allowed";
1958 return {};
1959 }
1960 if (pubkey.IsFullyValid()) {
1961 if (permit_uncompressed || pubkey.IsCompressed()) {
1962 ret.emplace_back(std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey, false));
1963 ++key_exp_index;
1964 return ret;
1965 } else {
1966 error = "Uncompressed keys are not allowed";
1967 return {};
1968 }
1969 } else if (data.size() == 32 && ctx == ParseScriptContext::P2TR) {
1970 unsigned char fullkey[33] = {0x02};
1971 std::copy(data.begin(), data.end(), fullkey + 1);
1972 pubkey.Set(std::begin(fullkey), std::end(fullkey));
1973 if (pubkey.IsFullyValid()) {
1974 ret.emplace_back(std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey, true));
1975 ++key_exp_index;
1976 return ret;
1977 }
1978 }
1979 error = strprintf("Pubkey '%s' is invalid", str);
1980 return {};
1981 }
1982 CKey key = DecodeSecret(str);
1983 if (key.IsValid()) {
1984 if (permit_uncompressed || key.IsCompressed()) {
1985 CPubKey pubkey = key.GetPubKey();
1986 out.keys.emplace(pubkey.GetID(), key);
1987 ret.emplace_back(std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey, ctx == ParseScriptContext::P2TR));
1988 ++key_exp_index;
1989 return ret;
1990 } else {
1991 error = "Uncompressed keys are not allowed";
1992 return {};
1993 }
1994 }
1995 }

Callers 1

ParsePubkeyFunction · 0.85

Calls 15

IsSpaceFunction · 0.85
IsHexFunction · 0.85
ParseHexFunction · 0.85
beginFunction · 0.85
endFunction · 0.85
DecodeSecretFunction · 0.85
DecodeExtKeyFunction · 0.85
DecodeExtPubKeyFunction · 0.85
ParseDeriveTypeFunction · 0.85
ParseKeyPathFunction · 0.85
IsValidNonHybridMethod · 0.80
IsFullyValidMethod · 0.80

Tested by

no test coverage detected