caelum0x 07136e7b73 feat(near-hackathon): fold Ration's audit-anchor work into this repo as ONE combined NEAR submission
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>
2026-11-11 15:32:24 +03:00

81 lines
3.3 KiB
Go

package nearanchor
import (
"encoding/hex"
"math/big"
"testing"
)
// TestTransactionEncodeMatchesNearAPIJS cross-checks this package's
// hand-rolled Borsh transaction encoding against a reference vector
// generated by the real, official near-api-js (actions.functionCall +
// createTransaction + encodeTransaction, run once via node against
// ~/products/blockhain/cred402's existing near-api-js install — see
// this test's sibling comment for the exact inputs used). If nearcore's
// wire format ever changes, or if this package's encoder regresses,
// this is the test that catches it before a real testnet submission
// ever gets signed and sent — far cheaper than discovering a format
// bug from a failed broadcast_tx_commit.
func TestTransactionEncodeMatchesNearAPIJS(t *testing.T) {
blockHash, err := base58Decode("4d6c4y3dKN7KhBbH4sy1xjhC1ZztwLZsHD1Q31a2FYxF")
if err != nil {
t.Fatalf("decode block hash: %v", err)
}
if len(blockHash) != 32 {
t.Fatalf("test block hash decoded to %d bytes, want 32", len(blockHash))
}
tx := transaction{
SignerID: "alice.testnet",
PublicKey: "ed25519:9DkDC24MnA2XKkALp8CsGYQ7cPA3N5ghig9tGeMeSR68",
Nonce: 42,
ReceiverID: "bob.testnet",
BlockHash: blockHash,
Actions: []action{functionCallAction{
MethodName: "commit_anchor",
Args: []byte(`{"a":1}`),
Gas: 100_000_000_000_000,
Deposit: big.NewInt(0),
}},
}
got, err := tx.encode()
if err != nil {
t.Fatalf("encode: %v", err)
}
// Generated via:
// node -e "
// const nearAPI = require('near-api-js');
// const { actions, createTransaction, encodeTransaction, PublicKey, baseDecode } = nearAPI;
// const publicKey = PublicKey.fromString('ed25519:9DkDC24MnA2XKkALp8CsGYQ7cPA3N5ghig9tGeMeSR68');
// const blockHash = baseDecode('4d6c4y3dKN7KhBbH4sy1xjhC1ZztwLZsHD1Q31a2FYxF');
// const action = actions.functionCall('commit_anchor', Buffer.from(JSON.stringify({a:1})), 100000000000000n, 0n);
// const tx = createTransaction('alice.testnet', publicKey, 'bob.testnet', 42n, [action], blockHash);
// console.log(Buffer.from(encodeTransaction(tx)).toString('hex'));
// "
// run against the real near-api-js package already vendored in
// ~/products/blockhain/cred402/node_modules (a separate, unrelated
// repo — used here only as a reference oracle, nothing copied).
wantHex := "0d000000616c6963652e746573746e6574007a2152b13b41a4e177443639c32d92b7b65664d2640285b605b6f06f6e570a012a000000000000000b000000626f622e746573746e657435d24e9cab1d7b323722a3dd4bdb0729849ac862ca932f5c0f0d900644ddc4c001000000020d000000636f6d6d69745f616e63686f72070000007b2261223a317d00407a10f35a000000000000000000000000000000000000"
if hex.EncodeToString(got) != wantHex {
t.Fatalf("transaction encode mismatch:\n got: %x\n want: %s", got, wantHex)
}
}
func TestDeployContractActionEncode(t *testing.T) {
code := []byte{0x00, 0x61, 0x73, 0x6d} // arbitrary 4 bytes, enough to check the length-prefix framing
w := newBorshWriter()
deployContractAction{Code: code}.encodeAction(w)
got := w.Bytes()
// tag(1 byte, value 1) + u32 length(4) + code bytes(4) = 9 bytes.
if len(got) != 9 {
t.Fatalf("encoded length = %d, want 9", len(got))
}
if got[0] != actionTagDeployContract {
t.Fatalf("tag byte = %d, want %d", got[0], actionTagDeployContract)
}
}