Skip to content

Commit

Permalink
replace global crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
scottym5797 committed Mar 28, 2024
1 parent 7c33fb9 commit f7dd817
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 61 deletions.
10 changes: 5 additions & 5 deletions packages/addresses/src/__tests__/public-key-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const MOCK_PUBLIC_KEY_BYTES = new Uint8Array([
describe('getAddressFromPublicKey', () => {
it('returns the public key that corresponds to a given secret key', async () => {
expect.assertions(1);
const publicKey = await crypto.subtle.importKey(
const publicKey = await require("crypto").subtle.importKey(
'raw',
MOCK_PUBLIC_KEY_BYTES,
'Ed25519',
Expand All @@ -20,7 +20,7 @@ describe('getAddressFromPublicKey', () => {
});
it('throws when the public key is non-extractable', async () => {
expect.assertions(1);
const publicKey = await crypto.subtle.importKey(
const publicKey = await require("crypto").subtle.importKey(
'raw',
MOCK_PUBLIC_KEY_BYTES,
'Ed25519',
Expand All @@ -31,7 +31,7 @@ describe('getAddressFromPublicKey', () => {
});
it('throws when called with a secret', async () => {
expect.assertions(1);
const publicKey = await crypto.subtle.generateKey(
const publicKey = await require("crypto").subtle.generateKey(
{
length: 256,
name: 'AES-GCM',
Expand Down Expand Up @@ -59,12 +59,12 @@ describe('getAddressFromPublicKey', () => {
),
])('throws when called with a $name/$__variant public key', async algorithm => {
expect.assertions(1);
const { publicKey } = await crypto.subtle.generateKey(algorithm, true, ['sign', 'verify']);
const { publicKey } = await require("crypto").subtle.generateKey(algorithm, true, ['sign', 'verify']);
await expect(() => getAddressFromPublicKey(publicKey)).rejects.toThrow();
});
it('throws when called with a private key', async () => {
expect.assertions(1);
const mockPrivateKey = await crypto.subtle.importKey(
const mockPrivateKey = await require("crypto").subtle.importKey(
'pkcs8',
new Uint8Array([
0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20, 0xf2,
Expand Down
4 changes: 2 additions & 2 deletions packages/addresses/src/program-derived-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function createProgramDerivedAddress({ programAddress, seeds }: ProgramDer
}, [] as number[]);
const base58EncodedAddressCodec = getAddressCodec();
const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);
const addressBytesBuffer = await crypto.subtle.digest(
const addressBytesBuffer = await require("crypto").subtle.digest(
'SHA-256',
new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES]),
);
Expand Down Expand Up @@ -163,7 +163,7 @@ export async function createAddressWithSeed({ baseAddress, programAddress, seed
throw new SolanaError(SOLANA_ERROR__ADDRESSES__PDA_ENDS_WITH_PDA_MARKER);
}

const addressBytesBuffer = await crypto.subtle.digest(
const addressBytesBuffer = await require("crypto").subtle.digest(
'SHA-256',
new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes]),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/addresses/src/public-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export async function getAddressFromPublicKey(publicKey: CryptoKey): Promise<Add
if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'Ed25519') {
throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_ED25519_PUBLIC_KEY);
}
const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);
const publicKeyBytes = await require("crypto").subtle.exportKey('raw', publicKey);
return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
}
2 changes: 1 addition & 1 deletion packages/codecs-strings/src/__benchmarks__/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const bench = new Bench({
const bytes32 = new Uint8Array(32);
let base58EncodedString: string;
function randomizeBytes() {
crypto.getRandomValues(bytes32);
require("crypto").getRandomValues(bytes32);
}
const base58Codec = getBase58Codec();

Expand Down
2 changes: 1 addition & 1 deletion packages/errors/src/__tests__/context-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function getTestContext() {
f: Symbol('hi'),
g: { foo: 'bar' },
h: new URL('http://anza.xyz'),
i: ((await crypto.subtle.generateKey('Ed25519', false /* extractable */, ['sign', 'verify'])) as CryptoKeyPair)
i: ((await require("crypto").subtle.generateKey('Ed25519', false /* extractable */, ['sign', 'verify'])) as CryptoKeyPair)
.privateKey,
j: Object.create(null),
k: null,
Expand Down
2 changes: 1 addition & 1 deletion packages/keys/src/__benchmarks__/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function generateKeyPairForTest() {
let randomBytes: Uint8Array;
function generateRandomBytesForTest() {
randomBytes ||= new Uint8Array(32);
crypto.getRandomValues(randomBytes);
require("crypto").getRandomValues(randomBytes);
}

let signature: SignatureBytes;
Expand Down
4 changes: 2 additions & 2 deletions packages/keys/src/__tests__/private-key-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('createPrivateKeyFromBytes', () => {
});
it('can be used to produce the expected signature', async () => {
expect.assertions(1);
const signature = await crypto.subtle.sign('Ed25519', privateKey, MOCK_DATA);
const signature = await require("crypto").subtle.sign('Ed25519', privateKey, MOCK_DATA);
expect(new Uint8Array(signature)).toEqual(MOCK_DATA_SIGNATURE);
});
});
Expand All @@ -54,7 +54,7 @@ describe('createPrivateKeyFromBytes', () => {
if (expectedExtractability) {
it('can be exported', async () => {
expect.assertions(1);
expect(new Uint8Array(await crypto.subtle.exportKey('pkcs8', privateKey))).toEqual(
expect(new Uint8Array(await require("crypto").subtle.exportKey('pkcs8', privateKey))).toEqual(
// prettier-ignore
new Uint8Array([
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/keys/src/__tests__/signatures-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('sign', () => {
});
it('produces signatures 64 bytes in length', async () => {
expect.assertions(1);
const { privateKey } = (await crypto.subtle.generateKey('Ed25519', /* extractable */ false, [
const { privateKey } = (await require("crypto").subtle.generateKey('Ed25519', /* extractable */ false, [
'sign',
])) as CryptoKeyPair;
const signature = await signBytes(privateKey, MOCK_DATA);
Expand All @@ -163,7 +163,7 @@ describe('sign', () => {
describe('verify', () => {
let mockPublicKey: CryptoKey;
beforeEach(async () => {
mockPublicKey = await crypto.subtle.importKey(
mockPublicKey = await require("crypto").subtle.importKey(
'raw',
MOCK_PUBLIC_KEY_BYTES,
'Ed25519',
Expand Down
6 changes: 3 additions & 3 deletions packages/keys/src/key-pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { signBytes, verifySignature } from './signatures';

export async function generateKeyPair(): Promise<CryptoKeyPair> {
await assertKeyGenerationIsAvailable();
const keyPair = await crypto.subtle.generateKey(
const keyPair = await require("crypto").subtle.generateKey(
/* algorithm */ 'Ed25519', // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20
/* extractable */ false, // Prevents the bytes of the private key from being visible to JS.
/* allowed uses */ ['sign', 'verify'],
Expand All @@ -25,13 +25,13 @@ export async function createKeyPairFromBytes(bytes: Uint8Array, extractable?: bo
throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { byteLength: bytes.byteLength });
}
const [publicKey, privateKey] = await Promise.all([
crypto.subtle.importKey('raw', bytes.slice(32), 'Ed25519', /* extractable */ true, ['verify']),
require("crypto").subtle.importKey('raw', bytes.slice(32), 'Ed25519', /* extractable */ true, ['verify']),
createPrivateKeyFromBytes(bytes.slice(0, 32), extractable),
]);

// Verify the key pair
const randomBytes = new Uint8Array(32);
crypto.getRandomValues(randomBytes);
require("crypto").getRandomValues(randomBytes);
const signedData = await signBytes(privateKey, randomBytes);
const isValid = await verifySignature(publicKey, signedData, randomBytes);
if (!isValid) {
Expand Down
2 changes: 1 addition & 1 deletion packages/keys/src/private-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?:
});
}
const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
return await crypto.subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);
return await require("crypto").subtle.importKey('pkcs8', privateKeyBytesPkcs8, 'Ed25519', extractable ?? false, ['sign']);
}
2 changes: 1 addition & 1 deletion packages/rpc-api/src/__tests__/request-airdrop-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('requestAirdrop', () => {
it('returns the signature of the airdrop', async () => {
expect.assertions(1);
const randomBytes = new Uint8Array(32);
crypto.getRandomValues(randomBytes);
require("crypto").getRandomValues(randomBytes);
const publicKeyAddress = getBase58Decoder().decode(randomBytes);
const resultPromise = rpc
.requestAirdrop(publicKeyAddress as Address, 5000000n as LamportsUnsafeBeyond2Pow53Minus1, {
Expand Down
18 changes: 9 additions & 9 deletions packages/rpc-api/src/__tests__/send-transaction-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('sendTransaction', () => {
feePayerAddressBytes: MOCK_PUBLIC_KEY_BYTES,
memoString: `Hello from the web3.js tests! [${performance.now()}]`,
});
const signature = new Uint8Array(await crypto.subtle.sign('Ed25519', secretKey, message));
const signature = new Uint8Array(await require("crypto").subtle.sign('Ed25519', secretKey, message));
const resultPromise = rpc
.sendTransaction(
Buffer.from(
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('sendTransaction', () => {
memoString: `Hello from the web3.js tests! [${performance.now()}]`,
version: 0xfe, // Version 126
});
const signature = new Uint8Array(await crypto.subtle.sign('Ed25519', secretKey, message));
const signature = new Uint8Array(await require("crypto").subtle.sign('Ed25519', secretKey, message));
const resultPromise = rpc
.sendTransaction(
Buffer.from(
Expand All @@ -195,7 +195,7 @@ describe('sendTransaction', () => {
expect.assertions(1);
const secretKey = await getSecretKey(MOCK_PRIVATE_KEY_BYTES);
const message = new Uint8Array([4, 5, 6]);
const signature = new Uint8Array(await crypto.subtle.sign('Ed25519', secretKey, message));
const signature = new Uint8Array(await require("crypto").subtle.sign('Ed25519', secretKey, message));
const resultPromise = rpc
.sendTransaction(
Buffer.from(
Expand All @@ -220,11 +220,11 @@ describe('sendTransaction', () => {
expect.assertions(1);
const [[secretKey, publicKeyBytes], { value: latestBlockhash }] = await Promise.all([
(async () => {
const keyPair = (await crypto.subtle.generateKey('Ed25519', /* extractable */ false, [
const keyPair = (await require("crypto").subtle.generateKey('Ed25519', /* extractable */ false, [
'sign',
'verify',
])) as CryptoKeyPair;
return [keyPair.privateKey, new Uint8Array(await crypto.subtle.exportKey('raw', keyPair.publicKey))];
return [keyPair.privateKey, new Uint8Array(await require("crypto").subtle.exportKey('raw', keyPair.publicKey))];
})(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
Expand All @@ -233,7 +233,7 @@ describe('sendTransaction', () => {
feePayerAddressBytes: publicKeyBytes,
memoString: `Hello from the web3.js tests! [${performance.now()}]`,
});
const signature = new Uint8Array(await crypto.subtle.sign('Ed25519', secretKey, message));
const signature = new Uint8Array(await require("crypto").subtle.sign('Ed25519', secretKey, message));
const resultPromise = rpc
.sendTransaction(
Buffer.from(
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('sendTransaction', () => {
feePayerAddressBytes: MOCK_INSUFFICIENT_BALANCE_PUBLIC_KEY_BYTES,
memoString: `Hello from the web3.js tests! [${performance.now()}]`,
});
const signature = new Uint8Array(await crypto.subtle.sign('Ed25519', secretKey, message));
const signature = new Uint8Array(await require("crypto").subtle.sign('Ed25519', secretKey, message));
const resultPromise = rpc
.sendTransaction(
Buffer.from(
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('sendTransaction', () => {
feePayerAddressBytes: MOCK_PUBLIC_KEY_BYTES,
memoString: `Hello from the web3.js tests! [${performance.now()}]`,
});
const signature = new Uint8Array(await crypto.subtle.sign('Ed25519', secretKey, message));
const signature = new Uint8Array(await require("crypto").subtle.sign('Ed25519', secretKey, message));
const resultPromise = rpc
.sendTransaction(
Buffer.from(
Expand Down Expand Up @@ -333,7 +333,7 @@ describe('sendTransaction', () => {
feePayerAddressBytes: MOCK_PUBLIC_KEY_BYTES,
memoString: `Hello from the web3.js tests! [${performance.now()}]`,
});
const signature = new Uint8Array(await crypto.subtle.sign('Ed25519', secretKey, message));
const signature = new Uint8Array(await require("crypto").subtle.sign('Ed25519', secretKey, message));
const resultPromise = rpc
.sendTransaction(
Buffer.from(
Expand Down
Loading

0 comments on commit f7dd817

Please sign in to comment.