From 53d3b9587261eac6af0d66887c89dfe1f9b1e1fe Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 17 Dec 2024 14:05:54 +0800 Subject: [PATCH] fix: coinselect failed in extension (#4) * chore: debug * chore: debug * chore: debug * feat: add self for bakcground * chore: debug * chore: debug * chore: remove console --- package.json | 2 +- third-party/utils/getRandomInt.js | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 772e6c7..ad405db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@onekeyfe/coinselect", - "version": "3.1.13", + "version": "3.1.14", "description": "A transaction input selection module for bitcoin.", "keywords": [ "coinselect", diff --git a/third-party/utils/getRandomInt.js b/third-party/utils/getRandomInt.js index 4e4c226..143d6b0 100644 --- a/third-party/utils/getRandomInt.js +++ b/third-party/utils/getRandomInt.js @@ -18,8 +18,31 @@ const getRandomInt = (min, max) => { throw new RangeError(`This function only provide 32 bits of entropy, therefore range cannot be more then 2^32.`); } const getRandomValues = typeof window !== 'undefined' - ? (array) => window.crypto.getRandomValues(array) - : (array) => (0, crypto_1.getRandomValues)(array); + ? (array) => { + try { + return window.crypto.getRandomValues(array); + } catch (e) { + console.error('Window crypto error:', e); + throw e; + } + } + : typeof self !== 'undefined' + ? (array) => { + try { + return self.crypto.getRandomValues(array); + } catch (e) { + console.error('Self crypto error:', e); + throw e; + } + } + : (array) => { + try { + return (0, crypto_1.getRandomValues)(array); + } catch (e) { + console.error('Node crypto error:', e); + throw e; + } + }; const array = new Uint32Array(1); const maxRange = MAX_RANGE_32_BITS - (MAX_RANGE_32_BITS % range); let randomValue;