Sfoglia il codice sorgente

sdk/contract_id: Add a derivation prefix for deriving ContractID.

parazyd 3 anni fa
parent
commit
93f3c70e0c
1 ha cambiato i file con 18 aggiunte e 4 eliminazioni
  1. 18 4
      src/sdk/src/crypto/contract_id.rs

+ 18 - 4
src/sdk/src/crypto/contract_id.rs

@@ -29,17 +29,24 @@ lazy_static! {
     // avoid hardcoding contract IDs for arbitrary contract deployments, because
     // avoid hardcoding contract IDs for arbitrary contract deployments, because
     // the contracts with 0 as their x coordinate can never have a valid signature.
     // the contracts with 0 as their x coordinate can never have a valid signature.
 
 
+    /// Derivation prefix for `ContractId`
+    pub static ref CONTRACT_ID_PREFIX: pallas::Base = pallas::Base::from(42);
+
     /// Contract ID for the native money contract
     /// Contract ID for the native money contract
     pub static ref MONEY_CONTRACT_ID: ContractId =
     pub static ref MONEY_CONTRACT_ID: ContractId =
-        ContractId::from(poseidon_hash([pallas::Base::zero(), pallas::Base::from(0)]));
+        ContractId::from(poseidon_hash([*CONTRACT_ID_PREFIX, pallas::Base::zero(), pallas::Base::from(0)]));
 
 
     /// Contract ID for the native DAO contract
     /// Contract ID for the native DAO contract
     pub static ref DAO_CONTRACT_ID: ContractId =
     pub static ref DAO_CONTRACT_ID: ContractId =
-        ContractId::from(poseidon_hash([pallas::Base::zero(), pallas::Base::from(1)]));
+        ContractId::from(poseidon_hash([*CONTRACT_ID_PREFIX, pallas::Base::zero(), pallas::Base::from(1)]));
 
 
     /// Contract ID for the native Consensus contract
     /// Contract ID for the native Consensus contract
     pub static ref CONSENSUS_CONTRACT_ID: ContractId =
     pub static ref CONSENSUS_CONTRACT_ID: ContractId =
-        ContractId::from(poseidon_hash([pallas::Base::zero(), pallas::Base::from(2)]));
+        ContractId::from(poseidon_hash([*CONTRACT_ID_PREFIX, pallas::Base::zero(), pallas::Base::from(2)]));
+
+    /// Contract ID for the native Deployooor contract
+    pub static ref DEPLOYOOOR_CONTRACT_ID: ContractId =
+        ContractId::from(poseidon_hash([*CONTRACT_ID_PREFIX, pallas::Base::zero(), pallas::Base::from(3)]));
 }
 }
 
 
 /// ContractId represents an on-chain identifier for a certain smart contract.
 /// ContractId represents an on-chain identifier for a certain smart contract.
@@ -51,7 +58,14 @@ impl ContractId {
     pub fn derive(deploy_key: SecretKey) -> Self {
     pub fn derive(deploy_key: SecretKey) -> Self {
         let public_key = PublicKey::from_secret(deploy_key);
         let public_key = PublicKey::from_secret(deploy_key);
         let (x, y) = public_key.xy();
         let (x, y) = public_key.xy();
-        let hash = poseidon_hash::<2>([x, y]);
+        let hash = poseidon_hash([*CONTRACT_ID_PREFIX, x, y]);
+        Self(hash)
+    }
+
+    /// Derive a contract ID from a `Publickey`
+    pub fn derive_public(public_key: PublicKey) -> Self {
+        let (x, y) = public_key.xy();
+        let hash = poseidon_hash([*CONTRACT_ID_PREFIX, x, y]);
         Self(hash)
         Self(hash)
     }
     }