Exemplo n.º 1
0
    def AddContract(self, contract):
        """
        Add a contract to the database.

        Args:
            contract(neo.SmartContract.Contract): a Contract instance.
        """
        super(UserWallet, self).AddContract(contract)

        try:
            db_contract = Contract.get(ScriptHash=contract.ScriptHash.ToBytes())
            db_contract.delete_instance()
        except Exception as e:
            logger.info("contract does not exist yet")

        sh = bytes(contract.ScriptHash.ToArray())
        address, created = Address.get_or_create(ScriptHash=sh)
        address.IsWatchOnly = False
        address.save()
        db_contract = Contract.create(RawData=contract.ToArray(),
                                      ScriptHash=contract.ScriptHash.ToBytes(),
                                      PublicKeyHash=contract.PublicKeyHash.ToBytes(),
                                      Address=address,
                                      Account=self.__dbaccount)

        logger.debug("Creating db contract %s " % db_contract)

        db_contract.save()
Exemplo n.º 2
0
    def AddContract(self, contract):

        super(UserWallet, self).AddContract(contract)

        db_contract = None
        try:
            db_contract = Contract.get(
                ScriptHash=contract.ScriptHash.ToBytes())
        except Exception as e:
            self.__log.debug("contract does not exist yet")

        if db_contract is not None:
            db_contract.PublicKeyHash = contract.PublicKeyHash.ToBytes()
        else:
            sh = bytes(contract.ScriptHash.ToArray())
            address, created = Address.get_or_create(ScriptHash=sh)
            address.save()
            db_contract = Contract.create(
                RawData=contract.ToArray(),
                ScriptHash=contract.ScriptHash.ToBytes(),
                PublicKeyHash=contract.PublicKeyHash.ToBytes(),
                Address=address,
                Account=self.__dbaccount)

            self.__log.debug("Creating db contract %s " % db_contract)

            db_contract.save()
Exemplo n.º 3
0
    def AddContract(self, contract):
        """
        Add a contract to the database.

        Args:
            contract(neo.SmartContract.Contract): a Contract instance.
        """
        super(UserWallet, self).AddContract(contract)

        try:
            db_contract = Contract.get(ScriptHash=contract.ScriptHash.ToBytes())
            db_contract.delete_instance()
        except Exception as e:
            logger.info("contract does not exist yet")

        sh = bytes(contract.ScriptHash.ToArray())
        address, created = Address.get_or_create(ScriptHash=sh)
        address.IsWatchOnly = False
        address.save()
        db_contract = Contract.create(RawData=contract.ToArray(),
                                      ScriptHash=contract.ScriptHash.ToBytes(),
                                      PublicKeyHash=contract.PublicKeyHash.ToBytes(),
                                      Address=address,
                                      Account=self.__dbaccount)

        logger.debug("Creating db contract %s " % db_contract)

        db_contract.save()
Exemplo n.º 4
0
    def DeleteAddress(self, script_hash):
        success, coins_toremove = super(UserWallet, self).DeleteAddress(script_hash)

        for coin in coins_toremove:
            try:
                c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex)
                c.delete_instance()
            except Exception as e:
                logger.error("Could not delete coin %s %s " % (coin, e))

        todelete = bytes(script_hash.ToArray())

        for c in Contract.select():

            address = c.Address
            if address.ScriptHash == todelete:
                c.delete_instance()
                address.delete_instance()

        try:
            address = Address.get(ScriptHash=todelete)
            address.delete_instance()
        except Exception as e:
            pass

        return True, coins_toremove
Exemplo n.º 5
0
    def DeleteAddress(self, script_hash):
        success, coins_toremove = super(UserWallet, self).DeleteAddress(script_hash)

        for coin in coins_toremove:
            try:
                c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex)
                c.delete_instance()
            except Exception as e:
                logger.error("Could not delete coin %s %s " % (coin, e))

        todelete = bytes(script_hash.ToArray())

        for c in Contract.select():

            address = c.Address
            if address.ScriptHash == todelete:
                c.delete_instance()
                address.delete_instance()

        try:
            address = Address.get(ScriptHash=todelete)
            address.delete_instance()
        except Exception as e:
            pass

        return True, coins_toremove
