From ddef67832df0b98c221ce89794d4bc3abf83bc4a Mon Sep 17 00:00:00 2001 From: Tim Oxley Date: Thu, 11 Mar 2021 10:12:22 -0500 Subject: [PATCH] Fix line lengths, unify dataunion address validation messages. --- src/Config.ts | 8 +++- src/Connection.ts | 4 +- src/dataunion/Contracts.ts | 43 +++++++++----------- src/index.ts | 5 ++- test/benchmarks/publish.js | 3 -- test/benchmarks/subscribe.js | 1 - test/integration/dataunion/signature.test.ts | 2 +- test/integration/dataunion/stats.test.ts | 7 +++- 8 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/Config.ts b/src/Config.ts index 03bd9c89d..e67c17afc 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -48,7 +48,9 @@ export type StrictStreamrClientOptions = { } } -export type StreamrClientOptions = Partial & { dataUnion: Partial}> +export type StreamrClientOptions = Partial & { + dataUnion: Partial +}> const { ControlMessage } = ControlLayer const { StreamMessage } = MessageLayer @@ -87,7 +89,9 @@ export default function ClientConfig(opts: StreamrClientOptions = {}) { tokenAddress: '0x0Cf0Ee63788A0849fE5297F3407f701E122cC023', dataUnion: { minimumWithdrawTokenWei: '1000000', // Threshold value set in AMB configs, smallest token amount to pass over the bridge - freeWithdraw: false, // if someone else pays for the gas when transporting the withdraw tx to mainnet; otherwise the client does the transport as self-service and pays the mainnet gas costs + // if someone else pays for the gas when transporting the withdraw tx to mainnet; + // otherwise the client does the transport as self-service and pays the mainnet gas costs + freeWithdraw: false, factoryMainnetAddress: '0x7d55f9981d4E10A193314E001b96f72FCc901e40', factorySidechainAddress: '0x1b55587Beea0b5Bc96Bb2ADa56bD692870522e9f', templateMainnetAddress: '0x5FE790E3751dd775Cb92e9086Acd34a2adeB8C7b', diff --git a/src/Connection.ts b/src/Connection.ts index f593a4620..3b2a6977e 100644 --- a/src/Connection.ts +++ b/src/Connection.ts @@ -314,7 +314,9 @@ export default class Connection extends EventEmitter { constructor(options = {}, debug?: Debug.Debugger) { super() - this._debug = (debug !== undefined) ? debug.extend(counterId(this.constructor.name)) : Debug(`StreamrClient::${counterId(this.constructor.name)}`) + this._debug = debug !== undefined + ? debug.extend(counterId(this.constructor.name)) + : Debug(`StreamrClient::${counterId(this.constructor.name)}`) this.options = options this.options.autoConnect = !!this.options.autoConnect diff --git a/src/dataunion/Contracts.ts b/src/dataunion/Contracts.ts index f4f65f3c6..fa87273bf 100644 --- a/src/dataunion/Contracts.ts +++ b/src/dataunion/Contracts.ts @@ -14,6 +14,12 @@ import { StreamrClient } from '../StreamrClient' const log = debug('StreamrClient::DataUnion') +function validateAddress(name: string, address: EthereumAddress) { + if (!isAddress(address)) { + throw new Error(`${name} is ${address ? 'not a valid Ethereum address' : 'missing'}`) + } +} + export class Contracts { ethereum: StreamrEthereum @@ -41,13 +47,8 @@ export class Contracts { } getDataUnionMainnetAddress(dataUnionName: string, deployerAddress: EthereumAddress) { - if (!isAddress(this.factoryMainnetAddress)) { - throw new Error('StreamrClient factoryMainnetAddress configuration is ' + (this.factoryMainnetAddress ? 'not a valid Ethereum address' : 'missing')) - } - - if (!isAddress(this.templateMainnetAddress)) { - throw new Error('StreamrClient templateMainnetAddress configuration is ' + (this.templateMainnetAddress ? 'not a valid Ethereum address' : 'missing')) - } + validateAddress('StreamrClient factoryMainnetAddress', this.factoryMainnetAddress) + validateAddress('StreamrClient templateMainnetAddress', this.templateMainnetAddress) // This magic hex comes from https://github.com/streamr-dev/data-union-solidity/blob/master/contracts/CloneLib.sol#L19 const codeHash = keccak256(`0x3d602d80600a3d3981f3363d3d373d3d3d363d73${this.templateMainnetAddress.slice(2)}5af43d82803e903d91602b57fd5bf3`) const salt = keccak256(defaultAbiCoder.encode(['string', 'address'], [dataUnionName, deployerAddress])) @@ -61,24 +62,18 @@ export class Contracts { } getDataUnionSidechainAddress(mainnetAddress: EthereumAddress) { - if (!isAddress(this.factorySidechainAddress)) { - throw new Error('StreamrClient factorySidechainAddress configuration is ' + (this.factorySidechainAddress ? 'not a valid Ethereum address' : 'missing')) - } - - if (!isAddress(this.templateSidechainAddress)) { - throw new Error('StreamrClient templateSidechainAddress configuration is ' + (this.templateSidechainAddress ? 'not a valid Ethereum address' : 'missing')) - } + validateAddress('StreamrClient factorySidechainAddress', this.factorySidechainAddress) + validateAddress('StreamrClient templateSidechainAddress', this.templateSidechainAddress) // This magic hex comes from https://github.com/streamr-dev/data-union-solidity/blob/master/contracts/CloneLib.sol#L19 - const codeHash = keccak256(`0x3d602d80600a3d3981f3363d3d373d3d3d363d73${this.templateSidechainAddress.slice(2)}5af43d82803e903d91602b57fd5bf3`) + const code = `0x3d602d80600a3d3981f3363d3d373d3d3d363d73${this.templateSidechainAddress.slice(2)}5af43d82803e903d91602b57fd5bf3` + const codeHash = keccak256(code) return getCreate2Address(this.factorySidechainAddress, hexZeroPad(mainnetAddress, 32), codeHash) } getMainnetContractReadOnly(contractAddress: EthereumAddress) { - if (isAddress(contractAddress)) { - const provider = this.ethereum.getMainnetProvider() - return new Contract(contractAddress, dataUnionMainnetABI, provider) - } - throw new Error(`${contractAddress} was not a good Ethereum address`) + validateAddress('contractAddress', contractAddress) + const provider = this.ethereum.getMainnetProvider() + return new Contract(contractAddress, dataUnionMainnetABI, provider) } getMainnetContract(contractAddress: EthereumAddress) { @@ -298,12 +293,12 @@ export class Contracts { throw new Error(`Mainnet data union "${duName}" contract ${duMainnetAddress} already exists!`) } - if (!isAddress(this.factoryMainnetAddress)) { - throw new Error('StreamrClient has invalid factoryMainnetAddress configuration.') - } + validateAddress('StreamrClient factoryMainnetAddress', this.factoryMainnetAddress) if (await mainnetProvider.getCode(this.factoryMainnetAddress) === '0x') { - throw new Error(`Data union factory contract not found at ${this.factoryMainnetAddress}, check StreamrClient.options.dataUnion.factoryMainnetAddress!`) + throw new Error( + `Data union factory contract not found at ${this.factoryMainnetAddress}, check StreamrClient.options.dataUnion.factoryMainnetAddress!` + ) } const factoryMainnet = new Contract(this.factoryMainnetAddress!, factoryMainnetABI, mainnetWallet) diff --git a/src/index.ts b/src/index.ts index bb4b4c70d..1f6d66e9b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,10 @@ export * from './dataunion/DataUnion' export * from './rest/authFetch' export * from './types' -// TODO should export these to support StreamMessageAsObject: export { StreamMessageType, ContentType, EncryptionType, SignatureType } from 'streamr-client-protocol/dist/src/protocol/message_layer/StreamMessage' +// TODO should export these to support StreamMessageAsObject: +// export { +// StreamMessageType, ContentType, EncryptionType, SignatureType +// } from 'streamr-client-protocol/dist/src/protocol/message_layer/StreamMessage' export { BigNumber } from '@ethersproject/bignumber' export { ConnectionInfo } from '@ethersproject/web' export { Contract } from '@ethersproject/contracts' diff --git a/test/benchmarks/publish.js b/test/benchmarks/publish.js index a98a158da..131cca6b4 100644 --- a/test/benchmarks/publish.js +++ b/test/benchmarks/publish.js @@ -1,13 +1,10 @@ const { format } = require('util') - const { Benchmark } = require('benchmark') // eslint-disable-next-line import/no-unresolved const StreamrClient = require('../../dist') const config = require('../integration/config') -console.log('StreamrClient', { StreamrClient }) - /* eslint-disable no-console */ let count = 100000 // pedantic: use large initial number so payload size is similar diff --git a/test/benchmarks/subscribe.js b/test/benchmarks/subscribe.js index 77c8a658a..afbb911ef 100644 --- a/test/benchmarks/subscribe.js +++ b/test/benchmarks/subscribe.js @@ -1,5 +1,4 @@ const { format } = require('util') - const { Benchmark } = require('benchmark') // eslint-disable-next-line import/no-unresolved diff --git a/test/integration/dataunion/signature.test.ts b/test/integration/dataunion/signature.test.ts index 65281fc0b..9f655e856 100644 --- a/test/integration/dataunion/signature.test.ts +++ b/test/integration/dataunion/signature.test.ts @@ -67,6 +67,7 @@ describe('DataUnion signature', () => { const isValid3 = await sidechainContract.signatureIsValid(memberWallet.address, member2Wallet.address, '3000000000000000', signature3) log(`Signature for all tokens ${memberWallet.address} -> ${member2Wallet.address}: ${signature}, checked ${isValid ? 'OK' : '!!!BROKEN!!!'}`) log(`Signature for 1 token ${memberWallet.address} -> ${member2Wallet.address}: ${signature2}, checked ${isValid2 ? 'OK' : '!!!BROKEN!!!'}`) + // eslint-disable-next-line max-len log(`Signature for 0.003 tokens ${memberWallet.address} -> ${member2Wallet.address}: ${signature3}, checked ${isValid3 ? 'OK' : '!!!BROKEN!!!'}`) log(`sidechainDU(${sidechainContract.address}) token bal ${await tokenSidechain.balanceOf(sidechainContract.address)}`) @@ -74,5 +75,4 @@ describe('DataUnion signature', () => { expect(isValid2).toBe(true) expect(isValid3).toBe(true) }, 100000) - }) diff --git a/test/integration/dataunion/stats.test.ts b/test/integration/dataunion/stats.test.ts index d9bcfe911..786181d35 100644 --- a/test/integration/dataunion/stats.test.ts +++ b/test/integration/dataunion/stats.test.ts @@ -56,7 +56,12 @@ describe('DataUnion stats', () => { }, 150000) it('member stats', async () => { - const memberStats = await Promise.all(activeMemberAddressList.concat([inactiveMember]).map((m) => queryClient.getDataUnion(dataUnion.getAddress()).getMemberStats(m))) + const memberStats = await Promise.all( + activeMemberAddressList + .concat([inactiveMember]) + .map((m) => queryClient.getDataUnion(dataUnion.getAddress()).getMemberStats(m)) + ) + const ZERO = BigNumber.from(0) expect(memberStats).toMatchObject([{ status: MemberStatus.ACTIVE,