def test_big_integer_div(self): b1 = BigInteger(55055055055055) b2 = BigInteger(55055055) b3 = b1 / b2 self.assertIsInstance(b3, BigInteger) self.assertEqual(b3, 1000000)
def test_big_integer_frombytes(self): b1 = BigInteger(8972340892734890723) ba = b1.ToByteArray() b2 = BigInteger.FromBytes(ba) self.assertEqual(b1, b2) self.assertTrue(b1.Equals(b2))
def test_big_integer_mul(self): b1 = BigInteger(55055055055055) b2 = BigInteger(55055055055) b3 = b1 * b2 self.assertIsInstance(b3, BigInteger) self.assertEqual(b3, 3031059087112109081053025)
def test_big_integer_float(self): b1 = BigInteger(5505.001) b2 = BigInteger(55055.999) b3 = b1 + b2 self.assertIsInstance(b3, BigInteger) self.assertEqual(b3, 60560)
def test_big_integer_add(self): b1 = BigInteger(10) b2 = BigInteger(20) b3 = b1 + b2 self.assertIsInstance(b3, BigInteger) self.assertEqual(b3, 30)
def test_big_integer_sub(self): b1 = BigInteger(5505505505505505050505) b2 = BigInteger(5505505505505505000000) b3 = b1 - b2 self.assertIsInstance(b3, BigInteger) self.assertEqual(b3, 50505)
def test_big_integer_to_ba(self): b1 = BigInteger(8972340892734890723) ba = b1.ToByteArray() integer = BigInteger.from_bytes(ba, 'little') self.assertEqual(integer, 8972340892734890723)
def parse_param(p, ignore_int=False, prefer_hex=True): if not ignore_int: try: val = int(p) out = BigInteger(val) return out except Exception as e: pass try: val = eval(p) if type(val) is bytearray: return val.hex() return val except Exception as e: pass if type(p) is str: if prefer_hex: return binascii.hexlify( p.encode('utf-8')) else: return p.encode('utf-8') return p
def GetBigInteger(self): try: b = BigInteger(int.from_bytes(self._value, 'little', signed=True)) return b except Exception as e: pass return self._value
def Blockchain_GetHeader(self, engine): data = engine.EvaluationStack.Pop().GetByteArray() header = None if len(data) <= 5: height = BigInteger.FromBytes(data) if Blockchain.Default() is not None: header = Blockchain.Default().GetHeaderBy(height_or_hash=height) elif height == 0: header = Blockchain.GenesisBlock().Header elif len(data) == 32: hash = UInt256(data=data) if Blockchain.Default() is not None: header = Blockchain.Default().GetHeaderBy(height_or_hash=hash) elif hash == Blockchain.GenesisBlock().Hash: header = Blockchain.GenesisBlock().Header engine.EvaluationStack.PushT( StackItem.FromInterface(header)) return True
def Blockchain_GetBlock(self, engine): data = engine.EvaluationStack.Pop() if data: data = data.GetByteArray() else: return False block = None if len(data) <= 5: height = BigInteger.FromBytes(data) if Blockchain.Default() is not None: block = Blockchain.Default().GetBlockByHeight(height) elif height == 0: block = Blockchain.GenesisBlock() elif len(data) == 32: hash = UInt256(data=data).ToBytes() if Blockchain.Default() is not None: block = Blockchain.Default().GetBlockByHash(hash=hash) elif hash == Blockchain.GenesisBlock().Hash: block = Blockchain.GenesisBlock().Header engine.EvaluationStack.PushT(StackItem.FromInterface(block)) return True
def New(value): typ = type(value) if typ is BigInteger: return Integer(value) elif typ is int: return Integer(BigInteger(value)) elif typ is float: return Integer(BigInteger(int(value))) elif typ is bool: return Boolean(value) elif typ is bytearray or typ is bytes: return ByteArray(value) elif typ is list: return Array(value) # logger.info("Could not create stack item for vaule %s %s " % (typ, value)) return value
def parse_param(p, wallet=None, ignore_int=False, prefer_hex=True): # first, we'll try to parse an array try: items = eval(p) if len(items) > 0 and type(items) is list: parsed = [] for item in items: parsed.append(parse_param(item, wallet)) return parsed except Exception as e: # print("couldnt eval items as array %s " % e) pass if not ignore_int: try: val = int(p) out = BigInteger(val) return out except Exception as e: pass try: val = eval(p) if type(val) is bytearray: return val.hex() return val except Exception as e: pass if type(p) is str: if wallet is not None: for na in wallet.NamedAddr: if na.Title == p: return bytearray(na.ScriptHash) # check for address strings like 'ANE2ECgA6YAHR5Fh2BrSsiqTyGb5KaS19u' and # convert them to a bytearray if len(p) == 34 and p[0] == 'A': addr = Helper.AddrStrToScriptHash(p).Data return addr if prefer_hex: return binascii.hexlify(p.encode('utf-8')) else: return p.encode('utf-8') return p
def test_big_integer_sign(self): b1 = BigInteger(3) b2 = BigInteger(0) b3 = BigInteger(-4) self.assertEqual(b1.Sign, 1) self.assertEqual(b2.Sign, 0) self.assertEqual(b3.Sign, -1) c1 = BigInteger(-100) c1_bytes = c1.ToByteArray() c2 = BigInteger.FromBytes(c1_bytes, signed=True) self.assertEqual(c2.Sign, -1) c2_unsigned = BigInteger.FromBytes(c1_bytes, signed=False) self.assertEqual(c2_unsigned.Sign, 1)
def parse_param(p): try: val = int(p) out = BigInteger(val) return out except Exception as e: pass try: val = eval(p) if type(val) is bytearray: return val.hex() return val except Exception as e: pass if type(p) is str: return binascii.hexlify(p.encode('utf-8')) return p
def GetHashCodeBytes(self): bigint = BigInteger(self.GetHashCode()) return bigint.ToByteArray()
def GetBigInteger(self): return BigInteger( int.from_bytes(self.GetByteArray(), 'little', signed=True))