import { createPublicClient, http, verifyMessage } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { publicKeyToAddress } from '../utils/index.js';
export const getAccountProofMessage = (accountAddress, publicKey) => {
  return `This message proves I am the owner of the account ${accountAddress} and the public key ${publicKey}`;
};
export const getKeyProofMessage = (accountAddress, publicKey) => {
  return `The public key ${publicKey} is owned by the account ${accountAddress}`;
};
export const accountProofDomain = {
  name: 'Hypergraph',
  version: '1'
};
export const proveIdentityOwnership = async (smartAccountClient, accountAddress, keys) => {
  if (!smartAccountClient.account) {
    throw new Error('Smart account client does not have an account');
  }
  if (!smartAccountClient.chain) {
    throw new Error('Smart account client does not have a chain');
  }
  const publicKey = keys.signaturePublicKey;
  const keyProofMessage = getKeyProofMessage(accountAddress, publicKey);
  const accountProof = await smartAccountClient.account.signTypedData({
    message: {
      message: getAccountProofMessage(accountAddress, publicKey)
    },
    types: {
      Message: [{
        name: 'message',
        type: 'string'
      }]
    },
    domain: accountProofDomain,
    primaryType: 'Message'
  });
  console.log('accountProof', accountProof);
  const account = privateKeyToAccount(keys.signaturePrivateKey);
  const keyProof = await account.signMessage({
    message: keyProofMessage
  });
  return {
    accountProof,
    keyProof
  };
};
export const verifyIdentityOwnership = async (accountAddress, publicKey, accountProof, keyProof, chain, rpcUrl) => {
  const keyProofMessage = getKeyProofMessage(accountAddress, publicKey);
  const publicClient = createPublicClient({
    chain,
    transport: http(rpcUrl)
  });
  // console.log('publicClient', publicClient);
  // console.log('rpcUrl', rpcUrl);
  // console.log('chain', chain);
  // console.log('accountProof', accountProof);
  // console.log('accountAddress', accountAddress);
  // console.log('publicKey', publicKey);
  const accountProofMessage = getAccountProofMessage(accountAddress, publicKey);
  const validAccountProof = await publicClient.verifyTypedData({
    address: accountAddress,
    message: {
      message: accountProofMessage
    },
    types: {
      Message: [{
        name: 'message',
        type: 'string'
      }]
    },
    domain: accountProofDomain,
    primaryType: 'Message',
    signature: accountProof
  });
  if (!validAccountProof) {
    console.log('Invalid account proof');
    return false;
  }
  const keyAddress = publicKeyToAddress(publicKey);
  const validKeyProof = await verifyMessage({
    address: keyAddress,
    message: keyProofMessage,
    signature: keyProof
  });
  if (!validKeyProof) {
    console.log('Invalid key proof');
    return false;
  }
  return true;
};
//# sourceMappingURL=prove-ownership.js.map