<commit_before>#!/usr/bin/env python2
# coding=utf-8

from __future__ import print_function, absolute_import
import argparse
import binascii
import random
import string
from pathlib import Path
from transaction_pb2 import Transaction, SignedTransaction, UnverifiedTransaction, Crypto
from util import hex2bytes, run_command, remove_hex_0x, recover_pub
from secp256k1 import PrivateKey
from ethereum.utils import sha3
import pysodium
from generate_account import generate
from block_number import block_number
from url_util import endpoint
from jsonrpcclient.http_client import HTTPClient
from log import logger

accounts_path = Path("../output/transaction")
if not accounts_path.is_dir():
    command = 'mkdir -p ../output/transaction'.split()
    for line in run_command(command):
        logger.debug(line)


def save_deploy(code):
    with open("../output/transaction/deploycode", "w+") as deployfile:
        deployfile.write(code)


def bin_code():
    with open("../output/compiled/bytecode", 'r') as binfile:
        bytecode = binfile.read()
        return bytecode


def private_key():
    with open("../output/accounts/privkey", 'r') as privfile:
        privkey = privfile.read()
        return privkey


def get_sender(privkey=None, newcrypto=False):
    generate(privkey, newcrypto)
    return _sender_from_file()


def _sender_from_file():
    with open("../output/accounts/address", 'r') as addressfile:
        address = addressfile.read()
        return address

def get_nonce(size=6):
    """Get a random string."""
    return (''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(size)))


def get_chainid():
    params = ['latest']
    chainid = 0

    try:
        url = endpoint()
        logger.debug(url)
        response = HTTPClient(url).request("cita_getMetaData", params)
        chainid = response['chainId']
        logger.debug(response)
    except:
        chainid = 0

    logger.debug("final chainId is {}".format(chainid))
    return chainid


def generate_deploy_data(current_height,
                         bytecode,
                         privatekey,
                         receiver=None,
                         newcrypto=False,
                         version=0):
    if newcrypto:
        data = _blake2b_ed25519_deploy_data(current_height, bytecode,
                                            privatekey, version, receiver)
    else:
        data = _sha3_secp256k1_deploy_data(current_height, bytecode,
                                           privatekey, version, receiver)

    return data


def _blake2b_ed25519_deploy_data(current_height,
                                 bytecode,
                                 privatekey,
                                 version=0,
                                 receiver=None):
    sender = get_sender(private_key, True)
    logger.debug(sender)
    nonce = get_nonce()
    logger.debug("nonce is {}".format(nonce))
    chainid = get_chainid()
    logger.debug("chainid is {}".format(chainid))

    tx = Transaction()
    tx.valid_until_block = current_height + 88
    tx.nonce = nonce
    tx.chain_id = chainid
    tx.version = version
    if receiver is not None:
        tx.to = receiver
    tx.data = hex2bytes(bytecode)

    message = _blake2b(tx.SerializeToString())
    logger.debug("blake2b msg")
    sig = pysodium.crypto_sign_detached(message, hex2bytes(privatekey))
    logger.debug("sig {}".format(binascii.b2a_hex(sig)))

    pubkey = pysodium.crypto_sign_sk_to_pk(hex2bytes(privatekey))
    logger.debug("pubkey is {}".format(binascii.b2a_hex(pubkey)))
    signature = binascii.hexlify(sig[:]) + binascii.hexlify(pubkey[:])
    logger.debug("signature is {}".format(signature))

    unverify_tx = UnverifiedTransaction()
    unverify_tx.transaction.CopyFrom(tx)
    unverify_tx.signature = hex2bytes(signature)
    unverify_tx.crypto = Crypto.Value('SECP')

    logger.info("unverify_tx is {}".format(
        binascii.hexlify(unverify_tx.SerializeToString())))
    return binascii.hexlify(unverify_tx.SerializeToString())


def _sha3_secp256k1_deploy_data(current_height,
                                bytecode,
                                privatekey,
                                version=0,
                                receiver=None):
    sender = get_sender(privatekey, False)
    if privatekey is None:
        temp = private_key()
        privkey = PrivateKey(hex2bytes(temp))
    else:
        privkey = PrivateKey(hex2bytes(privatekey))

    logger.debug(sender)
    nonce = get_nonce()
    logger.debug("nonce is {}".format(nonce))
    chainid = get_chainid()
    logger.debug("chainid is {}".format(chainid))

    tx = Transaction()
    tx.valid_until_block = current_height + 88
    tx.nonce = nonce
    tx.chain_id = chainid
    tx.quota = 9999999
    tx.version = version
    if receiver is not None:
        tx.to = receiver
    tx.data = hex2bytes(bytecode)

    message = sha3(tx.SerializeToString())

    logger.debug("hash message: {}")
    sign_recover = privkey.ecdsa_sign_recoverable(message, raw=True)
    sig = privkey.ecdsa_recoverable_serialize(sign_recover)

    signature = binascii.hexlify(sig[0]) + binascii.hexlify(
        bytes(bytearray([sig[1]])))<commit_msg>set quota smaller<commit_after>#!/usr/bin/env python2
# coding=utf-8

