import * as c32check from 'c32check' ; import * as LruCache from 'lru-cache' ; type c32AddressFn = typeof c32check . c32address ; const MAX_ADDR_CACHE_SIZE = 50000 ; export const ADDR_CACHE_ENV_VAR = 'STACKS_ADDRESS_CACHE_SIZE' ; let addressLruCache ; export function getAddressLruCache ( ) { if ( addressLruCache === undefined ) { let cacheSize = MAX_ADDR_CACHE_SIZE ; const envAddrCacheVar = process . env [ ADDR_CACHE_ENV_VAR ] ; if ( envAddrCacheVar ) { cacheSize = Number . parseInt ( envAddrCacheVar ) ; } addressLruCache = new LruCache < string , string > ( { max : cacheSize } ) ; } return addressLruCache ; } const c32EncodeInjectedSymbol = Symbol ( ) ; let origC32AddressProp ; let origC32AddressFn ; function createC32AddressCache ( origFn ) { const c32addressCached = ( version , hash160hex ) => { const cacheKey = `${ version }${ hash160hex }` ; const addrCache = getAddressLruCache ( ) ; let addrVal = addrCache . get ( cacheKey ) ; if ( addrVal === undefined ) { addrVal = origFn ( version , hash160hex ) ; addrCache . set ( cacheKey , addrVal ) ; } return addrVal ; } ; Object . defineProperty ( c32addressCached , c32EncodeInjectedSymbol , { value : true } ) ; return c32addressCached ; } /**
 * Override the `c32address` function on the `c32check` module to use an LRU cache
 * where commonly used encoded address strings can be cached.
 */ export function injectC32addressEncodeCache ( ) { if ( c32EncodeInjectedSymbol in c32check . c32address ) { return ; } const c32checkModule = require ( 'c32check' ) ; const origProp = Object . getOwnPropertyDescriptor ( c32checkModule , 'c32address' ) ; if ( ! origProp ) { throw new Error ( `Could not get property descriptor for 'c32address' on module 'c32check'` ) ; } origC32AddressProp = origProp ; const origFn = origProp . get ?. ( ) ; if ( ! origFn ) { throw new Error ( `Falsy result for 'c32address' property getter on 'c32check' module` ) ; } origC32AddressFn = origFn ; const newFn = createC32AddressCache ( origFn ) ; Object . defineProperty ( c32checkModule , 'c32address' , { get : ( ) => newFn } ) ; } export function restoreC32AddressModule ( ) { if ( addressLruCache !== undefined ) { addressLruCache . reset ( ) ; addressLruCache = undefined ; } if ( origC32AddressProp !== undefined ) { const c32checkModule = require ( 'c32check' ) ; Object . defineProperty ( c32checkModule , 'c32address' , origC32AddressProp ) ; origC32AddressProp = undefined ; } }