Exemplo n.º 6
0
    def __init__(self, public, is_mainnet):
        self._path = 'main123' if is_mainnet else 'test123'
        self.AddressVersion = 23
        self._lock = RLock()
        self._indexedDB = Blockchain.Default()
        self.BuildDatabase()


        self._keys = {}
        self._contracts = self.LoadContracts()

        for key, contract in self._contracts.items():
            print('contract ScriptHash', contract.ScriptHash)
        print('initializing', flush=True)

        kp = OnlyPublicKeyPair(public)
        self._keys[kp.PublicKeyHash.ToBytes()] = kp
        contract = WalletContract.CreateSignatureContract(kp.PublicKey)
        if contract.ScriptHash.ToBytes() not in self._contracts.keys():
            self._contracts[contract.ScriptHash.ToBytes()] = contract
            sh = bytes(contract.ScriptHash.ToArray())
            address, created = Address.get_or_create(ScriptHash=sh)
            address.IsWatchOnly = False
            address.save()
            db_contract = Contract.create(RawData=contract.ToArray(),
                    ScriptHash=contract.ScriptHash.ToBytes(),
                    PublicKeyHash=contract.PublicKeyHash.ToBytes(),
                    Address=address,
                    Account=None
            )






        self.LoadNamedAddresses()
        self._watch_only = self.LoadWatchOnly()
        self._tokens = self.LoadNEP5Tokens()
        self._coins = self.LoadCoins()
#        self.initialize_holds()

        self._holds = VINHold.filter(IsComplete=False)

        # Handle EventHub events for SmartContract decorators
        @events.on(SmartContractEvent.RUNTIME_NOTIFY)
        def call_on_event(sc_event):
            # Make sure this event is for this specific smart contract
            self.on_notify_sc_event(sc_event)



        try:
            self._current_height = int(self.LoadStoredData('Height'))
        except:
            print('setting height to 0')
            self._current_height = 0
            self.SaveStoredData('Height', self._current_height)
Exemplo n.º 7
0
    def LoadContracts(self):
        ctr = {}

        for ct in Contract.select():
            data = binascii.unhexlify(ct.RawData)
            contract = Helper.AsSerializableWithType(data, 'neo.SmartContract.Contract.Contract')
            ctr[contract.ScriptHash.ToBytes()] = contract

        return ctr
Exemplo n.º 8
0
    def LoadContracts(self):
        ctr = {}

        for ct in Contract.select():
            data = binascii.unhexlify(ct.RawData)
            contract = Helper.AsSerializableWithType(data, 'neo.SmartContract.Contract.Contract')
            ctr[contract.ScriptHash.ToBytes()] = contract

        return ctr
Exemplo n.º 9
0
    def __init__(self, public, is_mainnet):
        self._path = 'main123' if is_mainnet else 'test123'
        self.AddressVersion = 23
        self._lock = RLock()
        self._indexedDB = Blockchain.Default()
        self.BuildDatabase()


        self._keys = {}
        self._contracts = self.LoadContracts()
#        for public in ['0294cd0a9e77f358f709e69d9375680b1eafe75373645192b5b251260f484577ea']:
        kp = OnlyPublicKeyPair(public)
        self._keys[kp.PublicKeyHash.ToBytes()] = kp
        contract = WalletContract.CreateSignatureContract(kp.PublicKey)
        if contract.ScriptHash.ToBytes() not in self._contracts.keys():
            self._contracts[contract.ScriptHash.ToBytes()] = contract
            sh = bytes(contract.ScriptHash.ToArray())
            address, created = Address.get_or_create(ScriptHash=sh)
            address.IsWatchOnly = False
            address.save()
            db_contract = Contract.create(RawData=contract.ToArray(),
                    ScriptHash=contract.ScriptHash.ToBytes(),
                    PublicKeyHash=contract.PublicKeyHash.ToBytes(),
                    Address=address,
                    Account=None
            )






        self.LoadNamedAddresses()
        self._watch_only = self.LoadWatchOnly()
        self._tokens = self.LoadNEP5Tokens()
        self._coins = self.LoadCoins()
        self.initialize_holds()
        try:
            self._current_height = int(self.LoadStoredData('Height'))
        except:
            print('setting height to 0')
            self._current_height = 0
            self.SaveStoredData('Height', self._current_height)