anonymous No title
No License Python
2021年04月08日
Copy Clone
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
How to use:
Install monacoin core, edit monacoin.conf, run monacoind or monacoin-qt
Install python3
pip3 install python-bitcoinrpc
pip3 install --upgrade 'pycoin<0.70'

python3 op_return.py
nohup python3 -u op_return.py &> op.log &
"""

import codecs
import time
import datetime
import sys
import random
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

from os import system
from pycoin.tx import script, Tx
from pycoin.tx.tx_utils import sign_tx
from pycoin.tx.TxOut import TxOut, standard_tx_out_script
from binascii import hexlify

import json
import warnings
from pycoin.serialize import b2h, h2b, h2b_rev
from pycoin.tx import Spendable


with open('wording.txt', "rU") as f:
    data = [v.rstrip('\n') for v in f.readlines()]
ndl = len(data)

def spendables_for_address(monacoin_address):
    address = [monacoin_address]
    r = rpc_connection.listunspent(0, 999999, address)

    spendables = []
    for u in r:
        coin_value = u["amount"] * 100000000
        script = h2b(u["scriptPubKey"])
        previous_hash = h2b_rev(u["txid"])
        previous_index = u["vout"]
        spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
    return spendables


rpc_user="username"
rpc_password="password"
rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:9402"%(rpc_user, rpc_password), timeout=60)

txfee = 0.00001
ret = rpc_connection.settxfee(txfee)
print(datetime.datetime.now(), "settxfee:", txfee, ret)

monacoin_address = "MVrSaQDy6PHpRq6GuGs1uVtJFWfnA6HgAh"

n = 1
best1 = rpc_connection.getbestblockhash()

while True :

    best2 = rpc_connection.getbestblockhash()
    if best2 != best1:
        best1 = best2

        for i in range(1):

            raw_message = data[random.randint(0, ndl - 1)]
            message = hexlify(raw_message.encode()).decode('utf8')
            if(len(message) > 160):
                exit("Message must be 80 bytes or less")
            monacoin_fee = 202 + len(message)/2 + 1  #(satoshi)

            spendables = spendables_for_address(monacoin_address)
            monacoin_sum = sum(spendable.coin_value for spendable in spendables)
            print(n, monacoin_sum, raw_message)

            if(monacoin_sum < monacoin_fee):
                exit("Not enough satoshis to cover the fee. found: {sum} need: {fee}".format(sum=monacoin_sum,fee=monacoin_fee))

            inputs = [spendable.tx_in() for spendable in spendables]

            outputs = []
            if (monacoin_sum > monacoin_fee):
                change_output_script = standard_tx_out_script(monacoin_address)
                outputs.append(TxOut(monacoin_sum - monacoin_fee, change_output_script))

            op_return_output_script = script.tools.compile("OP_RETURN %s" % message)
            outputs.append(TxOut(0, op_return_output_script))

            tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
            tx.set_unspents(spendables)

            ret = rpc_connection.signrawtransactionwithwallet(tx.as_hex())
            ret = rpc_connection.sendrawtransaction(ret["hex"])
            print(ret)

            n += 1
            time.sleep(1)

    time.sleep(1)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
How to use:
Install monacoin core, edit monacoin.conf, run monacoind or monacoin-qt
Install python3
pip3 install python-bitcoinrpc
pip3 install --upgrade 'pycoin<0.70'

python3 op_return.py
nohup python3 -u op_return.py &> op.log &
"""

import codecs
import time
import datetime
import sys
import random
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

from os import system
from pycoin.tx import script, Tx
from pycoin.tx.tx_utils import sign_tx
from pycoin.tx.TxOut import TxOut, standard_tx_out_script
from binascii import hexlify

import json
import warnings
from pycoin.serialize import b2h, h2b, h2b_rev
from pycoin.tx import Spendable


with open('wording.txt', "rU") as f:
    data = [v.rstrip('\n') for v in f.readlines()]
ndl = len(data)

def spendables_for_address(monacoin_address):
    address = [monacoin_address]
    r = rpc_connection.listunspent(0, 999999, address)

    spendables = []
    for u in r:
        coin_value = u["amount"] * 100000000
        script = h2b(u["scriptPubKey"])
        previous_hash = h2b_rev(u["txid"])
        previous_index = u["vout"]
        spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
    return spendables


rpc_user="username"
rpc_password="password"
rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:9402"%(rpc_user, rpc_password), timeout=60)

txfee = 0.00001
ret = rpc_connection.settxfee(txfee)
print(datetime.datetime.now(), "settxfee:", txfee, ret)

monacoin_address = "MVrSaQDy6PHpRq6GuGs1uVtJFWfnA6HgAh"

n = 1
best1 = rpc_connection.getbestblockhash()

while True :

    best2 = rpc_connection.getbestblockhash()
    if best2 != best1:
        best1 = best2

        for i in range(1):

            raw_message = data[random.randint(0, ndl - 1)]
            message = hexlify(raw_message.encode()).decode('utf8')
            if(len(message) > 160):
                exit("Message must be 80 bytes or less")
            monacoin_fee = 202 + len(message)/2 + 1  #(satoshi)

            spendables = spendables_for_address(monacoin_address)
            monacoin_sum = sum(spendable.coin_value for spendable in spendables)
            print(n, monacoin_sum, raw_message)

            if(monacoin_sum < monacoin_fee):
                exit("Not enough satoshis to cover the fee. found: {sum} need: {fee}".format(sum=monacoin_sum,fee=monacoin_fee))

            inputs = [spendable.tx_in() for spendable in spendables]

            outputs = []
            if (monacoin_sum > monacoin_fee):
                change_output_script = standard_tx_out_script(monacoin_address)
                outputs.append(TxOut(monacoin_sum - monacoin_fee, change_output_script))

            op_return_output_script = script.tools.compile("OP_RETURN %s" % message)
            outputs.append(TxOut(0, op_return_output_script))

            tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
            tx.set_unspents(spendables)

            ret = rpc_connection.signrawtransactionwithwallet(tx.as_hex())
            ret = rpc_connection.sendrawtransaction(ret["hex"])
            print(ret)

            n += 1
            time.sleep(1)

    time.sleep(1)
anonymous
Anonymous
2021年04月14日
よさそう
Output