def constructTx(self, ref_block_num=None, ref_block_prefix=None):
        """ Construct the actual transaction and store it in the class's dict
            store

        """
        ops = list()
        if self.crea.is_connected() and self.crea.rpc.get_use_appbase():
            # appbase disabled by now
            # broadcasting does not work at the moment
            appbase = not self._use_condenser_api
        else:
            appbase = False
        for op in self.ops:
            # otherwise, we simply wrap ops into Operations
            ops.extend(
                [Operation(op, appbase=appbase, prefix=self.crea.prefix)])

        # We no wrap everything into an actual transaction
        expiration = formatTimeFromNow(self.expiration or self.crea.expiration)
        if ref_block_num is None or ref_block_prefix is None:
            ref_block_num, ref_block_prefix = transactions.getBlockParams(
                self.crea.rpc)
        self.tx = Signed_Transaction(ref_block_prefix=ref_block_prefix,
                                     expiration=expiration,
                                     operations=ops,
                                     ref_block_num=ref_block_num,
                                     custom_chains=self.crea.custom_chains,
                                     prefix=self.crea.prefix)

        super(TransactionBuilder, self).update(self.tx.json())
        self._unset_require_reconstruction()
 def test_verify_transaction(self):
     stm = self.stm
     block = Block(22005665, crea_instance=stm)
     trx = block.transactions[28]
     signed_tx = Signed_Transaction(trx)
     key = signed_tx.verify(chain=stm.chain_params, recover_parameter=False)
     public_key = format(Base58(key[0]), stm.prefix)
     self.assertEqual(
         public_key,
         "STM4tzr1wjmuov9ftXR6QNv7qDWsbShMBPQpuwatZsfSc5pKjRDfq")
Example #3
0
 def doit(self, printWire=False, ops=None):
     if ops is None:
         ops = [Operation(self.op)]
     tx = Signed_Transaction(ref_block_num=self.ref_block_num,
                             ref_block_prefix=self.ref_block_prefix,
                             expiration=self.expiration,
                             operations=ops)
     tx = tx.sign([self.wif], chain=self.prefix)
     tx.verify([PrivateKey(self.wif, prefix=u"STM").pubkey], self.prefix)
     txWire = hexlify(py23_bytes(tx)).decode("ascii")
Example #4
0
 def doit(self, printWire=False, ops=None):
     ops = [Operation(ops)]
     tx = Signed_Transaction(ref_block_num=self.ref_block_num,
                             ref_block_prefix=self.ref_block_prefix,
                             expiration=self.expiration,
                             operations=ops)
     start = timer()
     tx = tx.sign([self.wif], chain=self.prefix)
     end1 = timer()
     tx.verify([PrivateKey(self.wif, prefix=u"STM").pubkey], self.prefix)
     end2 = timer()
     return end2 - end1, end1 - start
 def test_signing_appbase(self):
     b = Blockchain(crea_instance=self.bts)
     st = None
     for block in b.blocks(start=25304468, stop=25304468):
         for trx in block.transactions:
             st = Signed_Transaction(trx.copy())
     self.assertTrue(st is not None)
