Skip to content

Commit 94ef1e6

Browse files
committed
Add SignatureData type
1 parent 3320210 commit 94ef1e6

File tree

9 files changed

+41
-29
lines changed

9 files changed

+41
-29
lines changed

core/src/consensus/solo_authority/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ mod params;
1818

1919
use std::sync::{Arc, Weak};
2020

21-
use ckey::{public_to_address, recover, Signature};
22-
use ctypes::{Address, H256, H520, U256};
21+
use ckey::{public_to_address, recover, Signature, SignatureData};
22+
use ctypes::{Address, H256, U256};
2323
use parking_lot::RwLock;
2424

2525
use self::params::SoloAuthorityParams;
@@ -69,7 +69,7 @@ fn verify_external(header: &Header, validators: &ValidatorSet) -> Result<(), Err
6969
use rlp::UntrustedRlp;
7070

7171
// Check if the signature belongs to a validator, can depend on parent state.
72-
let sig = UntrustedRlp::new(&header.seal()[0]).as_val::<H520>()?;
72+
let sig = UntrustedRlp::new(&header.seal()[0]).as_val::<SignatureData>()?;
7373
let signer = public_to_address(&recover(&sig.into(), &header.bare_hash())?);
7474

7575
if *header.author() != signer {
@@ -107,7 +107,7 @@ impl ConsensusEngine<CodeChainMachine> for SoloAuthority {
107107
if self.validators.contains(header.parent_hash(), author) {
108108
// account should be permanently unlocked, otherwise sealing will fail
109109
if let Ok(signature) = self.sign(header.bare_hash()) {
110-
return Seal::Regular(vec![::rlp::encode(&(&H520::from(signature) as &[u8])).into_vec()])
110+
return Seal::Regular(vec![::rlp::encode(&(&SignatureData::from(signature) as &[u8])).into_vec()])
111111
} else {
112112
ctrace!(SOLO_AUTHORITY, "generate_seal: FAIL: accounts secret key unavailable");
113113
}
@@ -192,7 +192,7 @@ impl ConsensusEngine<CodeChainMachine> for SoloAuthority {
192192

193193
#[cfg(test)]
194194
mod tests {
195-
use ctypes::H520;
195+
use ckey::SignatureData;
196196

197197
use super::super::super::block::{IsBlock, OpenBlock};
198198
use super::super::super::header::Header;
@@ -210,7 +210,7 @@ mod tests {
210210
fn can_do_signature_verification_fail() {
211211
let engine = Spec::new_test_solo_authority().engine;
212212
let mut header: Header = Header::default();
213-
header.set_seal(vec![::rlp::encode(&H520::default()).into_vec()]);
213+
header.set_seal(vec![::rlp::encode(&SignatureData::default()).into_vec()]);
214214

215215
let verify_result = engine.verify_block_external(&header);
216216
assert!(verify_result.is_err());

core/src/consensus/tendermint/message.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use std::cmp;
1818

1919
use ccrypto::blake256;
20-
use ckey::{public_to_address, recover};
21-
use ctypes::{Address, Bytes, H256, H520};
20+
use ckey::{public_to_address, recover, SignatureData};
21+
use ctypes::{Address, Bytes, H256};
2222
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
2323

2424
use super::super::super::error::Error;
@@ -123,11 +123,17 @@ impl Decodable for TendermintMessage {
123123
pub struct ConsensusMessage {
124124
pub vote_step: VoteStep,
125125
pub block_hash: Option<BlockHash>,
126-
pub signature: H520,
126+
pub signature: SignatureData,
127127
}
128128

129129
impl ConsensusMessage {
130-
pub fn new(signature: H520, height: Height, view: View, step: Step, block_hash: Option<BlockHash>) -> Self {
130+
pub fn new(
131+
signature: SignatureData,
132+
height: Height,
133+
view: View,
134+
step: Step,
135+
block_hash: Option<BlockHash>,
136+
) -> Self {
131137
ConsensusMessage {
132138
signature,
133139
block_hash,
@@ -158,15 +164,15 @@ pub fn consensus_view(header: &Header) -> Result<View, ::rlp::DecoderError> {
158164
}
159165

160166
/// Proposal signature.
161-
pub fn proposal_signature(header: &Header) -> Result<H520, ::rlp::DecoderError> {
167+
pub fn proposal_signature(header: &Header) -> Result<SignatureData, ::rlp::DecoderError> {
162168
UntrustedRlp::new(header.seal().get(1).expect("seal passed basic verification; seal has 3 fields; qed").as_slice())
163169
.as_val()
164170
}
165171

166172
impl Message for ConsensusMessage {
167173
type Round = VoteStep;
168174

169-
fn signature(&self) -> H520 {
175+
fn signature(&self) -> SignatureData {
170176
self.signature
171177
}
172178

@@ -215,7 +221,7 @@ pub fn message_info_rlp(vote_step: &VoteStep, block_hash: Option<BlockHash>) ->
215221
s.out()
216222
}
217223

218-
pub fn message_full_rlp(signature: &H520, vote_info: &Bytes) -> Bytes {
224+
pub fn message_full_rlp(signature: &SignatureData, vote_info: &Bytes) -> Bytes {
219225
let mut s = RlpStream::new_list(2);
220226
s.append(signature).append_raw(vote_info, 1);
221227
s.out()

core/src/consensus/tendermint/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
2323
use std::sync::{Arc, Weak};
2424

2525
use ccrypto::blake256;
26-
use ckey::{public_to_address, recover, Message, Signature};
26+
use ckey::{public_to_address, recover, Message, Signature, SignatureData};
2727
use cnetwork::{Api, NetworkExtension, NodeId, TimerToken};
28-
use ctypes::{Address, Bytes, H256, H520, U128, U256};
28+
use ctypes::{Address, Bytes, H256, U128, U256};
2929
use parking_lot::{Mutex, RwLock};
3030
use rand::{thread_rng, Rng};
3131
use rlp::{self, Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
@@ -749,7 +749,7 @@ where
749749
let mut addresses = HashSet::new();
750750
let ref header_signatures_field = header.seal().get(2).ok_or(BlockError::InvalidSeal)?;
751751
for rlp in UntrustedRlp::new(header_signatures_field).iter() {
752-
let signature: H520 = rlp.as_val()?;
752+
let signature: SignatureData = rlp.as_val()?;
753753
let address = (self.recover)(&signature.into(), &message)?;
754754

755755
if !self.subchain_validators.contains(header.parent_hash(), &address) {

core/src/consensus/vote_collector.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ use std::collections::{BTreeMap, HashMap, HashSet};
1818
use std::fmt::Debug;
1919
use std::hash::Hash;
2020

21-
use ctypes::{Address, Bytes, H256, H520};
21+
use ckey::SignatureData;
22+
use ctypes::{Address, Bytes, H256};
2223
use parking_lot::RwLock;
2324
use rlp::{Encodable, RlpStream};
2425

2526
pub trait Message: Clone + PartialEq + Eq + Hash + Encodable + Debug {
2627
type Round: Clone + PartialEq + Eq + Hash + Default + Debug + Ord;
2728

28-
fn signature(&self) -> H520;
29+
fn signature(&self) -> SignatureData;
2930

3031
fn block_hash(&self) -> Option<H256>;
3132

@@ -43,7 +44,7 @@ pub struct VoteCollector<M: Message> {
4344
#[derive(Debug, Default)]
4445
struct StepCollector<M: Message> {
4546
voted: HashMap<Address, M>,
46-
block_votes: HashMap<Option<H256>, HashMap<H520, Address>>,
47+
block_votes: HashMap<Option<H256>, HashMap<SignatureData, Address>>,
4748
messages: HashSet<M>,
4849
}
4950

@@ -136,7 +137,7 @@ impl<M: Message + Default + Encodable + Debug> VoteCollector<M> {
136137
}
137138

138139
/// Collects the signatures for a given round and hash.
139-
pub fn round_signatures(&self, round: &M::Round, block_hash: &H256) -> Vec<H520> {
140+
pub fn round_signatures(&self, round: &M::Round, block_hash: &H256) -> Vec<SignatureData> {
140141
let guard = self.votes.read();
141142
guard
142143
.get(round)

key/src/ecdsa.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ use super::{public_to_address, Address, Error, Message, Private, Public, SECP256
4444

4545
pub const ECDSA_SIGNATURE_LENGTH: usize = 65;
4646

47+
pub type ECDSASignatureData = H520;
48+
4749
/// Signature encoded as RSV components
4850
#[repr(C)]
4951
pub struct ECDSASignature([u8; 65]);

key/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use codechain_types::H256;
4343
#[cfg(feature = "ecdsa")]
4444
pub use ecdsa::{
4545
recover_ecdsa as recover, sign_ecdsa as sign, verify_ecdsa as verify, verify_ecdsa_address as verify_address,
46-
ECDSASignature as Signature, ECDSA_SIGNATURE_LENGTH as SIGNATURE_LENGTH,
46+
ECDSASignature as Signature, ECDSASignatureData as SignatureData, ECDSA_SIGNATURE_LENGTH as SIGNATURE_LENGTH,
4747
};
4848
pub use error::Error;
4949
pub use exchange::exchange;
@@ -55,7 +55,7 @@ pub use rustc_serialize::hex;
5555
#[cfg(feature = "schnorr")]
5656
pub use schnorr::{
5757
recover_schnorr as recover, sign_schnorr as sign, verify_schnorr as verify,
58-
verify_schnorr_address as verify_address, SchnorrSignature as Signature,
58+
verify_schnorr_address as verify_address, SchnorrSignature as Signature, SchnorrSignatureData as SignatureData,
5959
SCHNORR_SIGNATURE_LENGTH as SIGNATURE_LENGTH,
6060
};
6161

key/src/schnorr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use super::{public_to_address, Address, Error, Message, Private, Public, SECP256
2828

2929
pub const SCHNORR_SIGNATURE_LENGTH: usize = 64;
3030

31+
pub type SchnorrSignatureData = H512;
32+
3133
pub struct SchnorrSignature([u8; 64]);
3234

3335
// manual implementation large arrays don't have trait impls by default.

vm/src/executor.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
use ccrypto::{blake256, keccak256, ripemd160, sha256};
18-
use ckey::{verify, Signature, SIGNATURE_LENGTH};
19-
use ctypes::{H256, H520, Public};
18+
use ckey::{verify, Signature, SignatureData, SIGNATURE_LENGTH};
19+
use ctypes::{H256, Public};
2020

2121
use instruction::{is_valid_unlock_script, Instruction};
2222

@@ -209,7 +209,8 @@ pub fn execute(
209209
}
210210
Instruction::ChkSig => {
211211
let pubkey = Public::from_slice(stack.pop()?.assert_len(64)?.as_ref());
212-
let signature = Signature::from(H520::from(stack.pop()?.assert_len(SIGNATURE_LENGTH)?.as_ref()));
212+
let signature =
213+
Signature::from(SignatureData::from(stack.pop()?.assert_len(SIGNATURE_LENGTH)?.as_ref()));
213214
let result = match verify(&pubkey, &signature, &tx_hash) {
214215
Ok(true) => 1,
215216
_ => 0,

vm/src/tests/executor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
use ccrypto::{blake256, BLAKE_EMPTY, BLAKE_NULL_RLP};
18-
use ckey::{sign, KeyPair, Private};
19-
use ctypes::{H160, H256, H520};
18+
use ckey::{sign, KeyPair, Private, SignatureData};
19+
use ctypes::{H160, H256};
2020

2121
use secp256k1::key::{SecretKey, MINUS_ONE_KEY, ONE_KEY};
2222

@@ -70,7 +70,7 @@ fn valid_pay_to_public_key() {
7070
let keypair = KeyPair::from_private(Private::from(SecretKey::from(ONE_KEY))).unwrap();
7171
let pubkey = <&[u8]>::from(keypair.public()).to_vec();
7272
let message = blake256("asdf");
73-
let signature = H520::from(sign(keypair.private(), &message).unwrap()).to_vec();
73+
let signature = SignatureData::from(sign(keypair.private(), &message).unwrap()).to_vec();
7474
let unlock_script = vec![Instruction::PushB(signature)];
7575
let lock_script = vec![Instruction::PushB(pubkey), Instruction::ChkSig];
7676

@@ -85,7 +85,7 @@ fn invalid_pay_to_public_key() {
8585
let lock_script = vec![Instruction::PushB(pubkey), Instruction::ChkSig];
8686

8787
let invalid_keypair = KeyPair::from_private(Private::from(SecretKey::from(MINUS_ONE_KEY))).unwrap();
88-
let invalid_signature = H520::from(sign(invalid_keypair.private(), &message).unwrap()).to_vec();
88+
let invalid_signature = SignatureData::from(sign(invalid_keypair.private(), &message).unwrap()).to_vec();
8989
let unlock_script = vec![Instruction::PushB(invalid_signature)];
9090

9191
assert_eq!(execute(&unlock_script[..], &[], &lock_script, message, Config::default()), Ok(ScriptResult::Fail));

0 commit comments

Comments
 (0)