def send_misocoin(to_address: str, amount: int): global global_utxos # I know this is slow in recovering the utxos, but ceebs try: accumulated_amount = 0 send_amount = int(amount) vins: List[Vin] = [] vouts: List[Vout] = [Vout(to_address, send_amount)] # Check that the to_address is valid sha1 hash if len(to_address.encode('utf-8')) != 40: # hex lenght of sha1 hash return {'error': 'The destination address is not valid'} # Construct vins and vouts for txid in global_utxos: for index in global_utxos[txid]: if global_utxos[txid][index]['address'] == account_address: if global_utxos[txid][index]['spent'] is None: accumulated_amount += global_utxos[txid][index][ 'amount'] vins.append(Vin(txid, index)) if (accumulated_amount >= send_amount): break if (accumulated_amount >= send_amount): break if (accumulated_amount < send_amount): return { 'error': 'You\'re trying to send {} misocoin when you have {} misocoin'. format(send_amount, accumulated_amount) } # Send remaining to self if (accumulated_amount > send_amount): vouts.append( Vout(account_address, accumulated_amount - send_amount)) # Create tx object and sign it tx = Transaction(vins, vouts) for i in range(len(tx.vins)): tx = mutils.sign_tx(tx, i, account_priv_key) # Send raw tx and return the txid return send_raw_tx(json.dumps(tx.toJSON())) except Exception as e: return {'error': str(e)}
def send_raw_tx(tx: str): global global_best_block, global_txs, global_utxos try: # Original tx _tx_og = copy.deepcopy(tx) # Create new tx from the json dump tx = Transaction.fromJSON(json.loads(tx)) # If is new tx then add it to block if tx.txid not in global_txs: # Add tx to global best block global_best_block, global_txs, global_utxos = mutils.add_tx_to_block( tx, global_best_block, global_txs, global_utxos) print('[INFO] txid {} added to block {}'.format( tx.txid, global_best_block.height)) # Broadcast transaction to connected nodes for node in global_nodes: try: misocoin_cli('send_raw_tx', [json.dumps(tx.toJSON())], **node) except Exception as e: pass return {'txid': tx.txid} except Exception as e: return {'error': str(e)}
def sign_tx(tx: Transaction, idx: int, priv_key: str) -> Transaction: ''' Signs the transaction Params: tx: Transaction object to be signed idx: Index of the vin to sign priv_key: private key used to prove that you authorized it ''' # Make a copy so we don't overwrite the original object _tx = copy.deepcopy(tx) # When we're signing, null out all the vins # Except for ours when hashing vin = _tx.vins[idx] vouts = _tx.vouts txid = _tx.txid # Message for the signature is the hash of the transaction # with all vins nulled except for the one we're signing tx_hash = get_hash(vins=[vin], vouts=vouts, txids=[txid]) pub_key = get_pub_key(priv_key) signature = sign_msg(tx_hash, priv_key) vin.signature = signature vin.pub_key = pub_key # Rebuild vins vins = _tx.vins vins[idx] = vin return Transaction(vins, vouts)
def create_raw_tx(vins, vouts): try: vins = json.loads(vins) vouts = json.loads(vouts) return Transaction.fromJSON({'vins': vins, 'vouts': vouts}).toJSON() except Exception as e: return {'error': str(e)}
def sign_raw_tx(tx: str, idx: int, pk: str): try: idx = int(idx) tx = Transaction.fromJSON(json.loads(tx)) signed_tx = mutils.sign_tx(tx, idx, pk) return signed_tx.toJSON() except Exception as e: return {'error': str(e)}
def create_raw_tx(vins: List[Vin], vouts: List[Vout]) -> Transaction: ''' Creates a new transaction object given a list of ins and outs ''' return Transaction(vins, vouts)