Browser-based git sync via Nostr with optional Bitcoin anchoring.
Subscribe to NIP-34 repo state events (kind 30618) and sync git repositories to browser IndexedDB using isomorphic-git.
npm install nostr-git-clientimport { NostrGitClient } from 'nostr-git-client';
const client = new NostrGitClient({
repo: 'https://solid.social/mel/public/myapp',
repoId: 'myapp',
branch: 'main',
relays: ['wss://relay.damus.io', 'wss://nos.lol'],
trusted: ['pubkey-hex...'] // Only sync from trusted publishers
});
client.on('sync', (event) => {
if (event.status === 'complete') {
console.log('Synced to:', event.commit);
}
});
client.connect();
await client.sync();
// Read files from synced repo
const content = await client.readFile('index.html');
const files = await client.listFiles('src');import { NostrGitClient, StateManager } from 'nostr-git-client';
const client = new NostrGitClient({
repo: 'https://solid.social/mel/public/game',
trusted: ['pubkey...']
});
const state = new StateManager({
sync: client,
file: 'state.json',
anchor: {
privkey: 'your-bitcoin-privkey-hex',
network: 'testnet4'
},
onChange: (newState) => {
console.log('State updated:', newState);
}
});
await state.init();
client.connect();
await client.sync();
// Anchor current state to Bitcoin
const anchor = await state.anchor();
console.log('Anchored:', anchor.txid);import { SelfDeploy } from 'nostr-git-client';
// Auto-reload when repo is updated
SelfDeploy.init({
repo: 'https://solid.social/mel/public/myapp',
trusted: ['pubkey...'],
autoReload: true,
onUpdate: (event) => {
console.log('Update detected:', event.commit);
}
});Main client for Nostr-based git sync.
Constructor options:
repo- Git repository URLrepoId- Repository identifier (default: derived from repo URL)branch- Branch to track (default: 'main')relays- Nostr relay URLstrusted- Trusted publisher pubkeyscorsProxy- CORS proxy URL (optional)
Methods:
connect()- Connect to relays and start listeningdisconnect()- Disconnect from relayssync(commit?)- Sync to specified commit or latestreadFile(path)- Read file content as stringreadFileBytes(path)- Read file as Uint8ArraylistFiles(path?)- List directory contentsgetAllFiles()- Get all files recursivelygetCommit()- Get current commit infoclear()- Delete local repository
Events:
sync- Sync status changesevent- Nostr events receivedconnect- Connected to relaydisconnect- Disconnected from relayerror- Errors
Manages JSON state with optional Bitcoin anchoring.
Constructor options:
sync- NostrGitClient instancefile- Path to JSON file (default: 'state.json')anchor- Bitcoin config:{ privkey, network }onChange- Callback on state changes
Methods:
init()- Initialize (loads blocktrails if anchoring)loadState()- Load state from fileget()- Get current stateanchor()- Anchor state to BitcoingetAnchors()- Get anchor historyverifyChain()- Verify anchor chain integrity
Auto-updating pages.
Static methods:
init(options)- Initialize self-deployloaderScript(options)- Generate embed scriptserviceWorkerCode()- Generate SW code
Options:
repo- Repository URL (required)trusted- Trusted pubkeys (required)autoReload- Auto reload on update (default: true)reloadDelay- Delay before reload in ms (default: 1000)onUpdate- Callback on updateonSync- Callback after sync
- isomorphic-git - Git in JavaScript
- @isomorphic-git/lightning-fs - IndexedDB filesystem
- nostr-tools - Nostr protocol
Optional:
- blocktrails - Bitcoin state anchoring
MIT