Skip to content
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

1122 refactor secp256k1 #1258

Merged
merged 8 commits into from
Sep 9, 2024
4 changes: 2 additions & 2 deletions apps/sdk-cloudflare-integration/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
clauseBuilder,
networkInfo,
secp256k1,
Secp256k1,
TransactionHandler,
TransactionUtils
} from '@vechain/sdk-core';
Expand Down Expand Up @@ -31,7 +31,7 @@ export default {
};

// Create private key
const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// 4 - Sign transaction
const signedTransaction = TransactionHandler.sign(
Expand Down
2 changes: 1 addition & 1 deletion docs/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Through the use of mnemonics and keystore, VeChainSDK ensures secure and user-fr
```typescript { name=keystore, category=example }
// 1 - Create private key using Secp256k1

const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// @NOTE you can use BIP 39 too!
// const words = Mnemonic.of()
Expand Down
6 changes: 3 additions & 3 deletions docs/certificates.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ It's important to note that certificates in the VeChainThor blockchain are self-
```typescript { name=sign_verify, category=example }
// 1 - Generate a private key and address for the signer

const privateKey = await secp256k1.generatePrivateKey();
const publicKey = secp256k1.derivePublicKey(privateKey);
const privateKey = await Secp256k1.generatePrivateKey();
const publicKey = Secp256k1.derivePublicKey(privateKey);
const signerAddress = Address.ofPublicKey(Buffer.from(publicKey)).toString();

// 2 - Create a certificate
Expand All @@ -56,7 +56,7 @@ const cert: Certificate = {
// 3 - Sign certificate

const jsonStr = certificate.encode(cert);
const signature = secp256k1.sign(Blake2b256.of(jsonStr).bytes, privateKey);
const signature = Secp256k1.sign(Blake2b256.of(jsonStr).bytes, privateKey);

// Add 0x to signature
cert.signature = Hex.of(signature).toString();
Expand Down
6 changes: 3 additions & 3 deletions docs/cryptography.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ Secp256k1 is mainly used for generating public and private key pairs in cryptogr
```typescript { name=secp256k1, category=example }
// 1 - Generate a private key.

const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();
console.log('Private key:', Hex.of(privateKey).toString());
// Private key: ...SOME_PRIVATE_KEY...

// 2 - Derive the public key and address from private key.
// By default, the key is returned in compressed form.

const publicKey = secp256k1.derivePublicKey(privateKey);
const publicKey = Secp256k1.derivePublicKey(privateKey);
const userAddress = Address.ofPublicKey(Buffer.from(publicKey)).toString();
console.log('User address:', userAddress);
// User address: 0x...SOME_ADDRESS...
Expand All @@ -69,7 +69,7 @@ const messageToSign = Txt.of('hello world');
const hash = Keccak256.of(messageToSign.bytes);
console.log(`Hash: ${hash.toString()}`);

const signature = secp256k1.sign(hash.bytes, privateKey);
const signature = Secp256k1.sign(hash.bytes, privateKey);
console.log('Signature:', Hex.of(signature).toString());
// Signature: ...SOME_SIGNATURE...
```
Expand Down
14 changes: 14 additions & 0 deletions docs/diagrams/architecture/secp256k1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```mermaid
classDiagram
class Secp256K1 {
+Uint8Array compressPublicKey(Uint8Array publicKey)$
+Uint8Array derivePublicKey(Uint8Array privateKey, boolean isCompressed)$
+Promise~Uint8Array~ async generatePrivateKey()$
+Uint8Array inflatePublicKey(Uint8Array publicKey)$
+boolean isValidMessageHash(Uint8Array hash)$
+boolean isValidPrivateKey(Uint8Array privateKey)$
+Uint8Array randomBytes(number|undefined bytesLength?)$
+Uint8Array recover(Uint8Array messageHash, Uint8Array sig)$
+Uint8Array sign(Uint8Array messageHash, Uint8Array privateKey)$
}
```
4 changes: 2 additions & 2 deletions docs/examples/accounts/keystore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { keystore, secp256k1 } from '@vechain/sdk-core';
import { keystore, Secp256k1 } from '@vechain/sdk-core';
import { expect } from 'expect';

// START_SNIPPET: KeystoreSnippet

// 1 - Create private key using Secp256k1

const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// @NOTE you can use BIP 39 too!
// const words = Mnemonic.of()
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/certificates/sign_verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import {
Address,
Blake2b256,
certificate,
secp256k1,
Secp256k1,
type Certificate
} from '@vechain/sdk-core';

// START_SNIPPET: SignVerifySnippet

// 1 - Generate a private key and address for the signer

const privateKey = await secp256k1.generatePrivateKey();
const publicKey = secp256k1.derivePublicKey(privateKey);
const privateKey = await Secp256k1.generatePrivateKey();
const publicKey = Secp256k1.derivePublicKey(privateKey);
const signerAddress = Address.ofPublicKey(Buffer.from(publicKey)).toString();

// 2 - Create a certificate
Expand All @@ -31,7 +31,7 @@ const cert: Certificate = {
// 3 - Sign certificate

const jsonStr = certificate.encode(cert);
const signature = secp256k1.sign(Blake2b256.of(jsonStr).bytes, privateKey);
const signature = Secp256k1.sign(Blake2b256.of(jsonStr).bytes, privateKey);

// Add 0x to signature
cert.signature = Hex.of(signature).toString();
Expand Down
16 changes: 8 additions & 8 deletions docs/examples/cryptography/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Address, Hex, Keccak256, Txt, secp256k1 } from '@vechain/sdk-core';
import { Address, Hex, Keccak256, Txt, Secp256k1 } from '@vechain/sdk-core';
import { expect } from 'expect';

// START_SNIPPET: Secp256k1Snippet

// 1 - Generate a private key.

const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();
console.log('Private key:', Hex.of(privateKey).toString());
// Private key: ...SOME_PRIVATE_KEY...

// 2 - Derive the public key and address from private key.
// By default, the key is returned in compressed form.

const publicKey = secp256k1.derivePublicKey(privateKey);
const publicKey = Secp256k1.derivePublicKey(privateKey);
const userAddress = Address.ofPublicKey(Buffer.from(publicKey)).toString();
console.log('User address:', userAddress);
// User address: 0x...SOME_ADDRESS...
Expand All @@ -23,20 +23,20 @@ const messageToSign = Txt.of('hello world');
const hash = Keccak256.of(messageToSign.bytes);
console.log(`Hash: ${hash.toString()}`);

const signature = secp256k1.sign(hash.bytes, privateKey);
const signature = Secp256k1.sign(hash.bytes, privateKey);
console.log('Signature:', Hex.of(signature).toString());
// Signature: ...SOME_SIGNATURE...

// END_SNIPPET: Secp256k1Snippet

// 4 - Test recovery of public key.
// By default, the recovered key is returned in compressed form.
// The methods `secp256k1.inflatePublicKey` and `secp256k1.compressPublicKey`
// The methods `Secp256k1.inflatePublicKey` and `Secp256k1.compressPublicKey`
// convert public keys among compressed and uncompressed form.

const recoveredPublicKey = secp256k1.recover(hash.bytes, signature);
const recoveredPublicKey = Secp256k1.recover(hash.bytes, signature);
expect(publicKey).toStrictEqual(
secp256k1.compressPublicKey(recoveredPublicKey)
Secp256k1.compressPublicKey(recoveredPublicKey)
);
expect(secp256k1.inflatePublicKey(publicKey)).toStrictEqual(recoveredPublicKey);
expect(Secp256k1.inflatePublicKey(publicKey)).toStrictEqual(recoveredPublicKey);
// Recovered public key is correct: true
4 changes: 2 additions & 2 deletions docs/examples/transactions/blockref-expiration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
secp256k1,
Secp256k1,
TransactionUtils,
TransactionHandler,
networkInfo,
Expand Down Expand Up @@ -36,7 +36,7 @@ const body: TransactionBody = {

// 3 - Create private key

const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// 4 - Sign transaction

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/transactions/multiple-clauses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
clauseBuilder,
networkInfo,
secp256k1,
Secp256k1,
type TransactionBody,
type TransactionClause,
TransactionHandler,
Expand Down Expand Up @@ -45,7 +45,7 @@ const body: TransactionBody = {
};

// Create private key
const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// 4 - Sign transaction

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/transactions/sign-decode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
clauseBuilder,
networkInfo,
secp256k1,
Secp256k1,
type TransactionBody,
type TransactionClause,
TransactionHandler,
Expand Down Expand Up @@ -39,7 +39,7 @@ const body: TransactionBody = {
};

// Create private key
const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// 4 - Sign transaction

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/transactions/tx-dependency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
networkInfo,
secp256k1,
Secp256k1,
TransactionUtils,
TransactionHandler,
type TransactionClause,
Expand Down Expand Up @@ -56,7 +56,7 @@ const txBBody: TransactionBody = {
};

// Define the senders private key
const senderPrivateKey = await secp256k1.generatePrivateKey();
const senderPrivateKey = await Secp256k1.generatePrivateKey();

// To define transaction B as dependant on transaction A
// We need to sign transaction A, and then get its Id
Expand Down
8 changes: 4 additions & 4 deletions docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const body: TransactionBody = {
};

// Create private key
const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// 4 - Sign transaction

Expand Down Expand Up @@ -98,7 +98,7 @@ const body: TransactionBody = {
};

// Create private key
const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// 4 - Sign transaction

Expand Down Expand Up @@ -217,7 +217,7 @@ const body: TransactionBody = {

// 3 - Create private key

const privateKey = await secp256k1.generatePrivateKey();
const privateKey = await Secp256k1.generatePrivateKey();

// 4 - Sign transaction

Expand Down Expand Up @@ -283,7 +283,7 @@ const txBBody: TransactionBody = {
};

// Define the senders private key
const senderPrivateKey = await secp256k1.generatePrivateKey();
const senderPrivateKey = await Secp256k1.generatePrivateKey();

// To define transaction B as dependant on transaction A
// We need to sign transaction A, and then get its Id
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/certificate/certificate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CertificateSignature } from '@vechain/sdk-errors';
import fastJsonStableStringify from 'fast-json-stable-stringify';
import { Blake2b256 } from '../vcdm/hash/Blake2b256';
import { secp256k1 } from '../secp256k1';
import { Address, Hex, Txt } from '../vcdm';
import { Blake2b256 } from '../vcdm/hash/Blake2b256';
import { Secp256k1 } from '../secp256k1';
import { type Certificate } from './types';

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ function encode(cert: Certificate): Uint8Array {
*
* Secure audit function.
* - {@link Blake2b256.of};
* - {@link secp256k1.sign}.
* - {@link Secp256k1.sign}.
*
* @param {Certificate} cert - The certificate to be signed.
* Any instance extending the {@link Certificate} interface is supported.
Expand All @@ -77,7 +77,7 @@ function sign(cert: Certificate, privateKey: Uint8Array): Certificate {
return {
...cert,
signature: Hex.of(
secp256k1.sign(Blake2b256.of(encode(cert)).bytes, privateKey)
Secp256k1.sign(Blake2b256.of(encode(cert)).bytes, privateKey)
).toString()
};
}
Expand All @@ -95,7 +95,7 @@ function sign(cert: Certificate, privateKey: Uint8Array): Certificate {
*
* Secure audit function.
* - {@link Blake2b256.of};
* - {@link secp256k1.recover}.
* - {@link Secp256k1.recover}.
*
* @param {Certificate} cert - The certificate to verify.
* Any instance extending the {@link Certificate} interface is supported.
Expand Down Expand Up @@ -127,7 +127,7 @@ function verify(cert: Certificate): void {
const sign = Hex.of(cert.signature).bytes;
const hash = Blake2b256.of(encode(cert)).bytes;
// The signer address is compared in lowercase to avoid
const signer = Address.ofPublicKey(secp256k1.recover(hash, sign))
const signer = Address.ofPublicKey(Secp256k1.recover(hash, sign))
.toString()
.toLowerCase();

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/hdkey/HDKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import * as s_bip32 from '@scure/bip32';
import * as s_bip39 from '@scure/bip39';
import * as nc_utils from '@noble/curves/abstract/utils';
import { base58 } from '@scure/base';
import { secp256k1 } from '../secp256k1';
import { HexUInt } from '../vcdm/HexUInt';
import { FPN } from '../vcdm';
import { Secp256k1 } from '../secp256k1';
import { Sha256 } from '../vcdm/hash/Sha256';
import {
InvalidHDKey,
InvalidHDKeyMnemonic,
InvalidSecp256k1PrivateKey
} from '@vechain/sdk-errors';
import { HexUInt } from '../vcdm/HexUInt';
import { Sha256 } from '../vcdm/hash/Sha256';
import { FPN } from '../vcdm';

/**
* This class extends the
Expand Down Expand Up @@ -167,7 +167,7 @@ class HDKey extends s_bip32.HDKey {
*
* @remarks Security auditable method, depends on
* * [base58.encode](https://github.com/paulmillr/scure-base);
* * {@link secp256k1.compressPublicKey};
* * {@link Secp256k1.compressPublicKey};
* * {@link Sha256};
* * [s_bip32.HDKey.fromExtendedKey](https://github.com/paulmillr/scure-bip32).
*/
Expand All @@ -179,7 +179,7 @@ class HDKey extends s_bip32.HDKey {
const header = nc_utils.concatBytes(
this.EXTENDED_PUBLIC_KEY_PREFIX,
chainCode,
secp256k1.compressPublicKey(publicKey)
Secp256k1.compressPublicKey(publicKey)
);
const checksum = Sha256.of(Sha256.of(header).bytes).bytes.subarray(
0,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/keystore/cryptography/ethers/keystore.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Implements the JSON Keystore v3 Wallet encryption, decryption, and validation functionality.
*/
import { Address } from '../../../vcdm';
import { ethers } from 'ethers';
import { Hex } from '../../../vcdm/Hex';
import { SCRYPT_PARAMS } from './const';
import { ethers } from 'ethers';
import { secp256k1 } from '../../../secp256k1';
import { Secp256k1 } from '../../../secp256k1';
import {
InvalidKeystore,
InvalidKeystoreParams,
stringifyData
} from '@vechain/sdk-errors';
import { type Keystore, type KeystoreAccount } from '../../types';
import { Address } from '../../../vcdm';

/**
* Encrypts a given private key into a keystore format using the specified password.
Expand All @@ -25,7 +25,7 @@ async function encrypt(
password: string
): Promise<Keystore> {
// Public and Address are derived from a private key
const derivePublicKey = secp256k1.derivePublicKey(privateKey);
const derivePublicKey = Secp256k1.derivePublicKey(privateKey);
const deriveAddress = Address.ofPublicKey(
Buffer.from(derivePublicKey)
).toString();
Expand Down
Loading