Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #102 from rnsdomains/develop
Browse files Browse the repository at this point in the history
v1.8.0
  • Loading branch information
ilanolkies authored Jun 11, 2020
2 parents 70727dd + 174ba05 commit 4572449
Show file tree
Hide file tree
Showing 61 changed files with 3,317 additions and 1,295 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ module.exports = {
'eol-last': 'error',
'no-underscore-dangle': ['error', { 'allowAfterThis': true }],
'max-len': ['error', { 'code': 100, 'ignoreStrings': true, 'ignoreComments': true }],
'class-methods-use-this': ['error', { 'exceptMethods': ['_validateDomainAndLabel'] }]
'class-methods-use-this': ['error', { 'exceptMethods': ['_validateDomainAndLabel', '_createResolver', '_getCoinTypeFromChainId'] }]
}
}
1,191 changes: 1,165 additions & 26 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rsksmart/rns",
"version": "1.7.4",
"version": "1.8.0",
"description": "RIF Name Service library.",
"keywords": [
"rsk",
Expand Down Expand Up @@ -34,11 +34,14 @@
"prepublishOnly": "npm test && npm run build && cp -r lib/types/ types/"
},
"dependencies": {
"@ensdomains/address-encoder": "^0.1.7",
"@rsksmart/rns-registry": "^1.0.2",
"@rsksmart/rns-resolver": "^1.0.2",
"@rsksmart/rns-reverse": "^1.0.2",
"@rsksmart/rns-rskregistrar": "^1.2.1",
"@rsksmart/rsk3": "^0.3.4",
"buffer": "^5.6.0",
"content-hash": "^2.5.2",
"eth-ens-namehash": "^2.0.8",
"js-sha3": "^0.8.0",
"rskjs-util": "^1.0.3",
Expand All @@ -55,6 +58,7 @@
"@babel/preset-typescript": "^7.8.3",
"@openzeppelin/test-environment": "^0.1.3",
"@openzeppelin/test-helpers": "^0.5.5",
"@openzeppelin/upgrades": "^2.8.0",
"@rsksmart/erc677": "^1.0.2",
"@rsksmart/rns-auction-registrar": "^1.0.2",
"@types/jest": "^25.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/@types/content-hash/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'content-hash';
29 changes: 14 additions & 15 deletions src/composer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Web3 from 'web3';
import { TransactionReceipt } from 'web3-eth';
import {
Composable, Options, ContractAddresses, Contracts,
} from './types';
import { createRegistry, createContractAddresses } from './factories';
import RNSError, { LIBRARY_NOT_COMPOSED } from './errors';
import RNSError, { LIBRARY_NOT_COMPOSED, NO_ACCOUNTS_TO_SIGN } from './errors';
import { getCurrentAddress } from './utils';
import { TransactionOptions } from './types/options';
import ErrorWrapper from './errors/ErrorWrapper';

export default abstract class implements Composable {
export default abstract class extends ErrorWrapper implements Composable {
private _contractAddresses!: ContractAddresses;

private _composed!: boolean;
Expand All @@ -19,16 +19,9 @@ export default abstract class implements Composable {

public blockchainApi: Web3;

/**
* Create RNS library.
*
* @remarks
* If the blockchain api points to RSK Mainnet or RSK Testnet, no options are required. Contract addresses are detected automatically.
*
* @param blockchainApi - Web3 or Rsk3 instance
* @param options - Overrides network defaults. Optional on RSK Mainnet and RSK Testnet, required for other networks.
*/
constructor(blockchainApi: Web3 | any, options?: Options) {
super(options && options.lang);

this.blockchainApi = blockchainApi as Web3;

// rsk3 eth namespace are exposed in the top level namespace
Expand Down Expand Up @@ -69,15 +62,21 @@ export default abstract class implements Composable {
protected async estimateGasAndSendTransaction(
contractMethod: any,
customOptions?: TransactionOptions,
): Promise<TransactionReceipt> {
): Promise<string> {
let options: any;

if (customOptions && customOptions.from) {
options = {
from: customOptions.from,
};
} else {
const sender = await getCurrentAddress(this.blockchainApi);
let sender;

try {
sender = await getCurrentAddress(this.blockchainApi);
} catch {
this._throw(NO_ACCOUNTS_TO_SIGN);
}

options = {
from: sender,
Expand Down Expand Up @@ -108,7 +107,7 @@ export default abstract class implements Composable {
}

return new Promise((resolve, reject) => contractMethod.send(options)
.on('confirmation', (confirmations: Number, receipt: TransactionReceipt) => resolve(receipt))
.on('transactionHash', (hash: string) => resolve(hash))
.on('error', reject));
}

Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const ADDR_INTERFACE = '0x3b3b57de';
export const SET_ADDR_INTERFACE = '0xd5fa2b00';
export const CHAIN_ADDR_INTERFACE = '0x8be4b5f6';
export const SET_CHAIN_ADDR_INTERFACE = '0xd278b400';
export const NEW_ADDR_INTERFACE = '0xf1cb7e06';
export const CONTENTHASH_INTERFACE = '0xbc1c58d1';
export const NAME_INTERFACE = '0x691f3431';
export const SET_NAME_INTERFACE = '0xc47f0027';
export const AVAILABLE_INTERFACE = '0x96e494e8';
Expand Down
74 changes: 74 additions & 0 deletions src/contenthash-helper.ts
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 '';
}
}
14 changes: 14 additions & 0 deletions src/errors/ErrorWrapper.ts
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);
}
}
16 changes: 13 additions & 3 deletions src/errors/RNSError.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { DEVPORTAL_ERRORS_URL } from '../constants';
import errors from './errors.json';
import { ErrorDictionary } from '../types';
import { Lang } from '../types/enums';

export default class extends Error {
ref: string;

id: string;

constructor(errorId: string) {
constructor(errorId: string, lang = Lang.en) {
let error = (errors as ErrorDictionary)[errorId];
if (!error) {
error = {
id: 'KB000', message: 'Unknown error',
id: 'KB000',
message: {
en: 'Unknown error',
es: 'Error desconocido',
ja: '不明なエラー',
ko: '알수없는 오류',
pt: 'Erro desconhecido',
ru: 'Неизвестная ошибка',
zh: '未知错误',
},
};
}

super(error.message);
super(error.message[lang]);
this.id = error.id;
this.ref = `${DEVPORTAL_ERRORS_URL}#${error.id.toLowerCase()}`;
}
Expand Down
Loading

0 comments on commit 4572449

Please sign in to comment.