Skip to content

Commit

Permalink
Merge branch 'master' into feat/bscprod
Browse files Browse the repository at this point in the history
  • Loading branch information
yomarion committed Oct 6, 2021
2 parents c6746d4 + 45f4ffb commit fb3fe7c
Show file tree
Hide file tree
Showing 48 changed files with 1,637 additions and 787 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"build": "lerna run build",
"clean": "lerna run clean",
"build:tsc": "tsc -b packages/advanced-logic packages/data-access packages/data-format packages/epk-decryption packages/epk-signature packages/ethereum-storage packages/integration-test packages/multi-format packages/payment-detection packages/prototype-estimator packages/request-client.js packages/request-logic packages/request-node packages/smart-contracts packages/toolbox packages/transaction-manager packages/types packages/usage-examples packages/utils packages/web3-signature packages/currency",
"build:tsc": "tsc -b packages/advanced-logic packages/currency packages/data-access packages/data-format packages/epk-decryption packages/epk-signature packages/ethereum-storage packages/integration-test packages/multi-format packages/payment-detection packages/prototype-estimator packages/request-client.js packages/request-logic packages/request-node packages/smart-contracts packages/toolbox packages/transaction-manager packages/types packages/usage-examples packages/utils packages/web3-signature",
"lint": "eslint packages/**/src/**/*.ts --quiet",
"lint-staged": "lint-staged",
"lerna": "lerna",
Expand Down
4 changes: 2 additions & 2 deletions packages/advanced-logic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"test:watch": "yarn test --watch"
},
"dependencies": {
"@requestnetwork/currency": "0.8.0",
"@requestnetwork/types": "0.35.0",
"@requestnetwork/utils": "0.35.0",
"@types/node": "14.14.16",
"lodash": "4.17.21",
"wallet-address-validator": "0.2.4"
"lodash": "4.17.21"
},
"devDependencies": {
"@types/jest": "26.0.13",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CurrencyManager } from '@requestnetwork/currency';
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import AbstractExtension from '../abstract-extension';
import Utils from '@requestnetwork/utils';
import AbstractExtension from '../abstract-extension';

