Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/utils/contents.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contentHash from 'content-hash'
import { utils } from 'ethers'

const supportedCodecs = ['ipfs-ns', 'swarm-ns']
const supportedCodecs = ['ipfs-ns', 'swarm-ns', 'onion', 'onion3']

export function decodeContenthash(encoded) {
let decoded, protocolType, error
Expand All @@ -16,6 +16,10 @@ export function decodeContenthash(encoded) {
protocolType = 'ipfs'
} else if (codec === 'swarm-ns') {
protocolType = 'bzz'
} else if (codec === 'onion') {
protocolType = 'onion'
} else if (codec === 'onion3') {
protocolType = 'onion3'
} else {
decoded = encoded
}
Expand All @@ -39,7 +43,7 @@ export function encodeContenthash(text) {
let content, contentType
let encoded = false
if (!!text) {
let matched = text.match(/^(ipfs|bzz):\/\/(.*)/)
let matched = text.match(/^(ipfs|bzz|onion):\/\/(.*)/)
if (matched) {
contentType = matched[1]
content = matched[2]
Expand All @@ -50,6 +54,8 @@ export function encodeContenthash(text) {
encoded = '0x' + contentHash.fromIpfs(content)
} else if (contentType === 'bzz') {
encoded = '0x' + contentHash.fromSwarm(content)
} else if (contentType === 'onion') {//In the future insert here general encoding given by contentHash library
encoded = '0x' + contentHash.fromOnion(content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be contentHash.encode('onion', content);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

} else {
console.warn('Unsupported protocol or invalid value', {
contentType,
Expand All @@ -63,3 +69,20 @@ export function encodeContenthash(text) {
}
return encoded
}

export function validateContent(encoded) {
let codec
try {
codec = contentHash.getCodec(encoded)
} catch (e) {
console.warn(e.message)
return false
}

return (
codec === 'ipfs-ns' ||
codec === 'swarm-ns' ||
codec === 'onion' ||
codec === 'onion3'
)
}
2 changes: 1 addition & 1 deletion src/utils/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function getPlaceholder(recordType, contentType) {
return 'Enter an Ethereum address'
case 'content':
if (contentType === 'contenthash') {
return 'Enter a content hash (eg: ipfs://..., bzz://...)'
return 'Enter a content hash (eg: ipfs://..., bzz://..., onion://)'
} else {
return 'Enter a content'
}
Expand Down