github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/tests/sawtooth_sc_test/addressing.py (about) 1 # Copyright 2017 Intel Corporation 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 16 import hashlib 17 18 19 def _hash(string): 20 return hashlib.sha512(string.encode('utf-8')).hexdigest() 21 22 23 # The first six characters of a T&T address are the first six 24 # characters of the hash of the T&T family name. The next two 25 # characters depend on the type of object being stored. There is no 26 # formula for deriving these infixes. 27 28 FAMILY_NAME = 'supply_chain' 29 30 NAMESPACE = _hash(FAMILY_NAME)[:6] 31 32 AGENT = 'ae' 33 PROPERTY = 'ea' 34 PROPOSAL = 'aa' 35 RECORD = 'ec' 36 RECORD_TYPE = 'ee' 37 38 39 def make_agent_address(identifier): 40 return ( 41 NAMESPACE 42 + AGENT 43 + _hash(identifier)[:62] 44 ) 45 46 47 def make_record_address(record_id): 48 return ( 49 NAMESPACE 50 + RECORD 51 + _hash(record_id)[:62] 52 ) 53 54 55 def make_record_type_address(type_name): 56 return ( 57 NAMESPACE 58 + RECORD_TYPE 59 + _hash(type_name)[:62] 60 ) 61 62 63 RECORD_TYPE_ADDRESS_RANGE = NAMESPACE + RECORD_TYPE 64 65 66 def make_property_address(record_id, property_name, page=0): 67 return ( 68 make_property_address_range(record_id) 69 + _hash(property_name)[:22] 70 + _num_to_page_number(page) 71 ) 72 73 74 def _num_to_page_number(num): 75 return hex(num)[2:].zfill(4) 76 77 78 def make_property_address_range(record_id): 79 return ( 80 NAMESPACE 81 + PROPERTY 82 + _hash(record_id)[:36] 83 ) 84 85 86 def make_proposal_address(record_id, agent_id): 87 return ( 88 NAMESPACE 89 + PROPOSAL 90 + _hash(record_id)[:36] 91 + _hash(agent_id)[:26] 92 )