caelum0x cff0ed3129 feat(near-hackathon): anchor audit-log's hash-chain tip to NEAR Testnet
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>
2026-11-09 14:19:33 +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)
}
}