Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIP-322 basic support #24058

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class TxVerbosity {
// core_read.cpp
CScript ParseScript(const std::string& s);
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode = false);
[[nodiscard]] bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness);
[[nodiscard]] bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true);
[[nodiscard]] bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
Expand Down
2 changes: 1 addition & 1 deletion src/core_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
return true;
}

static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
{
// General strategy:
// - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Wallet
virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;

//! Sign message
virtual SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) = 0;
virtual SigningResult signMessage(const MessageSignatureFormat format, const std::string& message, const CTxDestination& address, std::string& str_sig) = 0;

//! Return whether wallet has private key.
virtual bool isSpendable(const CTxDestination& dest) = 0;
Expand Down
30 changes: 22 additions & 8 deletions src/qt/signverifymessagedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,6 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
return;
}
const PKHash* pkhash = std::get_if<PKHash>(&destination);
if (!pkhash) {
ui->addressIn_SM->setValid(false);
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");
ui->statusLabel_SM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
return;
}

WalletModel::UnlockContext ctx(model->requestUnlock());
if (!ctx.isValid())
Expand All @@ -138,7 +131,7 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()

const std::string& message = ui->messageIn_SM->document()->toPlainText().toStdString();
std::string signature;
SigningResult res = model->wallet().signMessage(message, *pkhash, signature);
SigningResult res = model->wallet().signMessage(MessageSignatureFormat::SIMPLE, message, destination, signature);

QString error;
switch (res) {
Expand Down Expand Up @@ -214,6 +207,27 @@ void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked()
QString("<nobr>") + tr("Message verified.") + QString("</nobr>")
);
return;
case MessageVerificationResult::OK_TIMELOCKED:
ui->statusLabel_VM->setText(
QString("<nobr>") + tr("Message verified, but includes timelocks.") + QString("</nobr>")
);
return;
case MessageVerificationResult::INCONCLUSIVE:
ui->statusLabel_VM->setText(
QString("<nobr>") + tr("Inconclusive.") + QString("</nobr>")
);
return;
case MessageVerificationResult::ERR_INVALID:
ui->statusLabel_VM->setText(
tr("Some check failed.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 8f9c561 "add basic BIP-322 message signing support"

I think this should just be Message verification failed. It could passthrough to the ERR_NOT_SIGNED result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to avoid using the same message for different errors, as it makes troubleshooting more difficult. That said, maybe "Some check failed" is not the right wording here...

);
return;
case MessageVerificationResult::ERR_POF:
// TODO: support proof of funds verifications
ui->statusLabel_VM->setText(
QString("</nobr>") + tr("Proof of funds verification unavailable right now.") + QString("</nobr>")
);
return;
case MessageVerificationResult::ERR_INVALID_ADDRESS:
ui->statusLabel_VM->setText(
tr("The entered address is invalid.") + QString(" ") +
Expand Down
7 changes: 7 additions & 0 deletions src/rpc/signmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ static RPCHelpMan verifymessage()
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
throw JSONRPCError(RPC_TYPE_ERROR, "Malformed base64 encoding");
case MessageVerificationResult::ERR_POF:
throw JSONRPCError(RPC_TYPE_ERROR, "BIP-322 Proof of funds is not yet supported"); // TODO: get access to UTXO set / mempool to handle this, then remove this error code?
case MessageVerificationResult::INCONCLUSIVE:
return false; // TODO: switch to a string based result? mix bool and strings?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 30642a1 "message: add BIP-322 verification support (without POF)"

This could just fallthrough to the return false below instead of having it's own return false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that the return false; statement itself is a placeholder for when a decision is made on how to deal with inconclusive proofs.

case MessageVerificationResult::ERR_INVALID:
case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
case MessageVerificationResult::ERR_NOT_SIGNED:
return false;
case MessageVerificationResult::OK_TIMELOCKED:
// TODO: switch to string based result? mix bool and strings?
case MessageVerificationResult::OK:
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,10 @@ bool GenericTransactionSignatureChecker<T>::CheckECDSASignature(const std::vecto
int nHashType = vchSig.back();
vchSig.pop_back();

if (m_require_sighash_all && nHashType != SIGHASH_ALL) {
return false;
}

// Witness sighashes need the amount.
if (sigversion == SigVersion::WITNESS_V0 && amount < 0) return HandleMissingData(m_mdb);

Expand Down Expand Up @@ -1686,6 +1690,9 @@ bool GenericTransactionSignatureChecker<T>::CheckSchnorrSignature(Span<const uns
uint8_t hashtype = SIGHASH_DEFAULT;
if (sig.size() == 65) {
hashtype = SpanPopBack(sig);
if (m_require_sighash_all && hashtype != SIGHASH_ALL) {
return set_error(serror, SCRIPT_ERR_SIG_HASHTYPE);
}
if (hashtype == SIGHASH_DEFAULT) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_HASHTYPE);
}
uint256 sighash;
Expand Down
5 changes: 3 additions & 2 deletions src/script/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,15 @@ class GenericTransactionSignatureChecker : public BaseSignatureChecker
unsigned int nIn;
const CAmount amount;
const PrecomputedTransactionData* txdata;
bool m_require_sighash_all;

protected:
virtual bool VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
virtual bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const;

public:
GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, MissingDataBehavior mdb) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn, MissingDataBehavior mdb) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, MissingDataBehavior mdb, bool require_sighash_all = false) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(nullptr), m_require_sighash_all(require_sighash_all) {}
GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn, MissingDataBehavior mdb, bool require_sighash_all = false) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(&txdataIn), m_require_sighash_all(require_sighash_all) {}
bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override;
bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override;
bool CheckLockTime(const CScriptNum& nLockTime) const override;
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ FUZZ_TARGET_INIT(message, initialize_message)
}
}
{
(void)MessageHash(random_message);
(void)MessageHash(random_message, MessageSignatureFormat::LEGACY);
(void)MessageVerify(fuzzed_data_provider.ConsumeRandomLengthString(1024), fuzzed_data_provider.ConsumeRandomLengthString(1024), random_message);
(void)SigningResultString(fuzzed_data_provider.PickValueInArray({SigningResult::OK, SigningResult::PRIVATE_KEY_NOT_AVAILABLE, SigningResult::SIGNING_FAILED}));
}
Expand Down
172 changes: 167 additions & 5 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <fs.h>
#include <hash.h> // For Hash()
#include <key.h> // For CKey
#include <key_io.h> // EncodeDestination
#include <outputtype.h> // For BIP-322 tests
#include <sync.h>
#include <test/util/logging.h>
#include <test/util/setup_common.h>
Expand Down Expand Up @@ -2605,23 +2607,55 @@ BOOST_AUTO_TEST_CASE(message_sign)
"Sign with a valid private key");

