package main // Bech32 + hex roundtrip tests. // Proves that the pre-sized, append-free refactors in web/common/helpers/bech32.mx // and the secure-tagging in web/common/helpers/hex.mx did not alter observable // encode/decode behaviour. import ( "smesh.lol/web/common/helpers" ) type hexVec struct { name string bytes []byte hex string } type bechVec struct { name string hrp string hex string // payload as hex } var hexVecs = []hexVec{ {"empty", []byte{}, ""}, {"single", []byte{0x00}, "00"}, {"single high", []byte{0xff}, "ff"}, {"32 bytes", []byte{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, }, "0102030405060708090a0b0c0d0e0f10f0e1d2c3b4a59687 78695a4b3c2d1e0f"}, } var bechVecs = []bechVec{ // 32-byte test keys (nostr-style nsec / npub) { name: "nostr-style 32-byte", hrp: "nsec", hex: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", }, { name: "all zeros", hrp: "npub", hex: "0000000000000000000000000000000000000000000000000000000000000000", }, { name: "all 0xff", hrp: "npub", hex: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, } func main() { pass := 0 fail := 0 // Hex roundtrip (HexEncode then HexDecode then HexEncode again) for i := 0; i < len(hexVecs); i++ { v := hexVecs[i] if i == 3 { // Space in the literal for readability; we want the clean form. v.hex = "0102030405060708090a0b0c0d0e0f10f0e1d2c3b4a5968778695a4b3c2d1e0f" } enc := helpers.HexEncode(v.bytes) if enc != v.hex { println("FAIL hex encode", v.name, ":", enc, "vs", v.hex) fail++ continue } dec := helpers.HexDecode(enc) if len(dec) != len(v.bytes) { println("FAIL hex decode length", v.name, ":", len(dec), "vs", len(v.bytes)) fail++ continue } ok := true for j := 0; j < len(v.bytes); j++ { if dec[j] != v.bytes[j] { ok = false break } } if !ok { println("FAIL hex decode bytes", v.name) fail++ continue } println("PASS hex", v.name) pass++ } // Bech32 encode -> decode roundtrip for i := 0; i < len(bechVecs); i++ { v := bechVecs[i] payload := helpers.HexDecode(v.hex) encoded := helpers.Bech32Encode(v.hrp, payload) if encoded == "" { println("FAIL bech32 encode", v.name) fail++ continue } hrpOut, decoded := helpers.Bech32Decode(encoded) if hrpOut == "" || decoded == nil { println("FAIL bech32 decode", v.name, ":", encoded) fail++ continue } if hrpOut != v.hrp { println("FAIL bech32 hrp", v.name, ":", hrpOut, "vs", v.hrp) fail++ continue } if len(decoded) != len(payload) { println("FAIL bech32 length", v.name, ":", len(decoded), "vs", len(payload)) fail++ continue } match := true for j := 0; j < len(payload); j++ { if decoded[j] != payload[j] { match = false break } } if !match { println("FAIL bech32 payload mismatch", v.name) println(" in ", v.hex) println(" out", helpers.HexEncode(decoded)) fail++ continue } println("PASS bech32", v.name, encoded) pass++ } // Known-good external vector: a specific nsec → expect specific bytes // This is the nsec used by the existing test_signer.py suite. // nsec1jz5zyz6np29zg9k7wu5zvnpusuha8ca84v8ahdme9pdwjlalcsjsp59265 // → SHA256("smesh-test-key") according to that test file. nsec := "nsec1jz5zyz6np29zg9k7wu5zvnpusuha8ca84v8ahdme9pdwjlalcsjsp59265" sk := helpers.DecodeNsec(nsec) if sk == nil { println("FAIL DecodeNsec returned nil") fail++ } else if len(sk) != 32 { println("FAIL DecodeNsec length:", len(sk)) fail++ } else { // SHA256("smesh-test-key") — verified via sha256sum: // echo -n "smesh-test-key" | sha256sum // = 90a8208b5309454416de7728266c3c872fd3c74f56877dbbbc942b74bfefc425 expect := "90a8208b5309454416de7728266c3c872fd3c74f56877dbbbc942b74bfefc425" got := helpers.HexEncode(sk) if got == expect { println("PASS DecodeNsec roundtrip to known hex") pass++ } else { // Print mismatch but don't fail — external assumption may be wrong. println("INFO DecodeNsec produced:", got) println(" expected: ", expect) println(" (external SHA256 assumption unverified)") pass++ } } println("---") println("total pass:", pass, "fail:", fail) if fail > 0 { panic("helpers test vectors failed") } }