from __future__ import print_function, absolute_import
import argparse
import binascii
import random
import string
from pathlib import Path
from transaction_pb2 import Transaction, SignedTransaction, UnverifiedTransaction, Crypto
from util import hex2bytes, run_command, remove_hex_0x, recover_pub
from secp256k1 import PrivateKey
from ethereum.utils import sha3
import pysodium
from generate_account import generate
from block_number import block_number
from url_util import endpoint
from jsonrpcclient.http_client import HTTPClient
from log import logger

accounts_path = Path("../output/transaction")
if not accounts_path.is_dir():
    command = 'mkdir -p ../output/transaction'.split()
    for line in run_command(command):
        logger.debug(line)


def save_deploy(code):
    with open("../output/transaction/deploycode", "w+") as deployfile:
        deployfile.write(code)


def bin_code():
    with open("../output/compiled/bytecode", 'r') as binfile:
        bytecode = binfile.read()
        return bytecode


def private_key():
    with open("../output/accounts/privkey", 'r') as privfile:
        privkey = privfile.read()
        return privkey


def get_sender(privkey=None, newcrypto=False):
    generate(privkey, newcrypto)
    return _sender_from_file()


def _sender_from_file():
    with open("../output/accounts/address", 'r') as addressfile:
        address = addressfile.read()
        return address

def get_nonce(size=6):
    """Get a random string."""
    return (''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(size)))


def get_chainid():
    params = ['latest']
    chainid = 0

    try:
        url = endpoint()
        logger.debug(url)
        response = HTTPClient(url).request("cita_getMetaData", params)
        chainid = response['chainId']
        logger.debug(response)
    except:
        chainid = 0

    logger.debug("final chainId is {}".format(chainid))
    return chainid


def generate_deploy_data(current_height,
                         bytecode,
                         privatekey,
                         receiver=None,
                         newcrypto=False,
                         version=0):
    if newcrypto:
        data = _blake2b_ed25519_deploy_data(current_height, bytecode,
                                            privatekey, version, receiver)
    else:
        data = _sha3_secp256k1_deploy_data(current_height, bytecode,
                                           privatekey, version, receiver)

    return data


def _blake2b_ed25519_deploy_data(current_height,
                                 bytecode,
                                 privatekey,
                                 version=0,
                                 receiver=None):
    sender = get_sender(private_key, True)
    logger.debug(sender)
    nonce = get_nonce()
    logger.debug("nonce is {}".format(nonce))
    chainid = get_chainid()
    logger.debug("chainid is {}".format(chainid))

    tx = Transaction()
    tx.valid_until_block = current_height + 88
    tx.nonce = nonce
    tx.chain_id = chainid
    tx.version = version
    if receiver is not None:
        tx.to = receiver
    tx.data = hex2bytes(bytecode)

    message = _blake2b(tx.SerializeToString())
    logger.debug("blake2b msg")
    sig = pysodium.crypto_sign_detached(message, hex2bytes(privatekey))
    logger.debug("sig {}".format(binascii.b2a_hex(sig)))

    pubkey = pysodium.crypto_sign_sk_to_pk(hex2bytes(privatekey))
    logger.debug("pubkey is {}".format(binascii.b2a_hex(pubkey)))
    signature = binascii.hexlify(sig[:]) + binascii.hexlify(pubkey[:])
    logger.debug("signature is {}".format(signature))

    unverify_tx = UnverifiedTransaction()
    unverify_tx.transaction.CopyFrom(tx)
    unverify_tx.signature = hex2bytes(signature)
    unverify_tx.crypto = Crypto.Value('SECP')

    logger.info("unverify_tx is {}".format(
        binascii.hexlify(unverify_tx.SerializeToString())))
    return binascii.hexlify(unverify_tx.SerializeToString())


def _sha3_secp256k1_deploy_data(current_height,
                                bytecode,
                                privatekey,
                                version=0,
                                receiver=None):
    sender = get_sender(privatekey, False)
    if privatekey is None:
        temp = private_key()
        privkey = PrivateKey(hex2bytes(temp))
    else:
        privkey = PrivateKey(hex2bytes(privatekey))

    logger.debug(sender)
    nonce = get_nonce()
    logger.debug("nonce is {}".format(nonce))
    chainid = get_chainid()
    logger.debug("chainid is {}".format(chainid))

    tx = Transaction()
    tx.valid_until_block = current_height + 88
    tx.nonce = nonce
    tx.chain_id = chainid
    tx.quota = 1000000
    tx.version = version
    if receiver is not None:
        tx.to = receiver
    tx.data = hex2bytes(bytecode)

    message = sha3(tx.SerializeToString())

    logger.debug("hash message: {}")
    sign_recover = privkey.ecdsa_sign_recoverable(message, raw=True)
    sig = privkey.ecdsa_recoverable_serialize(sign_recover)

    signature = binascii.hexlify(sig[0]) + binascii.hexlify(
        bytes(bytearray([sig[1]])))
{'fraction_non_alphanumeric': 0.10185008582872401, 'fraction_numerical': 0.012016021361815754, 'mean_word_length': 2.6249567922571724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7199162', 'n_tokens_mistral': 3234, 'n_tokens_neox': 3092, 'n_words': 653}