BOOST_CHECK_EQUAL(expected_signature, generated_signature);

// BIP-322 tests
// (no signing done here, as we need a wallet to do so)

auto pubkey = privkey.GetPubKey();
MessageVerificationResult mvr{MessageVerificationResult::OK};

// LEGACY pubkey type
auto dest_legacy = GetDestinationForKey(pubkey, OutputType::LEGACY);
BOOST_CHECK_EQUAL("15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs", EncodeDestination(dest_legacy));
auto txs_legacy = BIP322Txs::Create(dest_legacy, message, mvr);
if (!txs_legacy || mvr != MessageVerificationResult::OK) {
BOOST_FAIL("Failed to create BIP-322 txs for legacy address");
}

// P2SH_SEGWIT pubkey type
auto dest_p2sh_segwit = GetDestinationForKey(pubkey, OutputType::P2SH_SEGWIT);
BOOST_CHECK_EQUAL("35uijJkf4rcCnGzEZsn12YJenTHToDKpr2", EncodeDestination(dest_p2sh_segwit));
auto txs_p2sh_segwit = BIP322Txs::Create(dest_p2sh_segwit, message, mvr);
if (!txs_p2sh_segwit || mvr != MessageVerificationResult::OK) {
BOOST_FAIL("Failed to create BIP-322 txs for p2sh-segwit address");
}

// BECH32
auto dest_bech32 = GetDestinationForKey(pubkey, OutputType::BECH32);
BOOST_CHECK_EQUAL("bc1q9cy7s7nmzah0m6mt2ftmu6x723esjxqkkl4wsw", EncodeDestination(dest_bech32));
auto txs_bech32 = BIP322Txs::Create(dest_bech32, message, mvr);
if (!txs_bech32 || mvr != MessageVerificationResult::OK) {
BOOST_FAIL("Failed to create BIP-322 txs for bech32 address");
}