/**
* Core of the address based payment networks
Expand All @@ -15,7 +16,7 @@ export default abstract class AddressBasedPaymentNetwork<
public extensionId: ExtensionTypes.ID,
public currentVersion: string,
public supportedNetworks: string[],
public supportedCurrencyType: string,
public supportedCurrencyType: RequestLogicTypes.CURRENCY,
) {
super(ExtensionTypes.TYPE.PAYMENT_NETWORK, extensionId, currentVersion);
this.actions = {
Expand Down Expand Up @@ -141,7 +142,45 @@ export default abstract class AddressBasedPaymentNetwork<
};
}

protected abstract isValidAddress(_address: string, _networkName?: string): boolean;
protected isValidAddress(address: string, networkName?: string): boolean {
if (networkName) {
return this.isValidAddressForNetwork(address, networkName);
}
return this.supportedNetworks.some((network) =>
this.isValidAddressForNetwork(address, network),
);
}

protected isValidAddressForNetwork(address: string, network: string): boolean {
switch (this.supportedCurrencyType) {
case RequestLogicTypes.CURRENCY.BTC:
return this.isValidAddressForSymbolAndNetwork(
address,
network === 'testnet' ? 'BTC-testnet' : 'BTC',
network,
);
case RequestLogicTypes.CURRENCY.ETH:
case RequestLogicTypes.CURRENCY.ERC20:
return this.isValidAddressForSymbolAndNetwork(address, 'ETH', 'mainnet');
default:
throw new Error(
`Default implementation of isValidAddressForNetwork() does not support currency type ${this.supportedCurrencyType}. Please override this method if needed.`,
);
}
}

protected isValidAddressForSymbolAndNetwork(
address: string,
symbol: string,
network: string,
): boolean {
const currencyManager = CurrencyManager.getDefault();
const currency = currencyManager.from(symbol, network);
if (!currency) {
throw new Error(`Currency not found in default manager: ${symbol} / ${network}`);
}
return CurrencyManager.validateAddress(address, currency);
}

/**
* Applies add payment address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export default class AnyToErc20ProxyPaymentNetwork extends Erc20FeeProxyPaymentN
extensionId: ExtensionTypes.ID = ExtensionTypes.ID.PAYMENT_NETWORK_ANY_TO_ERC20_PROXY,
currentVersion: string = CURRENT_VERSION,
) {
super(extensionId, currentVersion, [], RequestLogicTypes.CURRENCY.ERC20);
super(
extensionId,
currentVersion,
Object.keys(supportedCurrencies),
RequestLogicTypes.CURRENCY.ERC20,
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';

import * as walletAddressValidator from 'wallet-address-validator';
import AddressBasedPaymentNetwork from '../address-based';

const CURRENT_VERSION = '0.1.0';
Expand All @@ -13,22 +11,11 @@ const BITCOIN_NETWORK = 'mainnet';
* Important: the addresses must be exclusive to the request
*/
export default class BitcoinAddressBasedPaymentNetwork extends AddressBasedPaymentNetwork {
public constructor() {
super(
ExtensionTypes.ID.PAYMENT_NETWORK_BITCOIN_ADDRESS_BASED,
CURRENT_VERSION,
[BITCOIN_NETWORK],
RequestLogicTypes.CURRENCY.BTC,
);
}

/**
* Check if a bitcoin address is valid
*
* @param address address to check
* @returns true if address is valid
*/
protected isValidAddress(address: string): boolean {
return walletAddressValidator.validate(address, 'bitcoin', 'prod');
public constructor(
public extensionId: ExtensionTypes.ID = ExtensionTypes.ID.PAYMENT_NETWORK_BITCOIN_ADDRESS_BASED,
public currentVersion: string = CURRENT_VERSION,
public supportedNetworks: string[] = [BITCOIN_NETWORK],
) {
super(extensionId, currentVersion, supportedNetworks, RequestLogicTypes.CURRENCY.BTC);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';

import * as walletAddressValidator from 'wallet-address-validator';
import AddressBasedPaymentNetwork from '../address-based';
import { ExtensionTypes } from '@requestnetwork/types';
import BitcoinAddressBasedPaymentNetwork from './mainnet-address-based';

const CURRENT_VERSION = '0.1.0';
const BITCOIN_NETWORK = 'testnet';
Expand All @@ -13,23 +11,10 @@ const BITCOIN_NETWORK = 'testnet';
* Every bitcoin transaction that reaches these addresses will be interpreted as payment or refund.
* Important: the addresses must be exclusive to the request
*/
export default class BitcoinTestnetAddressBasedPaymentNetwork extends AddressBasedPaymentNetwork {
export default class BitcoinTestnetAddressBasedPaymentNetwork extends BitcoinAddressBasedPaymentNetwork {
public constructor() {
super(
ExtensionTypes.ID.PAYMENT_NETWORK_TESTNET_BITCOIN_ADDRESS_BASED,
CURRENT_VERSION,
[BITCOIN_NETWORK],
RequestLogicTypes.CURRENCY.BTC,
);
}

/**
* Check if a bitcoin address is valid
*
* @param address address to check
* @returns true if address is valid
*/
protected isValidAddress(address: string): boolean {
return walletAddressValidator.validate(address, 'bitcoin', BITCOIN_NETWORK);
super(ExtensionTypes.ID.PAYMENT_NETWORK_TESTNET_BITCOIN_ADDRESS_BASED, CURRENT_VERSION, [
BITCOIN_NETWORK,
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';

import * as walletAddressValidator from 'wallet-address-validator';

import AddressBasedPaymentNetwork from '../address-based';

const CURRENT_VERSION = '0.1.0';
Expand All @@ -23,14 +20,4 @@ export default class Erc20AddressBasedPaymentNetwork extends AddressBasedPayment
RequestLogicTypes.CURRENCY.ERC20,
);
}

/**
* Check if an ethereum address is valid
*
* @param {string} address address to check
* @returns {boolean} true if address is valid
*/
protected isValidAddress(address: string): boolean {
return walletAddressValidator.validate(address, 'ethereum');
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import FeeReferenceBasedPaymentNetwork from '../fee-reference-based';

import * as walletAddressValidator from 'wallet-address-validator';

const CURRENT_VERSION = '0.2.0';

/**
Expand All @@ -26,18 +24,8 @@ export default class Erc20FeeProxyPaymentNetwork<
'bsctest',
'bsc',
],
public supportedCurrencyType: string = RequestLogicTypes.CURRENCY.ERC20,
public supportedCurrencyType: RequestLogicTypes.CURRENCY = RequestLogicTypes.CURRENCY.ERC20,
) {
super(extensionId, currentVersion, supportedNetworks, supportedCurrencyType);
}

/**
* Check if an ethereum address is valid
*
* @param {string} address address to check
* @returns {boolean} true if address is valid
*/
protected isValidAddress(address: string): boolean {
return walletAddressValidator.validate(address, 'ethereum');
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';

import ReferenceBasedPaymentNetwork from '../reference-based';

const CURRENT_VERSION = '0.1.0';

import * as walletAddressValidator from 'wallet-address-validator';

/**
* Implementation of the payment network to pay in ERC20 based on a reference provided to a proxy contract.
*/
Expand All @@ -16,18 +13,8 @@ export default class Erc20ProxyPaymentNetwork<
public extensionId: ExtensionTypes.ID = ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT,
public currentVersion: string = CURRENT_VERSION,
public supportedNetworks: string[] = ['mainnet', 'rinkeby', 'private'],
public supportedCurrencyType: string = RequestLogicTypes.CURRENCY.ERC20,
public supportedCurrencyType: RequestLogicTypes.CURRENCY = RequestLogicTypes.CURRENCY.ERC20,
) {
super(extensionId, currentVersion, supportedNetworks, supportedCurrencyType);
}

/**
* Check if an ethereum address is valid
*
* @param {string} address address to check
* @returns {boolean} true if address is valid
*/
protected isValidAddress(address: string): boolean {
return walletAddressValidator.validate(address, 'ethereum');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import FeeReferenceBasedPaymentNetwork from '../fee-reference-based';
import * as walletAddressValidator from 'wallet-address-validator';

const CURRENT_VERSION = '0.1.0';

Expand All @@ -17,14 +16,4 @@ export default class EthereumFeeProxyPaymentNetwork<
) {
super(extensionId, currentVersion, supportedNetworks, RequestLogicTypes.CURRENCY.ETH);
}

/**
* Check if an ethereum address is valid
*
* @param {string} address address to check
* @returns {boolean} true if address is valid
*/
protected isValidAddress(address: string): boolean {
return walletAddressValidator.validate(address, 'ethereum');
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';

import ReferenceBasedPaymentNetwork from '../reference-based';

import * as walletAddressValidator from 'wallet-address-validator';

const CURRENT_VERSION = '0.2.0';
const supportedNetworks = [
'mainnet',
Expand All @@ -29,14 +26,4 @@ export default class EthInputPaymentNetwork extends ReferenceBasedPaymentNetwork
) {
super(extensionId, currentVersion, supportedNetworks, RequestLogicTypes.CURRENCY.ETH);
}

/**
* Check if an ethereum address is valid
*
* @param {string} address address to check
* @returns {boolean} true if address is valid
*/
protected isValidAddress(address: string): boolean {
return walletAddressValidator.validate(address, 'ethereum');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default abstract class ReferenceBasedWithFeePaymentNetwork<
public extensionId: ExtensionTypes.ID,
public currentVersion: string,
public supportedNetworks: string[],
public supportedCurrencyType: string,
public supportedCurrencyType: RequestLogicTypes.CURRENCY,
) {
super(extensionId, currentVersion, supportedNetworks, supportedCurrencyType);
this.actions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ export default abstract class NativeTokenPaymentNetwork extends ReferenceBasedPa
}
return super.createCreationAction(creationParameters);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected isValidAddress(_address: string, _networkName?: string): boolean {
throw new Error(
`Default implementation of isValidAddress() does not support native tokens. Please override this method.`,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ExtensionTypes } from '@requestnetwork/types';
import { UnsupportedNetworkError } from './address-based';

import NativeTokenPaymentNetwork from './native-token';

const CURRENT_VERSION = '0.2.0';
Expand All @@ -26,25 +25,21 @@ export default class NearNativePaymentNetwork extends NativeTokenPaymentNetwork
protected isValidAddress(address: string, networkName?: string): boolean {
switch (networkName) {
case 'aurora':
return this.isValidMainnet(address);
return this.isValidMainnetAddress(address);
case 'aurora-testnet':
return this.isValidTestnet(address);
return this.isValidTestnetAddress(address);
case undefined:
return this.isValidMainnet(address) || this.isValidTestnet(address);
return this.isValidMainnetAddress(address) || this.isValidTestnetAddress(address);
default:
throw new UnsupportedNetworkError(networkName, this.supportedNetworks);
}
}

private isValidNear(address: string): boolean {
return !!address.match(/^(([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+$/);
}

private isValidTestnet(address: string): boolean {
return this.isValidNear(address) && !address.match(/\.near$/);
private isValidMainnetAddress(address: string): boolean {
return this.isValidAddressForSymbolAndNetwork(address, 'NEAR', 'aurora');
}

private isValidMainnet(address: string): boolean {
return this.isValidNear(address) && !address.match(/\.testnet$/);
private isValidTestnetAddress(address: string): boolean {
return this.isValidAddressForSymbolAndNetwork(address, 'NEAR-testnet', 'aurora-testnet');
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtensionTypes } from '@requestnetwork/types';
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import AddressBasedPaymentNetwork from './address-based';

// Regex for "at least 16 hexadecimal numbers". Used to validate the salt
Expand All @@ -15,7 +15,7 @@ export default abstract class ReferenceBasedPaymentNetwork<
public extensionId: ExtensionTypes.ID,
public currentVersion: string,
public supportedNetworks: string[],
public supportedCurrencyType: string,
public supportedCurrencyType: RequestLogicTypes.CURRENCY,
) {
super(extensionId, currentVersion, supportedNetworks, supportedCurrencyType);
}
Expand Down
3 changes: 0 additions & 3 deletions packages/advanced-logic/src/typing.d.ts

This file was deleted.

Loading

0 comments on commit fb3fe7c

Please sign in to comment.