示例#1
0
    def get_bytes(self, for_signing: bool=False):
        result = []
        result.append(struct.pack(">B", self._type))  # byte
        result.append(struct.pack(">Q", self._timestamp))  # Long

        if self._type in [self.type_coin_generation, self.type_seed, self.type_standard, self.type_cycle]:
            result.append(struct.pack(">Q", self._amount))  # Long
            result.append(self._receiver_identifier)
        elif self._type == self.type_cycle_signature:
            result.append(self._sender_identifier)
            result.append(struct.pack(">B", self._cycle_transaction_vote))  # byte
            result.append(self._cycle_transaction_signature)
            if not for_signing:
                result.append(self._signature)
            
        
        if self._type in [self.type_seed, self.type_standard, self.type_cycle]:
            if for_signing:
                result.append(self._previous_block_hash)
            else:
                result.append(struct.pack(">Q", self._previous_hash_height))  # Long
            
            result.append(self._sender_identifier)

            # For serializing, we use the raw sender data with a length specifier. For signing, we use the double-
            # SHA-256 of the user data. This will allow us to remove inappropriate or illegal metadata from the
            # blockchain at a later date by replacing it with its double-SHA-256 without compromising the signature
            # integrity.
            if for_signing:
                result.append(HashUtil.double_sha256(self._sender_data))
            else:
                result.append(struct.pack(">B", len(self._sender_data)))  # byte
                result.append(self._sender_data)

            if not for_signing:
                result.append(self._signature)

                # For cycle transactions, order the signatures by verifier identifier. In the v1 blockchain, the
                # cycleSignatures field is used. In the v2 blockchain, the cycleSignatureTransactions field is used.
                if self._type == self.type_cycle:
                    if self.cycle_signatures:
                        
                        result.append(struct.pack(">I", len(self._cycle_signatures)))  # int, 4
                        for identifier, signature in sorted(self._cycle_signatures, key=lambda x: x[0]):
                            result.append(identifier)
                            result.append(signature)
                    else:
                        result.append(struct.pack(">I", len(self._cycle_signature_transactions)))  # int, 4
                        for identifier, transaction in sorted(self._cycle_signature_transactions, key=lambda x: x[0]):
                            result.append(struct.pack(">Q", transaction.timestamp))  # Long
                            result.append(transaction.sender_identifier);
                            result.append(transaction.cycle_transaction_vote);
                            result.append(transaction.signature);
                        
                    
                
            
        

        return b''.join(result)
示例#2
0
def test_double_2(verbose=False):
    hello = b''
    hex = HashUtil.double_sha256(hello).hex()
    if verbose:
        print("hex", hex)
    assert hex == '5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456'
示例#3
0
def test_double_1(verbose=False):
    hello = b"hello"
    hex = HashUtil.double_sha256(hello).hex()
    if verbose:
        print("hex", hex)
    assert hex == '9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50'
示例#4
0
def test_single_2(verbose=False):
    hello = b''
    hex = HashUtil.single_sha256(hello).hex()
    if verbose:
        print("hex", hex)
    assert hex == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
示例#5
0
def test_single_1(verbose=False):
    hello = b"hello"
    hex = HashUtil.single_sha256(hello).hex()
    if verbose:
        print("hex", hex)
    assert hex == '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
示例#6
0
 def get_hash(self) -> bytes:
     return HashUtil.double_sha256(self.get_bytes())
示例#7
0
 def get_hash(self) -> bytes:
     return HashUtil.double_sha256(self._verifier_signature)