Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
"@ensdomains/ens-022": "npm:@ensdomains/[email protected]",
"@ensdomains/ethregistrar": "^1.2.2",
"@ensdomains/resolver": "^0.1.6",
"content-hash": "^2.3.2",
"content-hash": "^2.4.1",
"cross-fetch": "^3.0.2",
"eth-ens-namehash": "^2.0.8",
"ethers": "^4.0.30",
"js-sha3": "^0.8.0",
"lodash": "^4.17.11",
"node-gyp": "^5.0.2",
Copy link
Member

Choose a reason for hiding this comment

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

what are we using node-gyp and rebuild for?

Copy link
Member

Choose a reason for hiding this comment

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

What are we using node-gyp and rebuild for

"rebuild": "^0.1.2",
"web3": "1.0.0-beta.34"
}
}
17 changes: 14 additions & 3 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 contentHash from 'content-hash/src'
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,13 @@ export function encodeContenthash(text) {
encoded = '0x' + contentHash.fromIpfs(content)
} else if (contentType === 'bzz') {
encoded = '0x' + contentHash.fromSwarm(content)
} else if (contentType === 'onion') {
console.log(content.length)
if(content.length == 16) {
encoded = '0x' + contentHash.encode('onion', content);
} else if(content.length == 56) {
encoded = '0x' + contentHash.encode('onion3', content);
}
} else {
console.warn('Unsupported protocol or invalid value', {
contentType,
Expand Down
60 changes: 60 additions & 0 deletions src/utils/contents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,64 @@ describe('test contenthash utility functions', () => {

expect(valid).toBe(false)
})

test('encodeContentHash returns encoded hash', () => {
const encodedContentHash = encodeContenthash(
'onion://3g2upl4pq6kufc4m'
)

expect(encodedContentHash).toBe(
'0xbc0333673275706c347071366b756663346d'
)
})

test('decodeContentHash returns decoded contenthash', () => {
const decoded = decodeContenthash(
'0xbc0333673275706c347071366b756663346d'
)

expect(decoded.decoded).toBe(
'3g2upl4pq6kufc4m'
)
expect(decoded.protocolType).toBe('onion')
expect(decoded.error).toBe(undefined)
})

test('isValidContent returns true for real contenthash', () => {
const valid = isValidContenthash(
'0xbc0333673275706c347071366b756663346d'
)

expect(valid).toBe(true)
})

test('encodeContentHash returns encoded hash', () => {
const encodedContentHash = encodeContenthash(
'onion://p53lf57qovyuvwsc6xnrppyply3vtqm7l6pcobkmyqsiofyeznfu5uqd'
)

expect(encodedContentHash).toBe(
'0xbd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164'
)
})

test('decodeContentHash returns decoded contenthash', () => {
const decoded = decodeContenthash(
'0xbd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164'
)

expect(decoded.decoded).toBe(
'p53lf57qovyuvwsc6xnrppyply3vtqm7l6pcobkmyqsiofyeznfu5uqd'
)
expect(decoded.protocolType).toBe('onion3')
expect(decoded.error).toBe(undefined)
})

test('isValidContent returns true for real contenthash', () => {
const valid = isValidContenthash(
'0xbd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164'
)

expect(valid).toBe(true)
})
})
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