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: add randomUUID into fuel-ts/crypto #2903

Merged
merged 11 commits into from
Aug 10, 2024
6 changes: 6 additions & 0 deletions .changeset/wet-camels-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/account": patch
"@fuel-ts/crypto": patch
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
---

feat: add randomUUID into `fuel-ts/crypto`
4 changes: 1 addition & 3 deletions packages/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
"graphql-request": "5.0.0",
"graphql-tag": "^2.12.6",
"portfinder": "^1.0.32",
"ramda": "^0.30.1",
"uuid": "^10.0.0"
"ramda": "^0.30.1"
},
"devDependencies": {
"type-fest": "^4.6.0",
Expand All @@ -79,7 +78,6 @@
"@graphql-codegen/typescript-generic-sdk": "^4.0.1",
"@graphql-codegen/typescript-operations": "^4.2.3",
"@types/ramda": "^0.30.1",
"@types/uuid": "^9.0.1",
"get-graphql-schema": "^2.1.2"
}
}
3 changes: 1 addition & 2 deletions packages/account/src/test-utils/launchNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { BYTES_32 } from '@fuel-ts/abi-coder';
import { randomBytes } from '@fuel-ts/crypto';
import { randomBytes, randomUUID } from '@fuel-ts/crypto';
import { FuelError } from '@fuel-ts/errors';
import type { SnapshotConfigs } from '@fuel-ts/utils';
import { defaultConsensusKey, hexlify, defaultSnapshotConfigs } from '@fuel-ts/utils';
import { randomUUID } from 'crypto';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs';
import os from 'os';
import path from 'path';
Expand Down
4 changes: 2 additions & 2 deletions packages/account/src/wallet/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 '@fuel-ts/utils';
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { JsonAbi } from '@fuel-ts/abi-coder';
import { Provider } from '@fuel-ts/account';
import * as setupTestProviderAndWalletsMod from '@fuel-ts/account/test-utils';
import { randomBytes, randomUUID } from '@fuel-ts/crypto';
import { FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError, safeExec } from '@fuel-ts/errors/test-utils';
import { hexlify, type SnapshotConfigs } from '@fuel-ts/utils';
import { getForcProject, waitUntilUnreachable } from '@fuel-ts/utils/test-utils';
import { randomBytes, randomUUID } from 'crypto';
import { existsSync, mkdirSync, readFileSync, rmSync } from 'fs';
import { writeFile, copyFile } from 'fs/promises';
import os from 'os';
Expand Down
2 changes: 2 additions & 0 deletions packages/crypto/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { decryptJsonWalletData, encryptJsonWalletData } from './encryptJsonWalle
import { computeHmac } from './hmac';
import { pbkdf2 } from './pbkdf2';
import { randomBytes } from './randomBytes';
import { randomUUID } from './randomUUID';
import { stringFromBuffer } from './stringFromBuffer';

const api: CryptoApi = {
Expand All @@ -23,6 +24,7 @@ const api: CryptoApi = {
computeHmac,
pbkdf2,
ripemd160,
randomUUID,
};

export default api;
5 changes: 5 additions & 0 deletions packages/crypto/src/browser/randomUUID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { CryptoApi } from '../types';

import { crypto } from './crypto';

export const randomUUID: CryptoApi['randomUUID'] = () => crypto.randomUUID();
1 change: 1 addition & 0 deletions packages/crypto/src/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const {
pbkdf2,
computeHmac,
ripemd160,
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 @@ -16,4 +16,5 @@ export const {
computeHmac,
pbkdf2,
ripemd160,
randomUUID,
} = cryptoApi;
2 changes: 2 additions & 0 deletions packages/crypto/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { decryptJsonWalletData, encryptJsonWalletData } from './encryptJsonWalle
import { computeHmac } from './hmac';
import { pbkdf2 } from './pbkdf2';
import { randomBytes } from './randomBytes';
import { randomUUID } from './randomUUID';
import { stringFromBuffer } from './stringFromBuffer';

const api: CryptoApi = {
Expand All @@ -23,6 +24,7 @@ const api: CryptoApi = {
computeHmac,
pbkdf2,
ripemd160,
randomUUID,
};

export default api;
5 changes: 5 additions & 0 deletions packages/crypto/src/node/randomUUID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { randomUUID as UUID } from 'crypto';

import type { CryptoApi } from '../types';

export const randomUUID: CryptoApi['randomUUID'] = () => UUID();
1 change: 1 addition & 0 deletions packages/crypto/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface CryptoApi {
encryptJsonWalletData(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Promise<Uint8Array>;
decryptJsonWalletData(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Promise<Uint8Array>;
computeHmac(algorithm: 'sha256' | 'sha512', key: BytesLike, data: BytesLike): string;
randomUUID(): string;
pbkdf2(
password: BytesLike,
salt: BytesLike,
Expand Down
15 changes: 15 additions & 0 deletions packages/crypto/test/randomUUID.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { randomUUID } from '..';

/**
* @group node
* @group browser
*/
describe('randomUUID', () => {
const UUID_V4_REGEX =
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-4[0-9a-fA-F]{3}\b-[89ABab][0-9a-fA-F]{3}\b-[0-9a-fA-F]{12}$/;

it('generates a V4 UUID', () => {
const uuidV4 = randomUUID();
expect(uuidV4).toMatch(UUID_V4_REGEX);
});
});
2 changes: 1 addition & 1 deletion packages/crypto/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src"]
"include": ["src", "test"]
}
17 changes: 0 additions & 17 deletions pnpm-lock.yaml

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

Loading