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)