Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove uuid dependency in favor of crypto.randomUUID() #1608

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/hot-hairs-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/crypto": minor
"@fuel-ts/wallet": minor
---

- Now exporting `randomUUID` from `@fuel-ts/crypto`
- Bumping `@fuel-ts/wallet` because it's consuming `randomUUID`
21 changes: 21 additions & 0 deletions packages/crypto/src/browser/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ if (!crypto) {
);
}

if (!crypto.randomUUID) {
throw new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'crypto.randomUUID' in current browser environment.`
);
}

if (!crypto.subtle) {
throw new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'crypto.subtle' in current browser environment.`
);
}

if (!crypto.getRandomValues) {
throw new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'crypto.getRandomValues' in current browser environment.`
);
}

if (!btoa) {
throw new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
Expand Down
1 change: 1 addition & 0 deletions packages/crypto/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const api: CryptoApi = {
keccak256,
decryptJsonWalletData,
encryptJsonWalletData,
randomUUID: crypto.randomUUID,
};

export default api;
1 change: 1 addition & 0 deletions packages/crypto/src/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const {
keccak256,
decryptJsonWalletData,
encryptJsonWalletData,
randomUUID,
} = cryptoApi;
1 change: 1 addition & 0 deletions packages/crypto/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const {
keccak256,
decryptJsonWalletData,
encryptJsonWalletData,
randomUUID,
} = cryptoApi;
3 changes: 3 additions & 0 deletions packages/crypto/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import crypto from 'crypto';

import { scrypt, keccak256 } from '../shared';
import type { CryptoApi } from '../types';

Expand All @@ -18,6 +20,7 @@ const api: CryptoApi = {
keccak256,
decryptJsonWalletData,
encryptJsonWalletData,
randomUUID: crypto.randomUUID,
};

export default api;
1 change: 1 addition & 0 deletions packages/crypto/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface CryptoApi {
keccak256(data: Uint8Array): Uint8Array;
encryptJsonWalletData(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Promise<Uint8Array>;
decryptJsonWalletData(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Promise<Uint8Array>;
randomUUID(): string;
}
26 changes: 26 additions & 0 deletions packages/crypto/test/crypto-browser/btoa-undefined.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';

import { CryptoMock } from './crypto-mock';

/**
* @group node
*/
describe('throws when btoa is unavailable', () => {
test('btoa is undefined', async () => {
if (!globalThis.crypto) {
// this is for node v18 where globalThis.crypto is undefined
// these are tests for the browser environment anyways so doing this is okay
vi.stubGlobal('crypto', new CryptoMock());
}
vi.stubGlobal('btoa', undefined);

await expectToThrowFuelError(
() => import('../../src/browser/crypto'),
new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'btoa' in current browser environment.`
)
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';

import { CryptoMock } from './crypto-mock';

/**
* @group node
*/
describe('throws when crypto.getRandomValues is unavailable', () => {
test('crypto.getRandomValues is undefined', async () => {
vi.stubGlobal('crypto', new CryptoMock('getRandomValues'));

await expectToThrowFuelError(
() => import('../../src/browser/crypto'),
new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'crypto.getRandomValues' in current browser environment.`
)
);
});
});
17 changes: 17 additions & 0 deletions packages/crypto/test/crypto-browser/crypto-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as cr from 'crypto';

export class CryptoMock {
constructor(private toUndefined?: 'subtle' | 'randomUUID' | 'getRandomValues') {}

get subtle() {
return this.toUndefined === 'subtle' ? undefined : cr.subtle;
}

get randomUUID() {
return this.toUndefined === 'randomUUID' ? undefined : cr.randomUUID;
}

get getRandomValues() {
return this.toUndefined === 'getRandomValues' ? undefined : cr.getRandomValues;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';

import { CryptoMock } from './crypto-mock';

/**
* @group node
*/
describe('throws when crypto.randomUUID is unavailable', () => {
test('crypto.randomUUID is undefined', async () => {
vi.stubGlobal('crypto', new CryptoMock('randomUUID'));

await expectToThrowFuelError(
() => import('../../src/browser/crypto'),
new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'crypto.randomUUID' in current browser environment.`
)
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';

import { CryptoMock } from './crypto-mock';

/**
* @group node
*/
describe('throws when crypto.subtle is unavailable', () => {
test('crypto.subtle is undefined', async () => {
vi.stubGlobal('crypto', new CryptoMock('subtle'));

await expectToThrowFuelError(
() => import('../../src/browser/crypto'),
new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'crypto.subtle' in current browser environment.`
)
);
});
});
19 changes: 19 additions & 0 deletions packages/crypto/test/crypto-browser/crypto-undefined.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';

/**
* @group node
*/
describe('throws when crypto is unavailable', () => {
test('crypto is undefined', async () => {
vi.stubGlobal('crypto', undefined);

await expectToThrowFuelError(
() => import('../../src/browser/crypto'),
new FuelError(
ErrorCode.ENV_DEPENDENCY_MISSING,
`Could not find 'crypto' in current browser environment.`
)
);
});
});
6 changes: 1 addition & 5 deletions packages/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
"@fuel-ts/errors": "workspace:*",
"ethers": "^6.7.1",
"portfinder": "^1.0.32",
"tree-kill": "^1.2.2",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/uuid": "^9.0.1"
"tree-kill": "^1.2.2"
}
}
4 changes: 2 additions & 2 deletions packages/wallet/src/keystore-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
stringFromBuffer,
decryptJsonWalletData,
encryptJsonWalletData,
randomUUID,
} from '@fuel-ts/crypto';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { AbstractAddress } from '@fuel-ts/interfaces';
import { hexlify } from 'ethers';
import { v4 as uuidv4 } from 'uuid';

export type KeystoreWallet = {
id: string;
Expand Down Expand Up @@ -90,7 +90,7 @@ export async function encryptKeystoreWallet(

// Construct keystore.
const keystore: KeystoreWallet = {
id: uuidv4(),
id: randomUUID(),
version: 3,
address: removeHexPrefix(ownerAddress.toHexString()),
crypto: {
Expand Down
16 changes: 0 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading