From bfcbea8616d18d4b134ebfeee392f12f698ff87c Mon Sep 17 00:00:00 2001 From: Roman Dvorkin <121502696+rdvorkin@users.noreply.github.com> Date: Mon, 2 Oct 2023 17:40:05 +0300 Subject: [PATCH 1/2] Bump cross-fetch to version 4 (#6463) * Bump cross-fetch to version 4 Fixes issues like https://github.com/lquixada/cross-fetch/issues/78, enabling to run web3.js in service worker * update web3-providers-http CHANGELOG.md --------- Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> --- packages/web3-providers-http/CHANGELOG.md | 10 +++++++++- packages/web3-providers-http/package.json | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/web3-providers-http/CHANGELOG.md b/packages/web3-providers-http/CHANGELOG.md index 3726992879d..fed7d430fc3 100644 --- a/packages/web3-providers-http/CHANGELOG.md +++ b/packages/web3-providers-http/CHANGELOG.md @@ -119,4 +119,12 @@ Documentation: - Dependencies updated -## [Unreleased] \ No newline at end of file +## [Unreleased] + +### Changed + +- Bump cross-fetch to version 4 (#6463). + +### Fixed + +- Fix issue lquixada/cross-fetch#78, enabling to run web3.js in service worker (#6463) diff --git a/packages/web3-providers-http/package.json b/packages/web3-providers-http/package.json index 0238af38ded..6cb6baaaf2b 100644 --- a/packages/web3-providers-http/package.json +++ b/packages/web3-providers-http/package.json @@ -60,7 +60,7 @@ "typescript": "^4.7.4" }, "dependencies": { - "cross-fetch": "^3.1.5", + "cross-fetch": "^4.0.0", "web3-errors": "^1.1.2", "web3-types": "^1.2.0", "web3-utils": "^4.0.6" From 90d78c1b7ffdc74f6ee71376a2b28da5bc55bc06 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 2 Oct 2023 11:06:55 -0400 Subject: [PATCH 2/2] add privateKeyToPublicKey function to accounts (#6466) --- packages/web3-eth-accounts/CHANGELOG.md | 6 ++++- packages/web3-eth-accounts/src/account.ts | 19 +++++++++++++ .../test/fixtures/account.ts | 27 ++++++++++++++++++- .../test/unit/account.test.ts | 9 +++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth-accounts/CHANGELOG.md b/packages/web3-eth-accounts/CHANGELOG.md index 1c3a8291f2e..719333003b0 100644 --- a/packages/web3-eth-accounts/CHANGELOG.md +++ b/packages/web3-eth-accounts/CHANGELOG.md @@ -139,6 +139,10 @@ Documentation: ## [Unreleased] +### Added + +- Added public function `privateKeyToPublicKey` + ### Fixed -- Fixed `recover` function, `v` will be normalized to value 0,1 (#6344) \ No newline at end of file +- Fixed `recover` function, `v` will be normalized to value 0,1 (#6344) diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index bb07577cd52..793f1f76bbc 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -386,6 +386,25 @@ export const privateKeyToAddress = (privateKey: Bytes): string => { return toChecksumAddress(`0x${address}`); }; +/** + * Get the public key from a private key + * + * @param privateKey - String or Uint8Array of 32 bytes + * @param isCompressed - if true, will generate a 33 byte compressed public key instead of a 65 byte public key + * @returns The public key + * @example + * ```ts + * privateKeyToAddress("0x1e046a882bb38236b646c9f135cf90ad90a140810f439875f2a6dd8e50fa261f", true) + * > "0x42beb65f179720abaa3ec9a70a539629cbbc5ec65bb57e7fc78977796837e537662dd17042e6449dc843c281067a4d6d8d1a1775a13c41901670d5de7ee6503a" // uncompressed public key + * ``` + */ + export const privateKeyToPublicKey = (privateKey: Bytes, isCompressed: boolean): string => { + const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); + + // Get public key from private key in compressed format + return `0x${bytesToHex(secp256k1.getPublicKey(privateKeyUint8Array, isCompressed)).slice(4)}`; // 0x and removing compression byte +}; + /** * encrypt a private key with a password, returns a V3 JSON Keystore * diff --git a/packages/web3-eth-accounts/test/fixtures/account.ts b/packages/web3-eth-accounts/test/fixtures/account.ts index 6e64595c28b..104b5688e97 100644 --- a/packages/web3-eth-accounts/test/fixtures/account.ts +++ b/packages/web3-eth-accounts/test/fixtures/account.ts @@ -24,7 +24,7 @@ import { IVLengthError, PBKDF2IterationsError, } from 'web3-errors'; -import { CipherOptions, KeyStore } from 'web3-types'; +import { CipherOptions, KeyStore, Bytes } from 'web3-types'; import { hexToBytes } from 'web3-utils'; import { AccessListEIP2930TxData, FeeMarketEIP1559TxData, TxData } from '../../src/tx/types'; import { sign, signTransaction, encrypt } from '../../src/account'; @@ -221,6 +221,31 @@ export const invalidPrivateKeytoAccountData: [ [new Uint8Array([]), new PrivateKeyLengthError()], ]; +export const validPrivateKeyToPublicKeyData: [ + Bytes, boolean, string // private key, isCompressed, public key +][] = [ + [ + "0x1e046a882bb38236b646c9f135cf90ad90a140810f439875f2a6dd8e50fa261f", // test string to uncompressed publickey + false, + "0x42beb65f179720abaa3ec9a70a539629cbbc5ec65bb57e7fc78977796837e537662dd17042e6449dc843c281067a4d6d8d1a1775a13c41901670d5de7ee6503a", + ], + [ + "0x1e046a882bb38236b646c9f135cf90ad90a140810f439875f2a6dd8e50fa261f", // test string to compressed publickey + true, + "0x42beb65f179720abaa3ec9a70a539629cbbc5ec65bb57e7fc78977796837e537", + ], + [ + hexToBytes("0xd933beabed94a9f23917576596b2bc64ffeacfe5ded09a99c0feee8369bd295d"), // test uint8array to uncompressed publickey + false, + "0x7891db4ed2d26584b0fa87329c40b398c940c08e7dbeb8e3dad83f34dba284c933fb14b1edd8893fa89af3823fd827ee59044033ca068803030afc294de5f390", + ], + [ + hexToBytes("0xd933beabed94a9f23917576596b2bc64ffeacfe5ded09a99c0feee8369bd295d"), // test uint8array to compressed publickey + true, + "0x7891db4ed2d26584b0fa87329c40b398c940c08e7dbeb8e3dad83f34dba284c9", + ], +]; + export const validEncryptData: [[any, string | Uint8Array, CipherOptions], KeyStore][] = [ [ [ diff --git a/packages/web3-eth-accounts/test/unit/account.test.ts b/packages/web3-eth-accounts/test/unit/account.test.ts index 1f61b74c5f7..d49689a38c5 100644 --- a/packages/web3-eth-accounts/test/unit/account.test.ts +++ b/packages/web3-eth-accounts/test/unit/account.test.ts @@ -28,6 +28,7 @@ import { recoverTransaction, sign, signTransaction, + privateKeyToPublicKey } from '../../src/account'; import { invalidDecryptData, @@ -42,6 +43,7 @@ import { validHashMessageData, validPrivateKeytoAccountData, validPrivateKeyToAddressData, + validPrivateKeyToPublicKeyData, validRecover, } from '../fixtures/account'; import { TransactionFactory } from '../../src/tx/transactionFactory'; @@ -91,6 +93,13 @@ describe('accounts', () => { }); }); }); + describe('privateKeyToPublicKey', () => { + describe('valid cases', () => { + it.each(validPrivateKeyToPublicKeyData)('%s', (privateKey, isCompressed, output) => { + expect(privateKeyToPublicKey(privateKey, isCompressed)).toEqual(output); + }); + }) + }) describe('Signing and Recovery of Transaction', () => { it.each(transactionsTestData)('sign transaction', async txData => {