Ejemplo n.º 1
0
    def LoadCoins(self):
        coins = {}

        try:
            for coin in Coin.select():
                reference = CoinReference(prev_hash=UInt256(coin.TxId), prev_index=coin.Index)
                output = TransactionOutput(UInt256(coin.AssetId), Fixed8(coin.Value), UInt160(coin.ScriptHash))
                walletcoin = WalletCoin.CoinFromRef(reference, output, coin.State)
                coins[reference] = walletcoin
        except Exception as e:
            print("could not load coins %s " % e)

        return coins
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def GenesisBlock():

        prev_hash = UInt256(data=bytearray(32))
        timestamp = int(
            datetime(2016, 7, 15, 15, 8, 21, tzinfo=pytz.utc).timestamp())
        index = 0
        consensus_data = 2083236893  #向比特币致敬 ( Pay Tribute To Bitcoin )
        next_consensus = Blockchain.GetConsensusAddress(
            Blockchain.StandbyValidators())
        script = Witness(bytearray(0), bytearray(PUSHT))

        mt = MinerTransaction()
        mt.Nonce = 2083236893

        output = TransactionOutput(
            Blockchain.SystemShare().Hash,
            Blockchain.SystemShare().Amount,
            Crypto.ToScriptHash(
                Contract.CreateMultiSigRedeemScript(
                    int(len(Blockchain.StandbyValidators()) / 2) + 1,
                    Blockchain.StandbyValidators())))

        it = IssueTransaction([], [output], [], [script])

        return Block(
            prev_hash, timestamp, index, consensus_data, next_consensus,
            script,
            [mt, Blockchain.SystemShare(),
             Blockchain.SystemCoin(), it], True)
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    def Hash(self):
        if not self.__hash:
            hashdata = self.RawData()
            ba = bytearray(binascii.unhexlify(hashdata))
            hash = bin_dbl_sha256(ba)
            self.__hash = UInt256(data=hash)

        return self.__hash
Ejemplo n.º 6
0
    def Blockchain_GetAsset(self, engine):

        hash = UInt256(data=engine.EvaluationStack.Pop().GetByteArray())

        asset = self._assets.TryGet(hash.ToBytes())
        if asset:
            engine.EvaluationStack.PushT(StackItem.FromInterface(asset))
            return True
        return False
Ejemplo n.º 7
0
    def Blockchain_GetAsset(self, engine):
        data = engine.EvaluationStack.Pop().GetByteArray()
        asset = None

        if Blockchain.Default() is not None:
            asset = Blockchain.Default().GetAssetState(UInt256(data=data))

        engine.EvaluationStack.PushT(StackItem.FromInterface(asset))
        return True
Ejemplo n.º 8
0
    def Account_GetBalance(self, engine):

        account = engine.EvaluationStack.Pop().GetInterface('neo.Core.State.AccountState.AccountState')
        assetId = UInt256( data=engine.EvaluationStack.Pop().GetByteArray())

        if account is None:
            return False
        balance = account.BalanceFor(assetId)
        engine.EvaluationStack.PushT(balance.GetData())
        return True
Ejemplo n.º 9
0
    def Blockchain_GetTransaction(self, engine):

        data = engine.EvaluationStack.Pop().GetByteArray()
        tx = None

        if Blockchain.Default() is not None:
            tx, height = Blockchain.Default().GetTransaction( UInt256(data=data))

        engine.EvaluationStack.PushT(StackItem.FromInterface(tx))
        return True
Ejemplo n.º 10
0
    def test_compareto_valid(self):
        u1 = UInt160(b'12345678901234567890')

        # Same value should return 0
        u2 = UIntBase(20, b'12345678901234567890')
        self.assertEqual(u1.CompareTo(u2), 0)

        # Higher digit in 'other' should return -1
        u2 = UIntBase(20, b'12345678901234567891')
        self.assertEqual(u1.CompareTo(u2), -1)

        # Lower digit in 'other' should return 1
        u2 = UIntBase(20, b'12345678901234567980')
        self.assertEqual(u1.CompareTo(u2), 1)

        # CompareTo across different UIntBase subclasses
        data = b'12345678901234567890'
        self.assertEqual(UInt160(data).CompareTo(UIntBase(len(data), data)), 0)
        self.assertEqual(UIntBase(len(data), data).CompareTo(UInt160(data)), 0)

        data = b'12345678901234567890123456789012'
        self.assertEqual(UInt256(data).CompareTo(UIntBase(len(data), data)), 0)
        self.assertEqual(UIntBase(len(data), data).CompareTo(UInt256(data)), 0)
