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 test_sign_transaction(): network_params = net_query('tppc') provider = Cryptoid(network='tppc') key = pa.Kutil( network='tppc', privkey=bytearray.fromhex( '9e321f5379c2d1c4327c12227e1226a7c2e08342d88431dcbb0063e1e715a36c') ) dest_address = pa.Kutil(network='tppc').address unspent = provider.select_inputs(key.address, 1) output = tx_output(network='tppc', value=Decimal(0.1), n=0, script=p2pkh_script(network='tppc', address=dest_address)) unsigned = MutableTransaction( version=1, ins=unspent['utxos'], outs=[output], locktime=Locktime(0), network=network_params, timestamp=int(time.time()), ) assert isinstance(sign_transaction(provider, unsigned, key), Transaction)
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 test_sign_transaction(): network_params = net_query('tppc') provider = Explorer(network='tppc') key = Kutil(network='tppc', privkey=bytearray.fromhex('9e321f5379c2d1c4327c12227e1226a7c2e08342d88431dcbb0063e1e715a36c') ) dest_address = 'mwn75Gavp6Y1tJxca53HeCj5zzERqWagr6' unspent = provider.select_inputs(key.address, 0.63) # 0.69 output = tx_output(network='tppc', value=Decimal(0.1), n=0, script=p2pkh_script(network='tppc', address=dest_address) ) unsigned = MutableTransaction( version=1, ins=unspent['utxos'], outs=[output], locktime=Locktime(0), network=network_params, timestamp=int(time.time()), ) parent_outputs = [find_parent_outputs(provider, i) for i in unsigned.ins] solver = P2pkhSolver(key._private_key) signed = unsigned.spend(parent_outputs, [solver for i in parent_outputs]) assert isinstance(signed, Transaction)
def test_tx_output(network): if network == 'peercoin': addr = 'PAdonateFczhZuKLkKHozrcyMJW7Y6TKvw' script = p2pkh_script(network, addr) txout = tx_output(network=network, value=Decimal(1.35), n=1, script=script) assert isinstance(txout, TxOut)
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 test_tx_output(network): if network == 'peercoin': addr = 'PAdonateFczhZuKLkKHozrcyMJW7Y6TKvw' if network == 'bitcoin': addr = '1FV9w4NvBnnNp4GMUNuqfzqGKvgBY5YTSB' script = p2pkh_script(network, addr) txout = tx_output(network=network, value=Decimal(1.35), n=1, script=script) assert isinstance(txout, TxOut)
def test_p2pkh_script(): addr = 'mvWDumZZZVD2nEC7hmsX8dMSHoGHAq5b6d' script = p2pkh_script('tppc', addr) assert isinstance(script, P2pkhScript)
def test_p2pkh_script(): addr = '1FV9w4NvBnnNp4GMUNuqfzqGKvgBY5YTSB' script = p2pkh_script('bitcoin', addr) assert isinstance(script, P2pkhScript)