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¶
options-IpfsOptions: options for IPFS constructor.remoteNode-any: ipfs-api instance to remote servercache-DfsCacheInterface(optional):DfsCacheInterfaceinstancelog-Function(optional): function to use for logging:(message, level) => {...}logLevel-LogLevel(optional): messages with this level will be logged withloglogLog-LogLogInterface(optional): container for collecting log messageslogLogLevel-LogLevel(optional): messages with this level will be pushed tologLog
Returns¶
Ipfs instance
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¶
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¶
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¶
name-string: name of the added filedata-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¶
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¶
hash-string: filehash of the pinned item
Returns¶
Promise resolves to void: resolved when done.
Example¶
const fileBuffer = await runtime.dfs.pinFileHash('QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz');
get¶
dfs.get(hash, returnBuffer);
get data from ipfs by ipfs hash
Parameters¶
hash-string: ipfs hash (or bytes32 encoded) of the datareturnBuffer-bool: should the function return the plain buffer, defaults tofalse
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"