import randomBytes from 'randombytes' import Bitcoin from '@psf/bitcoincashjs-lib' class Crypto { /** * @api Crypto.sha256() sha256() * @apiName sha256 * @apiGroup Crypto * @apiDescription Utility for creating sha256 hash digests of data * * @apiExample Example usage: * // buffer from hex * let buffer = Buffer.from('0101010101010101', 'hex') * bchjs.Crypto.sha256(buffer) * // * * // buffer from hex * let buffer = Buffer.from('031ad329b3117e1d1e2974406868e575d48cff88e8128ba0eedb10da053785033b', 'hex') * bchjs.Crypto.sha256(buffer) * // * * // buffer from hex * let buffer = Buffer.from('03123464075c7a5fa6b8680afa2c962a02e7bf071c6b2395b0ac711d462cac9354', 'hex') * bchjs.Crypto.sha256(buffer) * // * * */ // Translate address from any address format into a specific format. static sha256 (buffer) { return Bitcoin.crypto.sha256(buffer) } /** * @api Crypto.ripemd160() ripemd160() * @apiName ripemd160 * @apiGroup Crypto * @apiDescription Utility for creating ripemd160 hash digests of data * * @apiExample Example usage: * // buffer from hex * let buffer = Buffer.from('0101010101010101', 'hex') * bchjs.Crypto.ripemd160(buffer) * // * * // buffer from hex * let buffer = Buffer.from('75618d82d1f6251f2ef1f42f5f0d5040330948a707ff6d69720dbdcb00b48aab', 'hex') * bchjs.Crypto.ripemd160(buffer) * // * * // buffer from hex * let buffer = Buffer.from('978c09dd46091d1922fa01e9f4a975b91a371f26ba8399de27d53801152121de', 'hex') * bchjs.Crypto.ripemd160(buffer) * // * */ static ripemd160 (buffer) { return Bitcoin.crypto.ripemd160(buffer) } /** * @api Crypto.hash256() hash256() * @apiName hash256 * @apiGroup Crypto * @apiDescription Utility for creating double sha256 hash digests of buffer encoded data. * * @apiExample Example usage: * // buffer from hex * let buffer = Buffer.from('0101010101010101', 'hex') * bchjs.Crypto.hash256(buffer) * // * * // buffer from hex * let buffer = Buffer.from('031ad329b3117e1d1e2974406868e575d48cff88e8128ba0eedb10da053785033b', 'hex') * bchjs.Crypto.hash256(buffer) * // * * // buffer from hex * let buffer = Buffer.from('03123464075c7a5fa6b8680afa2c962a02e7bf071c6b2395b0ac711d462cac9354', 'hex') * bchjs.Crypto.hash256(buffer) * // * */ static hash256 (buffer) { return Bitcoin.crypto.hash256(buffer) } /** * @api Crypto.hash160() hash160() * @apiName hash160 * @apiGroup Crypto * @apiDescription Utility for creating ripemd160(sha256()) hash digests of buffer encoded data. * * @apiExample Example usage: * // buffer from hex * let buffer = Buffer.from('0101010101010101', 'hex') * bchjs.Crypto.hash160(buffer) * // * * // buffer from hex * let buffer = Buffer.from('031ad329b3117e1d1e2974406868e575d48cff88e8128ba0eedb10da053785033b', 'hex') * bchjs.Crypto.hash160(buffer) * // * * // buffer from hex * let buffer = Buffer.from('03123464075c7a5fa6b8680afa2c962a02e7bf071c6b2395b0ac711d462cac9354', 'hex') * bchjs.Crypto.hash160(buffer) * * */ static hash160 (buffer) { return Bitcoin.crypto.hash160(buffer) } /** * @api Crypto.randomBytes() randomBytes() * @apiName randomBytes * @apiGroup Crypto * @apiDescription Generates cryptographically strong pseudo-random data. The size argument is a number indicating the number of bytes to generate. * * @apiExample Example usage: * bchjs.Crypto.randomBytes(16) * // * * bchjs.Crypto.randomBytes(20) * // * * bchjs.Crypto.randomBytes(24) * // * * bchjs.Crypto.randomBytes(28) * // * * bchjs.Crypto.randomBytes(32) * // * */ static randomBytes (size = 16) { return randomBytes(size) } } export default Crypto