Exemplo n.º 1
0
 def prepare(self, toaddr, paymentvalue, gprice, glimit, data=bytearray(b"")):
     """Build a transaction to be signed.
     toaddr in hex without 0x
     value in wei, gprice in Wei
     """
     if self.ERC20:
         maxspendable = self.getbalance(False)
         balance_eth = self.getbalance()
         if balance_eth < (gprice * glimit):
             raise NotEnoughTokens("Not enough native ETH funding for the tx fee")
     else:
         maxspendable = self.getbalance() - (gprice * glimit)
     if paymentvalue > maxspendable or paymentvalue < 0:
         if self.ERC20:
             sym = self.token_symbol
         else:
             sym = "native ETH"
         raise NotEnoughTokens(f"Not enough {sym} tokens for the tx")
     self.nonce = int2bytearray(self.getnonce())
     self.gasprice = int2bytearray(gprice)
     self.startgas = int2bytearray(glimit)
     if self.ERC20:
         self.to = bytearray.fromhex(self.ERC20[2:])
         self.value = int2bytearray(int(0))
         self.data = bytearray.fromhex(TRANSFERT_FUNCTION + "00" * 12 + toaddr) + uint256(
             paymentvalue
         )
     else:
         self.to = bytearray.fromhex(toaddr)
         self.value = int2bytearray(int(paymentvalue))
         self.data = data
     v = int2bytearray(self.chainID)
     r = int2bytearray(0)
     s = int2bytearray(0)
     signing_tx = rlp_encode(
         [
             self.nonce,
             self.gasprice,
             self.startgas,
             self.to,
             self.value,
             self.data,
             v,
             r,
             s,
         ]
     )
     self.datahash = sha3(signing_tx)
     return (signing_tx, self.datahash)
Exemplo n.º 2
0
    def prepare(self, toaddr, paymentvalue, fee):
        utxos = self.getutxos()
        balance = self.balance_fmutxos(utxos)
        maxspendable = balance - fee
        if paymentvalue > maxspendable or paymentvalue < 0:
            raise NotEnoughTokens("Not enough fund for the tx")
        inputs = self.selectutxos(paymentvalue + fee, utxos)
        invalue = self.balance_fmutxos(inputs)
        changevalue = invalue - paymentvalue - fee
        outs = [{"value": paymentvalue, "address": toaddr}]
        if changevalue > 0:
            outs.append({"value": changevalue, "address": self.address})
        self.tx = cryptolib.coins.dogecoin.Doge(testnet=self.testnet).mktx(
            inputs, outs)
        script = cryptolib.coins.mk_pubkey_script(self.address)

        # Finish tx
        # Sign each input
        self.leninputs = len(inputs)
        datahashes = []
        for i in range(self.leninputs):
            signing_tx = cryptolib.coins.signature_form(
                self.tx, i, script, cryptolib.coins.SIGHASH_ALL)
            datahashes.append(
                cryptolib.coins.bin_txhash(signing_tx,
                                           cryptolib.coins.SIGHASH_ALL))
        return datahashes
Exemplo n.º 3
0
 def prepare(self, toaddr, paymentvalue):
     """Build a transaction to be signed.
     value in lamport
     """
     maxspendable = self.getbalance() - FEE_LEVEL
     if paymentvalue > maxspendable or paymentvalue < 0:
         sym = "native SOL"
         raise NotEnoughTokens(f"Not enough {sym} tokens for the tx")
     transaction = gen_transfer_transaction(
         self.address, toaddr, paymentvalue, self.get_recentBlockHash()
     )
     return serialize_tx(transaction)
Exemplo n.º 4
0
 def selectutxos(self, amount, utxos):
     sorted_utxos = sorted(utxos, key=lambda x: x["value"], reverse=True)
     sel_utxos = []
     for sutxo in sorted_utxos:
         amount -= sutxo["value"]
         sel_utxos.append(sutxo)
         if 0 >= amount:
             break
     if 0 >= amount:
         return sel_utxos
     else:
         raise NotEnoughTokens("Not enough utxos values for the tx")
