|
5 | 5 | from pypeerassets.networks import net_query |
6 | 6 | from pypeerassets.transactions import (tx_output, |
7 | 7 | p2pkh_script, |
| 8 | + nulldata_script, |
8 | 9 | make_raw_transaction, |
9 | 10 | Locktime) |
10 | 11 |
|
@@ -55,3 +56,36 @@ def sendto(self, address: Union[str], amount: Union[float], |
55 | 56 | signedtx = sign_transaction(provider, unsigned_tx, Settings.key) |
56 | 57 |
|
57 | 58 | return sendtx(signedtx) |
| 59 | + |
| 60 | + def opreturn(self, string: hex, locktime: int=0) -> str: |
| 61 | + '''send op_return transaction''' |
| 62 | + |
| 63 | + network_params = net_query(Settings.network) |
| 64 | + |
| 65 | + inputs = provider.select_inputs(Settings.key.address, 0.01) |
| 66 | + |
| 67 | + outs = [tx_output(network=provider.network, |
| 68 | + value=Decimal(0), n=1, |
| 69 | + script=nulldata_script(bytes.fromhex(string)) |
| 70 | + ) |
| 71 | + ] |
| 72 | + |
| 73 | + # first round of txn making is done by presuming minimal fee |
| 74 | + change_sum = Decimal(inputs['total'] - network_params.min_tx_fee) |
| 75 | + |
| 76 | + outs.append( |
| 77 | + tx_output(network=provider.network, |
| 78 | + value=change_sum, n=len(outs)+1, |
| 79 | + script=p2pkh_script(address=Settings.key.address, |
| 80 | + network=provider.network)) |
| 81 | + ) |
| 82 | + |
| 83 | + unsigned_tx = make_raw_transaction(network=provider.network, |
| 84 | + inputs=inputs['utxos'], |
| 85 | + outputs=outs, |
| 86 | + locktime=Locktime(locktime) |
| 87 | + ) |
| 88 | + |
| 89 | + signedtx = sign_transaction(provider, unsigned_tx, Settings.key) |
| 90 | + |
| 91 | + return sendtx(signedtx) |
0 commit comments