Skip to content

Commit 58b5fa0

Browse files
authored
Merge pull request #1186 from achow101/createfunded-explicit-value
Add include_explicit to walletcreatefundedpsbt
2 parents 9c06762 + 42cbdc8 commit 58b5fa0

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

src/blindpsbt.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,21 @@ static bool CreateBlindValueProof(std::vector<unsigned char>& rangeproof, const
136136
return res == 1;
137137
}
138138

139+
bool CreateBlindValueProof(std::vector<unsigned char>& rangeproof, const uint256& value_blinder, const CAmount amount, const CConfidentialValue& conf_value, const CConfidentialAsset& conf_asset)
140+
{
141+
secp256k1_pedersen_commitment value_commit;
142+
int ret = secp256k1_pedersen_commitment_parse(secp256k1_blind_context, &value_commit, conf_value.vchCommitment.data());
143+
assert(ret == 1);
144+
145+
secp256k1_generator asset_gen;
146+
ret = secp256k1_generator_parse(secp256k1_blind_context, &asset_gen, conf_asset.vchCommitment.data());
147+
assert(ret == 1);
148+
149+
return CreateBlindValueProof(rangeproof, value_blinder, amount, value_commit, asset_gen);
150+
}
151+
139152
// Create an explicit value rangeproof which proves that the commitment commits to an explicit value
140-
static bool CreateBlindAssetProof(std::vector<unsigned char>& assetproof, const CAsset& asset, const CConfidentialAsset& asset_commit, const uint256& asset_blinder)
153+
bool CreateBlindAssetProof(std::vector<unsigned char>& assetproof, const CAsset& asset, const CConfidentialAsset& asset_commit, const uint256& asset_blinder)
141154
{
142155
const unsigned char zero32[32] = {0};
143156
secp256k1_surjectionproof proof;

src/blindpsbt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void CreateValueCommitment(CConfidentialValue& conf_value, secp256k1_pedersen_co
5050
BlindingStatus BlindPSBT(PartiallySignedTransaction& psbt, std::map<uint32_t, std::tuple<CAmount, CAsset, uint256, uint256>> our_input_data, std::map<uint32_t, std::pair<CKey, CKey>> our_issuances_to_blind);
5151

5252

53+
bool CreateBlindValueProof(std::vector<unsigned char>& rangeproof, const uint256& value_blinder, const CAmount amount, const CConfidentialValue& conf_value, const CConfidentialAsset& conf_asset);
54+
bool CreateBlindAssetProof(std::vector<unsigned char>& assetproof, const CAsset& asset, const CConfidentialAsset& asset_commit, const uint256& asset_blinder);
5355
bool VerifyBlindValueProof(CAmount value, const CConfidentialValue& conf_value, const std::vector<unsigned char>& proof, const CConfidentialAsset& conf_asset);
5456
bool VerifyBlindAssetProof(const uint256& asset, const std::vector<unsigned char>& proof, const CConfidentialAsset& conf_asset);
5557
BlindProofResult VerifyBlindProofs(const PSBTOutput& o);

src/wallet/rpcwallet.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,7 @@ void FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& fee_out,
33373337
{"replaceable", UniValueType(UniValue::VBOOL)},
33383338
{"conf_target", UniValueType(UniValue::VNUM)},
33393339
{"estimate_mode", UniValueType(UniValue::VSTR)},
3340+
{"include_explicit", UniValueType(UniValue::VBOOL)},
33403341
},
33413342
true, true);
33423343

@@ -4909,6 +4910,7 @@ static RPCHelpMan walletcreatefundedpsbt()
49094910
{"conf_target", RPCArg::Type::NUM, RPCArg::DefaultHint{"wallet -txconfirmtarget"}, "Confirmation target in blocks"},
49104911
{"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"unset"}, std::string() + "The fee estimate mode, must be one of (case insensitive):\n"
49114912
" \"" + FeeModes("\"\n\"") + "\""},
4913+
{"include_explicit", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include explicit values and assets and their proofs for blinded inputs"},
49124914
},
49134915
"options"},
49144916
{"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"},
@@ -5089,10 +5091,13 @@ static RPCHelpMan walletcreatefundedpsbt()
50895091
throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing output for reissuance tokens");
50905092
}
50915093

5094+
// Determine whether to include explicit values
5095+
bool include_explicit = request.params[3].exists("include_explicit") && request.params[3]["include_explicit"].get_bool();
5096+
50925097
// Fill transaction with out data but don't sign
50935098
bool bip32derivs = request.params[4].isNull() ? true : request.params[4].get_bool();
50945099
bool complete = true;
5095-
const TransactionError err{wallet.FillPSBT(psbtx, complete, 1, false, bip32derivs, true)};
5100+
const TransactionError err{wallet.FillPSBT(psbtx, complete, 1, false, bip32derivs, true, nullptr, include_explicit)};
50965101
if (err != TransactionError::OK) {
50975102
throw JSONRPCTransactionError(err);
50985103
}

src/wallet/wallet.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <wallet/wallet.h>
77

