How to use the blspy.PrivateKey.from_seed function in blspy

To help you get started, we’ve selected a few blspy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Chia-Network / chia-blockchain / tests / block_tools.py View on Github external
from src.types.fees_target import FeesTarget
from src.types.full_block import FullBlock
from src.types.header import Header, HeaderData
from src.types.header_block import HeaderBlock
from src.types.proof_of_space import ProofOfSpace
from src.types.proof_of_time import ProofOfTime
from src.types.sized_bytes import bytes32
from src.util.errors import NoProofsOfSpaceFound
from src.util.ints import uint8, uint32, uint64

# Can't go much lower than 19, since plots start having no solutions
k: uint8 = uint8(19)
# Uses many plots for testing, in order to guarantee proofs of space at every height
num_plots = 80
# Use the empty string as the seed for the private key
pool_sk: PrivateKey = PrivateKey.from_seed(b"")
pool_pk: PublicKey = pool_sk.get_public_key()
plot_sks: List[PrivateKey] = [
    PrivateKey.from_seed(pn.to_bytes(4, "big")) for pn in range(num_plots)
]
plot_pks: List[PublicKey] = [sk.get_public_key() for sk in plot_sks]

farmer_sk: PrivateKey = PrivateKey.from_seed(b"coinbase")
coinbase_target = sha256(bytes(farmer_sk.get_public_key())).digest()
fee_target = sha256(bytes(farmer_sk.get_public_key())).digest()
n_wesolowski = uint8(3)


class BlockTools:
    """
    Tools to generate blocks for testing.
    """
github Chia-Network / chia-blockchain / tests / block_tools.py View on Github external
from src.util.errors import NoProofsOfSpaceFound
from src.util.ints import uint8, uint32, uint64

# Can't go much lower than 19, since plots start having no solutions
k: uint8 = uint8(19)
# Uses many plots for testing, in order to guarantee proofs of space at every height
num_plots = 80
# Use the empty string as the seed for the private key
pool_sk: PrivateKey = PrivateKey.from_seed(b"")
pool_pk: PublicKey = pool_sk.get_public_key()
plot_sks: List[PrivateKey] = [
    PrivateKey.from_seed(pn.to_bytes(4, "big")) for pn in range(num_plots)
]
plot_pks: List[PublicKey] = [sk.get_public_key() for sk in plot_sks]

farmer_sk: PrivateKey = PrivateKey.from_seed(b"coinbase")
coinbase_target = sha256(bytes(farmer_sk.get_public_key())).digest()
fee_target = sha256(bytes(farmer_sk.get_public_key())).digest()
n_wesolowski = uint8(3)


