<commit_before>package identity

import (
	"bytes"
	"crypto/rand"
	"encoding/binary"
	"encoding/hex"
	"errors"

	ed "github.com/FactomProject/ed25519"
	"github.com/FactomProject/factom"
	"github.com/FactomProject/factomd/common/primitives"
)

type BlockSigningKey struct {
	version          []byte
	message          []byte
	rootChainID      []byte
	newPubKey        []byte
	timestamp        []byte
	identityPreimage []byte
	signiture        []byte

	subchain string
}

func MakeBlockSigningKeySeeded(rootChainIDStr string, subchainID string, privateKey *[64]byte, signingkey *[64]byte) (*BlockSigningKey, []byte, error) {
	block, _, err := MakeBlockSigningKeyFixed(rootChainIDStr, subchainID, privateKey, false)
	if err != nil {
		return nil, nil, err
	}

	block.newPubKey = signingkey[:32]
	block.newPubKey = signingkey[:32]

	return block, signingkey[32:], nil
}

func MakeBlockSigningKey(rootChainIDStr string, subchainID string, privateKey *[64]byte) (*BlockSigningKey, []byte, error) {
	a, b, c := MakeBlockSigningKeyFixed(rootChainIDStr, subchainID, privateKey, false)
	return a, b, c
}

// Creates a new BlockSigningKey type. Used to change keys in identity chain
func MakeBlockSigningKeyFixed(rootChainIDStr string, subchainID string, privateKey *[64]byte, random bool) (*BlockSigningKey, []byte, error) {
	rootChainID, err := hex.DecodeString(rootChainIDStr)
	if err != nil {
		return nil, nil, err
	}
	if bytes.Compare(rootChainID[:ProofOfWorkLength], ProofOfWorkChainID[:ProofOfWorkLength]) != 0 {
		return nil, nil, errors.New("Error making a new block signing key: Root ChainID invalid")
	}
	b := new(BlockSigningKey)
	b.version = []byte{0x00}
	b.message = []byte("New Block Signing Key")
	b.rootChainID = rootChainID
	b.subchain = subchainID

	pub, priv, err := ed.GenerateKey(rand.Reader)
	if err != nil {
		return nil, nil, err
	}
	if !random {
		buf := new(bytes.Buffer)<commit_msg>Fixed signature on seeded new block signing key. Was not creating signature again<commit_after>package identity

import (
	"bytes"
	"crypto/rand"
	"encoding/binary"
	"encoding/hex"
	"errors"

	ed "github.com/FactomProject/ed25519"
	"github.com/FactomProject/factom"
	"github.com/FactomProject/factomd/common/primitives"
)

type BlockSigningKey struct {
	version          []byte
	message          []byte
	rootChainID      []byte
	newPubKey        []byte
	timestamp        []byte
	identityPreimage []byte
	signiture        []byte

	subchain string
}

func MakeBlockSigningKeySeeded(rootChainIDStr string, subchainID string, privateKey *[64]byte, signingkey *[64]byte) (*BlockSigningKey, []byte, error) {
	block, _, err := MakeBlockSigningKeyFixed(rootChainIDStr, subchainID, privateKey, false)
	if err != nil {
		return nil, nil, err
	}

	block.newPubKey = signingkey[32:]

	t := primitives.NewTimestampNow().GetTimeSeconds()
	by := make([]byte, 8)
	binary.BigEndian.PutUint64(by, uint64(t))
	block.timestamp = by

	preI := make([]byte, 0)
	preI = append(preI, []byte{0x01}...)
	preI = append(preI, privateKey[32:]...)
	block.identityPreimage = preI

	sig := ed.Sign(privateKey, block.versionToTimestamp())
	block.signiture = sig[:]

	return block, signingkey[:32], nil
}

func MakeBlockSigningKey(rootChainIDStr string, subchainID string, privateKey *[64]byte) (*BlockSigningKey, []byte, error) {
	a, b, c := MakeBlockSigningKeyFixed(rootChainIDStr, subchainID, privateKey, false)
	return a, b, c
}

// Creates a new BlockSigningKey type. Used to change keys in identity chain
func MakeBlockSigningKeyFixed(rootChainIDStr string, subchainID string, privateKey *[64]byte, random bool) (*BlockSigningKey, []byte, error) {
	rootChainID, err := hex.DecodeString(rootChainIDStr)
	if err != nil {
		return nil, nil, err
	}
	if bytes.Compare(rootChainID[:ProofOfWorkLength], ProofOfWorkChainID[:ProofOfWorkLength]) != 0 {
		return nil, nil, errors.New("Error making a new block signing key: Root ChainID invalid")
	}
	b := new(BlockSigningKey)
	b.version = []byte{0x00}
	b.message = []byte("New Block Signing Key")
	b.rootChainID = rootChainID
	b.subchain = subchainID

	pub, priv, err := ed.GenerateKey(rand.Reader)
	if err != nil {
		return nil, nil, err
	}
	if !random {
		buf := new(bytes.Buffer)
{'fraction_non_alphanumeric': 0.12216654736339776, 'fraction_numerical': 0.013123359580052493, 'mean_word_length': 4.734610123119015, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3487040', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1407, 'n_words': 362}