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
…134)

* fix(kibisis): remove web crypto api dependency when generating uuid

* chore: squash

* chore: squash

* chore: squash

* chore: squash
  • Loading branch information
kieranroneill authored Jan 30, 2024
1 parent 85f8bef commit 54b43e8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
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
35 changes: 35 additions & 0 deletions src/clients/kibisis/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { generateUuid } from './utils'
import { randomUUID, getRandomValues } from 'crypto'

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', () => {
Object.defineProperty(global, 'crypto', {
configurable: true,
value: {
getRandomValues
}
})

const result = generateUuid()

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

Object.defineProperty(global, 'crypto', {
value: {
getRandomValues,
randomUUID
}
})
})
})
})
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 (global.crypto.randomUUID) {
return global.crypto.randomUUID()
}

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

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

0 comments on commit 54b43e8

Please sign in to comment.