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>
135 lines
3.8 KiB
Go
135 lines
3.8 KiB
Go
package nearanchor
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"math/big"
|
|
)
|
|
|
|
// Action variant tags, exactly as defined by nearcore's
|
|
// near_primitives::transaction::Action Borsh enum (the tag is the
|
|
// variant's zero-based position in that enum, encoded as a single u8
|
|
// ahead of the variant's own fields). This package only ever needs two
|
|
// of the eight variants: DeployContract (to deploy
|
|
// contracts-near/audit_anchor's wasm) and FunctionCall (to call
|
|
// commit_anchor/new).
|
|
const (
|
|
actionTagCreateAccount = 0
|
|
actionTagDeployContract = 1
|
|
actionTagFunctionCall = 2
|
|
actionTagTransfer = 3
|
|
actionTagStake = 4
|
|
actionTagAddKey = 5
|
|
actionTagDeleteKey = 6
|
|
actionTagDeleteAccount = 7
|
|
)
|
|
|
|
// action is anything that can Borsh-encode itself as one
|
|
// near_primitives Action variant (tag byte plus fields).
|
|
type action interface {
|
|
encodeAction(w *borshWriter)
|
|
}
|
|
|
|
// deployContractAction is Action::DeployContract { code: Vec<u8> }.
|
|
type deployContractAction struct {
|
|
Code []byte
|
|
}
|
|
|
|
func (a deployContractAction) encodeAction(w *borshWriter) {
|
|
w.WriteU8(actionTagDeployContract)
|
|
w.WriteBytes(a.Code)
|
|
}
|
|
|
|
// functionCallAction is Action::FunctionCall { method_name, args, gas,
|
|
// deposit }. Deposit is always zero for this package's calls (new,
|
|
// commit_anchor are both non-payable), but the field is still a real
|
|
// u128 write so the wire format matches nearcore's exactly.
|
|
type functionCallAction struct {
|
|
MethodName string
|
|
Args []byte
|
|
Gas uint64
|
|
Deposit *big.Int
|
|
}
|
|
|
|
func (a functionCallAction) encodeAction(w *borshWriter) {
|
|
w.WriteU8(actionTagFunctionCall)
|
|
w.WriteString(a.MethodName)
|
|
w.WriteBytes(a.Args)
|
|
w.WriteU64(a.Gas)
|
|
deposit := a.Deposit
|
|
if deposit == nil {
|
|
deposit = big.NewInt(0)
|
|
}
|
|
w.WriteU128(deposit)
|
|
}
|
|
|
|
// transaction is near_primitives::transaction::Transaction's Borsh
|
|
// layout: signer_id, public_key (KeyType tag + 32 raw bytes), nonce,
|
|
// receiver_id, block_hash (32 raw bytes), then a Vec<Action>.
|
|
type transaction struct {
|
|
SignerID string
|
|
PublicKey string // "ed25519:<base58>"
|
|
Nonce uint64
|
|
ReceiverID string
|
|
BlockHash []byte // 32 raw bytes
|
|
Actions []action
|
|
}
|
|
|
|
func (t transaction) encode() ([]byte, error) {
|
|
if len(t.BlockHash) != 32 {
|
|
return nil, fmt.Errorf("nearanchor: block hash must be 32 bytes, got %d", len(t.BlockHash))
|
|
}
|
|
pubBytes, err := publicKeyBytes(t.PublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
w := newBorshWriter()
|
|
w.WriteString(t.SignerID)
|
|
w.WriteU8(keyTypeED25519)
|
|
w.WriteFixed(pubBytes)
|
|
w.WriteU64(t.Nonce)
|
|
w.WriteString(t.ReceiverID)
|
|
w.WriteFixed(t.BlockHash)
|
|
w.WriteU32(uint32(len(t.Actions)))
|
|
for _, a := range t.Actions {
|
|
a.encodeAction(w)
|
|
}
|
|
return w.Bytes(), nil
|
|
}
|
|
|
|
// signedTransaction is the Borsh-serialized form nearcore's
|
|
// broadcast_tx_commit expects: the Transaction bytes followed by a
|
|
// Signature (KeyType tag + 64 raw bytes).
|
|
type signedTransaction struct {
|
|
TxBytes []byte
|
|
Signature []byte // 64 raw ed25519 signature bytes
|
|
}
|
|
|
|
func (s signedTransaction) encode() []byte {
|
|
w := newBorshWriter()
|
|
w.WriteFixed(s.TxBytes)
|
|
w.WriteU8(keyTypeED25519)
|
|
w.WriteFixed(s.Signature)
|
|
return w.Bytes()
|
|
}
|
|
|
|
// signTransaction Borsh-encodes tx, signs sha256(encoded tx) with priv
|
|
// (nearcore always signs the hash of the serialized transaction, never
|
|
// the raw transaction bytes), and returns both the signed, ready-to-
|
|
// broadcast bytes and the transaction hash (base58-encoded, matching
|
|
// what nearcore itself reports back as the transaction's id) for
|
|
// logging/verification.
|
|
func signTransaction(tx transaction, priv ed25519.PrivateKey) (signedBytes []byte, txHashBase58 string, err error) {
|
|
txBytes, err := tx.encode()
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
hash := sha256.Sum256(txBytes)
|
|
sig := ed25519.Sign(priv, hash[:])
|
|
|
|
signed := signedTransaction{TxBytes: txBytes, Signature: sig}
|
|
return signed.encode(), base58Encode(hash[:]), nil
|
|
}
|