def test_startNewRound(self): payerAcct = adminAcct param_list = [] param_list.append("test1".encode()) param_list1 = [] a = 1000 b = 89 # Within Contract # hash1 = sha256(1000) = 4307fbbb7e5b7c8f0339b060d171c49c881901d78ab712e60d805af9f9dc4ca1 # hash2 = sha256(89) = 18f5384d58bcb1bba0bcd9e6a6781d1a6ac2cc280c330ecbab6cb7931b721552 # hash3 = hash1 ^ hash2 = 5bf2c3f626e7cd34a38569867709d986e2dbcdff86841c2da6eced6ae2ae59f3 # h1 = Digest.sha256(int.to_bytes(1000, 2, 'little'), is_hex=True) # h2 = Digest.sha256(int.to_bytes(89, 1, 'little'), is_hex=True) # h = hex(int(h1, 16) ^ int(h2, 16))[2::] # self.assertEqual('4307fbbb7e5b7c8f0339b060d171c49c881901d78ab712e60d805af9f9dc4ca1', h1) # self.assertEqual('18f5384d58bcb1bba0bcd9e6a6781d1a6ac2cc280c330ecbab6cb7931b721552', h2) # self.assertEqual('5bf2c3f626e7cd34a38569867709d986e2dbcdff86841c2da6eced6ae2ae59f3', h) hash1 = Digest.sha256(bigint_to_neo_bytes(1000), 0).hex() print("hash1 is ", hash1, type(hash1)) hash2 = Digest.sha256(bigint_to_neo_bytes(89), 0).hex() print("hash2 is ", hash2, type(hash2)) h1 = (bytearray.fromhex(hash1)) h1.reverse() h2 = (bytearray.fromhex(hash2)) h2.reverse() res = int(h1.hex(), 16) ^ int(h2.hex(), 16) tmph = hex(res) h3 = bytearray.fromhex(tmph[2:]) h3.reverse() hash3 = h3.hex() print("hash is ", hash3) param_list1.append(1000) param_list1.append(89) param_list1.append(h3) param_list.append(param_list1) print("***** test1", param_list) hash = self.test_invoke(payerAcct, param_list) print("hash === test1", hash) # time.sleep(6) self.test_handleEvent("startNewRound", hash) return True
def test_bigint_to_neo_bytes(self): bs = util.bigint_to_neo_bytes(-9175052165852779861) bs1 = util.bigint_to_neo_bytes(9175052165852779861) bs2 = util.bigint_to_neo_bytes(-9199634313818843819) bs3 = util.bigint_to_neo_bytes(9199634313818843819) bs4 = util.bigint_to_neo_bytes(-8380656) bs5 = util.bigint_to_neo_bytes(8380656) bs6 = util.bigint_to_neo_bytes(-8446192) bs7 = util.bigint_to_neo_bytes(8446192) bs8 = util.bigint_to_neo_bytes(-0) bs9 = util.bigint_to_neo_bytes(0) self.assertEqual(bs.hex(), "abaaaaaaaaaaab80") self.assertEqual(bs1.hex(), "555555555555547f") self.assertEqual(bs2.hex(), "5555555555555480") self.assertTrue(bs3.hex() == "abaaaaaaaaaaab7f") self.assertTrue(bs4.hex() == "101f80") self.assertTrue(bs5.hex() == "f0e07f") self.assertTrue(bs6.hex() == "101f7fff") self.assertTrue(bs7.hex() == "f0e08000") print(bs8.hex()) print(bs9.hex())
def get_map_bytes(param_dict: dict): builder = ParamsBuilder() builder.emit(BuildParams.Type.maptype.value) builder.emit(util.bigint_to_neo_bytes(len(param_dict))) for key, value in param_dict.items(): builder.emit(BuildParams.Type.bytearraytype.value) builder.emit_push_byte_array(str(key).encode()) if isinstance(value, bytearray) or isinstance(value, bytes): builder.emit(BuildParams.Type.bytearraytype.value) builder.emit_push_byte_array(bytearray(value)) elif isinstance(value, str): builder.emit(BuildParams.Type.bytearraytype.value) builder.emit_push_byte_array(value.encode()) elif isinstance(value, int): builder.emit(BuildParams.Type.integertype.value) builder.emit_push_integer(int(value)) else: raise Exception("param error") return builder.to_array()
def emit_push_integer(self, num: int): if num == -1: return self.emit(PUSHM1) elif num == 0: return self.emit(PUSH0) elif 0 < num < 16: return self.emit(int.from_bytes(PUSH1, 'little') - 1 + num) # elif num < 0x10000: # return self.emit_push_byte_array(num.to_bytes(2, "little")) # elif num.bit_length() < 32: # return self.emit_push_byte_array(num.to_bytes(4, "little")) # elif num.bit_length() < 40: # return self.emit_push_byte_array(num.to_bytes(5, "little")) # elif num.bit_length() < 48: # return self.emit_push_byte_array(num.to_bytes(6, "little")) # elif num.bit_length() < 56: # return self.emit_push_byte_array(num.to_bytes(7, "little")) # else: # return self.emit_push_byte_array(num.to_bytes(8, "little")) bs = util.bigint_to_neo_bytes(num) return self.emit_push_byte_array(bs)
def test_getHash(self, value): hash1 = Digest.sha256(bigint_to_neo_bytes(value), 0).hex() return hash1
def test_bigint_to_neo_bytes(self): bs = util.bigint_to_neo_bytes(1) self.assertEqual(bs.hex(), '01')