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

94 lines
2.4 KiB
Go

package nearanchor
import "math/big"
// base58Alphabet is the Bitcoin/NEAR base58 alphabet (no '0', 'O', 'I',
// 'l' — the characters that are easy to confuse with each other or with
// '0'/'1'). NEAR account keys and tx/block hashes are always encoded
// with this exact alphabet, so this file hand-rolls encode/decode rather
// than pulling in a third-party base58 dependency for ~40 lines of math
// this package needs in exactly two places (keys.go, rpcclient.go).
const base58Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
var base58DecodeMap = func() [256]int8 {
var m [256]int8
for i := range m {
m[i] = -1
}
for i, c := range base58Alphabet {
m[byte(c)] = int8(i)
}
return m
}()
// base58Encode encodes raw bytes the same way NEAR's own SDKs do:
// leading 0x00 bytes become leading '1' characters (base58's standard
// convention), the rest is a big-endian base-256 -> base-58 conversion.
func base58Encode(data []byte) string {
if len(data) == 0 {
return ""
}
zeros := 0
for zeros < len(data) && data[zeros] == 0 {
zeros++
}
num := new(big.Int).SetBytes(data)
base := big.NewInt(58)
zero := big.NewInt(0)
mod := new(big.Int)
var out []byte
for num.Cmp(zero) > 0 {
num.DivMod(num, base, mod)
out = append(out, base58Alphabet[mod.Int64()])
}
for i := 0; i < zeros; i++ {
out = append(out, base58Alphabet[0])
}
// out was built least-significant-digit-first; reverse it.
for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 {
out[i], out[j] = out[j], out[i]
}
return string(out)
}
// base58Decode is base58Encode's inverse. Returns an error on any
// character outside base58Alphabet so a malformed key/hash string fails
// loudly instead of silently decoding to the wrong bytes.
func base58Decode(s string) ([]byte, error) {
if s == "" {
return nil, nil
}
zeros := 0
for zeros < len(s) && s[zeros] == base58Alphabet[0] {
zeros++
}
num := new(big.Int)
base := big.NewInt(58)
for i := 0; i < len(s); i++ {
digit := base58DecodeMap[s[i]]
if digit < 0 {
return nil, &base58Error{char: s[i]}
}
num.Mul(num, base)
num.Add(num, big.NewInt(int64(digit)))
}
decoded := num.Bytes()
out := make([]byte, zeros+len(decoded))
copy(out[zeros:], decoded)
return out, nil
}
type base58Error struct {
char byte
}
func (e *base58Error) Error() string {
return "nearanchor: invalid base58 character '" + string(e.char) + "'"
}