@datafund/swarm-provenance

TypeScript SDK for storing and retrieving provenance data via the Swarm network.
Requirements
- Node.js >= 18.0.0
- viem >= 2.0.0 (optional, for blockchain anchoring only)
Installation
pnpm add @datafund/swarm-provenance
For blockchain anchoring features, also install viem:
pnpm add @datafund/swarm-provenance viem
Quick Start
import { ProvenanceClient } from '@datafund/swarm-provenance';
const client = new ProvenanceClient();
const result = await client.upload('Hello, World!', {
standard: 'my-provenance-v1',
});
console.log('Uploaded:', result.reference);
const downloaded = await client.download(result.reference);
console.log('Content:', new TextDecoder().decode(downloaded.file));
Blockchain Anchoring
import { ChainClient, fromPrivateKey } from '@datafund/swarm-provenance/chain';
const chain = new ChainClient({ chain: 'base-sepolia' });
const exists = await chain.verifyOnChain(contentHash);
const record = await chain.getDataRecord(contentHash);
import { fromEip1193Provider } from '@datafund/swarm-provenance/chain';
const signer = await fromEip1193Provider(window.ethereum);
const chain = new ChainClient({ chain: 'base-sepolia', signer });
const result = await chain.anchor(contentHash, 'dataset');
const signer = await fromPrivateKey('0x...', 'https://sepolia.base.org');
const chain = new ChainClient({ chain: 'base-sepolia', signer });
await chain.anchor(contentHash, 'dataset');
Features
- Simple API: High-level
upload() and download() methods handle the full workflow
- Automatic stamp management: Acquires stamps from the pool automatically
- Notary signing: Optional cryptographic signatures for data authenticity
- Content verification: Automatic SHA256 hash verification on download
- Blockchain anchoring: Register data hashes on-chain for immutable provenance
- Browser + Node.js: Works in both environments with native
fetch
- TypeScript first: Full type definitions included
API
ProvenanceClient
const client = new ProvenanceClient({
gatewayUrl?: string,
timeout?: number,
});
Upload
const result = await client.upload(content, {
sign?: 'notary',
standard?: string,
stampId?: string,
poolSize?: 'small' | 'medium' | 'large',
contentType?: string,
});
Download
const result = await client.download(reference, {
verify?: boolean,
});
Other Methods
await client.health();
await client.notaryInfo();
await client.poolStatus();
await client.acquireStamp('small');
Error Handling
import {
ProvenanceError,
GatewayConnectionError,
StampError,
NotaryError,
VerificationError,
} from '@datafund/swarm-provenance';
try {
await client.upload(content);
} catch (error) {
if (error instanceof StampError) {
console.error('Stamp acquisition failed:', error.message);
} else if (error instanceof GatewayConnectionError) {
console.error('Gateway error:', error.statusCode, error.message);
}
}
Advanced Usage
Low-level utilities
import {
buildMetadata,
extractContent,
verifyContentHash,
sha256Hex,
bytesToBase64,
base64ToBytes,
} from '@datafund/swarm-provenance';
const metadata = buildMetadata(content, {
stampId: 'my-stamp',
standard: 'v1',
});
const originalContent = extractContent(metadata);
const isValid = verifyContentHash(metadata);
Signature verification
import {
verifySignature,
verifyAllSignatures,
} from '@datafund/swarm-provenance';
const result = verifySignature(signature, metadata, expectedSigner);
Blockchain Anchoring (/chain)
The chain module provides on-chain data provenance via a DataProvenance smart contract. It uses viem as an optional peer dependency (see Installation).
ChainClient
import { ChainClient } from '@datafund/swarm-provenance/chain';
const chain = new ChainClient({
chain: 'base-sepolia',
rpcUrl?: string,
signer?: ChainSigner,
});
Read Operations (no signer required)
await chain.verifyOnChain(dataHash);
await chain.getDataRecord(dataHash);
await chain.getUserDataRecords('0x...');
await chain.hasAddressAccessed(dataHash, '0x...');
await chain.isAuthorizedDelegate(owner, delegate);
Write Operations (signer required)
const result = await chain.anchor(dataHash, 'dataset');
await chain.anchorFor(dataHash, 'dataset', ownerAddress);
await chain.recordAccess(dataHash);
await chain.recordTransformation(originalHash, newHash, 'filtered PII');
import { DataStatus } from '@datafund/swarm-provenance/chain';
await chain.setDataStatus(dataHash, DataStatus.RESTRICTED);
await chain.transferOwnership(dataHash, newOwnerAddress);
await chain.setDelegate(delegateAddress, true);
await chain.setDelegate(delegateAddress, false);
await chain.batchAnchor([
{ dataHash: hash1, dataType: 'dataset' },
{ dataHash: hash2, dataType: 'model' },
]);
await chain.batchRecordAccess([hash1, hash2]);
await chain.batchSetDataStatus([
{ dataHash: hash1, status: DataStatus.RESTRICTED },
]);
Signer Factories
import {
fromEip1193Provider,
fromPrivateKey,
fromViemWalletClient,
} from '@datafund/swarm-provenance/chain';
const signer = await fromEip1193Provider(window.ethereum);
const signer = await fromPrivateKey('0x...', 'https://sepolia.base.org');
const signer = fromViemWalletClient(walletClient);
Chain Error Handling
import {
ChainConnectionError,
ChainTransactionError,
DataNotRegisteredError,
SignerRequiredError,
} from '@datafund/swarm-provenance/chain';
try {
await chain.anchor(hash, 'dataset');
} catch (error) {
if (error instanceof SignerRequiredError) {
console.error('Connect a wallet first');
} else if (error instanceof ChainTransactionError) {
console.error('Transaction failed:', error.txHash);
}
}
Supported Networks
| Base Sepolia (testnet) | base-sepolia | 0x9a3c6F47B69211F05891CCb7aD33596290b9fE64 |
| Base (mainnet) | base | Not yet deployed |
Demo App
A reference React app is available at examples/web-app/ with upload, download, notary signing, and blockchain anchoring:
cd examples/web-app
pnpm install
pnpm dev
Open http://localhost:5173 to try the full workflow.
Development
pnpm install
pnpm build
pnpm test
pnpm test:integration
cd examples/web-app && pnpm test
pnpm typecheck
pnpm lint
Contributing
Contributions are welcome. Please open an issue first to discuss what you'd like to change.
- Fork the repo
- Create a feature branch from
development (git checkout -b feature/my-feature development)
- Commit your changes
- Push and open a PR against
development
All PRs to main require a review. See the development section for build and test commands.
Related Projects
License
MIT