// TODO: BECH32M
}

BOOST_AUTO_TEST_CASE(message_verify)
{
BOOST_CHECK_EQUAL(
MessageVerify(
"invalid address",
"signature should be irrelevant",
"AA==",
"message too"),
MessageVerificationResult::ERR_INVALID_ADDRESS);

BOOST_CHECK_EQUAL(
MessageVerify(
"3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV",
"signature should be irrelevant",
"AA==",
"message too"),
MessageVerificationResult::ERR_ADDRESS_NO_KEY);
MessageVerificationResult::ERR_INVALID /* ERR_ADDRESS_NO_KEY */);

BOOST_CHECK_EQUAL(
MessageVerify(
Expand All @@ -2635,7 +2669,7 @@ BOOST_AUTO_TEST_CASE(message_verify)
"1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"message should be irrelevant"),
MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED);
MessageVerificationResult::ERR_INVALID /* ERR_PUBKEY_NOT_RECOVERED */);

BOOST_CHECK_EQUAL(
MessageVerify(
Expand All @@ -2657,6 +2691,124 @@ BOOST_AUTO_TEST_CASE(message_verify)
"IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzIAaHRtbCeDZINyavx14=",
"Trust me"),
MessageVerificationResult::OK);

// BIP-322 tests

// privkey: L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k

BOOST_CHECK_EQUAL(
MessageVerify(
"bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l",
"AkcwRAIgM2gBAQqvZX15ZiysmKmQpDrG83avLIT492QBzLnQIxYCIBaTpOaD20qRlEylyxFSeEA2ba9YOixpX8z46TSDtS40ASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=",
""),
MessageVerificationResult::OK);

BOOST_CHECK_EQUAL(
MessageVerify(
"bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l",
"AkcwRAIgZRfIY3p7/DoVTty6YZbWS71bc5Vct9p9Fia83eRmw2QCICK/ENGfwLtptFluMGs2KsqoNSk89pO7F29zJLUx9a/sASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=",
"Hello World"),
MessageVerificationResult::OK);

kallewoof marked this conversation as resolved.
Show resolved Hide resolved
// BIP322 signature created using buidl-python library with same parameters as test on line 2596
BOOST_CHECK_EQUAL(
MessageVerify(
"bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l",
"AkgwRQIhAOzyynlqt93lOKJr+wmmxIens//zPzl9tqIOua93wO6MAiBi5n5EyAcPScOjf1lAqIUIQtr3zKNeavYabHyR8eGhowEhAsfxIAMZZEKUPYWI4BruhAQjzFT8FSFSajuFwrDL1Yhy",
"Hello World"),
Comment on lines +2717 to +2718

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get this value in my implementation, yet it is not reflected in the BIP

MessageVerificationResult::OK);

kallewoof marked this conversation as resolved.
Show resolved Hide resolved
// 2-of-3 p2sh multisig BIP322 signature (created with the buidl-python library)
// Keys are defined as (HDRootWIF, bip322_path)
// Key1 (L4DksdGZ4KQJfcLHD5Dv25fu8Rxyv7hHi2RjZR4TYzr8c6h9VNrp, m/45'/0/0/1)
// Key2 (KzSRqnCVwjzY8id2X5oHEJWXkSHwKUYaAXusjwgkES8BuQPJnPNu, m/45'/0/0/3)
// Key3 (L1zt9Rw7HrU7jaguMbVzhiX8ffuVkmMis5wLHddXYuHWYf8u8uRj, m/45'/0/0/6)
// BIP322 includes signs from Key2 and Key3
BOOST_CHECK_EQUAL(
MessageVerify(
"3LnYoUkFrhyYP3V7rq3mhpwALz1XbCY9Uq",
"AAAAAAHNcfHaNfl8f/+ZC2gTr8aF+0KgppYjKM94egaNm/u1ZAAAAAD8AEcwRAIhAJ6hdj61vLDP+aFa30qUZQmrbBfE0kiOObYvt5nqPSxsAh9IrOKFwflfPRUcQ/5e0REkdFHVP2GGdUsMgDet+sNlAUcwRAIgH3eW/VyFDoXvCasd8qxgwj5NDVo0weXvM6qyGXLCR5YCIEwjbEV6fS6RWP6QsKOcMwvlGr1/SgdCC6pW4eH87/YgAUxpUiECKJfGy28imLcuAeNBLHCNv3NRP5jnJwFDNRXCYNY/vJ4hAv1RQtaZs7+vKqQeWl2rb/jd/gMxkEjUnjZdDGPDZkMLIQL65cH2X5O7LujjTLDL2l8Pxy0Y2UUR99u1qCfjdz7dklOuAAAAAAEAAAAAAAAAAAFqAAAAAA==",
"This will be a p2sh 2-of-3 multisig BIP 322 signed message"),
MessageVerificationResult::OK);

