diff --git a/cache.js b/cache.js deleted file mode 100644 index eeb9433..0000000 --- a/cache.js +++ /dev/null @@ -1,81 +0,0 @@ -const MAX_CACHE_SIZE = 100; - -const transformArrayBufferIntoStr = (str) => { - if ( - !str || - str.length === 0 || - (!isArrayBuffer(str) && typeof str !== "string") - ) { - return; - } - - let newStr = str; - - // Trim leading `0x` if present - if (newStr.slice(0, 2) === "0x") { - newStr = newStr.slice(2); - } else if (isArrayBuffer(newStr)) { - newStr = Buffer.from(newStr).toString("hex"); - } - - return newStr; -}; - -const generateDefKey = (data, to) => { - if (!data || !to) { - return; - } - - const dataStr = transformArrayBufferIntoStr(data); - const toStr = transformArrayBufferIntoStr(to); - - if (!dataStr || !toStr) { - return; - } - - return `${toStr}${dataStr.slice(0, 8)}`; -}; - -const getCachedDef = (tx, cache) => { - return cache.get(generateDefKey(tx.data, tx.to)); -}; - -const saveDefToCache = (tx, cache, def) => { - if (!tx.data || tx.data.length === 0) { - return; - } - - const key = generateDefKey(tx.data, tx.to); - if (!key) { - return; - } - - if (cache.size >= MAX_CACHE_SIZE) { - cache.delete(cache.keys().next().value); - } - cache.set(key, def); -}; - -const serializeCache = (cache) => { - return JSON.stringify([...cache]); -}; - -const deserializeCache = (cache) => { - return new Map(JSON.parse(cache)); -}; - -const isArrayBuffer = (value) => { - return ( - value && - value.buffer instanceof ArrayBuffer && - value.byteLength !== undefined - ); -}; - -module.exports = { - generateDefKey, - getCachedDef, - saveDefToCache, - serializeCache, - deserializeCache, -}; diff --git a/index.js b/index.js index 4cd3261..806efc9 100644 --- a/index.js +++ b/index.js @@ -5,12 +5,6 @@ const SDK = require('gridplus-sdk'); const EthTx = require('@ethereumjs/tx'); const { addHexPrefix } = require("@ethereumjs/util"); const rlp = require('rlp'); -const { - serializeCache, - deserializeCache, - getCachedDef, - saveDefToCache, -} = require("./cache"); const keyringType = 'Lattice Hardware'; const HARDENED_OFFSET = 0x80000000; const PER_PAGE = 5; @@ -22,7 +16,6 @@ const CONNECT_TIMEOUT = 20000; class LatticeKeyring extends EventEmitter { constructor (opts={}) { super() - this.requestCache = new Map(); this.type = keyringType; this._resetDefaults(); this.deserialize(opts); @@ -52,8 +45,6 @@ class LatticeKeyring extends EventEmitter { this.network = opts.network; if (opts.page) this.page = opts.page; - if (opts.requestCache) - this.requestCache = deserializeCache(opts.requestCache) return; } @@ -73,7 +64,6 @@ class LatticeKeyring extends EventEmitter { network: this.network, page: this.page, hdPath: this.hdPath, - requestCache: serializeCache(this.requestCache), }; } @@ -187,18 +177,9 @@ class LatticeKeyring extends EventEmitter { encodingType: SDK.Constants.SIGNING.ENCODINGS.EVM, signerPath, }; - let def; - const cachedDef = getCachedDef(tx, this.requestCache) - - if (cachedDef) { - def = cachedDef - } else { - const supportsDecoderRecursion = fwVersion.major > 0 || fwVersion.minor >=16; - // Check if we can decode the calldata - const { def: fetchedDef } = await SDK.Utils.fetchCalldataDecoder(tx.data, tx.to, chainId, supportsDecoderRecursion); - saveDefToCache(tx, this.requestCache, fetchedDef); - def = fetchedDef - } + const supportsDecoderRecursion = fwVersion.major > 0 || fwVersion.minor >=16; + // Check if we can decode the calldata + const { def } = await SDK.Utils.fetchCalldataDecoder(tx.data, tx.to, chainId, supportsDecoderRecursion); if (def) { data.decoder = def; } @@ -448,7 +429,6 @@ class LatticeKeyring extends EventEmitter { this.unlockedAccount = 0; this.network = null; this.hdPath = STANDARD_HD_PATH; - this.requestCache = new Map() } async _openConnectorTab(url) { @@ -731,7 +711,6 @@ class LatticeKeyring extends EventEmitter { } return activeWallet.uid.toString('hex'); } - } // -----