def test_xor_bytes(self): a = b"12345" b = b"09876" expected_result = b'\x01\x0b\x0b\x03\x03' # Should work result = Helper.xor_bytes(a, b) self.assertEqual(result, expected_result) # Should not work on inequal length byte objects with self.assertRaises(AssertionError) as context: Helper.xor_bytes(a, b"x")
def Sign(self, context): """ Sign the verifiable items ( Transaction, Block, etc ) in the context with the Keypairs in this wallet. Args: context (ContractParameterContext): the context to sign Returns: bool: if signing is successful for all contracts in this wallet. """ success = False for hash in context.ScriptHashes: contract = self.GetContract(hash) if contract is None: continue key = self.GetKeyByScriptHash(hash) if key is None: continue signature = Helper.Sign(context.Verifiable, key) res = context.AddSignature(contract, key.PublicKey, signature) success |= res return success
def Verify(self): if not self.Hash.ToBytes() == GetGenesis().Hash.ToBytes(): return False bc = GetBlockchain() if not bc.ContainsBlock(self.Index): return False if self.Index > 0: prev_header = GetBlockchain().GetHeader(self.PrevHash.ToBytes()) if prev_header is None: return False if prev_header.Index + 1 != self.Index: return False if prev_header.Timestamp >= self.Timestamp: return False # this should be done to actually verify the block if not Helper.VerifyScripts(self): return False return True
def Sign(self, context): success = False for hash in context.ScriptHashes: print("checkhing hash...") contract = self.GetContract(hash) if contract is None: print("contract is none, return") continue key = self.GetKeyByScriptHash(hash) print("key is %s " % key) if key is None: print("key is none") continue print("Signing.... %s %s " % (context.Verifiable, key)) signature = Helper.Sign(context.Verifiable, key) print("signature %s " % signature) res = context.AddSignature(contract, key.PublicKey, signature) print("result is %s " % res) success |= res print("success is %s " % success) return success
def Verify(self): print("verifying block base 1") if not self.Hash.ToBytes() == GetGenesis().Hash.ToBytes(): return False print("verifying block base @ %s " % self.Index) bc = GetBlockchain() print("BC: %s " % bc) if not bc.ContainsBlock(self.Index): print("blockchin didnt contain block index") return False print("verifying block base 3 %s " % self.PrevHash.ToBytes()) if self.Index > 0: prev_header = GetBlockchain().GetHeader(self.PrevHash.ToBytes()) if prev_header == None: return False if prev_header.Index + 1 != self.Index: return False if prev_header.Timestamp >= self.Timestamp: return False print("Will verify scripts!!") #this should be done to actually verify the block if not Helper.VerifyScripts(self): print("could not verify scripts") return False return True
def Sign(self, context): success = False for hash in context.ScriptHashes: contract = self.GetContract(hash) if contract is None: continue key = self.GetKeyByScriptHash(hash) if key is None: continue signature = Helper.Sign(context.Verifiable, key) success |= context.AddSignature(contract, key.PublicKey, signature) return success
def RawData(self): return Helper.GetHashData(self)
def ToArray(self): return Helper.ToArray(self)
def GetMessage(self): return Helper.GetHashData(self)
def test_random_key(self): a = Helper.random_key() self.assertEqual(len(a), 64) b = Helper.random_key() self.assertNotEqual(a, b)
def test_base256_encode(self): val = 1234567890 res = Helper.base256_encode(val) self.assertEqual(res, bytearray(b'\xd2\x02\x96I'))
def Hash(self): if not self._hash: self._hash = bin_dbl_sha256(Helper.GetHashData(self)) return self._hash