Sfoglia il codice sorgente

use TryFrom when getting PublicKey from Address

ghassmo 4 anni fa
parent
commit
72f4c775e1
4 ha cambiato i file con 12 aggiunte e 12 eliminazioni
  1. 1 1
      src/bin/cashierd.rs
  2. 5 5
      src/bin/darkfid.rs
  3. 5 5
      src/crypto/keypair.rs
  4. 1 1
      src/util/address.rs

+ 1 - 1
src/bin/cashierd.rs

@@ -451,7 +451,7 @@ impl Cashierd {
                 mint_address = "";
             }
             let drk_pub_key = Address::from_str(drk_pub_key.into())?;
-            let drk_pub_key: PublicKey = PublicKey::from(drk_pub_key);
+            let drk_pub_key: PublicKey = PublicKey::try_from(drk_pub_key)?;
 
             // check if the drk public key already exist
             let check = self

+ 5 - 5
src/bin/darkfid.rs

@@ -292,7 +292,7 @@ impl Darkfid {
         let addr_str = args.unwrap()[0].as_str().unwrap();
 
         let result: Result<()> = async {
-            let public = PublicKey::from(Address::from_str(addr_str.into())?);
+            let public = PublicKey::try_from(Address::from_str(addr_str.into())?)?;
             self.client.lock().await.set_default_keypair(&public).await?;
             Ok(())
         }
@@ -566,7 +566,7 @@ impl Darkfid {
                 let cashier_public = cashier_public.result.as_str().unwrap();
 
                 let cashier_public: PublicKey =
-                    PublicKey::from(Address::from_str(cashier_public.into())?);
+                    PublicKey::try_from(Address::from_str(cashier_public.into())?)?;
 
                 self.client
                     .lock()
@@ -649,7 +649,7 @@ impl Darkfid {
         }
 
         let result: Result<()> = async {
-            let drk_address: PublicKey = PublicKey::from(Address::from_str(address.into())?);
+            let drk_address: PublicKey = PublicKey::try_from(Address::from_str(address.into())?)?;
 
             let decimals: usize = 8;
             let amount = decode_base10(amount, decimals, true)?;
@@ -685,7 +685,7 @@ async fn start(
     let mut cashier_keys = Vec::new();
 
     if let Some(cpub) = local_cashier {
-        let cashier_public: PublicKey = PublicKey::from(Address::from_str(cpub.into())?);
+        let cashier_public: PublicKey = PublicKey::try_from(Address::from_str(cpub.into())?)?;
 
         cashiers.push(Cashier {
             name: "localCashier".into(),
@@ -701,7 +701,7 @@ async fn start(
             }
 
             let cashier_public: PublicKey =
-                PublicKey::from(Address::from_str(cashier.public_key.into())?);
+                PublicKey::try_from(Address::from_str(cashier.public_key.into())?)?;
 
             cashiers.push(Cashier {
                 name: cashier.name,

+ 5 - 5
src/crypto/keypair.rs

@@ -1,4 +1,4 @@
-use std::io;
+use std::{convert::TryFrom, io};
 
 use halo2_gadgets::ecc::FixedPoints;
 use pasta_curves::{
@@ -80,12 +80,12 @@ impl PublicKey {
     }
 }
 
-// TODO maybe it's better to use TryFrom
-impl From<Address> for PublicKey {
-    fn from(address: Address) -> PublicKey {
+impl TryFrom<Address> for PublicKey {
+    type Error = Error;
+    fn try_from(address: Address) -> Result<Self> {
         let mut bytes = [0u8; 32];
         bytes.copy_from_slice(&address.0[1..33]);
-        Self::from_bytes(&bytes).unwrap()
+        Self::from_bytes(&bytes)
     }
 }
 

+ 1 - 1
src/util/address.rs

@@ -101,7 +101,7 @@ mod tests {
         // from/to PublicKey
         let keypair = Keypair::random(&mut OsRng);
         let address = Address::from(keypair.public);
-        assert_eq!(keypair.public, PublicKey::from(address));
+        assert_eq!(keypair.public, PublicKey::try_from(address)?);
 
         // from/to string
         let address_str = address.to_string();