// 3-of-3 p2wsh multisig BIP322 signature (created with the buidl-python library)
// Keys are defined as (HDRootWIF, bip322_path)
// Key1 (L4DksdGZ4KQJfcLHD5Dv25fu8Rxyv7hHi2RjZR4TYzr8c6h9VNrp, m/45'/0/0/6)
// Key2 (KzSRqnCVwjzY8id2X5oHEJWXkSHwKUYaAXusjwgkES8BuQPJnPNu, m/45'/0/0/9)
// Key3 (L1zt9Rw7HrU7jaguMbVzhiX8ffuVkmMis5wLHddXYuHWYf8u8uRj, m/45'/0/0/11)
BOOST_CHECK_EQUAL(
MessageVerify(
"bc1qlqtuzpmazp2xmcutlwv0qvggdvem8vahkc333usey4gskug8nutsz53msw", "BQBIMEUCIQDQoXvGKLH58exuujBOta+7+GN7vi0lKwiQxzBpuNuXuAIgIE0XYQlFDOfxbegGYYzlf+tqegleAKE6SXYIa1U+uCcBRzBEAiATegywVl6GWrG9jJuPpNwtgHKyVYCX2yfuSSDRFATAaQIgTLlU6reLQsSIrQSF21z3PtUO2yAUseUWGZqRUIE7VKoBSDBFAiEAgxtpidsU0Z4u/+5RB9cyeQtoCW5NcreLJmWXZ8kXCZMCIBR1sXoEinhZE4CF9P9STGIcMvCuZjY6F5F0XTVLj9SjAWlTIQP3dyWvTZjUENWJowMWBsQrrXCUs20Gu5YF79CG5Ga0XSEDwqI5GVBOuFkFzQOGH5eTExSAj2Z/LDV/hbcvAPQdlJMhA17FuuJd+4wGuj+ZbVxEsFapTKAOwyhfw9qpch52JKxbU64=",
"This will be a p2wsh 3-of-3 multisig BIP 322 signed message"),
MessageVerificationResult::OK);

// Single key p2tr BIP322 signature (created with the buidl-python library)
// PrivateKeyWIF L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k
BOOST_CHECK_EQUAL(
MessageVerify(
"bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3",
"AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==",
"Hello World"),
MessageVerificationResult::OK);

// Same p2tr BIP322 signature as above (created with the buidl-python library)
// Signature should not verify against the message
BOOST_CHECK_EQUAL(
MessageVerify(
"bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3",
"AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==",
"Hello World - This should fail"),
MessageVerificationResult::ERR_INVALID);

// wrong address

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test case that demonstrates the issue with taproot signatures that @richerandprettier refers to. It seems that p2tr signatures validate against any message under the current impl.

Is there some extra configuration required to setup bitcoin with schnorr & taproot, wondering if these sigs are processed at all?

I ran this against the buidl-python code - buidl-bitcoin/buidl-python#140 - and get the expected results. Will dig into the code when i get some time.

Suggested change
// wrong address
// Single key p2tr BIP322 signature (created with the buidl-python library)
// PrivateKeyWIF L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k
BOOST_CHECK_EQUAL(
MessageVerify(
"bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3", "AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==",
"Hello World"),
MessageVerificationResult::OK);
// Same p2tr BIP322 signature as above (created with the buidl-python library)
// Signature should not verify against the message
BOOST_CHECK_EQUAL(
MessageVerify(
"bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3",
"AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==",
"Hello World - This should fail"),
MessageVerificationResult::ERR_INVALID);
// wrong address

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test! Immensely helpful. Looking into what's causing the issue now.


