-
Notifications
You must be signed in to change notification settings - Fork 38.7k
BIP-322: Generic signed message format #16440
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
Closed
kallewoof
wants to merge
5
commits into
bitcoin:master
from
kallewoof:feature-generic-signed-message-format
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d496977
script: add simple signature support (checker/creator)
kallewoof f8ecbc1
script: add BIP-322 helpers and wallet interface
kallewoof 39ee690
rpc: update RPC commands to BIP-322
kallewoof 789bb98
qt/gui: update message signing/verification to BIP-322
kallewoof ebf4d23
test: update signmessage related tests
kallewoof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) 2019-2020 The Bitcoin Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include <script/proof.h> | ||
|
|
||
| /** | ||
| * Text used to signify that a signed message follows and to prevent | ||
| * inadvertently signing a transaction. | ||
| */ | ||
| const std::string MESSAGE_MAGIC = "Bitcoin Signed Message:\n"; | ||
|
|
||
| namespace proof | ||
| { | ||
|
|
||
| Result SignMessage::Prepare(const std::string& message, std::set<CScript>& inputs_out, uint256& sighash_out, CScript& spk_out) const { | ||
| Result rv = Purpose::Prepare(m_scriptpubkey, inputs_out); | ||
| if (rv != Result::Valid) return rv; | ||
| CHashWriter hw(SER_DISK, 0); | ||
| std::string s = MESSAGE_MAGIC + message; | ||
| hw << m_scriptpubkey << s; | ||
| sighash_out = hw.GetHash(); | ||
| spk_out = m_scriptpubkey; | ||
| return Result::Valid; | ||
| } | ||
|
|
||
| void SignMessageWithSigningProvider(std::unique_ptr<SigningProvider> sp, const std::string& message, const CTxDestination& destination, std::vector<uint8_t>& signature_out) | ||
| { | ||
| signature_out.clear(); | ||
|
|
||
| // if this is a P2PKH, use the legacy approach | ||
| const PKHash *pkhash = boost::get<PKHash>(&destination); | ||
| if (pkhash) { | ||
| CKey key; | ||
| if (!sp->GetKey(CKeyID(*pkhash), key)) { | ||
| throw privkey_unavailable_error(); | ||
| } | ||
|
|
||
| CHashWriter ss(SER_GETHASH, 0); | ||
| ss << MESSAGE_MAGIC << message; | ||
|
|
||
| if (!key.SignCompact(ss.GetHash(), signature_out)) { | ||
| throw signing_error(); | ||
| } | ||
| } else { | ||
| SignMessageWorkspace p; | ||
|
|
||
| p.AppendDestinationChallenge(destination); | ||
|
|
||
| p.Prove(message, std::move(sp)); | ||
|
|
||
| CVectorWriter w(SER_DISK, PROTOCOL_VERSION, signature_out, 0); | ||
| w << p.m_proof; | ||
| } | ||
| } | ||
|
|
||
| void SignMessageWithPrivateKey(const CKey& key, OutputType address_type, const std::string& message, std::vector<uint8_t>& signature_out) | ||
| { | ||
| if (address_type == OutputType::LEGACY) { | ||
| CHashWriter ss(SER_GETHASH, 0); | ||
| ss << MESSAGE_MAGIC << message; | ||
|
|
||
| if (!key.SignCompact(ss.GetHash(), signature_out)) { | ||
| throw signing_error(); | ||
| } | ||
| } else { | ||
| SignMessageWorkspace p; | ||
|
|
||
| p.AppendPrivKeyChallenge(key, address_type); | ||
|
|
||
| p.Prove(message); | ||
|
|
||
| CVectorWriter w(SER_DISK, PROTOCOL_VERSION, signature_out, 0); | ||
| w << p.m_proof; | ||
| } | ||
| } | ||
|
|
||
| Result VerifySignature(const std::string& message, const CTxDestination& destination, const std::vector<uint8_t>& signature) | ||
| { | ||
| // if this is a P2PKH, use the legacy approach | ||
| const PKHash* pkhash = boost::get<PKHash>(&destination); | ||
| if (pkhash) { | ||
| CHashWriter ss(SER_GETHASH, 0); | ||
| ss << MESSAGE_MAGIC << message; | ||
| CPubKey pubkey; | ||
| return ResultFromBool(pubkey.RecoverCompact(ss.GetHash(), signature) && pubkey.GetID() == *pkhash); | ||
| } | ||
|
|
||
| SignMessageWorkspace p; | ||
|
|
||
| p.AppendDestinationChallenge(destination); | ||
|
|
||
| CDataStream stream(signature, SER_DISK, PROTOCOL_VERSION); | ||
| try { | ||
| stream >> p.m_proof; | ||
| return p.Verify(message); | ||
| } catch (const std::runtime_error&) { | ||
| return Result::Error; | ||
| } | ||
| } | ||
|
|
||
| } // namespace proof |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this make signatures trivially malleable? Also, maybe SIGHASH_ALL is somewhat abused here. Do we need a flag at all?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's a problem to have malleable signature in this case, but perhaps this should be bolted down to always say
SIGHASH_ALL.We do not need the flag, in all honesty, but removing it means special-casing stuff in various places, so it felt simpler to just keep it in there.