diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 8e7850f6..708dbd92 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -818,8 +818,7 @@ const createPostNew = async (tags, title, content) => { const mySecret = require('../Mediator').getMySecret() await Common.Utils.asyncForEach(content, async c => { - // @ts-expect-error - const uuid = Gun.text.random() + const uuid = Utils.gunID() newPost.contentItems[uuid] = c if ( (c.type === 'image/embedded' || c.type === 'video/embedded') && diff --git a/services/gunDB/contact-api/jobs/onOrders.js b/services/gunDB/contact-api/jobs/onOrders.js index 481778cc..b8fc07f9 100644 --- a/services/gunDB/contact-api/jobs/onOrders.js +++ b/services/gunDB/contact-api/jobs/onOrders.js @@ -212,8 +212,8 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => { logger.info( `onOrders() -> Will now place the encrypted invoice in order to response usergraph: ${addr}` ) - //@ts-expect-error - const ackNode = Gun.text.random() + + const ackNode = Utils.gunID() /** @type {import('shock-common').Schema.OrderResponse} */ const orderResponse = { diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index ab83a728..6243a933 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -233,6 +233,21 @@ const isNodeOnline = async pub => { ) } +/** + * @returns {string} + */ +const gunID = () => { + // Copied from gun internals + let s = '' + let l = 24 // you are not going to make a 0 length random number, so no need to check type + const c = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz' + while (l > 0) { + s += c.charAt(Math.floor(Math.random() * c.length)) + l-- + } + return s +} + module.exports = { dataHasSoul, delay, @@ -242,5 +257,6 @@ module.exports = { promisifyGunNode: require('./promisifygun'), timeout5, timeout2, - isNodeOnline + isNodeOnline, + gunID } diff --git a/services/gunDB/contact-api/utils/index.spec.js b/services/gunDB/contact-api/utils/index.spec.js new file mode 100644 index 00000000..228a9908 --- /dev/null +++ b/services/gunDB/contact-api/utils/index.spec.js @@ -0,0 +1,14 @@ +/** + * @format + */ +const expect = require('expect') + +const { gunID } = require('./index') + +describe('gunID()', () => { + it('generates 24-chars-long unique IDs', () => { + const id = gunID() + expect(id).toBeTruthy() + expect(id.length).toBe(24) + }) +}) diff --git a/services/gunDB/rpc/index.js b/services/gunDB/rpc/index.js index 9ff9865b..7bf64d41 100644 --- a/services/gunDB/rpc/index.js +++ b/services/gunDB/rpc/index.js @@ -17,6 +17,7 @@ const { $$__SHOCKWALLET__ENCRYPTED__ } = require('../Mediator') const logger = require('../../../config/log') +const Utils = require('../contact-api/utils') /** * @typedef {import('../contact-api/SimpleGUN').ValidDataValue} ValidDataValue * @typedef {import('./types').ValidRPCDataValue} ValidRPCDataValue @@ -266,8 +267,7 @@ async function set(rawPath, value) { if (Array.isArray(theValue)) { // we'll create a set of sets - // @ts-expect-error - const uuid = Gun.text.random() + const uuid = Utils.gunID() // here we are simulating the top-most set() const subPath = rawPath + PATH_SEPARATOR + uuid @@ -278,8 +278,7 @@ async function set(rawPath, value) { return uuid } else if (Schema.isObj(theValue)) { - // @ts-expect-error - const uuid = Gun.text.random() // we'll handle UUID ourselves + const uuid = Utils.gunID() // we'll handle UUID ourselves // so we can use our own put() diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index d5e982d5..18e8ec03 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -6,7 +6,6 @@ // @ts-check /// /// -const RealGun = require('gun') const uuid = require('uuid/v1') const mapValues = require('lodash/mapValues') const { fork } = require('child_process') @@ -16,12 +15,15 @@ const logger = require('../../config/log') const { mergePuts, isPopulated } = require('./misc') const gunUUID = () => { - const RG = /** @type {any} */ (RealGun) - if (typeof RG.text === 'object' && typeof RG.text.random === 'function') { - return RG.text.random() + // Copied from gun internals + let s = '' + let l = 24 // you are not going to make a 0 length random number, so no need to check type + const c = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz' + while (l > 0) { + s += c.charAt(Math.floor(Math.random() * c.length)) + l-- } - // This probably won't happen - throw new ReferenceError() + return s } /** diff --git a/utils/GunSmith/GunSmith.spec.js b/utils/GunSmith/GunSmith.spec.js index b8aced25..b5e90b6b 100644 --- a/utils/GunSmith/GunSmith.spec.js +++ b/utils/GunSmith/GunSmith.spec.js @@ -23,8 +23,8 @@ const instance = Gun({ }) const user = instance.user() -const alias = words() -const pass = words() +const alias = words({ exactly: 2 }).join('') +const pass = words({ exactly: 2 }).join('') /** * @param {number} ms diff --git a/utils/index.js b/utils/index.js index 09549b5d..5b619eda 100644 --- a/utils/index.js +++ b/utils/index.js @@ -1,7 +1,6 @@ /** * @format */ -const Gun = require('gun') const { asyncFilter } = require('./helpers') @@ -9,10 +8,15 @@ const { asyncFilter } = require('./helpers') * @returns {string} */ const gunUUID = () => { - // @ts-expect-error Not typed - const uuid = Gun.text.random() - - return uuid + // Copied from gun internals + let s = '' + let l = 24 // you are not going to make a 0 length random number, so no need to check type + const c = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz' + while (l > 0) { + s += c.charAt(Math.floor(Math.random() * c.length)) + l-- + } + return s } module.exports = {