Skip to content

Commit

Permalink
Merge #726: Support descriptors with checksums in initpegoutwallet
Browse files Browse the repository at this point in the history
5af17a4 Update initpegoutwallet help to describe derivation paths properly. (Gregory Sanders)
55c4c6c Fix other bug in initpegoutwallet descriptor parsing (Steven Roose)
28dac17 Temp disable pegout descriptors that are not pkh() (Steven Roose)
fdba8c4 Support descriptors with checksums in initpegoutwallet (Steven Roose)

Pull request description:

  Non-pkh descriptor types will be re-enabled once entire liquid network functionary set is updated to use elements daemons.

Tree-SHA512: 41203f767c4879251eb958f938a8cc88b8f5d03ffa16bb598645f5c1bd5b45d7e6468cadcb3c5c12abd62c73913834718b9ad48a24f721537d1e8a0a3cf7e6fc
  • Loading branch information
instagibbs committed Sep 30, 2019
2 parents 280c33f + 5af17a4 commit ed03bfc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
7 changes: 0 additions & 7 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,6 @@ class DescriptorImpl : public Descriptor
}
};

//TODO(stevenroose) remove if unused
CScript P2PKHGetScript(const CPubKey& pubkey) { return GetScriptForDestination(PKHash(pubkey)); }
CScript P2PKGetScript(const CPubKey& pubkey) { return GetScriptForRawPubKey(pubkey); }
CScript P2WPKHGetScript(const CPubKey& pubkey) { return GetScriptForDestination(WitnessV0KeyHash(pubkey.GetID())); }
CScript ConvertP2SH(const CScript& script) { return GetScriptForDestination(ScriptHash(script)); }
CScript ConvertP2WSH(const CScript& script) { return GetScriptForDestination(WitnessV0ScriptHash(script)); }

/** Construct a vector with one element, which is moved into it. */
template<typename T>
std::vector<T> Singleton(T elem)
Expand Down
19 changes: 16 additions & 3 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4915,7 +4915,7 @@ UniValue initpegoutwallet(const JSONRPCRequest& request)
"\nThis call is for Liquid network initialization on the Liquid wallet. The wallet generates a new Liquid pegout authorization key (PAK) and stores it in the Liquid wallet. It then combines this with the `bitcoin_descriptor` to finally create a PAK entry for the network. This allows the user to send Liquid coins directly to a secure offline Bitcoin wallet at the derived path from the bitcoin_descriptor using the `sendtomainchain` command. Losing the Liquid PAK or offline Bitcoin root key will result in the inability to pegout funds, so immediate backup upon initialization is required.\n" +
HelpRequiringPassphrase(pwallet),
{
{"bitcoin_descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The Bitcoin descriptor that includes a single extended pubkey. Must be one of the following: pkh(<xpub>), sh(wpkh(<xpub>)), or wpkh(<xpub>). This is used as the root for the Bitcoin destination wallet. The derivation path from the xpub will be `0/k`, reflecting the external chain of the wallet. DEPRECATED: If a plain xpub is given, pkh(<xpub>) is assumed. See link for more details on script descriptors: https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md"},
{"bitcoin_descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The Bitcoin descriptor that includes a single extended pubkey. Must be one of the following: pkh(<xpub>), sh(wpkh(<xpub>)), or wpkh(<xpub>). This is used as the destination chain for the Bitcoin destination wallet. The derivation path from the xpub is given by the descriptor, typically `0/k`, reflecting the external chain of the wallet. DEPRECATED: If a plain xpub is given, pkh(<xpub>) is assumed, with the `0/k` derivation from that xpub. See link for more details on script descriptors: https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md"},
{"bip32_counter", RPCArg::Type::NUM , /* default */ "0", "The `k` in `0/k` to be set as the next address to derive from the `bitcoin_descriptor`. This will be stored in the wallet and incremented on each successful `sendtomainchain` invocation."},
{"liquid_pak", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED_NAMED_ARG, "The Liquid wallet pubkey in hex to be used as the Liquid PAK for pegout authorization. The private key must be in the wallet if argument is given. If this argument is not provided one will be generated and stored in the wallet automatically and returned."}
},
Expand Down Expand Up @@ -4982,19 +4982,32 @@ UniValue initpegoutwallet(const JSONRPCRequest& request)
}

FlatSigningProvider provider;
auto desc = Parse(bitcoin_desc, provider);
auto desc = Parse(bitcoin_desc, provider, false); // don't require checksum
if (!desc) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "bitcoin_descriptor is not a valid descriptor string.");
} else if (!desc->IsRange()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "bitcoin_descriptor must be a ranged descriptor.");
}

// For our manual pattern matching, we don't want the checksum part.
auto checksum_char = bitcoin_desc.find('#');
if (checksum_char != std::string::npos) {
bitcoin_desc = bitcoin_desc.substr(0, checksum_char);
}

// Three acceptable descriptors:
bool is_liquid = Params().NetworkIDString() == "liquidv1";
if (bitcoin_desc.substr(0, 8) == "sh(wpkh("
&& bitcoin_desc.substr(bitcoin_desc.size()-2, 2) == "))") {
if(is_liquid) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "bitcoin_descriptor is not supported by Liquid; try pkh(<xpub>) or <xpub>.");
}
xpub_str = bitcoin_desc.substr(8, bitcoin_desc.size()-2);
} else if (bitcoin_desc.substr(0, 5) == "wpkh("
&& bitcoin_desc.substr(bitcoin_desc.size()-1, 1) == ")") {
if(is_liquid) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "bitcoin_descriptor is not supported by Liquid; try pkh(<xpub>) or <xpub>.");
}
xpub_str = bitcoin_desc.substr(5, bitcoin_desc.size()-1);
} else if (bitcoin_desc.substr(0, 4) == "pkh("
&& bitcoin_desc.substr(bitcoin_desc.size()-1, 1) == ")") {
Expand All @@ -5005,7 +5018,7 @@ UniValue initpegoutwallet(const JSONRPCRequest& request)

// Strip off leading key origin
if (xpub_str.find("]") != std::string::npos) {
xpub_str = xpub_str.substr(xpub_str.find("]"), std::string::npos);
xpub_str = xpub_str.substr(xpub_str.find("]")+1, std::string::npos);
}

// Strip off following range
Expand Down
4 changes: 4 additions & 0 deletions test/functional/feature_pak.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def run_test(self):

assert_raises_rpc_error(-8, "bip32_counter must be between 0 and 1,000,000,000, inclusive.", self.nodes[1].initpegoutwallet, xpub, 1000000001)

# Make sure we can also prepend the key origin to the xpub.
self.nodes[1].initpegoutwallet("pkh([deadbeef/44h/0h/0h]"+xpub+"/0/*)")
new_init = self.nodes[1].initpegoutwallet(xpub, 2)
assert_equal(self.nodes[1].getwalletpakinfo()["bip32_counter"], "2")
assert_equal(new_init["address_lookahead"][0], init_results[1]["address_lookahead"][2])
Expand Down Expand Up @@ -167,6 +169,8 @@ def run_test(self):

# Peg out with each new type, check that destination script matches
wpkh_desc = "wpkh("+xpub+"/0/*)"
# add a valid checksum
wpkh_desc = self.nodes[1].getdescriptorinfo(wpkh_desc)["descriptor"]
wpkh_info = self.nodes[1].initpegoutwallet(wpkh_desc)
wpkh_pak_info = self.nodes[1].getwalletpakinfo()

Expand Down

0 comments on commit ed03bfc

Please sign in to comment.