def test_make_raw_transaction(): tx = make_raw_transaction("bitcoin", [], [], Locktime(0)) assert isinstance(tx, MutableTransaction) tx = make_raw_transaction("peercoin", [], [], Locktime(300000)) assert isinstance(tx, MutableTransaction)
def vote_init(vote: Vote, inputs: dict, change_address: str) -> bytes: '''initialize vote transaction, must be signed by the deck_issuer privkey''' network_params = net_query(vote.deck.network) deck_vote_tag_address = deck_vote_tag(vote.deck) tx_fee = network_params.min_tx_fee # settle for min tx fee for now for utxo in inputs['utxos']: utxo['txid'] = unhexlify(utxo['txid']) utxo['scriptSig'] = unhexlify(utxo['scriptSig']) outputs = [{ "redeem": 0.01, "outputScript": transactions.monosig_script(deck_vote_tag_address) }, { "redeem": 0, "outputScript": transactions.op_return_script(vote.to_protobuf) }, { "redeem": float(inputs['total']) - float(tx_fee) - float(0.01), "outputScript": transactions.monosig_script(change_address) }] return transactions.make_raw_transaction(inputs['utxos'], outputs)
def opreturn(self, string: hex, locktime: int = 0) -> str: '''send op_return transaction''' network_params = net_query(Settings.network) inputs = provider.select_inputs(Settings.key.address, 0.01) outs = [ tx_output(network=provider.network, value=Decimal(0), n=1, script=nulldata_script(bytes.fromhex(string))) ] # first round of txn making is done by presuming minimal fee change_sum = Decimal(inputs['total'] - network_params.min_tx_fee) outs.append( tx_output(network=provider.network, value=change_sum, n=len(outs) + 1, script=p2pkh_script(address=Settings.key.address, network=provider.network))) unsigned_tx = make_raw_transaction(network=provider.network, inputs=inputs['utxos'], outputs=outs, locktime=Locktime(locktime)) signedtx = sign_transaction(provider, unsigned_tx, Settings.key) return sendtx(signedtx)
def card_transfer(provider: Provider, card: CardTransfer, inputs: dict, change_address: str, locktime: int = 0) -> Transaction: '''Prepare the CardTransfer Transaction object : card - CardTransfer object : inputs - utxos (has to be owned by deck issuer) : change_address - address to send the change to : locktime - tx locked until block n=int ''' network_params = net_query(provider.network) pa_params = param_query(provider.network) if card.deck_p2th is None: raise Exception("card.deck_p2th required for tx_output") outs = [ tx_output(network=provider.network, value=pa_params.P2TH_fee, n=0, script=p2pkh_script(address=card.deck_p2th, network=provider.network)), # deck p2th tx_output(network=provider.network, value=Decimal(0), n=1, script=nulldata_script( card.metainfo_to_protobuf)) # op_return ] for addr, index in zip(card.receiver, range(len(card.receiver))): outs.append( # TxOut for each receiver, index + 2 because we have two outs already tx_output(network=provider.network, value=Decimal(0), n=index + 2, script=p2pkh_script(address=addr, network=provider.network))) # first round of txn making is done by presuming minimal fee change_sum = Decimal(inputs['total'] - network_params.min_tx_fee - pa_params.P2TH_fee) outs.append( tx_output(network=provider.network, value=change_sum, n=len(outs) + 1, script=p2pkh_script(address=change_address, network=provider.network))) unsigned_tx = make_raw_transaction(network=provider.network, inputs=inputs['utxos'], outputs=outs, locktime=Locktime(locktime)) return unsigned_tx
def deck_spawn(provider: Provider, deck: Deck, inputs: dict, change_address: str, locktime: int = 0) -> Transaction: '''Creates Deck spawn raw transaction. : key - Kutil object which we'll use to sign the tx : deck - Deck object : card - CardTransfer object : inputs - utxos (has to be owned by deck issuer) : change_address - address to send the change to : locktime - tx locked until block n=int ''' network_params = net_query(deck.network) pa_params = param_query(deck.network) if deck.production: p2th_addr = pa_params.P2TH_addr else: p2th_addr = pa_params.test_P2TH_addr # first round of txn making is done by presuming minimal fee change_sum = Decimal(inputs['total'] - network_params.min_tx_fee - pa_params.P2TH_fee) txouts = [ tx_output(network=deck.network, value=pa_params.P2TH_fee, n=0, script=p2pkh_script(address=p2th_addr, network=deck.network)), # p2th tx_output(network=deck.network, value=Decimal(0), n=1, script=nulldata_script( deck.metainfo_to_protobuf)), # op_return tx_output(network=deck.network, value=change_sum, n=2, script=p2pkh_script(address=change_address, network=deck.network)) # change ] unsigned_tx = make_raw_transaction(network=deck.network, inputs=inputs['utxos'], outputs=txouts, locktime=Locktime(locktime)) return unsigned_tx
def sendto(self, address: Union[str], amount: Union[float], locktime: int = 0) -> str: '''send coins to address''' if not len(address) == amount: raise RecieverAmountMismatch network_params = net_query(Settings.network) inputs = provider.select_inputs(Settings.key.address, sum(amount)) outs = [] for addr, index, amount in zip(address, range(len(address)), amount): outs.append( tx_output(network=Settings.network, value=Decimal(amount), n=index, script=p2pkh_script(address=addr, network=Settings.network))) # first round of txn making is done by presuming minimal fee change_sum = Decimal(inputs['total'] - network_params.min_tx_fee) outs.append( tx_output(network=provider.network, value=change_sum, n=len(outs) + 1, script=p2pkh_script(address=Settings.key.address, network=provider.network))) unsigned_tx = make_raw_transaction(network=provider.network, inputs=inputs['utxos'], outputs=outs, locktime=Locktime(locktime)) signedtx = sign_transaction(provider, unsigned_tx, Settings.key) return sendtx(signedtx)
def vote_cast(vote: Vote, choice_index: int, inputs: dict, change_address: str) -> bytes: '''vote cast transaction''' network_params = net_query(vote.deck.network) vote_cast_addr = vote.vote_choice_address[choice_index] tx_fee = network_params.min_tx_fee # settle for min tx fee for now for utxo in inputs['utxos']: utxo['txid'] = unhexlify(utxo['txid']) utxo['scriptSig'] = unhexlify(utxo['scriptSig']) outputs = [{ "redeem": 0.01, "outputScript": transactions.monosig_script(vote_cast_addr) }, { "redeem": float(inputs['total']) - float(tx_fee) - float(0.01), "outputScript": transactions.monosig_script(change_address) }] return transactions.make_raw_transaction(inputs['utxos'], outputs)