class BlockTools:
    """
    Tools to generate blocks for testing.
    """

    def __init__(self):
        plot_seeds: List[bytes32] = [
            ProofOfSpace.calculate_plot_seed(pool_pk, plot_pk) for plot_pk in plot_pks
        ]
        self.filenames: List[str] = [
            os.path.join(
github Chia-Network / chia-blockchain / tests / test_blockchain.py View on Github external
async def test_harvester_signature(self, initial_blockchain):
        blocks, b = initial_blockchain
        # Time too far in the past
        block_bad = FullBlock(
            HeaderBlock(
                blocks[9].header_block.proof_of_space,
                blocks[9].header_block.proof_of_time,
                blocks[9].header_block.challenge,
                Header(
                    blocks[9].header_block.header.data,
                    PrivateKey.from_seed(b"0").sign_prepend(b"random junk"),
                ),
            ),
            blocks[9].body,
        )
        assert (await b.receive_block(block_bad)) == ReceiveBlockResult.INVALID_BLOCK
github Chia-Network / chia-blockchain / src / scripts / regenerate_keys.py View on Github external
yn = input(f"The keys file {key_config_filename} already exists. Are you sure"
                   f" you want to override the keys? Plots might become invalid. (y/n): ")
        if not (yn.lower() == "y" or yn.lower() == "yes"):
            quit()
    else:
        # Create the file if if doesn't exist
        open(key_config_filename, "a").close()

    key_config = safe_load(open(key_config_filename, "r"))
    if key_config is None:
        key_config = {}

    if args.farmer:
        # Replaces the farmer's private key. The farmer target allows spending
        # of the fees.
        farmer_sk = PrivateKey.from_seed(token_bytes(32))
        farmer_target = sha256(bytes(farmer_sk.get_public_key())).digest()
        key_config["farmer_sk"] = bytes(farmer_sk).hex()
        key_config["farmer_target"] = farmer_target.hex()
        with open(key_config_filename, "w") as f:
            safe_dump(key_config, f)
    if args.harvester:
        # Replaces the harvester's sk seed. Used to generate plot private keys, which are
        # used to sign farmed blocks.
        key_config["sk_seed"] = token_bytes(32).hex()
        with open(key_config_filename, "w") as f:
            safe_dump(key_config, f)
    if args.pool:
        # Replaces the pools keys and targes. Only useful if running a pool, or doing
        # solo farming. The pool target allows spending of the coinbase.
        pool_sks = [PrivateKey.from_seed(token_bytes(32)) for _ in range(2)]
        pool_target = sha256(bytes(pool_sks[0].get_public_key())).digest()
github Chia-Network / chia-blockchain / src / full_node.py View on Github external
for head in heads:
                assert head.challenge
                if head.challenge.get_hash() == request.challenge_hash:
                    target_head = head
            if target_head is None:
                # TODO: should we still allow the farmer to farm?
                log.warning(
                    f"Challenge hash: {request.challenge_hash} not in one of three heads"
                )
                return

            # TODO: use mempool to grab best transactions, for the selected head
            transactions_generator: bytes32 = sha256(b"").digest()
            # TODO: calculate the fees of these transactions
            fees: FeesTarget = FeesTarget(request.fees_target_puzzle_hash, uint64(0))
            aggregate_sig: Signature = PrivateKey.from_seed(b"12345").sign(b"anything")
            # TODO: calculate aggregate signature based on transactions
            # TODO: calculate cost of all transactions
            cost = uint64(0)

            # Creates a block with transactions, coinbase, and fees
            body: Body = Body(
                request.coinbase,
                request.coinbase_signature,
                fees,
                aggregate_sig,
                transactions_generator,
                cost,
            )

            # Creates the block header
            prev_header_hash: bytes32 = target_head.header.get_hash()
github Chia-Network / chia-blockchain / src / scripts / regenerate_keys.py View on Github external
farmer_sk = PrivateKey.from_seed(token_bytes(32))
        farmer_target = sha256(bytes(farmer_sk.get_public_key())).digest()
        key_config["farmer_sk"] = bytes(farmer_sk).hex()
        key_config["farmer_target"] = farmer_target.hex()
        with open(key_config_filename, "w") as f:
            safe_dump(key_config, f)
    if args.harvester:
        # Replaces the harvester's sk seed. Used to generate plot private keys, which are
        # used to sign farmed blocks.
        key_config["sk_seed"] = token_bytes(32).hex()
        with open(key_config_filename, "w") as f:
            safe_dump(key_config, f)
    if args.pool:
        # Replaces the pools keys and targes. Only useful if running a pool, or doing
        # solo farming. The pool target allows spending of the coinbase.
        pool_sks = [PrivateKey.from_seed(token_bytes(32)) for _ in range(2)]
        pool_target = sha256(bytes(pool_sks[0].get_public_key())).digest()
        key_config["pool_sks"] = [bytes(pool_sk).hex() for pool_sk in pool_sks]
        key_config["pool_target"] = pool_target.hex()
        with open(key_config_filename, "w") as f:
            safe_dump(key_config, f)
github Chia-Network / chia-blockchain / src / protocol / client.py View on Github external
async def main():
    # Get a reference to the event loop as we plan to use
    # low-level APIs.
    loop = asyncio.get_running_loop()

    on_con_lost = loop.create_future()

    transport, protocol = await loop.create_connection(
        lambda: ChiaProtocol(on_con_lost, loop, lambda x: x),
        '127.0.0.1', 8888)

    ppk = PrivateKey.from_seed(b"123").get_public_key()
    await protocol.send("create_plot", CreatePlot(16, ppk, b"myplot_1.dat"))
    # protocol.send("create_plot", CreatePlot(17, ppk, b"myplot_2.dat"))
    await protocol.send("new_challenge", NewChallenge(bytes([77]*32)))

    # Wait until the protocol signals that the connection
    # is lost and close the transport.
    try:
        await on_con_lost
    finally:
        transport.close()
github Chia-Network / chia-blockchain / scripts / create_plots.py View on Github external
pool_pk: PublicKey
    if len(args.pool_pub_key) > 0:
        # Use the provided pool public key, useful for using an external pool
        pool_pk = PublicKey.from_bytes(bytes.fromhex(args.pool_pub_key))
    else:
        # Use the pool public key from the config, useful for solo farming
        pool_sk = PrivateKey.from_bytes(bytes.fromhex(key_config["pool_sks"][0]))
        pool_pk = pool_sk.get_public_key()

    print(
        f"Creating {args.num_plots} plots of size {args.size}, sk_seed {sk_seed.hex()} ppk {pool_pk}"
    )

    for i in range(args.num_plots):
        # Generate a sk based on the seed, plot size (k), and index
        sk: PrivateKey = PrivateKey.from_seed(
            sk_seed + args.size.to_bytes(1, "big") + i.to_bytes(4, "big")
        )

        # The plot seed is based on the pool and plot pks
        plot_seed: bytes32 = ProofOfSpace.calculate_plot_seed(
            pool_pk, sk.get_public_key()
        )
        filename: str = f"plot-{i}-{args.size}-{plot_seed}.dat"
        full_path: str = os.path.join(plot_root, filename)
        if os.path.isfile(full_path):
            print(f"Plot {filename} already exists")
        else:
            # Creates the plot. This will take a long time for larger plots.
            plotter: DiskPlotter = DiskPlotter()
            plotter.create_plot_disk(full_path, args.size, bytes([]), plot_seed)
github Chia-Network / chia-blockchain / src / util / genesis_block.py View on Github external
from src.types.challenge import Challenge
from src.types.block_header import BlockHeader, BlockHeaderData
from src.types.proof_of_space import ProofOfSpace
from src.types.proof_of_time import ProofOfTime, ProofOfTimeOutput
from src.types.classgroup import ClassgroupElement
from src.consensus import constants, pot_iterations, block_rewards
from src.util.ints import uint64, uint32, uint8
from src.types.coinbase import CoinbaseInfo
from src.types.fees_target import FeesTarget
from lib.chiavdf.inkfish.create_discriminant import create_discriminant
from lib.chiavdf.inkfish.classgroup import ClassGroup
from lib.chiavdf.inkfish.proof_of_time import create_proof_of_time_nwesolowski


# Use the empty string as the seed for the private key
sk: PrivateKey = PrivateKey.from_seed(b'')
pool_pk = sk.get_public_key()
plot_pk = sk.get_public_key()
coinbase_target = sha256(sk.get_public_key().serialize()).digest()
fee_target = sha256(sk.get_public_key().serialize()).digest()
k = 19
n_wesolowski = 3

genesis_block_hardcoded = b'\x15N3\xd3\xf9H\xc2K\x96\xfe\xf2f\xa2\xbf\x87\x0e\x0f,\xd0\xd4\x0f6s\xb1".\\\xf5\x8a\xb4\x03\x84\x8e\xf9\xbb\xa1\xca\xdef3:\xe4?\x0c\xe5\xc6\x12\x80\x15N3\xd3\xf9H\xc2K\x96\xfe\xf2f\xa2\xbf\x87\x0e\x0f,\xd0\xd4\x0f6s\xb1".\\\xf5\x8a\xb4\x03\x84\x8e\xf9\xbb\xa1\xca\xdef3:\xe4?\x0c\xe5\xc6\x12\x80\x13\x00\x00\x00\x98\xf9\xeb\x86\x90Kj\x01\x1cZk_\xe1\x9c\x03;Z\xb9V\xe2\xe8\xa5\xc8\n\x0c\xbbU\xa6\xc5\xc5\xbcH\xa3\xb3fd\xcd\xb8\x83\t\xa9\x97\x96\xb5\x91G \xb2\x9e\x05\\\x91\xe1<\xee\xb1\x06\xc3\x18~XuI\xc8\x8a\xb5b\xd7.7\x96Ej\xf3DThs\x18s\xa5\xd4C\x1ea\xfd\xd5\xcf\xb9o\x18\xea6n\xe22*\xb0]%\x15\xd0i\x83\xcb\x9a\xa2.+\x0f1\xcd\x03Z\xf3]\'\xbf|\x8b\xa6\xbcF\x10\xe8Q\x19\xaeZ~\xe5\x1f\xf1)\xa3\xfb\x82\x1a\xb8\x12\xce\x19\xc8\xde\xb9n\x08[\xef\xfd\xf9\x0c\xec\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\'\x8c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07~\xccf\x88\xef\xc8z;\xc9\x99\xdaSO\xa2\xdbC\x84\xe1\x9d\xc9Iv\xdbH\xb4\x9fiI\x1ew\xa78Gu\xe0\x9bg\xfdtBU\xfa\xe8\x9f\x13A\xb76iVx\xadU~\x8bj^\xeaV\xfd@\xdf,\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\xedVsY\xdf\xa18.\x050\x90\x9c\xb3\xed\xaa\xb0Kv\x81{\x9a\xce=\xed\xc2\xc9m/\xce\x9b[\x04M\xbb\xe8\xdeCNg\xb6\xee\x01\x8e{\x8dEk\xecHt\x8d\xab\xbb\x19\x91\xfa\x1aT\xb4\xf871\xbc\x1b\x95~\xd5|\xc3\xb8\x84oG\xf2\xe2\x93b,\xdf\x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xbaQ\x00\x12\xfd\x1aQ\xbe#t\x14\x1cF\xa0k\xdc\x08\x9b0\xe1>\x14\xf6\xc2.\xd8jp\xa6\xf4\xe7\xc9.o\xd1\x02\x1d\xd9\n\x1f\xaa\x9b\xc00_zF\x8f\xac\xbb\xe9\x9b`\x8c\xdd\xcb\xb0[=\x07\xe0\xc3\x10\xcehG\x89<\x0b\x08)\xd1\xe8\x99z9\xed\x08YJ6\x185\xd1\xbf9e&4\xb0\x18\xb7\x93\xfb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x002\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00]e\xe8\xd1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00~[u\x1f\x81\x7f\x0c)\x05\xe6\xfd\xe5\xd14\\a\n\xc6I\xccJ\x0cXk\xcf,Z\x1c\xdb>\xe0\xc3z!\xc9N\xd5\x03\x8b^\xd9\xe6\xc7I\xba\xb1\x0fm\xd4\xa0=\xb6^s\x94_f\xb5\xc1\\n\xfe\xf9\xd2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00A\xd0\xe6k\xb8 FullBlock:
    plot_seed: bytes32 = ProofOfSpace.calculate_plot_seed(pool_pk, plot_pk)
    filename: str = "genesis-plot-" + token_hex(10)

    plotter = DiskPlotter()
    try: