-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add verification that we have already received a signature for the transaction for the selected input and validator #7
Merged
alekseysidorov
merged 1 commit into
exonum:master
from
alekseysidorov:4-multiple-signatures-for-txid
Apr 6, 2017
Merged
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,85 @@ | ||
use std::mem; | ||
|
||
use libc::c_void; | ||
use byteorder::{LittleEndian, ByteOrder}; | ||
use bitcoin::blockdata::script::Script; | ||
use bitcoin::blockdata::transaction::SigHashType; | ||
use secp256k1::ffi; | ||
use secp256k1::{ContextFlag, Secp256k1}; | ||
use secp256k1::{Message, Signature}; | ||
use secp256k1::key::SecretKey; | ||
use secp256k1::key; | ||
use secp256k1::Error; | ||
|
||
|
||
use anchoring_btc_service::transactions::RawBitcoinTx; | ||
|
||
|
||
/// The structure with the same memory representation as the `secp256k1::Secp256k1`. | ||
#[derive(Clone, Copy)] | ||
struct Context { | ||
pub ctx: *mut ffi::Context, | ||
pub caps: ContextFlag, | ||
} | ||
|
||
impl Context { | ||
/// Same as the 'secp256k1::Secp256k1::sign` but has a nonce argument. | ||
pub fn sign(&self, msg: &Message, sk: &key::SecretKey, nonce: u64) -> Result<Signature, Error> { | ||
if self.caps == ContextFlag::VerifyOnly || self.caps == ContextFlag::None { | ||
return Err(Error::IncapableContext); | ||
} | ||
|
||
let nonce_array = { | ||
let mut data = [0; 32]; | ||
LittleEndian::write_u64(&mut data, nonce); | ||
data | ||
}; | ||
|
||
let mut ret = unsafe { ffi::Signature::blank() }; | ||
unsafe { | ||
// We can assume the return value because it's not possible to construct | ||
// an invalid signature from a valid `Message` and `SecretKey` | ||
assert_eq!(ffi::secp256k1_ecdsa_sign(self.ctx, | ||
&mut ret, | ||
msg.as_ptr(), | ||
sk.as_ptr(), | ||
ffi::secp256k1_nonce_function_rfc6979, | ||
nonce_array.as_ptr() as *const c_void), | ||
1); | ||
} | ||
Ok(Signature::from(ret)) | ||
} | ||
} | ||
|
||
fn get_ffi_context(ctx: &mut Secp256k1) -> Context { | ||
unsafe { | ||
let ctx_ptr: *mut Context = mem::transmute(ctx as *mut Secp256k1); | ||
*ctx_ptr | ||
} | ||
} | ||
|
||
fn sign_with_nonce(ctx: &mut Secp256k1, | ||
msg: &Message, | ||
sk: &key::SecretKey, | ||
nonce: u64) | ||
-> Result<Signature, Error> { | ||
let ctx = get_ffi_context(ctx); | ||
ctx.sign(msg, sk, nonce) | ||
} | ||
|
||
pub fn sign_tx_input_with_nonce(tx: &RawBitcoinTx, | ||
input: usize, | ||
subscript: &Script, | ||
sec_key: &SecretKey, | ||
nonce: u64) | ||
-> Vec<u8> { | ||
let sighash = tx.signature_hash(input, subscript, SigHashType::All.as_u32()); | ||
// Make signature | ||
let mut context = Secp256k1::new(); | ||
let msg = Message::from_slice(&sighash[..]).unwrap(); | ||
let sign = sign_with_nonce(&mut context, &msg, sec_key, nonce).unwrap(); | ||
// Serialize signature | ||
let mut sign_data = sign.serialize_der(&context); | ||
sign_data.push(SigHashType::All.as_u32() as u8); | ||
sign_data | ||
} |
This file contains 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 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
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.
Maybe create a new Exonum datatype encompassing a
(txid, validator, input_index)
triple, and use it here? The current approach may be not sustainable; e.g., we may want to provide proofs to light clients and/or change general serialization logic in the future.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 can not represent the custom type as
&[u8]
.