8+
#include <blindpsbt.h>
89
#include <chain.h>
910
#include <consensus/consensus.h>
1011
#include <consensus/validation.h>
@@ -1817,7 +1818,7 @@ bool CWallet::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint,
18171818
}
18181819

18191820
// ELEMENTS: split FillPSBT into FillPSBData and SignPSBT
1820-
TransactionError CWallet::FillPSBTData(PartiallySignedTransaction& psbtx, bool bip32derivs) const
1821+
TransactionError CWallet::FillPSBTData(PartiallySignedTransaction& psbtx, bool bip32derivs, bool include_explicit) const
18211822
{
18221823
const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);
18231824
LOCK(cs_wallet);
@@ -1841,6 +1842,17 @@ TransactionError CWallet::FillPSBTData(PartiallySignedTransaction& psbtx, bool b
18411842
if (*input.prev_out < wtx.tx->witness.vtxoutwit.size() && !wtx.tx->witness.vtxoutwit[*input.prev_out].vchRangeproof.empty()) {
18421843
input.m_utxo_rangeproof = wtx.tx->witness.vtxoutwit[*input.prev_out].vchRangeproof;
18431844
}
1845+
const CTxOut& utxo = wtx.tx->vout[*input.prev_out];
1846+
if (include_explicit && utxo.nAsset.IsCommitment()) {
1847+
const CAsset asset = wtx.GetOutputAsset(*input.prev_out);
1848+
input.m_explicit_asset = asset.id;
1849+
CreateBlindAssetProof(input.m_asset_proof, asset, utxo.nAsset, wtx.GetOutputAssetBlindingFactor(*input.prev_out));
1850+
1851+
if (utxo.nValue.IsCommitment()) {
1852+
input.m_explicit_value = wtx.GetOutputValueOut(*input.prev_out);
1853+
CreateBlindValueProof(input.m_value_proof, wtx.GetOutputAmountBlindingFactor(*input.prev_out), *input.m_explicit_value, utxo.nValue, utxo.nAsset);
1854+
}
1855+
}
18441856
}
18451857
}
18461858
}
@@ -2109,11 +2121,11 @@ TransactionError CWallet::SignPSBT(PartiallySignedTransaction& psbtx, bool& comp
21092121
}
21102122

21112123
// This function remains for backwards compatibility. It will not succeed in Elements unless everything involved is non-blinded.
2112-
TransactionError CWallet::FillPSBT(PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs, bool imbalance_ok, size_t* n_signed) const
2124+
TransactionError CWallet::FillPSBT(PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs, bool imbalance_ok, size_t* n_signed, bool include_explicit) const
21132125
{
21142126
complete = false;
21152127
TransactionError te;
2116-
te = FillPSBTData(psbtx, bip32derivs);
2128+
te = FillPSBTData(psbtx, bip32derivs, include_explicit);
21172129
if (te != TransactionError::OK) {
21182130
return te;
21192131
}

src/wallet/wallet.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
608608
bool sign = true,
609609
bool bip32derivs = true,
610610
bool imbalance_ok = false,
611-
size_t* n_signed = nullptr) const;
611+
size_t* n_signed = nullptr,
612+
bool include_explicit = false) const;
612613

613614
// ELEMENTS
614-
TransactionError FillPSBTData(PartiallySignedTransaction& psbtx, bool bip32derivs = false) const;
615+
TransactionError FillPSBTData(PartiallySignedTransaction& psbtx, bool bip32derivs = false, bool include_explicit = false) const;
615616
TransactionError SignPSBT(PartiallySignedTransaction& psbtx, bool& complete, int sighash_type = 1, bool sign = true, bool imbalance_ok = false, bool bip32derivs = false, size_t* n_signed = nullptr) const;
616617
BlindingStatus WalletBlindPSBT(PartiallySignedTransaction& psbtx) const;
617618
// end ELEMENTS

test/functional/rpc_psbt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,14 @@ def run_ca_tests(self):
679679
self.nodes[0].generate(1)
680680
self.sync_all()
681681

682+
# Check include_explicit option
683+
psbt = self.nodes[2].walletcreatefundedpsbt([{"txid": txid_conf_2, "vout": 1}], [{self.get_address(True, 0): 24.998, "blinder_index": 0}, {"fee": 0.001}], 0, {"include_explicit": True})["psbt"]
684+
decoded = self.nodes[1].decodepsbt(psbt)
685+
assert "explicit_value" in decoded["inputs"][0]
686+
assert "value_proof" in decoded["inputs"][0]
687+
assert "explicit_asset" in decoded["inputs"][0]
688+
assert "asset_proof" in decoded["inputs"][0]
689+
682690
# Try to send conf->conf
683691
conf_addr_4 = self.get_address(True, 0)
684692
psbt = self.nodes[2].createpsbt([{"txid": txid_conf_2, "vout": 1}], [{conf_addr_4: 24.998, "blinder_index": 0}, {"fee": 0.001}])

0 commit comments

Comments
 (0)