Exemplo n.º 5
0
    def prepare(self, toaddr, paymentvalue, fee):
        utxos = self.getutxos()
        balance = self.balance_fmutxos(utxos)
        maxspendable = balance - fee
        if paymentvalue > maxspendable or paymentvalue < 0:
            raise NotEnoughTokens("Not enough fund for the tx")
        inputs = self.selectutxos(paymentvalue + fee, utxos)
        invalue = self.balance_fmutxos(inputs)
        changevalue = invalue - paymentvalue - fee
        outs = [{"value": paymentvalue, "address": toaddr}]
        if toaddr.startswith("bc0") or toaddr.startswith("tb0"):
            outs[0]["segwit"] = True
        if toaddr.startswith("bc1") or toaddr.startswith("tb1"):
            outs[0]["new_segwit"] = True
        if changevalue > 0:
            outs.append({"value": changevalue, "address": self.address})
            if self.segwit == 1:
                outs[-1]["segwit"] = True
            if self.segwit == 2:
                outs[-1]["new_segwit"] = True
        self.tx = cryptolib.coins.bitcoin.Bitcoin(testnet=self.testnet).mktx(
            inputs, outs)
        if self.segwit == 0:
            script = cryptolib.coins.mk_pubkey_script(self.address)
        elif self.segwit == 2 or self.segwit == 1:
            script = cryptolib.coins.mk_p2wpkh_scriptcode(self.pubkey)
        else:
            raise Exception("Not valid segwit option")

        # Finish tx
        # Sign each input
        self.leninputs = len(inputs)
        datahashes = []
        for i in range(self.leninputs):
            signing_tx = cryptolib.coins.signature_form(
                self.tx, i, script, cryptolib.coins.SIGHASH_ALL)
            datahashes.append(
                cryptolib.coins.bin_txhash(signing_tx,
                                           cryptolib.coins.SIGHASH_ALL))
        return datahashes
Exemplo n.º 6
0
 def prepare(self, to_account, pay_value):
     bal_str = self.getbalance()
     balance = float(bal_str[0][:-4]) if len(bal_str) > 0 else 0.0
     if pay_value > balance or pay_value < 0:
         raise NotEnoughTokens("Not enough fund for the tx")
     if isinstance(pay_value, (int, float)):
         qty = "%.4f %s" % (pay_value, "EOS")
     else:
         raise Exception("pay_value must be int or float")
     args = {
         "from": self.account,
         "to": to_account,
         "quantity": qty,
         "memo": "",
     }
     payload = {
         "account": "eosio.token",
         "name": "transfer",
         "authorization": [{
             "actor": self.account,
             "permission": "active",
         }],
     }
     data = self.api.abi_json_to_bin(payload["account"], payload["name"],
                                     args)
     payload["data"] = data["binargs"]
     trx = {"actions": [payload]}
     trx["expiration"] = near_future_iso_str(60)
     chain_info, lib_info = self.api.get_chain_lib_info()
     trx["ref_block_num"] = chain_info[
         "last_irreversible_block_num"] & 0xFFFF
     trx["ref_block_prefix"] = lib_info["ref_block_prefix"]
     trx["max_net_usage_words"] = 0
     trx["max_cpu_usage_ms"] = 0
     trx["delay_sec"] = 0
     trx["context_free_actions"] = []
     datahash = compute_sig_hash(serialize_tx(trx), chain_info["chain_id"])
     return trx, datahash
Exemplo n.º 7
0
 def power_up(self, value):
     bal_str = self.getbalance()
     balance = float(bal_str[0][:-4]) if len(bal_str) > 0 else 0.0
     if (float(POWER_UP_PAYMENT) + value) > balance:
         raise NotEnoughTokens("Not enough fund for the tx and the powerup")
     args = {
         "payer": self.account,
         "receiver": self.account,
         "days": 1,
         "cpu_frac": 1000 * CPU_per_tx,
         "net_frac": 10000,
         "max_payment": f"{POWER_UP_PAYMENT} EOS",
     }
     payload = {
         "account": "eosio",
         "name": "powerup",
         "authorization": [{
             "actor": self.account,
             "permission": "active",
         }],
     }
     data = self.api.abi_json_to_bin(payload["account"], payload["name"],
                                     args)
     payload["data"] = data["binargs"]
     ptrx = {"actions": [payload]}
     ptrx["expiration"] = near_future_iso_str(60)
     chain_info, lib_info = self.api.get_chain_lib_info()
     ptrx["ref_block_num"] = chain_info[
         "last_irreversible_block_num"] & 0xFFFF
     ptrx["ref_block_prefix"] = lib_info["ref_block_prefix"]
     ptrx["max_net_usage_words"] = 0
     ptrx["max_cpu_usage_ms"] = 0
     ptrx["delay_sec"] = 0
     ptrx["context_free_actions"] = []
     pdatahash = compute_sig_hash(serialize_tx(ptrx),
                                  chain_info["chain_id"])
     return ptrx, pdatahash