import createHash from "create-hash" ; import { validate } from "wallet-address-validator" ; import BTCValidator from "wallet-address-validator/src/bitcoin_validator" ; import { BitcoinNetworkConfig , BitcoinNetworkConfigMap , BitcoinNetworkInput , isBitcoinNetworkConfig , } from "./types" ; export const validateAddress = ( address , asset , network ) : boolean => { if ( asset === "DGB" ) { const currency = { name : "digibyte" , symbol : "dgb" , addressTypes : { prod : [ "1e" , "3f" ] , testnet : [ "7e" , "8c" ] } , validator : BTCValidator , segwitHrp : network === "prod" ? "dgb" : "dgbt" , } ; return currency . validator . isValidAddress ( address , currency , network ) ; } return validate ( address , asset , network ) ; } ; export const StandardBitcoinExplorer = ( baseUrl ) : BitcoinNetworkConfig [ "explorer" ] => ( { url : baseUrl , address : ( address ) => `${ baseUrl . replace ( /\/$/ , "" ) }/address/${ address }` , transaction : ( transaction ) => `${ baseUrl . replace ( /\/$/ , "" ) }/tx/${ transaction || "" }` , } ) ; export const SoChainExplorer = ( chainPath , chainId ) : BitcoinNetworkConfig [ "explorer" ] => ( { url : `https://sochain.com/${ chainPath }` , address : ( address ) => `https://sochain.com/address/${ chainId }/${ address }` , transaction : ( transaction ) => `https://sochain.com/tx/${ chainId }/${ transaction }` , } ) ; export const resolveBitcoinNetworkConfig = ( configMap , renNetwork ) : BitcoinNetworkConfig => { let networkConfig ; if ( renNetwork && isBitcoinNetworkConfig ( renNetwork ) ) { networkConfig = renNetwork ; } else { networkConfig = configMap [ renNetwork ] ; } if ( ! networkConfig ) { throw new Error ( `Unsupported network '${ String ( renNetwork ? typeof renNetwork === "string" ? renNetwork : renNetwork . selector : renNetwork ) }'. Valid options are 'mainnet', 'testnet' or a BitcoinNetworkConfig object.` ) ; } return networkConfig ; } ; /** Calculate the sha256 hash of the input. */ export const sha256 = ( input ) : Buffer => createHash ( "sha256" ) . update ( input ) . digest ( ) ; /** Calculate the ripemd160 hash of the input. */ export const ripemd160 = ( input ) : Buffer => createHash ( "rmd160" ) . update ( input ) . digest ( ) ; /**
 * hash160 is used to calculate the Bitcoin address from a private key, and is
 * equivalent to `ripemd160(sha256(publicKey))`
 */ export const hash160 = ( publicKey ) : Buffer => ripemd160 ( sha256 ( publicKey ) ) ;