Owner directive (2026-11-11): submit a single project to the NEAR Hackathon instead of two separate entries from the same person. Vendors Ration's NEAR audit-anchor work (github.com/caelum0x/ration, commit cff0ed3 + follow-ups) into ration-anchor/ as a first-class component: - contracts-near/audit_anchor (NEAR Rust contract, 5/5 cargo tests green, target/ deleted) - signer/ — a new, self-contained Go module (no dependency on Ration's other services or go.work) with the hand-rolled NEAR transaction signer + JSON-RPC client (cross-checked byte-for-byte against near-api-js) and the hash-chained audit ledger it anchors; 15/15 Go tests green (go test ./... -race) - docs/ and media/ — Ration's original pitch deck, demo recordings, and submission checklist Re-verified live via raw NEAR Testnet JSON-RPC this pass: ration-anchor-286ca315f1.testnet still deployed and queryable, the deliberate replay-rejection transaction still fails with nearcore's own "sequence must strictly advance" panic — no redeploy needed. Provenance notes added throughout (package doc comments, lib.rs header, README) crediting the origin repo. README.md and HACKATHON.md rewritten to tell one story: Cred402's SpendGuard enforces the protocol-level spending limit, the vendored Audit Anchor proves the tamper-evident spend history, both independently verifiable on NEAR Testnet. No mainnet claims anywhere (see prior overclaim fix 524f2dd). cred402's own build (npm run typecheck) and existing tests are untouched. Co-Authored-By: Claude Opus 5.5 (1M context) <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...)
|
|
}
|