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

82 lines
3.4 KiB
Go

// Package nearanchor submits Ration's audit-log chain-tip to a minimal
// NEAR smart contract (contracts-near/audit_anchor) on NEAR Testnet, so
// the audit trail's hash chain is independently, publicly verifiable
// without trusting Ration's own database. See the contract's own doc
// comment for the "why", and cmd/near-anchor for the CLI this package
// backs.
//
// This package deliberately has zero near-api-go/near-cli dependency:
// at the time this was written there was no actively maintained Go SDK
// for NEAR transaction signing, so it talks to NEAR's JSON-RPC directly
// (rpcclient.go) and Borsh-encodes/signs transactions itself
// (borsh.go, transaction.go) using exactly nearcore's documented wire
// format — the same approach this repo already takes for its own
// hand-rolled WAL and hash-chain formats (services/wal,
// services/audit-log/internal/store).
package nearanchor
import (
"crypto/ed25519"
"fmt"
"strings"
)
// KeyType mirrors nearcore's single-byte KeyType tag used in both
// PublicKey and Signature Borsh encodings. ED25519 (0) is the only
// variant this package ever produces or expects — NEAR's SECP256K1 (1)
// variant exists in the protocol but nothing in Ration uses it.
const keyTypeED25519 = 0
// ParseSecretKey parses a NEAR secret key string of the form
// "ed25519:<base58>" (near-cli/near-api-js's standard format — the
// base58 payload is the 64-byte concatenation of the 32-byte seed and
// the 32-byte public key, which is exactly Go's crypto/ed25519.PrivateKey
// wire layout, so no reshuffling is needed) into a usable ed25519
// private key plus its "ed25519:<base58>" public-key string form.
func ParseSecretKey(secretKey string) (ed25519.PrivateKey, string, error) {
const prefix = "ed25519:"
if !strings.HasPrefix(secretKey, prefix) {
return nil, "", fmt.Errorf("nearanchor: secret key must start with %q", prefix)
}
raw, err := base58Decode(strings.TrimPrefix(secretKey, prefix))
if err != nil {
return nil, "", fmt.Errorf("nearanchor: decode secret key: %w", err)
}
if len(raw) != ed25519.PrivateKeySize {
return nil, "", fmt.Errorf("nearanchor: secret key decoded to %d bytes, want %d", len(raw), ed25519.PrivateKeySize)
}
priv := ed25519.PrivateKey(raw)
pub := priv.Public().(ed25519.PublicKey)
return priv, "ed25519:" + base58Encode(pub), nil
}
// publicKeyBytes decodes a "ed25519:<base58>" public key string back
// into its raw 32 bytes.
func publicKeyBytes(publicKey string) ([]byte, error) {
const prefix = "ed25519:"
if !strings.HasPrefix(publicKey, prefix) {
return nil, fmt.Errorf("nearanchor: public key must start with %q", prefix)
}
raw, err := base58Decode(strings.TrimPrefix(publicKey, prefix))
if err != nil {
return nil, fmt.Errorf("nearanchor: decode public key: %w", err)
}
if len(raw) != ed25519.PublicKeySize {
return nil, fmt.Errorf("nearanchor: public key decoded to %d bytes, want %d", len(raw), ed25519.PublicKeySize)
}
return raw, nil
}
// GenerateKeyPair creates a fresh random ed25519 key pair and returns it
// in NEAR's standard "ed25519:<base58>" secret/public string form, for
// bootstrapping a brand-new testnet account (see cmd/near-anchor's
// create-account subcommand).
func GenerateKeyPair() (secretKey, publicKey string, err error) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
return "", "", fmt.Errorf("nearanchor: generate key pair: %w", err)
}
return "ed25519:" + base58Encode(priv), "ed25519:" + base58Encode(pub), nil
}