Просмотр исходного кода

crypto: Minor functions for ContractId stuff.

Luther Blissett 3 лет назад
Родитель
Сommit
99bda9efe1
3 измененных файлов с 28 добавлено и 0 удалено
  1. 20 0
      src/crypto/contract_id.rs
  2. 5 0
      src/crypto/keypair.rs
  3. 3 0
      src/crypto/mod.rs

+ 20 - 0
src/crypto/contract_id.rs

@@ -0,0 +1,20 @@
+use pasta_curves::pallas;
+
+use super::{
+    keypair::{PublicKey, SecretKey},
+    util::poseidon_hash,
+};
+use crate::serial::{SerialDecodable, SerialEncodable};
+
+/// Contract ID used to reference smart contracts on the ledger.
+#[repr(C)]
+#[derive(Debug, Copy, Clone, SerialEncodable, SerialDecodable)]
+pub struct ContractId(pallas::Base);
+
+/// Derive a ContractId given a secret deploy key.
+pub fn derive_contract_id(deploy_key: SecretKey) -> ContractId {
+    let public_key = PublicKey::from_secret(deploy_key);
+    let (x, y) = public_key.xy();
+    let hash = poseidon_hash::<2>([x, y]);
+    ContractId(hash)
+}

+ 5 - 0
src/crypto/keypair.rs

@@ -114,6 +114,11 @@ impl PublicKey {
     pub fn y(&self) -> pallas::Base {
         *self.0.to_affine().coordinates().unwrap().y()
     }
+
+    pub fn xy(&self) -> (pallas::Base, pallas::Base) {
+        let coords = self.0.to_affine().coordinates().unwrap();
+        (*coords.x(), *coords.y())
+    }
 }
 
 impl FromStr for PublicKey {

+ 3 - 0
src/crypto/mod.rs

@@ -15,6 +15,9 @@ pub mod token_list;
 pub mod types;
 pub mod util;
 
+/// Contract ID definitions
+pub mod contract_id;
+
 /// VDF (Verifiable Delay Function) using MiMC
 pub mod mimc_vdf;