Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!isString(txHash)) {
throw new TypeError('Transaction hash should be a string')
}
if (!(/^[A-Fa-f0-9]+$/.test(txHash))) {
throw new TypeError('Transaction hash should be a valid hex string')
}
const transaction = await this.client.getMethod('getTransactionByHash')(txHash)
if (transaction) {
const valid = this.validateTransaction(transaction)
if (!valid) {
const errors = this.validateTransaction.errors
throw new InvalidProviderResponseError(`Provider returned an invalid transaction: ${errors[0].dataPath} ${errors[0].message}`)
}
}
return transaction
}
if (!isString(blockHash)) {
throw new TypeError('Block hash should be a string')
}
if (!(/^[A-Fa-f0-9]+$/.test(blockHash))) {
throw new TypeError('Block hash should be a valid hex string')
}
if (!isBoolean(includeTx)) {
throw new TypeError('Second parameter should be boolean')
}
const block = await this.client.getMethod('getBlockByHash')(blockHash, includeTx)
if (!this.validateBlock(block)) {
throw new InvalidProviderResponseError('Provider returned an invalid block')
}
return block
}
async generateBlock (numberOfBlocks) {
if (!isNumber(numberOfBlocks)) {
throw new TypeError('First argument should be a number')
}
const blockHashes = await this.client.getMethod('generateBlock')(numberOfBlocks)
if (!isArray(blockHashes)) {
throw new InvalidProviderResponseError('Response should be an array')
}
const invalidBlock = find(blockHashes, blockHash => !(/^[A-Fa-f0-9]+$/.test(blockHash)))
if (invalidBlock) {
throw new InvalidProviderResponseError('Invalid block(s) found in provider\'s reponse')
}
return blockHashes
}
async generateBlock (numberOfBlocks) {
if (!isNumber(numberOfBlocks)) {
throw new TypeError('First argument should be a number')
}
const blockHashes = await this.client.getMethod('generateBlock')(numberOfBlocks)
if (!isArray(blockHashes)) {
throw new InvalidProviderResponseError('Response should be an array')
}
const invalidBlock = find(blockHashes, blockHash => !(/^[A-Fa-f0-9]+$/.test(blockHash)))
if (invalidBlock) {
throw new InvalidProviderResponseError('Invalid block(s) found in provider\'s reponse')
}
return blockHashes
}
async getBlockByNumber (blockNumber, includeTx = false) {
if (!isNumber(blockNumber)) {
throw new TypeError('Invalid Block number')
}
if (!isBoolean(includeTx)) {
throw new TypeError('Second parameter should be boolean')
}
const block = await this.client.getMethod('getBlockByNumber')(blockNumber, includeTx)
const valid = this.validateBlock(block)
if (!valid) {
const errors = this.validateBlock.errors
throw new InvalidProviderResponseError(`Provider returned an invalid block, ${errors[0].dataPath} ${errors[0].message}`)
}
return block
}
async getAddresses (startingIndex = 0, numAddresses = 1, change = false) {
const addresses = await this.client.getMethod('getAddresses')(startingIndex, numAddresses, change)
if (!isArray(addresses)) {
throw new InvalidProviderResponseError('Provider returned an invalid response')
}
return addresses
}
async getBalance (addresses) {
if (!isArray(addresses)) {
addresses = [ addresses ]
}
const balance = await this.client.getMethod('getBalance')(addresses)
if (!BigNumber.isBigNumber(balance)) {
throw new InvalidProviderResponseError('Provider returned an invalid response')
}
return balance
}
async getBlockHeight () {
const blockHeight = await this.client.getMethod('getBlockHeight')()
if (!isNumber(blockHeight)) {
throw new InvalidProviderResponseError('Provider returned an invalid block height')
}
return blockHeight
}
async sendRawTransaction (rawTransaction) {
const txHash = await this.client.getMethod('sendRawTransaction')(rawTransaction)
if (!isString(txHash)) {
throw new InvalidProviderResponseError('sendRawTransaction method should return a transaction id string')
}
return txHash
}