Example #6
0
 def get_tx_size(self, op):
     """Returns the tx size of an operation"""
     ops = [Operation(op)]
     prefix = u"CREA"
     wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
     ref_block_num = 34294
     ref_block_prefix = 3707022213
     expiration = "2016-04-06T08:29:27"
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     txWire = hexlify(py23_bytes(tx)).decode("ascii")
     tx_size = len(txWire)
     return tx_size
    def test_Transfer(self):
        transferJson = {
            'from': 'test',
            'to': 'test1',
            'amount': "1.000 CREA",
            'memo': 'foobar'
        }
        t = Transfer(transferJson)
        self.assertEqual(transferJson, json.loads(str(t)))
        self.assertEqual(transferJson, t.json())
        self.assertEqual(transferJson, t.toJson())
        self.assertEqual(transferJson, t.__json__())

        transferJson = {
            'from': 'test',
            'to': 'test1',
            'amount': ['3000', 3, '@@000000037'],
            'memo': 'foobar'
        }
        t = Transfer(transferJson)
        self.assertEqual(transferJson, json.loads(str(t)))
        self.assertEqual(transferJson, t.json())
        self.assertEqual(transferJson, t.toJson())
        self.assertEqual(transferJson, t.__json__())

        o = Operation(Transfer(transferJson))
        self.assertEqual(o.json()[1], transferJson)
        tx = {
            'ref_block_num': 0,
            'ref_block_prefix': 0,
            'expiration': '2018-04-07T09:30:53',
            'operations': [o],
            'extensions': [],
            'signatures': []
        }
        s = Signed_Transaction(tx)
        s.sign(wifkeys=[wif], chain="CREAAPPBASE")
        self.assertEqual(s.json()["operations"][0][1], transferJson)
    def sign(self, reconstruct_tx=True):
        """ Sign a provided transaction with the provided key(s)
            One or many wif keys to use for signing a transaction.
            The wif keys can be provided by "appendWif" or the
            signer can be defined "appendSigner". The wif keys
            from all signer that are defined by "appendSigner
            will be loaded from the wallet.

            :param bool reconstruct_tx: when set to False and tx
                is already contructed, it will not reconstructed
                and already added signatures remain

        """
        if not self._is_constructed() or (self._is_constructed()
                                          and reconstruct_tx):
            self.constructTx()
        if "operations" not in self or not self["operations"]:
            return
        if self.crea.use_sc2:
            return
        # We need to set the default prefix, otherwise pubkeys are
        # presented wrongly!
        if self.crea.rpc is not None:
            operations.default_prefix = (self.crea.chain_params["prefix"])
        elif "blockchain" in self:
            operations.default_prefix = self["blockchain"]["prefix"]

        try:
            signedtx = Signed_Transaction(**self.json(with_prefix=True))
            signedtx.add_custom_chains(self.crea.custom_chains)
        except:
            raise ValueError("Invalid TransactionBuilder Format")

        if not any(self.wifs):
            raise MissingKeyError

        signedtx.sign(self.wifs, chain=self.crea.chain_params)
        self["signatures"].extend(signedtx.json().get("signatures"))
        return signedtx
