docsShare

Share

Shares access to private files with other wallets.

Signature

async share(pieceCid: string, options: ShareOptions): Promise<void>

Description

The share function grants access to private files by minting access NFTs to recipient addresses. Only private files can be shared - public files are already accessible to everyone. The function requires you to be the file owner.

Parameters

ParameterTypeRequiredDescription
pieceCidstringYesFilecoin piece CID of the file to share
optionsShareOptionsYesShare configuration options

ShareOptions

interface ShareOptions {
  recipient: string;                           // Wallet address to grant access to
  debug?: boolean;                             // Enable debug logging
  metadata?: {
    sharedBy?: string;                         // Address of the sharer
    sharedAt?: string;                         // Timestamp of sharing
    message?: string;                          // Optional message about the share
  };
}

Returns

Promise<void> - A Promise that resolves when the sharing operation is completed.

Examples

Basic Share Operation

import { Synapse } from '@filoz/synapse-sdk';
import { SynapseStorageSDK } from '@keypo/synapse-storage-sdk';
import { ethers } from 'ethers';
 
// Initialize wallet and provider
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY);
const provider = new ethers.JsonRpcProvider('https://api.calibration.node.glif.io/rpc/v1');
const signer = wallet.connect(provider);
 
// Initialize Synapse
const synapse = await Synapse.create({
  signer,
  withCDN: true,
});
 
// Initialize SDK
const sdk = new SynapseStorageSDK(synapse, {
  network: 'calibration',
  rpcUrl: 'https://api.calibration.node.glif.io/rpc/v1',
  encryption: {
    registryAddress: '0x8370eE1a51B5F31cc10E2f4d786Ff20198B10BBE',
    validationAddress: '0x35ADB6b999AbcD5C9CdF2262c7190C7b96ABcE4C',
    bundlerRpcUrl: 'https://rpc.zerodev.app/api/v3/YOUR_PROJECT_ID/chain/84532'
  },
  storage: {
    withCDN: true
  }
}, process.env.PRIVATE_KEY);
 
// Share file with another wallet
const pieceCid = 'bafk2bzacedt7j7lnpxvzfdks7ooqvzf2lgjklhkh...';
const recipientAddress = '0x1234567890123456789012345678901234567890';
 
await sdk.share(pieceCid, {
  recipient: recipientAddress,
  debug: true,
  metadata: {
    sharedBy: await signer.getAddress(),
    sharedAt: new Date().toISOString(),
    message: 'Shared via Synapse Storage SDK'
  }
});
 
console.log('File shared successfully!');
console.log('An access NFT has been minted to the recipient\'s address');

With Error Handling and Validation

async function shareFileWithValidation(pieceCid, recipientAddress) {
  try {
    // Validate inputs
    if (!pieceCid) {
      throw new Error('Piece CID is required');
    }
    
    if (!recipientAddress || !ethers.isAddress(recipientAddress)) {
      throw new Error('Valid recipient address is required');
    }
    
    console.log(`Sharing file: ${pieceCid}`);
    console.log(`Recipient: ${recipientAddress}`);
    
    // Share the file
    await sdk.share(pieceCid, {
      recipient: recipientAddress,
      debug: true,
      metadata: {
        sharedBy: await signer.getAddress(),
        sharedAt: new Date().toISOString(),
        message: 'Shared via SDK with validation'
      }
    });
    
    console.log('File shared successfully!');
    console.log('The recipient has been granted access to decrypt this file');
    console.log('An access NFT has been minted to the recipient\'s address');
    
    // Provide instructions for recipient
    console.log('\nThe recipient can now download this file using:');
    console.log(`1. Set PIECE_CID_PRIVATE=${pieceCid} in their .env file`);
    console.log('2. Run: npm run download');
    
  } catch (error) {
    if (error.message.includes('File not found')) {
      console.error('Error: Make sure the piece CID exists and you own this file');
    } else if (error.message.includes('File is public')) {
      console.error('Error: Public files don\'t need to be shared - anyone can already decrypt them');
    } else if (error.message.includes('Permission denied') || error.message.includes('not the file owner')) {
      console.error('Error: You can only share files that you own');
    } else {
      console.error('Sharing failed:', error.message);
    }
    throw error;
  }
}
 
// Usage
await shareFileWithValidation(
  'bafk2bzacedt7j7lnpxvzfdks7ooqvzf2lgjklhkh...',
  '0x1234567890123456789012345678901234567890'
);

Notes

  • Only private files can be shared - public files are already accessible to everyone
  • You must be the file owner to share a file
  • Each recipient gets an individual access NFT that grants decryption rights
  • Use list to find piece CIDs for files you want to share
  • Recipients can download shared files using the same piece CID
  • Sharing operations are recorded on-chain through NFT minting

See Also

  • list - For finding piece CIDs of your uploaded files
  • upload - For uploading and encrypting files
  • download - For downloading and decrypting files
  • delete - For permanently deleting files
  • configuration - SDK configuration options