Ejemplo n.º 11
0
    def test_compareto_invalid_datatype(self):
        u1 = UIntBase(20, b'12345678901234567890')

        with self.assertRaises(Exception):
            self.assertEqual(u1.CompareTo('asd'), 0)

        with self.assertRaises(Exception):
            self.assertEqual(u1.CompareTo(b'asd'), 0)

        with self.assertRaises(Exception):
            self.assertEqual(u1.CompareTo(123), 0)

        # Cannot compare uints with different lengths
        with self.assertRaises(Exception):
            a = UInt256(b'12345678901234567890123456789012')
            b = UIntBase(20, b'12345678901234567890')
            a.CompareTo(b)
Ejemplo n.º 12
0
def notify_withdraw_approved(event_args):

    try:
        to = event_args[0].GetByteArray()
        if len(to) == 20:
            to = Crypto.ToAddress(UInt160(data=to))

        res = {'event': 'Withdraw Request Approved', 'to': to}

        vin_requests = event_args[1]

        output = []
        for item in vin_requests.GetArray():
            vin = item.GetArray()
            txid = UInt256(data=vin[0].GetByteArray())
            index = vin[1].GetBigInteger()
            output.append({'txid': txid.ToString(), 'index': index})

        res['vins'] = output

        print("[Neo.Runtime.Notify] %s " % json.dumps(res, indent=4))
    except Exception as e:
        print("couldnt do witdraw approved? %s " % e)
Ejemplo n.º 13
0
    def __Build(leaves):
        if len(leaves) < 1:
            raise Exception('Leaves must have length')
        if len(leaves) == 1:
            return leaves[0]

        num_parents = int((len(leaves) + 1) / 2)
        parents = [MerkleTreeNode() for i in range(0, num_parents)]

        for i in range(0, num_parents):
            node = parents[i]
            node.LeftChild = leaves[i * 2]
            leaves[i * 2].Parent = node
            if(i * 2 + 1 == len(leaves)):
                node.RightChild = node.LeftChild
            else:
                node.RightChild = leaves[i * 2 + 1]
                leaves[i * 2 + 1].Parent = node

            hasharray = bytearray(node.LeftChild.Hash.ToArray() + node.RightChild.Hash.ToArray())
            node.Hash = UInt256(data=Crypto.Hash256(hasharray))

        return MerkleTree.__Build(parents)
Ejemplo n.º 14
0
def parse_hold_vins(results):
    print("results!!! %s " % results)

    holds = results[0].GetByteArray()
    holdlen = len(holds)
    numholds = int(holdlen / 33)
    print("holds, holdlen, numholds %s %s " % (holds, numholds))
    vins = []
    for i in range(0, numholds):
        hstart = i * 33
        hend = hstart + 33
        item = holds[hstart:hend]

        vin_index = item[0]
        vin_tx_id = UInt256(data=item[1:])
        print("VIN INDEX, VIN TX ID: %s %s" % (vin_index, vin_tx_id))

        t_input = TransactionInput(prevHash=vin_tx_id, prevIndex=vin_index)

        print("found tinput: %s " % json.dumps(t_input.ToJson(), indent=4))

        vins.append(t_input)

    return vins
Ejemplo n.º 15
0
 def Hash(self):
     if not self.__hash:
         ba = bytearray(binascii.unhexlify(self.GetHashData()))
         hash = Crypto.Hash256(ba)
         self.__hash = UInt256(data=hash)
     return self.__hash
Ejemplo n.º 16
0
    def test_initialization(self):
        u0 = UInt256()
        self.assertEqual(hash(u0), 0)

        u1 = UInt256(b'12345678901234567890123456789012')
        self.assertEqual(hash(u1), 875770417)
Ejemplo n.º 17
0
    def test_initialization_invalid(self):
        with self.assertRaises(Exception):
            u1 = UInt256(b'12345')

        with self.assertRaises(Exception):
            u1 = UInt256('12345678901234567890123456789012')
Ejemplo n.º 18
0
 def ReadUInt256(self):
     return UInt256(data=bytearray(self.ReadBytes(32)))