Skip to content

Commit

Permalink
Merge branch '4.x' into fix-schema/6470
Browse files Browse the repository at this point in the history
  • Loading branch information
kkeolmusae authored Oct 2, 2023
2 parents 2c9b789 + 90d78c1 commit 988fcbb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/web3-eth-accounts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ Documentation:

## [Unreleased]

### Added

- Added public function `privateKeyToPublicKey`

### Fixed

- Fixed `recover` function, `v` will be normalized to value 0,1 (#6344)
- Fixed `recover` function, `v` will be normalized to value 0,1 (#6344)
19 changes: 19 additions & 0 deletions packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
27 changes: 26 additions & 1 deletion packages/web3-eth-accounts/test/fixtures/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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][] = [
[
[
Expand Down
9 changes: 9 additions & 0 deletions packages/web3-eth-accounts/test/unit/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
recoverTransaction,
sign,
signTransaction,
privateKeyToPublicKey
} from '../../src/account';
import {
invalidDecryptData,
Expand All @@ -42,6 +43,7 @@ import {
validHashMessageData,
validPrivateKeytoAccountData,
validPrivateKeyToAddressData,
validPrivateKeyToPublicKeyData,
validRecover,
} from '../fixtures/account';
import { TransactionFactory } from '../../src/tx/transactionFactory';
Expand Down Expand Up @@ -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 => {
Expand Down
10 changes: 9 additions & 1 deletion packages/web3-providers-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,12 @@ Documentation:

- Dependencies updated

## [Unreleased]
## [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)
2 changes: 1 addition & 1 deletion packages/web3-providers-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 988fcbb

Please sign in to comment.