mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 04:05:52 +00:00
Switch to using `@noble/hashes`, `@noble/curves`, `@scure/base`, `@scure/bip32`, and `@scure/bip39`. This replaces `crypto` polyfills (such as `crypto-browserify`), `create-hash`, `elliptic`, `hash.js`, `bn.js` (both versions), and their many dependencies. This also means there are 33 less dependencies downloaded when running a fresh `npm install` and will make the project much easier to maintain. This reduces the bundle size by 44% (82kb minified and gzipped) over the current 3.0 branch as well as reducing the amount of configuration required to bundle. Closes #1814, #1817, #2272, and #2306 Co-authored-by: Caleb Kniffen <ckniffen@ripple.com>
33 lines
770 B
TypeScript
33 lines
770 B
TypeScript
import { createHash } from 'crypto'
|
|
import { Hash, HashFn, Input } from './types'
|
|
import normalizeInput from './normalizeInput'
|
|
|
|
/**
|
|
* Wrap createHash from node to provide an interface that is isomorphic
|
|
*
|
|
* @param type - the hash name
|
|
* @param fn - {createHash} the hash factory
|
|
*/
|
|
export default function wrapCryptoCreateHash(
|
|
type: string,
|
|
fn: typeof createHash,
|
|
): HashFn {
|
|
function hashFn(input: Input): Uint8Array {
|
|
return fn(type).update(normalizeInput(input)).digest()
|
|
}
|
|
|
|
hashFn.create = (): Hash => {
|
|
const hash = fn(type)
|
|
return {
|
|
update(input: Input): Hash {
|
|
hash.update(normalizeInput(input))
|
|
return this
|
|
},
|
|
digest(): Uint8Array {
|
|
return hash.digest()
|
|
},
|
|
}
|
|
}
|
|
return hashFn
|
|
}
|