OverviewFilesCommitsIssuesDocsReleasesPeers

hyperident / test-keet-api4.js

70 lines · javascript ref:
import IdentityKey from 'keet-identity-key';
import sodium from 'sodium-universal';

async function test() {
  try {
    // Generate a test mnemonic and identity
    const mnemonic = IdentityKey.generateMnemonic();
    const identityKey = await IdentityKey.from({ mnemonic });
    
    console.log('--- Generating keypairs with sodium-universal ---');
    
    // Generate keypairs using sodium-universal (same as keet-identity-key uses)
    const device1PublicKey = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES);
    const device1SecretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES);
    sodium.crypto_sign_keypair(device1PublicKey, device1SecretKey);
    
    const device2PublicKey = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES);
    const device2SecretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES);
    sodium.crypto_sign_keypair(device2PublicKey, device2SecretKey);
    
    // Create keypair objects
    const device1 = {
      publicKey: device1PublicKey,
      secretKey: device1SecretKey
    };
    
    const device2 = {
      publicKey: device2PublicKey,
      secretKey: device2SecretKey
    };
    
    console.log('Device1 public key length:', device1PublicKey.length);
    console.log('Device1 secret key length:', device1SecretKey.length);
    
    // Bootstrap first device
    const proof1 = await identityKey.bootstrap(device1.publicKey);
    console.log('Bootstrap successful, proof length:', proof1.length);
    
    // Now try attestDevice with sodium-generated keys
    console.log('\n--- Testing IdentityKey.attestDevice with sodium keys ---');
    try {
      const proof2 = IdentityKey.attestDevice(device2.publicKey, device1, proof1);
      console.log('AttestDevice successful!');
      console.log('Proof2 is Buffer:', Buffer.isBuffer(proof2));
      console.log('Proof2 length:', proof2.length);
      
      // Test verification
      console.log('\n--- Testing verification ---');
      const info = IdentityKey.verify(proof2);
      if (info === null) {
        console.log('Verification failed');
      } else {
        console.log('Verification successful!');
        console.log('Identity matches:', Buffer.compare(info.identityPublicKey, identityKey.identityPublicKey) === 0);
        console.log('Device matches:', Buffer.compare(info.devicePublicKey, device2.publicKey) === 0);
      }
      
    } catch (error) {
      console.error('AttestDevice failed:', error.message);
      console.error('Stack:', error.stack);
    }
    
  } catch (error) {
    console.error('Error:', error.message);
    console.error('Stack:', error.stack);
  }
}

test();