-
Notifications
You must be signed in to change notification settings - Fork 9
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
src: BTC sign message implemented and tested #53
Conversation
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.
gj!!
please setup your editor to run rustfmt to format the code on save, I see the code is unformatted.
.github/README.md
Outdated
For Intel Macs where the homebrew commands are not installed in /opt/homebrew folder: | ||
|
||
AR=/usr/local/opt/llvm/bin/llvm-ar CC=/usr/local/opt/llvm/bin/clang make wasm | ||
|
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.
please make a separate commit for this as it has nothing to do with the sign message functionality.
maybe drop Intel
? or is it really about intel macs?
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 have experienced the error in an Intel Mac and I am unsure about other ones. That's why I wanted to be specific. But I will add that to another commit.
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.
yeah but maybe for Intel Macs it could also be in the other location, or the same might be necessary for non-Intel Macs :P unless we know for sure it's best to be generic
examples/btc_sign_msg.rs
Outdated
let _addr = paired_bitbox.btc_address( | ||
pb::BtcCoin::Btc, | ||
&keypath, | ||
&script_config_addr, | ||
true, | ||
).await | ||
.unwrap(); |
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.
delete this? it has nothing to do with sign_msg.
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.
It was implemented because a similar call is also made in the python version of the code, but I can delete as it is not that relevant.
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.
Ah now I see, but there it is called with display=False, so the address can be printed to stdout before getting the message from stdin. I'd say all of that is not needed here.
src/btc.rs
Outdated
}), | ||
}; | ||
|
||
let response: pb::btc_response::Response = self.query_proto_btc(pb::btc_request::Request::SignMessage(request)).await?; |
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.
can drop the : pb::btc_response::Response
as it is inferred. if you want to keep it, drop pb::
as you already imported btc_response
at the top.
src/btc.rs
Outdated
_ => return Err(Error::UnexpectedResponse), | ||
}; | ||
|
||
let request: pb::AntiKleptoSignatureRequest = pb::AntiKleptoSignatureRequest { |
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.
same, no need to declare the type : pb::AntiKleptoSignatureRequest
here.
i would maybe even inline this struct into the request below.
src/btc.rs
Outdated
host_nonce: host_nonce.to_vec(), | ||
}; | ||
|
||
let response: btc_response::Response = self.query_proto_btc(pb::btc_request::Request::AntikleptoSignature(request)).await?; |
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.
drop type declaration here too and everywhere below
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.
Okay, removing all type declarations. I was using them for my own comfort.
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 see. I'd say it is not idiomatic. Type declarations are usually only put if the compiler can't infer it or if the type is something complicated or involve generics, but in these cases the types are trivial.
src/btc.rs
Outdated
|
||
let mut sig: Vec<u8> = signature[..64].to_vec(); | ||
let recid: u8 = signature[64].clone(); | ||
let compressed: u8 = 4; |
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.
keep the comment from the bitbox02.py impl:
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.
Of course, my bad. Adding right away.
src/btc.rs
Outdated
let compressed: u8 = 4; | ||
let electrum_sig65: u8 = 27 + compressed + recid; | ||
sig.splice(0..0, electrum_sig65.to_be_bytes()); | ||
Ok(sig) |
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.
in the Go library and in the Python library we return a tuple (signature, recID, electrumSig65)
.
Please do the same here. Maybe best to add a new struct with these three fields and return that.
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.
Where should I define the struct exactly? At the beginning of the same file I assume.
src/btc.rs
Outdated
crate::antiklepto::verify_ecdsa(&host_nonce, &signer_commitment, &signature)?; | ||
|
||
let mut sig: Vec<u8> = signature[..64].to_vec(); | ||
let recid: u8 = signature[64].clone(); |
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.
no need to call .clone()
btw: you can run clippy, the linter, like cargo clippy --features="usb"
and see it complain about this ;) the CI runs clippy as well.
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.
You're right. The command also complained about this :) I think I did instinctly.
f52e41e
to
e99e24e
Compare
may I propose a better title?
it is shorter, and |
I implemented the requested changes and now pushing a new commit according to them |
e99e24e
to
be840cd
Compare
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.
almost there! thanks!
.github/README.md
Outdated
@@ -41,7 +41,7 @@ Therefore a new clang compiler and archiver needs to be installed via: | |||
In order to use that new clang compiler and archiver specify it when runing `make wasm`: | |||
|
|||
AR=/opt/homebrew/opt/llvm/bin/llvm-ar CC=/opt/homebrew/opt/llvm/bin/clang make wasm | |||
|
|||
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.
nit: accidental change. pls also set up your editor to delete trailing whitespace :)
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.
You're right, adjusting it
src/btc.rs
Outdated
@@ -198,6 +198,13 @@ pub struct Transaction { | |||
pub locktime: u32, | |||
} | |||
|
|||
#[derive(Debug, PartialEq)] | |||
pub struct Signature { |
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.
please rename - Signature is too generic and it is not used by the other sign functions. SignMessageResult
or SignMessageSignature
is better.
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 also thought of that, but used Signature
since it was not used earlier. However, for future implementation, your suggestion is better
pub struct Signature { | ||
pub sig: Vec<u8>, | ||
pub recid: u8, | ||
pub electrum_sig65: Vec<u8>, |
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.
moving the docstring comment here could be helpful to users of this library:
src/btc.rs
Outdated
let mut electrum_sig65 = signature.clone(); | ||
electrum_sig65.splice(0..0, sig65.to_be_bytes()); |
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.
it is slightly awkward to serialize a one byte integer as big endian - it is just one byte ;) you can just do [sig65]
.
Both lines could also be replaced with this, which is probably a bit more readable:
let mut electrum_sig65 = vec![sig65];
electrum_sig65.extend_from_slice(&sig)
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.
Due to my inexperience in Rust, I just did it the first way I discovered :). Updating them according to your suggestion.
.btc_sign_message(pb::BtcCoin::Btc, script_config_sign_msg, b"message") | ||
.await | ||
.unwrap(); | ||
} |
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.
Maybe print the result: println!("Signature: {:?}", signature)
where signature
is the full result.
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.
Yes, I was planning to also ask this while implementing, but apparently I forgot. Thanks for reminding :)
The implementation of BTC message signing has been completed and it also supports antiklepto verification. Furthermore, an example in 'examples' folder. Signed-off-by: asi345 <[email protected]>
be840cd
to
2886a7d
Compare
Okay, I updated the code, adjusting according to your review |
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.
tACK
Addresses the request in the issue : #49
The implementation of BTC message signing has been completed and it also supports antiklepto verification. Furthermore, a test is added as an example in 'examples' folder.