Skip to content

Commit

Permalink
Merge branch '1.0.0' into chore/src-01-sro-01
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonneal authored Jan 28, 2025
2 parents 1a32e82 + 0a30d35 commit f9cfc78
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 251 deletions.
2 changes: 1 addition & 1 deletion apps/sdk-vite-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
"vitest": "^2.1.4",
"vitest-browser-react": "^0.0.4"
}
}
}
38 changes: 31 additions & 7 deletions packages/core/src/secp256k1/Secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Secp256k1 {
* * {@link global.crypto.subtle.generateKey};
* * [nc_secp256k1.utils.randomPrivateKey](https://github.com/paulmillr/noble-secp256k1).
*/
public static async generatePrivateKey(): Promise<Uint8Array> {
public static async _generatePrivateKey(): Promise<Uint8Array> {
try {
return nc_secp256k1.utils.randomPrivateKey();
} catch (e) {
Expand All @@ -132,6 +132,32 @@ class Secp256k1 {
}
}

/**
* Generates a new Secp256k1 private key using a secure random number generator.
*
* @return {Promise<Uint8Array>} A promise that resolves to a Uint8Array representing the generated private key.
* This encoded private key is suitable for cryptographic operations.
* @throws {InvalidSecp256k1PrivateKey} Throws an error if private key generation fails if a secure random number
* generator is not provided by the hosting operating system.
*
* @remarks Security auditable method, depends on
* * [nc_secp256k1.utils.randomPrivateKey](https://github.com/paulmillr/noble-secp256k1).
*/
public static async generatePrivateKey(): Promise<Uint8Array> {
return await new Promise<Uint8Array>((resolve) => {
try {
resolve(nc_secp256k1.utils.randomPrivateKey());
} catch (e) {
throw new InvalidSecp256k1PrivateKey(
'Secp256k1.generatePrivateKey',
'Private key generation failed: ensure you have a secure random number generator available at runtime.',
undefined,
e
);
}
});
}

/**
* Inflate a compressed public key to its uncompressed form.
*
Expand Down Expand Up @@ -192,20 +218,18 @@ class Secp256k1 {
* {@link {@link global.crypto} is used as fall back togenerate
* the random sequence.
*
* @param {number} [bytesLength=32] - Optional. The number of random bytes to generate.
* @param {number} [bytesLength=32] - Optional. The number of random bytes to generate, 32 by default.
* @return {Uint8Array} - A Uint8Array containing the random bytes.
*
* @remarks Security auditable method, depends on
* * {@link global.crypto.getRandomValues};
* * {@link global.crypto.getRandomValues};
* * [nh_randomBytes](https://github.com/paulmillr/noble-hashes).
*/
public static randomBytes(bytesLength?: number): Uint8Array {
public static randomBytes(bytesLength: number = 32): Uint8Array {
try {
return nh_randomBytes(bytesLength);
} catch (e) {
return global.crypto.getRandomValues(
new Uint8Array(bytesLength ?? 32)
);
return global.crypto.getRandomValues(new Uint8Array(bytesLength));
}
}

Expand Down
31 changes: 8 additions & 23 deletions packages/core/src/vcdm/Address.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Keccak256 } from './hash/Keccak256';
import { HDKey } from '../hdkey/HDKey';
import { Hex } from './Hex';
import { HexUInt } from './HexUInt';
import { InvalidDataType, InvalidHDKey } from '@vechain/sdk-errors';
import { Keccak256 } from './hash/Keccak256';
import { Secp256k1 } from '../secp256k1/Secp256k1';
import { Txt } from './Txt';
import {
InvalidDataType,
InvalidHDKey,
InvalidSecp256k1PrivateKey
} from '@vechain/sdk-errors';

/**
* Represents a VeChain Address as unsigned integer.
Expand Down Expand Up @@ -89,29 +85,18 @@ class Address extends HexUInt {
}

/**
* Create an Address instance from the given private key.
*
* @param {Uint8Array} privateKey - The private key to convert.
*
* @param {boolean} [isCompressed=true] - The flag to indicate if the derived public key should be compressed.
* Generates an Address object from the given private key.
*
* @returns {Address} The converted address.
*
* @remarks Security auditable method, depends on
* * {@link Secp256k1.derivePublicKey}.
* @param {Uint8Array} privateKey - The private key used to derive the corresponding address.
* @return {Address} The derived Address object.
* @throws {InvalidDataType} If the provided private key is invalid or cannot derive an address.
*/
public static ofPrivateKey(
privateKey: Uint8Array,
isCompressed: boolean = true
): Address {
public static ofPrivateKey(privateKey: Uint8Array): Address {
try {
return Address.ofPublicKey(
Secp256k1.derivePublicKey(privateKey, isCompressed)
Secp256k1.derivePublicKey(privateKey, true)
);
} catch (error) {
if (error instanceof InvalidSecp256k1PrivateKey) {
throw error;
}
throw new InvalidDataType(
'Address.ofPrivateKey',
'not a valid private key',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/vcdm/hash/Keccak256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { Hex } from '../Hex';
import { HexUInt } from '../HexUInt';

/**
* Represents the result of an [SHA-3](https://en.wikipedia.org/wiki/SHA-3) [KECCAK 256](https://keccak.team/keccak.html) hash operation.
* Represents the result of an [KECCAK 256](https://keccak.team/keccak.html) hash operation.
*
* @extends HexUInt
*/
class Keccak256 extends HexUInt {
/**
* Generates the [KECCAK 256](https://eth-hash.readthedocs.io/en/stable/) hash of the given input.
* Generates the [KECCAK 256](https://keccak.team/keccak.html) hash of the given input.
*
* @param {bigint | number | string | Uint8Array | Hex} exp - The input value to hash.
*
Expand Down
7 changes: 6 additions & 1 deletion packages/core/tests/keystore/keystore.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, test } from '@jest/globals';
import {
InvalidDataType,
InvalidKeystore,
InvalidKeystoreParams,
InvalidSecp256k1PrivateKey,
Expand Down Expand Up @@ -60,7 +61,11 @@ import { encryptionPassword } from './fixture';
new TextEncoder().encode('wrong private key'),
encryptionPassword
)
).rejects.toThrowError(InvalidSecp256k1PrivateKey);
).rejects.toThrowError(
experimentalCryptography
? InvalidDataType
: InvalidSecp256k1PrivateKey
);
});

/**
Expand Down
Loading

0 comments on commit f9cfc78

Please sign in to comment.