-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helpers): add IPFS helper function
parses v0 ipfs hashes and returns addresses and strings. Will return a link to the blob if the hash resolves to anything other than a string or and address
- Loading branch information
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import axios from 'axios' | ||
import Web3Utils from 'web3-utils' | ||
|
||
// default endpoints to be queried | ||
const ipfsEndpoints = ['https://ipfs.autark.xyz:5001/api/v0'] | ||
|
||
export default () => | ||
async (cid, node = '', ...keys) => { | ||
node && ipfsEndpoints.push(node) | ||
let rejectedPromises = 0 | ||
const results = await Promise.all( | ||
ipfsEndpoints.map(async ipfsEndpoint => { | ||
const ipfsQuery = `${ipfsEndpoint}/cat?arg=${cid}` | ||
try { | ||
const { data } = await axios.get(ipfsQuery) | ||
return getValue(data, keys, ipfsQuery) | ||
} catch (err) { | ||
return ++rejectedPromises < ipfsEndpoints.length | ||
? null : { | ||
type: 'string', | ||
value: `failed getting data from IPFS: ${err}` | ||
} | ||
} | ||
}) | ||
) | ||
return results.reduce((finalResult, candidate) => candidate || finalResult) | ||
} | ||
|
||
const getValue = (data, keys = [], ipfsQuery) => { | ||
const key = keys.shift() | ||
if (!key) { | ||
switch (typeof data) { | ||
case 'string': | ||
return Web3Utils.isAddress(data) | ||
? { | ||
type: 'address', | ||
value: Web3Utils.toChecksumAddress(data) | ||
} | ||
: { | ||
type: 'string', | ||
value: data | ||
} | ||
default: | ||
return { | ||
type: 'string', | ||
value: ipfsQuery | ||
} | ||
} | ||
} | ||
|
||
return data[key] ? getValue(data[key], keys, ipfsQuery) : { value: `failed to find value for key: ${key}`, type: 'string' } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters