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

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
}