<commit_before>import {
  pubKeyfromPrivKey,
  publicKeyToString,
  StacksPublicKey,
  makeRandomPrivKey,
  createStacksPrivateKey,
  getPublicKey,
  privateKeyToString,
  getAddressFromPrivateKey,
  getAddressFromPublicKey,
} from '../src/keys';

import { serializeDeserialize } from './macros';
import { StacksMessageType, TransactionVersion } from '../src/constants';

test('Stacks public key and private keys', () => {
  const privKeyString = 'edf9aee84d9b7abc145504dde6726c64f369d37ee34ded868fabd876c26570bc';
  const pubKeyString =
    '<KEY>' +
    '<KEY>';
  const pubKey = pubKeyfromPrivKey(privKeyString);
  expect(publicKeyToString(pubKey)).toBe(pubKeyString);

  const deserialized = serializeDeserialize(pubKey, StacksMessageType.PublicKey) as StacksPublicKey;
  expect(publicKeyToString(deserialized)).toBe(pubKeyString);

  const privKey = createStacksPrivateKey(privKeyString);
  expect(publicKeyToString(getPublicKey(privKey))).toBe(pubKeyString);

  const randomKey = makeRandomPrivKey();
  expect(privateKeyToString(randomKey).length).toEqual(64);

  expect(getAddressFromPrivateKey(privKeyString)).toBe('<KEY>');
  expect(getAddressFromPrivateKey(Buffer.from(privKeyString, 'hex'))).toBe(
    'SPZG6BAY4JVR9RNAB1HY92B<KEY>'
  );

  expect(getAddressFromPrivateKey(privKeyString, TransactionVersion.Testnet)).toBe(
    'STZG6BAY4JVR9RNAB1HY92B<KEY>'
  );
  expect(
    getAddressFromPrivateKey(Buffer.from(privKeyString, 'hex'), TransactionVersion.Testnet)
  ).toBe('STZG6BAY4JVR9RNAB1HY92B7Q208ZYY4HZG8ZXFM');

  expect(getAddressFromPublicKey(pubKeyString)).toBe('<KEY>');
  expect(getAddressFromPublicKey(Buffer.from(pubKeyString, 'hex'))).toBe(
    '<KEY>'
  );

  expect(getAddressFromPublicKey(pubKeyString, TransactionVersion.Testnet)).toBe(
    '<KEY>'
  );
  expect(
    getAddressFromPublicKey(Buffer.from(pubKeyString, 'hex'), TransactionVersion.Testnet)
  ).toBe('<KEY>');
});
<commit_msg>test: public key from signature<commit_after>import {
  compressPublicKey,
  createStacksPrivateKey,
  getAddressFromPrivateKey,
  getAddressFromPublicKey,
  getPublicKey,
  makeRandomPrivKey,
  privateKeyToString,
  pubKeyfromPrivKey,
  publicKeyFromSignature,
  publicKeyToString,
  signWithKey,
  StacksPublicKey,
  PubKeyEncoding,
  StacksMessageType,
  TransactionVersion
} from '../src';

import { serializeDeserialize } from './macros';

test('Stacks public key and private keys', () => {
  const privKeyString = 'edf9aee84d9b7abc145504dde6726c64f369d37ee34ded868fabd876c26570bc';
  const pubKeyString =
    '<KEY>' +
    '<KEY>';
  const pubKey = pubKeyfromPrivKey(privKeyString);
  expect(publicKeyToString(pubKey)).toBe(pubKeyString);

  const deserialized = serializeDeserialize(pubKey, StacksMessageType.PublicKey) as StacksPublicKey;
  expect(publicKeyToString(deserialized)).toBe(pubKeyString);

  const privKey = createStacksPrivateKey(privKeyString);
  expect(publicKeyToString(getPublicKey(privKey))).toBe(pubKeyString);

  const randomKey = makeRandomPrivKey();
  expect(privateKeyToString(randomKey).length).toEqual(64);

  expect(getAddressFromPrivateKey(privKeyString)).toBe('<KEY>');
  expect(getAddressFromPrivateKey(Buffer.from(privKeyString, 'hex'))).toBe(
    '<KEY>'
  );

  expect(getAddressFromPrivateKey(privKeyString, TransactionVersion.Testnet)).toBe(
    '<KEY>'
  );
  expect(
    getAddressFromPrivateKey(Buffer.from(privKeyString, 'hex'), TransactionVersion.Testnet)
  ).toBe('<KEY>');

  expect(getAddressFromPublicKey(pubKeyString)).toBe('<KEY>');
  expect(getAddressFromPublicKey(Buffer.from(pubKeyString, 'hex'))).toBe(
    'SPZG6BAY4JVR9RNAB1HY92B7Q208ZYY4HZEA9PX5'
  );

  expect(getAddressFromPublicKey(pubKeyString, TransactionVersion.Testnet)).toBe(
    'STZG6BAY4JVR9RNAB1HY92B7Q208ZYY4HZG8ZXFM'
  );
  expect(
    getAddressFromPublicKey(Buffer.from(pubKeyString, 'hex'), TransactionVersion.Testnet)
  ).toBe('ST<KEY>');

  const compressedPubKey = compressPublicKey(pubKey.data).data.toString('hex');
  expect(compressedPubKey).toBe('<KEY>');
});

test('Retrieve public key from signature', () => {
  const privKey = createStacksPrivateKey('edf9aee84d9b7abc145504dde6726c64f369d37ee34ded868fabd876c26570bc');
  const uncompressedPubKey =
    '<KEY>';
  const compressedPubKey = '<KEY>';

  const message = 'hello world';
  const sig = signWithKey(privKey, message);

  const uncompressedPubKeyFromSig = publicKeyFromSignature(message, sig, PubKeyEncoding.Uncompressed)
  const compressedPubKeyFromSig = publicKeyFromSignature(message, sig, PubKeyEncoding.Compressed)

  expect(uncompressedPubKeyFromSig).toBe(uncompressedPubKey);
  expect(compressedPubKeyFromSig).toBe(compressedPubKey);
})

{'fraction_non_alphanumeric': 0.1210560206052801, 'fraction_numerical': 0.03391285683623095, 'mean_word_length': 7.118466898954704, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, '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': '3211822', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1535, 'n_words': 197}