OverviewFilesCommitsIssuesDocsReleasesPeers

hyperident / test-keet-api3.js

67 lines · javascript ref:
import IdentityKey from 'keet-identity-key';
import { CryptoManager } from './lib/crypto.js';
import crypto from 'node:crypto';

async function test() {
  try {
    // Generate a test mnemonic and identity
    const mnemonic = IdentityKey.generateMnemonic();
    const identityKey = await IdentityKey.from({ mnemonic });
    
    // Try using Node.js crypto directly to create keypairs in the format keet-identity-key expects
    console.log('--- Generating keypairs with Node.js crypto ---');
    const device1KeyPair = crypto.generateKeyPairSync('ed25519');
    const device2KeyPair = crypto.generateKeyPairSync('ed25519');
    
    // Extract raw keys
    const device1PublicRaw = device1KeyPair.publicKey.export({ type: 'spki', format: 'der' }).slice(-32);
    const device1PrivateRaw = device1KeyPair.privateKey.export({ type: 'pkcs8', format: 'der' }).slice(-32);
    
    const device2PublicRaw = device2KeyPair.publicKey.export({ type: 'spki', format: 'der' }).slice(-32);
    const device2PrivateRaw = device2KeyPair.privateKey.export({ type: 'pkcs8', format: 'der' }).slice(-32);
    
    // Try different keypair formats
    const device1Formats = [
      // Format 1: secretKey property
      {
        publicKey: device1PublicRaw,
        secretKey: device1PrivateRaw
      },
      // Format 2: privateKey property
      {
        publicKey: device1PublicRaw,
        privateKey: device1PrivateRaw
      },
      // Format 3: Node.js KeyObject
      device1KeyPair,
      // Format 4: Just the raw keys as object
      {
        public: device1PublicRaw,
        secret: device1PrivateRaw
      }
    ];
    
    // Bootstrap first device
    const proof1 = await identityKey.bootstrap(device1PublicRaw);
    console.log('Bootstrap result length:', proof1.length);
    
    // Test each format
    for (let i = 0; i < device1Formats.length; i++) {
      console.log(`\n--- Testing format ${i + 1} ---`);
      try {
        const proof2 = IdentityKey.attestDevice(device2PublicRaw, device1Formats[i], proof1);
        console.log(`Format ${i + 1} successful:`, Buffer.isBuffer(proof2));
        break; // If successful, we found the right format
      } catch (error) {
        console.error(`Format ${i + 1} failed:`, error.message);
      }
    }
    
  } catch (error) {
    console.error('Error:', error.message);
    console.error('Stack:', error.stack);
  }
}

test();