BOOST_CHECK_EQUAL(
MessageVerify(
"bc1qkecg9ly2xwxqgdy9egpuy87qc9x26smpts562s",
"AkcwRAIgM2gBAQqvZX15ZiysmKmQpDrG83avLIT492QBzLnQIxYCIBaTpOaD20qRlEylyxFSeEA2ba9YOixpX8z46TSDtS40ASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=",
""),
MessageVerificationResult::ERR_INVALID);

BOOST_CHECK_EQUAL(
MessageVerify(
"bc1qkecg9ly2xwxqgdy9egpuy87qc9x26smpts562s",
"AkcwRAIgZRfIY3p7/DoVTty6YZbWS71bc5Vct9p9Fia83eRmw2QCICK/ENGfwLtptFluMGs2KsqoNSk89pO7F29zJLUx9a/sASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=",
"Hello World"),
MessageVerificationResult::ERR_INVALID);

// wrong signature / message (signatures swapped)

BOOST_CHECK_EQUAL(
MessageVerify(
"bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l",
"AkcwRAIgZRfIY3p7/DoVTty6YZbWS71bc5Vct9p9Fia83eRmw2QCICK/ENGfwLtptFluMGs2KsqoNSk89pO7F29zJLUx9a/sASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=",
""),
MessageVerificationResult::ERR_INVALID);

BOOST_CHECK_EQUAL(
MessageVerify(
"bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l",
"AkcwRAIgM2gBAQqvZX15ZiysmKmQpDrG83avLIT492QBzLnQIxYCIBaTpOaD20qRlEylyxFSeEA2ba9YOixpX8z46TSDtS40ASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=",
"Hello World"),
MessageVerificationResult::ERR_INVALID);

// invalid address

BOOST_CHECK_EQUAL(
MessageVerify(
"bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx1l",
"AkcwRAIgM2gBAQqvZX15ZiysmKmQpDrG83avLIT492QBzLnQIxYCIBaTpOaD20qRlEylyxFSeEA2ba9YOixpX8z46TSDtS40ASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=",
""),
MessageVerificationResult::ERR_INVALID_ADDRESS);

// malformed signature

BOOST_CHECK_EQUAL(
MessageVerify(
"bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l",
"AkcwRAIgClVQ8S9yX1h8YThlGElD9lOrQbOwbFDjkYb0ebfiq+oCIDHgb/X9WNalNNtqTXb465ufbv9JuLxcJf8qi7DP6yOXASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI",
""),
MessageVerificationResult::ERR_MALFORMED_SIGNATURE);
}

BOOST_AUTO_TEST_CASE(message_hash)
Expand All @@ -2670,10 +2822,20 @@ BOOST_AUTO_TEST_CASE(message_hash)

const uint256 signature_hash = Hash(unsigned_tx);
const uint256 message_hash1 = Hash(prefixed_message);
const uint256 message_hash2 = MessageHash(unsigned_tx);
const uint256 message_hash2 = MessageHash(unsigned_tx, MessageSignatureFormat::LEGACY);

BOOST_CHECK_EQUAL(message_hash1, message_hash2);
BOOST_CHECK_NE(message_hash1, signature_hash);

// BIP-322 tests

const uint256 signature_hash_0x = MessageHash("", MessageSignatureFormat::FULL);
const uint256 signature_hash_Hello_World = MessageHash("Hello World", MessageSignatureFormat::FULL);

std::vector<unsigned char> vec(signature_hash_0x.begin(), signature_hash_0x.end());
BOOST_CHECK_EQUAL("c90c269c4f8fcbe6880f72a721ddfbf1914268a794cbb21cfafee13770ae19f1", HexStr(vec));
vec = std::vector<unsigned char>(signature_hash_Hello_World.begin(), signature_hash_Hello_World.end());
BOOST_CHECK_EQUAL("f0eb03b1a75ac6d9847f55c624a99169b5dccba2a31f5b23bea77ba270de0a7a", HexStr(vec));
}

BOOST_AUTO_TEST_CASE(remove_prefix)
Expand Down
Loading