Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/bitcore-node/src/providers/chain-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,9 @@ class ChainStateProxy implements CSP.ChainStateProvider {
streamMissingWalletAddresses(params) {
return this.get(params).streamMissingWalletAddresses(params);
}

isValid(params) {
return this.get(params).isValid(params);
}
}
export let ChainStateProvider = new ChainStateProxy();
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { StringifyJsonStream } from '../../../utils/stringifyJsonStream';
import { StateStorage } from '../../../models/state';
import { SpentHeightIndicators, CoinJSON } from '../../../types/Coin';
import { Config } from '../../../services/config';
import { Validation } from 'crypto-wallet-core';

@LoggifyClass
export class InternalStateProvider implements CSP.IChainStateService {
Expand Down Expand Up @@ -548,4 +549,44 @@ export class InternalStateProvider implements CSP.IChainStateService {
}
return locatorBlocks.map(block => block.hash);
}

public isValid(params) {
const { input } = params;

if (this.isValidBlockOrTx(input)) {
return { isValid: true, type: 'blockOrTx' };
} else if (this.isValidAddress(params)) {
return { isValid: true, type: 'addr' };
} else if (this.isValidBlockIndex(input)) {
return { isValid: true, type: 'blockOrTx' };
} else {
return { isValid: false, type: 'invalid' };
}
}

private isValidBlockOrTx(inputValue: string): boolean {
const regexp = /^[0-9a-fA-F]{64}$/;
if (regexp.test(inputValue)) {
return true;
} else {
return false;
}
}

private isValidAddress(params): boolean {
const { chain, network, input } = params;
const addr = this.extractAddress(input);
return !!Validation.validateAddress(chain, network, addr);
}

private isValidBlockIndex(inputValue): boolean {
return isFinite(inputValue);
}

private extractAddress(address: string): string {
const extractedAddress = address
.replace(/^(bitcoincash:|bchtest:|bitcoin:)/i, '')
.replace(/\?.*/, '');
return extractedAddress || address;
}
}
23 changes: 23 additions & 0 deletions packages/bitcore-node/src/routes/api/valid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import express = require('express');
const router = express.Router({ mergeParams: true });
import { ChainStateProvider } from '../../providers/chain-state';

router.get('/:input', async function(req, res) {
let { input, chain, network } = req.params;
try {
let isValid = await ChainStateProvider.isValid({
chain,
network,
input
});
return res.send(isValid);
} catch (err) {
return res.status(500).send(err);
}
});


module.exports = {
router: router,
path: '/valid'
};
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ export declare namespace CSP {
res: Response;
};

export type isValidParams = ChainNetwork & {
input: string;
}

export type Provider<T> = { get(params: { chain: string }): T };
export type ChainStateProvider = Provider<IChainStateService> & IChainStateService;
export interface IChainStateService {
Expand Down Expand Up @@ -147,6 +151,7 @@ export declare namespace CSP {
getCoinsForTx(params: { chain: string; network: string; txid: string }): Promise<CoinListingJSON>;
getLocalTip(params): Promise<IBlock | null>;
getLocatorHashes(params): Promise<any>;
isValid(params: isValidParams): {isValid: boolean, type:string};
}

type ChainStateServices = { [key: string]: IChainStateService };
Expand Down