Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ZeroNet #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"base58check": "^2.0.0",
"bech32": "^1.1.3",
"bs58": "^4.0.1",
"content-hash": "^2.4.1",
"content-hash": "^2.4.4",
"cross-fetch": "^3.0.2",
"eth-ens-namehash": "^2.0.8",
"ethers": "4.0.36",
Expand Down
12 changes: 8 additions & 4 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', 'onion', 'onion3']
const supportedCodecs = ['ipfs-ns', 'swarm-ns', 'onion', 'onion3', 'zeronet']

export function decodeContenthash(encoded) {
let decoded, protocolType, error
Expand All @@ -20,6 +20,8 @@ export function decodeContenthash(encoded) {
protocolType = 'onion'
} else if (codec === 'onion3') {
protocolType = 'onion3'
} else if (codec === 'zeronet') {
protocolType = 'zeronet'
} else {
decoded = encoded
}
Expand Down Expand Up @@ -47,7 +49,7 @@ export function encodeContenthash(text) {
let content, contentType
let encoded = false
if (!!text) {
let matched = text.match(/^(ipfs|bzz|onion|onion3):\/\/(.*)/) || text.match(/\/(ipfs)\/(.*)/)
let matched = text.match(/^(ipfs|bzz|onion|onion3|zeronet):\/\/(.*)/) || text.match(/\/(ipfs)\/(.*)/)
if (matched) {
contentType = matched[1]
content = matched[2]
Expand All @@ -64,12 +66,14 @@ export function encodeContenthash(text) {
}
} else if (contentType === 'onion') {
if(content.length == 16) {
encoded = '0x' + contentHash.encode('onion', content);
encoded = '0x' + contentHash.encode('onion', content)
}
} else if (contentType === 'onion3') {
if(content.length == 56) {
encoded = '0x' + contentHash.encode('onion3', content);
encoded = '0x' + contentHash.encode('onion3', content)
}
} else if (contentType === 'zeronet') {
encoded = '0x' + contentHash.encode('zeronet', content)
} else {
console.warn('Unsupported protocol or invalid value', {
contentType,
Expand Down
28 changes: 28 additions & 0 deletions src/utils/contents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ describe('test contenthash utility functions for swarm', () => {
'0xbd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164'
)
})

test('encodeContentHash returns encoded hash for zeronet protocol', () => {
const encodedContentHash = encodeContenthash(
'zeronet://1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D'
)

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

describe('decodeContentHash', () => {
Expand Down Expand Up @@ -99,6 +109,16 @@ describe('test contenthash utility functions for swarm', () => {
expect(decoded.protocolType).toBe('onion3')
expect(decoded.error).toBe(undefined)
})

test('decodeContentHash returns decoded contenthash for zeronet protocol', () => {
const decoded = decodeContenthash(
'0xe6013148654c4c6f34757a6a614c65744678364e4833504d774650337162526254663344'
)

expect(decoded.decoded).toBe('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D')
expect(decoded.protocolType).toBe('zeronet')
expect(decoded.error).toBe(undefined)
})
})

describe('isValidContent', () => {
Expand Down Expand Up @@ -132,6 +152,14 @@ describe('test contenthash utility functions for swarm', () => {
expect(valid).toBe(true)
})

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

expect(valid).toBe(true)
})

test('isValidContent returns false for non hex', () => {
const valid = isValidContenthash(
'0xe40101fa011b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef31680z'
Expand Down
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://..., onion://..., onion3://...)'
return 'Enter a content hash (eg: ipfs://..., bzz://..., onion://..., onion3://..., zeronet://...)'
} else {
return 'Enter a content'
}
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2783,13 +2783,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.2.tgz#0cf68bb9ddf5f2be7961c3a85178cb85dba78cb4"
integrity sha1-DPaLud318r55YcOoUXjLhdunjLQ=

content-hash@^2.4.1:
version "2.4.3"
resolved "https://registry.yarnpkg.com/content-hash/-/content-hash-2.4.3.tgz#89e6d295bbf2c53fb69b3d8fb43ae572ac7b6096"
integrity sha512-QBfQRlBBCJP94fV8zcUMChGMKzQMAZX6rn36yldc2A16C47tWdOTYjPosyZ7/AfdyW/xB5cP3RgZIAomnUDGIA==
content-hash@^2.4.4:
version "2.4.4"
resolved "https://registry.yarnpkg.com/content-hash/-/content-hash-2.4.4.tgz#4bec87caecfcff8cf1a37645301cbef4728a083f"
integrity sha512-3FaUsqt7VR725pVxe0vIScGI5efmpryIXdVSeXafQ63fb5gRGparAQlAGxTSOiv0yRg7YeliseXuB20ByD1duQ==
dependencies:
cids "^0.6.0"
multicodec "^0.5.4"
multicodec "^0.5.5"
multihashes "^0.4.15"

content-type@~1.0.4:
Expand Down Expand Up @@ -6344,10 +6344,10 @@ multibase@~0.6.0:
dependencies:
base-x "3.0.4"

multicodec@^0.5.4:
version "0.5.4"
resolved "https://registry.yarnpkg.com/multicodec/-/multicodec-0.5.4.tgz#ab2afb0cd00cd853c5a2eecb0dd1404dc7745388"
integrity sha512-0lPLiZ58b2jyXylx2qgda9/6N0YCNIpBxRsZ8sxYayVjEKh58XyNN74VTTQOR/ZCQFgbj0CsqfyRpEDPPlOMkw==
multicodec@^0.5.5:
version "0.5.5"
resolved "https://registry.yarnpkg.com/multicodec/-/multicodec-0.5.5.tgz#55c2535b44eca9ea40a13771420153fe075bb36d"
integrity sha512-1kOifvwAqp9IdiiTKmpK2tS+LY6GHZdKpk3S2EvW4T32vlwDyA3hJoZtGauzqdedUPVNGChnTksEotVOCVlC+Q==
dependencies:
varint "^5.0.0"

Expand Down