github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/processor/src/addressing.rs (about)

     1  // Copyright 2018 Cargill Incorporated
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  use crypto::digest::Digest;
    16  use crypto::sha2::Sha512;
    17  
    18  const FAMILY_NAME: &str = "supply_chain";
    19  const AGENT: &str = "ae";
    20  const PROPERTY: &str = "ea";
    21  const PROPOSAL: &str = "aa";
    22  const RECORD: &str = "ec";
    23  const RECORD_TYPE: &str = "ee";
    24  
    25  pub fn get_supply_chain_prefix() -> String {
    26      let mut sha = Sha512::new();
    27      sha.input_str(&FAMILY_NAME);
    28      sha.result_str()[..6].to_string()
    29  }
    30  
    31  pub fn hash(to_hash: &str, num: usize) -> String {
    32      let mut sha = Sha512::new();
    33      sha.input_str(to_hash);
    34      let temp = sha.result_str().to_string();
    35      let hash = match temp.get(..num) {
    36          Some(x) => x,
    37          None => "",
    38      };
    39      hash.to_string()
    40  }
    41  
    42  pub fn make_agent_address(identifier: &str) -> String {
    43      get_supply_chain_prefix() + &AGENT + &hash(identifier, 62)
    44  }
    45  
    46  pub fn make_record_address(record_id: &str) -> String {
    47      get_supply_chain_prefix() + &RECORD + &hash(record_id, 62)
    48  }
    49  
    50  pub fn make_record_type_address(type_name: &str) -> String {
    51      get_supply_chain_prefix() + &RECORD_TYPE + &hash(type_name, 62)
    52  }
    53  
    54  pub fn make_property_address(record_id: &str, property_name: &str, page: u32) -> String {
    55      make_property_address_range(record_id) + &hash(property_name, 22) + &num_to_page_number(page)
    56  }
    57  
    58  pub fn make_property_address_range(record_id: &str) -> String {
    59      get_supply_chain_prefix() + &PROPERTY + &hash(record_id, 36)
    60  }
    61  
    62  pub fn num_to_page_number(page: u32) -> String {
    63      format!("{:01$x}", page, 4)
    64  }
    65  
    66  pub fn make_proposal_address(record_id: &str, agent_id: &str) -> String {
    67      get_supply_chain_prefix() + PROPOSAL + &hash(record_id, 36) + &hash(agent_id, 26)
    68  }