Skip to content

Commit

Permalink
rename SecretKey into KeyPair (since it contains a dalek key pair)
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2bd committed Sep 9, 2021
1 parent 779a646 commit be773a9
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions fastpay/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl AuthorityConfig {
#[derive(Serialize, Deserialize)]
pub struct AuthorityServerConfig {
pub authority: AuthorityConfig,
pub key: SecretKey,
pub key: KeyPair,
}

impl AuthorityServerConfig {
Expand Down Expand Up @@ -97,7 +97,7 @@ pub struct UserAccount {
deserialize_with = "address_from_base64"
)]
pub address: FastPayAddress,
pub key: SecretKey,
pub key: KeyPair,
pub next_sequence_number: SequenceNumber,
pub balance: Balance,
pub sent_certificates: Vec<CertifiedTransferOrder>,
Expand Down
6 changes: 3 additions & 3 deletions fastpay_core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct AuthorityState {
/// Committee of this FastPay instance.
pub committee: Committee,
/// The signature key of the authority.
pub secret: SecretKey,
pub secret: KeyPair,
/// Offchain states of FastPay accounts.
pub accounts: BTreeMap<FastPayAddress, AccountOffchainState>,
/// The latest transaction index of the blockchain that the authority has seen.
Expand Down Expand Up @@ -321,7 +321,7 @@ impl AccountOffchainState {
}

impl AuthorityState {
pub fn new(committee: Committee, name: AuthorityName, secret: SecretKey) -> Self {
pub fn new(committee: Committee, name: AuthorityName, secret: KeyPair) -> Self {
AuthorityState {
committee,
name,
Expand All @@ -336,7 +336,7 @@ impl AuthorityState {
pub fn new_shard(
committee: Committee,
name: AuthorityName,
secret: SecretKey,
secret: KeyPair,
shard_id: u32,
number_of_shards: u32,
) -> Self {
Expand Down
25 changes: 11 additions & 14 deletions fastpay_core/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub type VersionNumber = SequenceNumber;
pub struct UserData(pub Option<[u8; 32]>);

// TODO: Make sure secrets are not copyable and movable to control where they are in memory
pub struct SecretKey(dalek::Keypair);
pub struct KeyPair(dalek::Keypair);

#[derive(Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Hash, Serialize, Deserialize)]
pub struct PublicKeyBytes(pub [u8; dalek::PUBLIC_KEY_LENGTH]);
Expand All @@ -43,13 +43,10 @@ pub type PrimaryAddress = PublicKeyBytes;
pub type FastPayAddress = PublicKeyBytes;
pub type AuthorityName = PublicKeyBytes;

pub fn get_key_pair() -> (FastPayAddress, SecretKey) {
pub fn get_key_pair() -> (FastPayAddress, KeyPair) {
let mut csprng = OsRng;
let keypair = dalek::Keypair::generate(&mut csprng);
(
PublicKeyBytes(keypair.public.to_bytes()),
SecretKey(keypair),
)
(PublicKeyBytes(keypair.public.to_bytes()), KeyPair(keypair))
}

pub fn address_as_base64<S>(key: &PublicKeyBytes, serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -88,17 +85,17 @@ pub fn dbg_addr(name: u8) -> FastPayAddress {
#[derive(Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub struct Signature(dalek::Signature);

impl SecretKey {
impl KeyPair {
/// Avoid implementing `clone` on secret keys to prevent mistakes.
pub fn copy(&self) -> SecretKey {
SecretKey(dalek::Keypair {
pub fn copy(&self) -> KeyPair {
KeyPair(dalek::Keypair {
secret: dalek::SecretKey::from_bytes(self.0.secret.as_bytes()).unwrap(),
public: dalek::PublicKey::from_bytes(self.0.public.as_bytes()).unwrap(),
})
}
}

impl Serialize for SecretKey {
impl Serialize for KeyPair {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
Expand All @@ -107,16 +104,16 @@ impl Serialize for SecretKey {
}
}

impl<'de> Deserialize<'de> for SecretKey {
fn deserialize<D>(deserializer: D) -> Result<SecretKey, D::Error>
impl<'de> Deserialize<'de> for KeyPair {
fn deserialize<D>(deserializer: D) -> Result<KeyPair, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let value = base64::decode(&s).map_err(|err| serde::de::Error::custom(err.to_string()))?;
let key = dalek::Keypair::from_bytes(&value)
.map_err(|err| serde::de::Error::custom(err.to_string()))?;
Ok(SecretKey(key))
Ok(KeyPair(key))
}
}

Expand Down Expand Up @@ -292,7 +289,7 @@ where
}

impl Signature {
pub fn new<T>(value: &T, secret: &SecretKey) -> Self
pub fn new<T>(value: &T, secret: &KeyPair) -> Self
where
T: Signable<Vec<u8>>,
{
Expand Down
4 changes: 2 additions & 2 deletions fastpay_core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct ClientState<AuthorityClient> {
/// Our FastPay address.
address: FastPayAddress,
/// Our signature key.
secret: SecretKey,
secret: KeyPair,
/// Our FastPay committee.
committee: Committee,
/// How to talk to this committee.
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<A> ClientState<A> {
#[allow(clippy::too_many_arguments)]
pub fn new(
address: FastPayAddress,
secret: SecretKey,
secret: KeyPair,
committee: Committee,
authority_clients: HashMap<AuthorityName, A>,
next_sequence_number: SequenceNumber,
Expand Down
4 changes: 2 additions & 2 deletions fastpay_core/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Transfer {
}

impl TransferOrder {
pub fn new(transfer: Transfer, secret: &SecretKey) -> Self {
pub fn new(transfer: Transfer, secret: &KeyPair) -> Self {
let signature = Signature::new(&transfer, secret);
Self {
transfer,
Expand All @@ -163,7 +163,7 @@ impl TransferOrder {

impl SignedTransferOrder {
/// Use signing key to create a signed object.
pub fn new(value: TransferOrder, authority: AuthorityName, secret: &SecretKey) -> Self {
pub fn new(value: TransferOrder, authority: AuthorityName, secret: &KeyPair) -> Self {
let signature = Signature::new(&value.transfer, secret);
Self {
value,
Expand Down
4 changes: 2 additions & 2 deletions fastpay_core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ fn init_state_with_account(address: FastPayAddress, balance: Balance) -> Authori
#[cfg(test)]
fn init_transfer_order(
sender: FastPayAddress,
secret: &SecretKey,
secret: &KeyPair,
recipient: Address,
amount: Amount,
) -> TransferOrder {
Expand All @@ -485,7 +485,7 @@ fn init_transfer_order(
#[cfg(test)]
fn init_certified_transfer_order(
sender: FastPayAddress,
secret: &SecretKey,
secret: &KeyPair,
recipient: Address,
amount: Amount,
authority_state: &AuthorityState,
Expand Down
4 changes: 2 additions & 2 deletions fastpay_core/src/unit_tests/fastpay_smart_contract_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn test_handle_redeem_transaction_double_spend() {

// helpers
#[cfg(test)]
fn init_contract() -> (FastPaySmartContractState, AuthorityName, SecretKey) {
fn init_contract() -> (FastPaySmartContractState, AuthorityName, KeyPair) {
let (authority_address, authority_key) = get_key_pair();
let mut authorities = BTreeMap::new();
authorities.insert(
Expand All @@ -157,7 +157,7 @@ fn init_funding_transaction() -> FundingTransaction {
fn init_redeem_transaction(
committee: Committee,
name: AuthorityName,
secret: SecretKey,
secret: KeyPair,
) -> RedeemTransaction {
let (sender_address, sender_key) = get_key_pair();
let primary_transfer = Transfer {
Expand Down

0 comments on commit be773a9

Please sign in to comment.