debug_test.go raw

   1  //go:build interop
   2  
   3  package marmot
   4  
   5  import (
   6  	"encoding/hex"
   7  	"testing"
   8  
   9  	"github.com/emersion/go-mls"
  10  	"git.smesh.lol/orly/pkg/nostr/interfaces/signer/p8k"
  11  )
  12  
  13  // TestDebug_CompareWireFormat dumps both Go and Rust raw KP bytes for comparison.
  14  func TestDebug_CompareWireFormat(t *testing.T) {
  15  	// Generate Go key package
  16  	sign, err := p8k.New()
  17  	if err != nil {
  18  		t.Fatal(err)
  19  	}
  20  	if err := sign.Generate(); err != nil {
  21  		t.Fatal(err)
  22  	}
  23  	kpp, err := GenerateKeyPackage(&LocalCrypto{Sign: sign})
  24  	if err != nil {
  25  		t.Fatal(err)
  26  	}
  27  	goBytes := kpp.Public.RawBytes()
  28  	goHex := hex.EncodeToString(goBytes)
  29  
  30  	// Get Rust key package bytes via interop binary
  31  	rust, _ := startRust(t)
  32  	resp := rust.send(t, map[string]string{
  33  		"cmd":   "dump_kp_bytes",
  34  		"relay": "wss://relay.example.com",
  35  	})
  36  	if !resp.OK {
  37  		t.Fatalf("rust dump_kp_bytes failed: %s", resp.Error)
  38  	}
  39  	rustHex := resp.Content
  40  
  41  	rustBytes, err := hex.DecodeString(rustHex)
  42  	if err != nil {
  43  		t.Fatalf("decode rust hex: %v", err)
  44  	}
  45  
  46  	t.Logf("Go  raw KP (%d bytes): %s", len(goBytes), goHex)
  47  	t.Logf("Rust raw KP (%d bytes): %s", len(rustBytes), rustHex)
  48  
  49  	// Find first byte difference
  50  	minLen := len(goBytes)
  51  	if len(rustBytes) < minLen {
  52  		minLen = len(rustBytes)
  53  	}
  54  	for i := 0; i < minLen; i++ {
  55  		if goBytes[i] != rustBytes[i] {
  56  			start := i - 4
  57  			if start < 0 {
  58  				start = 0
  59  			}
  60  			end := i + 20
  61  			if end > minLen {
  62  				end = minLen
  63  			}
  64  			t.Logf("FIRST DIFF at byte %d:", i)
  65  			t.Logf("  Go   [%d:%d]: %s", start, end, hex.EncodeToString(goBytes[start:end]))
  66  			t.Logf("  Rust [%d:%d]: %s", start, end, hex.EncodeToString(rustBytes[start:end]))
  67  			break
  68  		}
  69  	}
  70  	if len(goBytes) != len(rustBytes) {
  71  		t.Logf("LENGTH DIFF: Go=%d, Rust=%d", len(goBytes), len(rustBytes))
  72  	}
  73  
  74  	// Try to parse the Rust raw bytes through go-mls
  75  	_, err = mls.UnmarshalRawKeyPackage(rustBytes)
  76  	if err != nil {
  77  		t.Logf("Go failed to parse Rust raw bytes: %v", err)
  78  	} else {
  79  		t.Log("Go successfully parsed Rust raw bytes")
  80  	}
  81  }
  82