class TransactionBuilder(dict):
    """ This class simplifies the creation of transactions by adding
        operations and signers.
        To build your own transactions and sign them

        :param dict tx: transaction (Optional). If not set, the new transaction is created.
        :param int expiration: Delay in seconds until transactions are supposed
            to expire *(optional)* (default is 30)
        :param Crea crea_instance: If not set, shared_crea_instance() is used

        .. testcode::

           from crea.transactionbuilder import TransactionBuilder
           from creabase.operations import Transfer
           from crea import Crea
           wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
           stm = Crea(nobroadcast=True, keys={'active': wif})
           tx = TransactionBuilder(crea_instance=stm)
           transfer = {"from": "test", "to": "test1", "amount": "1 CREA", "memo": ""}
           tx.appendOps(Transfer(transfer))
           tx.appendSigner("test", "active") # or tx.appendWif(wif)
           signed_tx = tx.sign()
           broadcast_tx = tx.broadcast()

    """
    def __init__(self,
                 tx={},
                 use_condenser_api=True,
                 crea_instance=None,
                 **kwargs):
        self.crea = crea_instance or shared_crea_instance()
        self.clear()
        if tx and isinstance(tx, dict):
            super(TransactionBuilder, self).__init__(tx)
            # Load operations
            self.ops = tx["operations"]
            self._require_reconstruction = False
        else:
            self._require_reconstruction = True
        self._use_condenser_api = use_condenser_api
        self.set_expiration(kwargs.get("expiration", self.crea.expiration))

    def set_expiration(self, p):
        """Set expiration date"""
        self.expiration = p

    def is_empty(self):
        """Check if ops is empty"""
        return not (len(self.ops) > 0)

    def list_operations(self):
        """List all ops"""
        if self.crea.is_connected() and self.crea.rpc.get_use_appbase():
            # appbase disabled by now
            appbase = not self._use_condenser_api
        else:
            appbase = False
        return [
            Operation(o, appbase=appbase, prefix=self.crea.prefix)
            for o in self.ops
        ]

    def _is_signed(self):
        """Check if signatures exists"""
        return "signatures" in self and bool(self["signatures"])

    def _is_constructed(self):
        """Check if tx is already constructed"""
        return "expiration" in self and bool(self["expiration"])

    def _is_require_reconstruction(self):
        return self._require_reconstruction

    def _set_require_reconstruction(self):
        self._require_reconstruction = True

    def _unset_require_reconstruction(self):
        self._require_reconstruction = False

    def __repr__(self):
        return str(self)

    def __str__(self):
        return str(self.json())

    def __getitem__(self, key):
        if key not in self:
            self.constructTx()
        return dict(self).__getitem__(key)

    def get_parent(self):
        """ TransactionBuilders don't have parents, they are their own parent
        """
        return self

    def json(self, with_prefix=False):
        """ Show the transaction as plain json
        """
        if not self._is_constructed() or self._is_require_reconstruction():
            self.constructTx()
        json_dict = dict(self)
        if with_prefix:
            json_dict["prefix"] = self.crea.prefix
        return json_dict

    def appendOps(self, ops, append_to=None):
        """ Append op(s) to the transaction builder

            :param list ops: One or a list of operations
        """
        if isinstance(ops, list):
            self.ops.extend(ops)
        else:
            self.ops.append(ops)
        self._set_require_reconstruction()

    def appendSigner(self, account, permission):
        """ Try to obtain the wif key from the wallet by telling which account
            and permission is supposed to sign the transaction
            It is possible to add more than one signer.
        """
        if not self.crea.is_connected():
            return
        if permission not in ["active", "owner", "posting"]:
            raise AssertionError("Invalid permission")
        account = Account(account, crea_instance=self.crea)
        if permission not in account:
            account = Account(account,
                              crea_instance=self.crea,
                              lazy=False,
                              full=True)
            account.clear_cache()
            account.refresh()
        if permission not in account:
            account = Account(account, crea_instance=self.crea)
        if permission not in account:
            raise AssertionError("Could not access permission")

        required_treshold = account[permission]["weight_threshold"]
        if self.crea.wallet.locked():
            raise WalletLocked()
        if self.crea.use_sc2 and self.crea.creaconnect is not None:
            self.crea.creaconnect.set_username(account["name"], permission)
            return

        def fetchkeys(account, perm, level=0):
            if level > 2:
                return []
            r = []
            for authority in account[perm]["key_auths"]:
                try:
                    wif = self.crea.wallet.getPrivateKeyForPublicKey(
                        authority[0])
                    if wif:
                        r.append([wif, authority[1]])
                except ValueError:
                    pass
                except MissingKeyError:
                    pass

            if sum([x[1] for x in r]) < required_treshold:
                # go one level deeper
                for authority in account[perm]["account_auths"]:
                    auth_account = Account(authority[0],
                                           crea_instance=self.crea)
                    r.extend(fetchkeys(auth_account, perm, level + 1))

            return r

        if account["name"] not in self.signing_accounts:
            # is the account an instance of public key?
            if isinstance(account, PublicKey):
                self.wifs.add(
                    self.crea.wallet.getPrivateKeyForPublicKey(str(account)))
            else:
                if permission not in account:
                    raise AssertionError("Could not access permission")
                required_treshold = account[permission]["weight_threshold"]
                keys = fetchkeys(account, permission)
                # If keys are empty, try again with active key
                if not keys and permission == "posting":
                    _keys = fetchkeys(account, "active")
                    keys.extend(_keys)
                # If keys are empty, try again with owner key
                if not keys and permission != "owner":
                    _keys = fetchkeys(account, "owner")
                    keys.extend(_keys)
                for x in keys:
                    self.wifs.add(x[0])

            self.signing_accounts.append(account["name"])

    def appendWif(self, wif):
        """ Add a wif that should be used for signing of the transaction.

            :param string wif: One wif key to use for signing
                a transaction.
        """
        if wif:
            try:
                PrivateKey(wif, prefix=self.crea.prefix)
                self.wifs.add(wif)
            except:
                raise InvalidWifError

    def clearWifs(self):
        """Clear all stored wifs"""
        self.wifs = set()

    def constructTx(self, ref_block_num=None, ref_block_prefix=None):
        """ Construct the actual transaction and store it in the class's dict
            store

        """
        ops = list()
        if self.crea.is_connected() and self.crea.rpc.get_use_appbase():
            # appbase disabled by now
            # broadcasting does not work at the moment
            appbase = not self._use_condenser_api
        else:
            appbase = False
        for op in self.ops:
            # otherwise, we simply wrap ops into Operations
            ops.extend(
                [Operation(op, appbase=appbase, prefix=self.crea.prefix)])

        # We no wrap everything into an actual transaction
        expiration = formatTimeFromNow(self.expiration or self.crea.expiration)
        if ref_block_num is None or ref_block_prefix is None:
            ref_block_num, ref_block_prefix = transactions.getBlockParams(
                self.crea.rpc)
        self.tx = Signed_Transaction(ref_block_prefix=ref_block_prefix,
                                     expiration=expiration,
                                     operations=ops,
                                     ref_block_num=ref_block_num,
                                     custom_chains=self.crea.custom_chains,
                                     prefix=self.crea.prefix)

        super(TransactionBuilder, self).update(self.tx.json())
        self._unset_require_reconstruction()

    def sign(self, reconstruct_tx=True):
        """ Sign a provided transaction with the provided key(s)
            One or many wif keys to use for signing a transaction.
            The wif keys can be provided by "appendWif" or the
            signer can be defined "appendSigner". The wif keys
            from all signer that are defined by "appendSigner
            will be loaded from the wallet.

            :param bool reconstruct_tx: when set to False and tx
                is already contructed, it will not reconstructed
                and already added signatures remain

        """
        if not self._is_constructed() or (self._is_constructed()
                                          and reconstruct_tx):
            self.constructTx()
        if "operations" not in self or not self["operations"]:
            return
        if self.crea.use_sc2:
            return
        # We need to set the default prefix, otherwise pubkeys are
        # presented wrongly!
        if self.crea.rpc is not None:
            operations.default_prefix = (self.crea.chain_params["prefix"])
        elif "blockchain" in self:
            operations.default_prefix = self["blockchain"]["prefix"]

        try:
            signedtx = Signed_Transaction(**self.json(with_prefix=True))
            signedtx.add_custom_chains(self.crea.custom_chains)
        except:
            raise ValueError("Invalid TransactionBuilder Format")

        if not any(self.wifs):
            raise MissingKeyError

        signedtx.sign(self.wifs, chain=self.crea.chain_params)
        self["signatures"].extend(signedtx.json().get("signatures"))
        return signedtx

    def verify_authority(self):
        """ Verify the authority of the signed transaction
        """
        try:
            self.crea.rpc.set_next_node_on_empty_reply(False)
            if self.crea.rpc.get_use_appbase():
                args = {'trx': self.json()}
            else:
                args = self.json()
            ret = self.crea.rpc.verify_authority(args, api="database")
            if not ret:
                raise InsufficientAuthorityError
            elif isinstance(ret, dict) and "valid" in ret and not ret["valid"]:
                raise InsufficientAuthorityError
        except Exception as e:
            raise e

    def get_potential_signatures(self):
        """ Returns public key from signature
        """
        if not self.crea.is_connected():
            raise OfflineHasNoRPCException("No RPC available in offline mode!")
        self.crea.rpc.set_next_node_on_empty_reply(False)
        if self.crea.rpc.get_use_appbase():
            args = {'trx': self.json()}
        else:
            args = self.json()
        ret = self.crea.rpc.get_potential_signatures(args, api="database")
        if 'keys' in ret:
            ret = ret["keys"]
        return ret

    def get_transaction_hex(self):
        """ Returns a hex value of the transaction
        """
        if not self.crea.is_connected():
            raise OfflineHasNoRPCException("No RPC available in offline mode!")
        self.crea.rpc.set_next_node_on_empty_reply(False)
        if self.crea.rpc.get_use_appbase():
            args = {'trx': self.json()}
        else:
            args = self.json()
        ret = self.crea.rpc.get_transaction_hex(args, api="database")
        if 'hex' in ret:
            ret = ret["hex"]
        return ret

    def get_required_signatures(self, available_keys=list()):
        """ Returns public key from signature
        """
        if not self.crea.is_connected():
            raise OfflineHasNoRPCException("No RPC available in offline mode!")
        self.crea.rpc.set_next_node_on_empty_reply(False)
        if self.crea.rpc.get_use_appbase():
            args = {'trx': self.json(), 'available_keys': available_keys}
            ret = self.crea.rpc.get_required_signatures(args, api="database")
        else:
            ret = self.crea.rpc.get_required_signatures(self.json(),
                                                        available_keys,
                                                        api="database")

        return ret

    def broadcast(self, max_block_age=-1):
        """ Broadcast a transaction to the crea network
            Returns the signed transaction and clears itself
            after broadast

            Clears itself when broadcast was not successfully.

            :param int max_block_age: parameter only used
                for appbase ready nodes

        """
        # Cannot broadcast an empty transaction
        if not self._is_signed():
            self.sign()

        if "operations" not in self or not self["operations"]:
            return
        ret = self.json()
        if self.crea.is_connected() and self.crea.rpc.get_use_appbase():
            # Returns an internal Error at the moment
            if not self._use_condenser_api:
                args = {'trx': self.json(), 'max_block_age': max_block_age}
                broadcast_api = "network_broadcast"
            else:
                args = self.json()
                broadcast_api = "condenser"
        else:
            args = self.json()
            broadcast_api = "network_broadcast"

        if self.crea.nobroadcast:
            log.info("Not broadcasting anything!")
            self.clear()
            return ret
        # Broadcast
        try:
            self.crea.rpc.set_next_node_on_empty_reply(False)
            if self.crea.use_sc2:
                ret = self.crea.creaconnect.broadcast(self["operations"])
            elif self.crea.blocking:
                ret = self.crea.rpc.broadcast_transaction_synchronous(
                    args, api=broadcast_api)
                if "trx" in ret:
                    ret.update(**ret.get("trx"))
            else:
                self.crea.rpc.broadcast_transaction(args, api=broadcast_api)
        except Exception as e:
            # log.error("Could Not broadcasting anything!")
            self.clear()
            raise e

        self.clear()
        return ret

    def clear(self):
        """ Clear the transaction builder and start from scratch
        """
        self.ops = []
        self.wifs = set()
        self.signing_accounts = []
        # This makes sure that _is_constructed will return False afterwards
        self["expiration"] = None
        super(TransactionBuilder, self).__init__({})

    def addSigningInformation(self, account, permission, reconstruct_tx=False):
        """ This is a private method that adds side information to a
            unsigned/partial transaction in order to simplify later
            signing (e.g. for multisig or coldstorage)

            Not needed when "appendWif" was already or is going to be used

            FIXME: Does not work with owner keys!

            :param bool reconstruct_tx: when set to False and tx
                is already contructed, it will not reconstructed
                and already added signatures remain

        """
        if not self._is_constructed() or (self._is_constructed()
                                          and reconstruct_tx):
            self.constructTx()
        self["blockchain"] = self.crea.chain_params

        if isinstance(account, PublicKey):
            self["missing_signatures"] = [str(account)]
        else:
            accountObj = Account(account, crea_instance=self.crea)
            authority = accountObj[permission]
            # We add a required_authorities to be able to identify
            # how to sign later. This is an array, because we
            # may later want to allow multiple operations per tx
            self.update(
                {"required_authorities": {
                    accountObj["name"]: authority
                }})
            for account_auth in authority["account_auths"]:
                account_auth_account = Account(account_auth[0],
                                               crea_instance=self.crea)
                self["required_authorities"].update(
                    {account_auth[0]: account_auth_account.get(permission)})

            # Try to resolve required signatures for offline signing
            self["missing_signatures"] = [x[0] for x in authority["key_auths"]]
            # Add one recursion of keys from account_auths:
            for account_auth in authority["account_auths"]:
                account_auth_account = Account(account_auth[0],
                                               crea_instance=self.crea)
                self["missing_signatures"].extend([
                    x[0] for x in account_auth_account[permission]["key_auths"]
                ])

    def appendMissingSignatures(self):
        """ Store which accounts/keys are supposed to sign the transaction

            This method is used for an offline-signer!
        """
        missing_signatures = self.get("missing_signatures", [])
        for pub in missing_signatures:
            try:
                wif = self.crea.wallet.getPrivateKeyForPublicKey(pub)
                if wif:
                    self.appendWif(wif)
            except MissingKeyError:
                wif = None