IPFS

Class Name Ipfs
Implements DfsInterface
Extends Logger
Source ipfs.ts
Examples ipfs.spec.ts

This is DfsInterface implementation, that relies on the IPFS framework.

constructor

new Ipfs(options);

Creates a new IPFS instance.

Parameters

  1. options - IpfsOptions: options for IPFS constructor.
    • remoteNode - any: ipfs-api instance to remote server
    • cache - DfsCacheInterface (optional): DfsCacheInterface instance
    • log - Function (optional): function to use for logging: (message, level) => {...}
    • logLevel - LogLevel (optional): messages with this level will be logged with log
    • logLog - LogLogInterface (optional): container for collecting log messages
    • logLogLevel - LogLevel (optional): messages with this level will be pushed to logLog

Returns

Ipfs instance

Example

const ipfs = new Ipfs({
    remoteNode,
    cache
  });

ipfsHashToBytes32

dfs.ipfsHashToBytes32(hash);

convert IPFS hash to bytes 32 see https://www.reddit.com/r/ethdev/comments/6lbmhy/a_practical_guide_to_cheap_ipfs_hash_storage_in

Parameters

  1. hash - string: IPFS hash

Returns

string: bytes32 string.

Example

runtime.dfs.ipfsHashToBytes32('QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz')
// returns 0x7D5A99F603F231D53A4F39D1521F98D2E8BB279CF29BEBFD0687DC98458E7F89

bytes32ToIpfsHash

dfs.bytes32ToIpfsHash(str);

convert bytes32 to IPFS hash see https://www.reddit.com/r/ethdev/comments/6lbmhy/a_practical_guide_to_cheap_ipfs_hash_storage_in

Parameters

  1. str - string: bytes32 string

Returns

string: IPFS Hash.

Example

runtime.dfs.ipfsHashToBytes32('0x7D5A99F603F231D53A4F39D1521F98D2E8BB279CF29BEBFD0687DC98458E7F8')
// returns QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz

add

dfs.add(name, data);

add content to ipfs file content is converted to Buffer (in NodeJS) or an equivalent “polyfill” (in browsers)

Parameters

  1. name - string: name of the added file
  2. data - buffer: data (as buffer) of the added file

Returns

string: ipfs hash of the data.

Example

const fileHash = await runtime.dfs.add(
  'about-maika-1.txt',
  Buffer.from('we have a cat called "Maika"', 'utf-8'),
);
console.log(fileHash);
// Output:
// 0x695adc2137f1f069ff697aa287d0eae486521925a23482f180b3ae4e6dbf8d70

addMultiple

dfs.addMultiple(files);

Multiple files can be added at once. This way of adding should be preferred for performance reasons, when adding files, as requests are combined.

Parameters

  1. files - FileToAdd[]: array with files to add

Returns

Promise resolves to string[]: ipfs hash array of the data.

Example

const fileHashes = await runtime.dfs.addMultiple([{
    path: 'about-maika-1.txt',
    content: Buffer.from('we have a cat called "Maika"', 'utf-8'),
  }, {
    path: 'about-maika-2.txt',
    content: Buffer.from('she can be grumpy from time to time"', 'utf-8'),
  }
]);
console.dir(fileHashes);
// Output:
// [ '0x695adc2137f1f069ff697aa287d0eae486521925a23482f180b3ae4e6dbf8d70',
//   '0x6b85c8b24b59b12a630141143c05bbf40a8adc56a8753af4aa41ebacf108b2e7' ]

pinFileHash

dfs.pinFileHash(hash);

pins file hashes on ipfs cluster

Parameters

  1. hash - string: filehash of the pinned item

Returns

Promise resolves to void: resolved when done.

Example

const fileBuffer = await runtime.dfs.pinFileHash('QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz');

remove

dfs.remove(hash);

unpins file hashes on ipfs cluster

Parameters

  1. hash - string: filehash of the pinned item

Returns

Promise resolves to void: resolved when done.

Example

await runtime.dfs.remove('QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz');

unPinFileHash

dfs.unPinFileHash(hash);

unpins file hashes on ipfs cluster

Parameters

  1. hash - string: filehash of the pinned item

Returns

Promise resolves to void: resolved when done.

Example

await runtime.dfs.unPinFileHash('QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz');

get

dfs.get(hash, returnBuffer);

get data from ipfs by ipfs hash

Parameters

  1. hash - string: ipfs hash (or bytes32 encoded) of the data
  2. returnBuffer - bool: if true the method will return a raw buffer holding the data (default false)

Returns

Promise resolves to string | buffer: data as text or buffer.

Example

const fileBuffer = await runtime.dfs.get('0x695adc2137f1f069ff697aa287d0eae486521925a23482f180b3ae4e6dbf8d70');
console.log(fileBuffer.toString('utf-8'));
// Output:
// we have a cat called "Maika"

= Additional Components =

Interfaces

FileToAdd

  1. path - string: name of the added file
  2. content - buffer: data (as buffer) of the added file