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

70 lines
2.4 KiB
Go

package nearanchor
import (
"fmt"
"os"
)
// defaultFunctionCallGas is 100 Tgas — nearcore's own convention for a
// single, simple contract call (the protocol-wide max per function
// call is 300 Tgas); commit_anchor/new are both small, O(1) methods, so
// this is comfortably more than either ever needs.
const defaultFunctionCallGas = 100_000_000_000_000
// Config holds everything the near-anchor CLI needs, read from the
// environment (RATION_NEAR_ANCHOR_* — matches every other Go service in
// this repo's env-var-based config convention, see
// services/audit-log/internal/config). None of these have safe
// defaults for SignerAccountID/SignerSecretKey/ContractID — Load
// returns an error rather than silently anchoring to nothing or signing
// with an empty key.
type Config struct {
RPCURL string
FaucetURL string
DataDir string
AuditLogPath string
ContractID string
SignerAccountID string
SignerSecretKey string
Gas uint64
}
// LoadForCommit reads the subset of config needed to read the local
// chain tip and submit it on-chain (near-anchor commit/verify) —
// ContractID, SignerAccountID and SignerSecretKey are required; the
// rest have the same sane defaults the rest of this repo already uses.
func LoadForCommit() (Config, error) {
cfg := Config{
RPCURL: getenvDefault("RATION_NEAR_ANCHOR_RPC_URL", DefaultRPCURL),
FaucetURL: getenvDefault("RATION_NEAR_ANCHOR_FAUCET_URL", DefaultFaucetURL),
DataDir: getenvDefault("RATION_AUDIT_LOG_DATA_DIR", "./data"),
ContractID: os.Getenv("RATION_NEAR_ANCHOR_CONTRACT_ID"),
SignerAccountID: os.Getenv("RATION_NEAR_ANCHOR_SIGNER_ACCOUNT_ID"),
SignerSecretKey: os.Getenv("RATION_NEAR_ANCHOR_SIGNER_SECRET_KEY"),
Gas: defaultFunctionCallGas,
}
cfg.AuditLogPath = os.Getenv("RATION_NEAR_ANCHOR_AUDIT_LOG_PATH")
var missing []string
if cfg.ContractID == "" {
missing = append(missing, "RATION_NEAR_ANCHOR_CONTRACT_ID")
}
if cfg.SignerAccountID == "" {
missing = append(missing, "RATION_NEAR_ANCHOR_SIGNER_ACCOUNT_ID")
}
if cfg.SignerSecretKey == "" {
missing = append(missing, "RATION_NEAR_ANCHOR_SIGNER_SECRET_KEY")
}
if len(missing) > 0 {
return Config{}, fmt.Errorf("nearanchor: missing required env var(s): %v", missing)
}
return cfg, nil
}
func getenvDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}