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>
94 lines
2.4 KiB
Go
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) + "'"
|
|
}
|