Skip to content

Commit

Permalink
fix(kibisis): remove web crypto api dependency when generating uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranroneill committed Jan 30, 2024
1 parent 380bd15 commit 3c13026
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
19 changes: 8 additions & 11 deletions src/clients/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,14 @@ abstract class BaseClient {

groupTransactionsBySender(transactions: TransactionsArray) {
function groupBySender(objectArray: TxnInfo[]) {
return objectArray.reduce(
function (acc, obj) {
const sender = obj.from
if (!acc[sender]) {
acc[sender] = []
}
acc[sender].push(obj)
return acc
},
{} as Record<string, Array<TxnInfo>>
)
return objectArray.reduce(function (acc, obj) {
const sender = obj.from
if (!acc[sender]) {
acc[sender] = []
}
acc[sender].push(obj)
return acc
}, {} as Record<string, Array<TxnInfo>>)
}

const decodedGroup = transactions.reduce((acc: TxnInfo[], [type, txn], index) => {
Expand Down
3 changes: 2 additions & 1 deletion src/clients/kibisis/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type {
SignTxnsResult
} from './types'
import { DecodedSignedTransaction, DecodedTransaction } from '../../types'
import { generateUuid } from './utils'

class KibisisClient extends BaseClient {
genesisHash: string
Expand Down Expand Up @@ -153,7 +154,7 @@ class KibisisClient extends BaseClient {
}: SendRequestWithTimeoutOptions<Params>): Promise<Result | undefined> {
return new Promise<Result | undefined>((resolve, reject) => {
const channel = new BroadcastChannel(ARC_0027_CHANNEL_NAME)
const requestId = crypto.randomUUID()
const requestId = generateUuid()
// eslint-disable-next-line prefer-const
let timer: number

Expand Down
32 changes: 32 additions & 0 deletions src/clients/kibisis/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { generateUuid } from './utils'

describe(`${__dirname}/utils`, () => {
const validUuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

describe('generateUuid()', () => {
it('should generate a valid uuid using the web crypto api', () => {
const result = generateUuid()

expect(validUuidRegex.test(result)).toBe(true)
})

it('should generate a valid uuid without the web crypto api', () => {
// eslint-disable-next-line @typescript-eslint/unbound-method
const cryptoRandomUUID = crypto.randomUUID

Object.defineProperty(crypto, 'randomUUID', {
configurable: true,
value: undefined
})

const result = generateUuid()

expect(validUuidRegex.test(result)).toBe(true)

Object.defineProperty(crypto, 'randomUUID', {
value: cryptoRandomUUID
})
})
})
})
22 changes: 22 additions & 0 deletions src/clients/kibisis/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Generates a UUID version 4 string. This function attempts to use the `crypto.randomUUID()` function from the Web
* Crypto API if it is available, otherwise it uses a polyfill method.
*
* NOTE: `crypto.randomUUID()` is not available in non-secure contexts; only localhost and HTTPS.
* @returns {string} a valid UUID version 4 string.
* @see {@link https://stackoverflow.com/a/2117523}
*/
export function generateUuid(): string {
if (crypto.randomUUID) {
return crypto.randomUUID()
}

return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (value) => {
const valueAsNumber: number = parseInt(value)

return (
valueAsNumber ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (valueAsNumber / 4)))
).toString(16)
})
}

0 comments on commit 3c13026

Please sign in to comment.