This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from rnsdomains/develop
v1.8.0
- Loading branch information
Showing
61 changed files
with
3,317 additions
and
1,295 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
declare module 'content-hash'; |
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,74 @@ | ||
// most of te code has been copied from https://github.com/ensdomains/ui/blob/b7d36a2a96f6c991a8e109f91f9bd6fb6b1f4589/src/utils/contents.js | ||
import contentHash from 'content-hash'; | ||
import ErrorWrapper from './errors/ErrorWrapper'; | ||
import { Options } from './types'; | ||
import { UNSUPPORTED_CONTENTHASH_PROTOCOL } from './errors'; | ||
|
||
export default class extends ErrorWrapper { | ||
constructor(options?: Options) { | ||
super(options && options.lang); | ||
} | ||
|
||
decodeContenthash(encoded: string) { | ||
let decoded = ''; | ||
let protocolType = ''; | ||
|
||
try { | ||
decoded = contentHash.decode(encoded); | ||
const codec = contentHash.getCodec(encoded); | ||
if (codec === 'ipfs-ns') { | ||
protocolType = 'ipfs'; | ||
} else if (codec === 'swarm-ns') { | ||
protocolType = 'bzz'; | ||
} else if (codec === 'onion') { | ||
protocolType = 'onion'; | ||
} else if (codec === 'onion3') { | ||
protocolType = 'onion3'; | ||
} else { | ||
this._throw(UNSUPPORTED_CONTENTHASH_PROTOCOL); | ||
} | ||
} catch (e) { | ||
this._throw(UNSUPPORTED_CONTENTHASH_PROTOCOL); | ||
} | ||
|
||
return { decoded, protocolType }; | ||
} | ||
|
||
encodeContenthash(text: string): string { | ||
let content = ''; | ||
let contentType = ''; | ||
if (text) { | ||
const matched = text.match(/^(ipfs|bzz|onion|onion3):\/\/(.*)/) | ||
|| text.match(/\/(ipfs)\/(.*)/); | ||
if (matched) { | ||
([, contentType, content] = matched); | ||
} | ||
|
||
try { | ||
if (contentType === 'ipfs') { | ||
if (content.length >= 4) { | ||
return `0x${contentHash.fromIpfs(content)}`; | ||
} | ||
} else if (contentType === 'bzz') { | ||
if (content.length >= 4) { | ||
return `0x${contentHash.fromSwarm(content)}`; | ||
} | ||
} else if (contentType === 'onion') { | ||
if (content.length === 16) { | ||
return `0x${contentHash.encode('onion', content)}`; | ||
} | ||
} else if (contentType === 'onion3') { | ||
if (content.length === 56) { | ||
return `0x${contentHash.encode('onion3', content)}`; | ||
} | ||
} else { | ||
this._throw(UNSUPPORTED_CONTENTHASH_PROTOCOL); | ||
} | ||
} catch (err) { | ||
this._throw(UNSUPPORTED_CONTENTHASH_PROTOCOL); | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
} |
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,14 @@ | ||
import RNSError from './RNSError'; | ||
import { Lang } from '../types/enums'; | ||
|
||
export default class { | ||
private lang: Lang; | ||
|
||
constructor(lang = Lang.en) { | ||
this.lang = lang; | ||
} | ||
|
||
protected _throw(errorId: string) { | ||
throw new RNSError(errorId, this.lang); | ||
} | ||
} |
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
Oops, something went wrong.