caelum0x cff0ed3129 feat(near-hackathon): anchor audit-log's hash-chain tip to NEAR Testnet
Adds a focused NEAR Hackathon 2026 entry built on Ration's existing,
real audit-log service rather than a Ration-on-NEAR rewrite: a minimal
NEAR smart contract (contracts-near/audit_anchor) storing/verifying
{tenant_id, chain_tip_hash, sequence, occurred_at_unix}, plus a
from-scratch Go NEAR transaction signer and CLI
(services/audit-log/internal/nearanchor, cmd/near-anchor) that reads
audit-log's real local chain tip and anchors it on-chain.

Verified live on NEAR Testnet (account ration-anchor-286ca315f1.testnet):
contract deployed and code_hash-matched, two successful on-chain
anchors, one deliberate replay correctly rejected by nearcore's own
sequence guard, and a real (non-mocked) Go integration test passing
against the live contract. Transaction encoding cross-checked
byte-for-byte against near-api-js. Zero regressions in existing Go/Rust
suites. Full detail and every tx hash in docs/near-hackathon/.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-09 14:19:33 +03:00

81 lines
1.7 KiB
Go

package nearanchor
import (
"bytes"
"testing"
)
func TestBase58RoundTrip(t *testing.T) {
cases := [][]byte{
{},
{0},
{0, 0, 1},
{1, 2, 3, 4, 5},
bytes.Repeat([]byte{0xff}, 32),
}
for _, c := range cases {
encoded := base58Encode(c)
decoded, err := base58Decode(encoded)
if err != nil {
t.Fatalf("decode(%q): %v", encoded, err)
}
if !bytes.Equal(decoded, c) {
t.Fatalf("round trip mismatch: got %x, want %x (via %q)", decoded, c, encoded)
}
}
}
// TestBase58KnownVectors checks against values independently produced
// by near-api-js's own baseEncode/baseDecode (see keys_test.go's doc
// comment for how these were generated) rather than only trusting this
// file's own round trip.
func TestBase58KnownVectors(t *testing.T) {
cases := []struct {
hex string
b58 string
}{
{"00", "1"},
{"0000", "11"},
{"61", "2g"},
{"626262", "a3gV"},
{"68656c6c6f20776f726c64", "StV1DL6CwTryKyV"},
}
for _, c := range cases {
raw := mustHexDecode(t, c.hex)
if got := base58Encode(raw); got != c.b58 {
t.Errorf("base58Encode(%s) = %q, want %q", c.hex, got, c.b58)
}
decoded, err := base58Decode(c.b58)
if err != nil {
t.Fatalf("base58Decode(%q): %v", c.b58, err)
}
if !bytes.Equal(decoded, raw) {
t.Errorf("base58Decode(%q) = %x, want %x", c.b58, decoded, raw)
}
}
}
func mustHexDecode(t *testing.T, s string) []byte {
t.Helper()
b := make([]byte, len(s)/2)
for i := 0; i < len(b); i++ {
hi := hexNibble(t, s[i*2])
lo := hexNibble(t, s[i*2+1])
b[i] = hi<<4 | lo
}
return b
}
func hexNibble(t *testing.T, c byte) byte {
t.Helper()
switch {
case c >= '0' && c <= '9':
return c - '0'
case c >= 'a' && c <= 'f':
return c - 'a' + 10
default:
t.Fatalf("invalid hex char %q", c)
return 0
}
}