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>
83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package nearanchor
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math/big"
|
|
)
|
|
|
|
// borshWriter is a minimal, hand-rolled Borsh serializer covering exactly
|
|
// the primitives nearcore's Transaction/Action/SignedTransaction types
|
|
// use (see transaction.go) — a u8/u32/u64 little-endian writer, a
|
|
// length-prefixed UTF-8 string writer, raw byte and fixed-size-array
|
|
// writers, and a little-endian u128 writer for Balance/deposit fields.
|
|
// This repo already hand-rolls its own binary formats elsewhere
|
|
// (services/wal's C WAL frames, services/ledger-core's WAL crypto), so
|
|
// implementing the small, fixed subset of Borsh this package needs
|
|
// directly — rather than depending on a third-party Go Borsh library
|
|
// whose struct-tag reflection would still need to special-case NEAR's
|
|
// enum/u128 encodings anyway — keeps this auditable in one file instead
|
|
// of two.
|
|
//
|
|
// Reference: https://borsh.io — fixed-width integers are little-endian,
|
|
// `String`/`Vec<T>` are a u32 length prefix followed by the elements,
|
|
// and there is no padding or alignment anywhere.
|
|
type borshWriter struct {
|
|
buf []byte
|
|
}
|
|
|
|
func newBorshWriter() *borshWriter {
|
|
return &borshWriter{buf: make([]byte, 0, 256)}
|
|
}
|
|
|
|
func (w *borshWriter) Bytes() []byte { return w.buf }
|
|
|
|
func (w *borshWriter) WriteU8(v uint8) {
|
|
w.buf = append(w.buf, v)
|
|
}
|
|
|
|
func (w *borshWriter) WriteU32(v uint32) {
|
|
var b [4]byte
|
|
binary.LittleEndian.PutUint32(b[:], v)
|
|
w.buf = append(w.buf, b[:]...)
|
|
}
|
|
|
|
func (w *borshWriter) WriteU64(v uint64) {
|
|
var b [8]byte
|
|
binary.LittleEndian.PutUint64(b[:], v)
|
|
w.buf = append(w.buf, b[:]...)
|
|
}
|
|
|
|
// WriteU128 writes a non-negative integer as 16 little-endian bytes,
|
|
// NEAR's Balance/deposit wire format. v must fit in 128 bits (every
|
|
// caller in this package passes a deposit of exactly 0, which always
|
|
// fits).
|
|
func (w *borshWriter) WriteU128(v *big.Int) {
|
|
var out [16]byte
|
|
b := v.Bytes() // big-endian, no leading zeros
|
|
for i := 0; i < len(b) && i < 16; i++ {
|
|
out[i] = b[len(b)-1-i] // reverse into little-endian
|
|
}
|
|
w.buf = append(w.buf, out[:]...)
|
|
}
|
|
|
|
// WriteString writes a Borsh `String`: a u32 length prefix (byte length,
|
|
// not rune count) followed by the raw UTF-8 bytes.
|
|
func (w *borshWriter) WriteString(s string) {
|
|
w.WriteU32(uint32(len(s)))
|
|
w.buf = append(w.buf, s...)
|
|
}
|
|
|
|
// WriteBytes writes a Borsh `Vec<u8>`: a u32 length prefix followed by
|
|
// the raw bytes (used for FunctionCall's `args`).
|
|
func (w *borshWriter) WriteBytes(b []byte) {
|
|
w.WriteU32(uint32(len(b)))
|
|
w.buf = append(w.buf, b...)
|
|
}
|
|
|
|
// WriteFixed writes exactly b's bytes with no length prefix — for
|
|
// Borsh's fixed-size arrays, e.g. a 32-byte public key or a 64-byte
|
|
// signature.
|
|
func (w *borshWriter) WriteFixed(b []byte) {
|
|
w.buf = append(w.buf, b...)
|
|
}
|