Files
self/app/tests/utils/ethers.test.ts
Justin Hernandez 5305ef83fc Feat: Improved import export sorting for app and common (#833)
* save import sorting work

* remove dupe headers and fix type errors

* sort imports and exports

* fix errors from export sorting

* fix tests

* codex feedback

* fix exports

* fix exports and tweak test build

* fix export and format

* fix license headers

* fix app building and clean up test errors

* fix android local e2e test

* improve caching

* final fixes

* remove invalid option

* fix sorting and get random values loading

* fix import sorting
2025-08-06 15:18:42 -07:00

56 lines
1.7 KiB
TypeScript

// 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 { ethers } from 'ethers';
import '../../src/utils/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',
);
});
});