-
Notifications
You must be signed in to change notification settings - Fork 179
Replace react-native-quick-crypto with @noble/hashes #841
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,55 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11 | ||
|
|
||
| // https://docs.ethers.org/v6/cookbook/react-native/ | ||
| import { hmac } from '@noble/hashes/hmac'; | ||
| import { pbkdf2 as noblePbkdf2 } from '@noble/hashes/pbkdf2'; | ||
| import { sha256 as nobleSha256 } from '@noble/hashes/sha256'; | ||
| import { sha512 as nobleSha512 } from '@noble/hashes/sha512'; | ||
| import { ethers } from 'ethers'; | ||
| import crypto from 'react-native-quick-crypto'; | ||
|
|
||
| ethers.randomBytes.register(length => { | ||
| return new Uint8Array(crypto.randomBytes(length)); | ||
| }); | ||
| function randomBytes(length: number): Uint8Array { | ||
| if (typeof globalThis.crypto?.getRandomValues !== 'function') { | ||
| throw new Error('globalThis.crypto.getRandomValues is not available'); | ||
| } | ||
| return globalThis.crypto.getRandomValues(new Uint8Array(length)); | ||
| } | ||
|
|
||
| ethers.computeHmac.register((algo, key, data) => { | ||
| return crypto.createHmac(algo, key).update(data).digest(); | ||
| }); | ||
| function computeHmac( | ||
| algo: 'sha256' | 'sha512', | ||
| key: Uint8Array, | ||
| data: Uint8Array, | ||
| ): Uint8Array { | ||
| const hash = algo === 'sha256' ? nobleSha256 : nobleSha512; | ||
| return hmac(hash, key, data); | ||
| } | ||
|
|
||
| ethers.pbkdf2.register((passwd, salt, iter, keylen, algo) => { | ||
| return crypto.pbkdf2Sync(passwd, salt, iter, keylen, algo); | ||
| }); | ||
| function pbkdf2( | ||
| password: Uint8Array, | ||
| salt: Uint8Array, | ||
| iterations: number, | ||
| keylen: number, | ||
| algo: 'sha256' | 'sha512', | ||
| ): Uint8Array { | ||
| const hash = algo === 'sha256' ? nobleSha256 : nobleSha512; | ||
| return noblePbkdf2(hash, password, salt, { c: iterations, dkLen: keylen }); | ||
| } | ||
|
|
||
| ethers.sha256.register(data => { | ||
| // @ts-expect-error | ||
| return crypto.createHash('sha256').update(data).digest(); | ||
| }); | ||
| function sha256(data: Uint8Array): Uint8Array { | ||
| return nobleSha256.create().update(data).digest(); | ||
| } | ||
|
|
||
| ethers.sha512.register(data => { | ||
| // @ts-expect-error | ||
| return crypto.createHash('sha512').update(data).digest(); | ||
| }); | ||
| function sha512(data: Uint8Array): Uint8Array { | ||
| return nobleSha512.create().update(data).digest(); | ||
| } | ||
|
|
||
| ethers.randomBytes.register(randomBytes); | ||
|
|
||
| ethers.computeHmac.register(computeHmac); | ||
|
|
||
| ethers.pbkdf2.register(pbkdf2); | ||
|
|
||
| ethers.sha256.register(sha256); | ||
|
|
||
| ethers.sha512.register(sha512); | ||
|
|
||
| export { computeHmac, pbkdf2, randomBytes, sha256, sha512 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11 | ||
|
|
||
| // Register crypto polyfills | ||
| import '../../src/utils/ethers'; | ||
|
|
||
| import { ethers } from 'ethers'; | ||
|
|
||
| describe('ethers crypto polyfills', () => { | ||
| it('randomBytes returns requested length and unique values', () => { | ||
| const a = ethers.randomBytes(16); | ||
| const b = ethers.randomBytes(16); | ||
|
|
||
| expect(a).toHaveLength(16); | ||
| expect(b).toHaveLength(16); | ||
| expect(ethers.hexlify(a)).not.toBe(ethers.hexlify(b)); | ||
| }); | ||
|
|
||
| it('computeHmac matches known vector', () => { | ||
| const result = ethers.computeHmac( | ||
| 'sha256', | ||
| ethers.toUtf8Bytes('key'), | ||
| ethers.toUtf8Bytes('data'), | ||
| ); | ||
| expect(ethers.hexlify(result)).toBe( | ||
| '0x5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0', | ||
| ); | ||
| }); | ||
|
|
||
| it('pbkdf2 derives expected key', () => { | ||
| const derived = ethers.pbkdf2( | ||
| ethers.toUtf8Bytes('password'), | ||
| ethers.toUtf8Bytes('salt'), | ||
| 1000, | ||
| 32, | ||
| 'sha256', | ||
| ); | ||
| expect(ethers.hexlify(derived)).toBe( | ||
| '0x632c2812e46d4604102ba7618e9d6d7d2f8128f6266b4a03264d2a0460b7dcb3', | ||
| ); | ||
| }); | ||
|
|
||
| it('sha256 hashes data correctly', () => { | ||
| const digest = ethers.sha256(ethers.toUtf8Bytes('hello')); | ||
| expect(ethers.hexlify(digest)).toBe( | ||
| '0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824', | ||
| ); | ||
| }); | ||
|
|
||
| it('sha512 hashes data correctly', () => { | ||
| const digest = ethers.sha512(ethers.toUtf8Bytes('hello')); | ||
| expect(ethers.hexlify(digest)).toBe( | ||
| '0x9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043', | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.