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

src: BTC sign message implemented and tested #53

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

asi345
Copy link
Collaborator

@asi345 asi345 commented Oct 25, 2023

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.

Copy link
Contributor

@benma benma left a 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.

Comment on lines 45 to 44
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

Copy link
Contributor

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?

Copy link
Collaborator Author

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.

Copy link
Contributor

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

Comment on lines 23 to 29
let _addr = paired_bitbox.btc_address(
pb::BtcCoin::Btc,
&keypath,
&script_config_addr,
true,
).await
.unwrap();
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

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?;
Copy link
Contributor

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 {
Copy link
Contributor

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?;
Copy link
Contributor

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

Copy link
Collaborator Author

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.

Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

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)
Copy link
Contributor

@benma benma Oct 25, 2023

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.

Copy link
Collaborator Author

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();
Copy link
Contributor

@benma benma Oct 25, 2023

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.

Copy link
Collaborator Author

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.

@benma
Copy link
Contributor

benma commented Oct 25, 2023

src: BTC sign message implemented and tested

may I propose a better title? src as context does not say much, almost any change will be in src.

btc: add btc_sign_message() and example

it is shorter, and tested does not mean much if there is no unit test. also generally in commit titles it is better to use the imperative form (e.g. add instead of added) for brevity.

@asi345
Copy link
Collaborator Author

asi345 commented Oct 26, 2023

I implemented the requested changes and now pushing a new commit according to them

Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

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

almost there! thanks!

@@ -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

Copy link
Contributor

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 :)

Copy link
Collaborator Author

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 {
Copy link
Contributor

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.

Copy link
Collaborator Author

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>,
Copy link
Contributor

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:

See https://github.com/spesmilo/electrum/blob/84dc181b6e7bb20e88ef6b98fb8925c5f645a765/electrum/ecc.py#L521-L523

src/btc.rs Outdated
Comment on lines 912 to 913
let mut electrum_sig65 = signature.clone();
electrum_sig65.splice(0..0, sig65.to_be_bytes());
Copy link
Contributor

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)

Copy link
Collaborator Author

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();
}
Copy link
Contributor

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.

Copy link
Collaborator Author

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]>
@asi345
Copy link
Collaborator Author

asi345 commented Oct 26, 2023

Okay, I updated the code, adjusting according to your review

Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

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

tACK

@asi345 asi345 merged commit e4ddcbf into BitBoxSwiss:master Oct 26, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants