Skip to content

Commit

Permalink
666 bloom, hashing and secp256k1 pure and secure functions. (#784)
Browse files Browse the repository at this point in the history
* refactor: #666 pure and secure hashing, secp256k and bloom functions

* refactor: #666 pure and secure hashing, secp256k and bloom functions

* refactor: #666 pure & secure hashing, secp256k and bloom functions, reworking docs

* refactor: #666 pure & secure hashing, secp256k and bloom functions, docs fixed

* refactor: #666 pure & secure hashing, secp256k and bloom functions, docs fixed

* refactor: #666 pure & secure hashing, secp256k and bloom functions, docs fixed

* refactor: #666 pure & secure hashing, secp256k and bloom functions, docs fixed

* refactor: #666 pure & secure hashing, secp256k and bloom functions, docs fixed
  • Loading branch information
lucanicoladebiasi authored Apr 11, 2024
1 parent 4c14b1b commit edabbd3
Show file tree
Hide file tree
Showing 44 changed files with 779 additions and 573 deletions.
5 changes: 4 additions & 1 deletion docs/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ const privateKey = secp256k1.generatePrivateKey();
// 2 - Encrypt/decrypt private key using Ethereum's keystore scheme

const keyStorePassword = 'your password';
const newKeyStore = await keystore.encrypt(privateKey, keyStorePassword);
const newKeyStore = await keystore.encrypt(
Buffer.from(privateKey),
keyStorePassword
);

// 3 - Throw for wrong password

Expand Down
4 changes: 2 additions & 2 deletions docs/certificates.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ It's important to note that certificates in the VechainThor blockchain are self-

const privateKey = secp256k1.generatePrivateKey();
const publicKey = secp256k1.derivePublicKey(privateKey);
const signerAddress = addressUtils.fromPublicKey(publicKey);
const signerAddress = addressUtils.fromPublicKey(Buffer.from(publicKey));

// 2 - Create a certificate

Expand All @@ -59,7 +59,7 @@ const jsonStr = certificate.encode(cert);
const signature = secp256k1.sign(blake2b256(jsonStr), privateKey);

// Add 0x to signature
cert.signature = '0x' + signature.toString('hex');
cert.signature = Hex0x.of(signature);

// Verify certificate
certificate.verify(cert);
Expand Down
11 changes: 6 additions & 5 deletions docs/cryptography.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ Secp256k1 is mainly used for generating public and private key pairs in cryptogr
* **Security Considerations**: The security of Secp256k1 relies on the difficulty of the elliptic curve discrete logarithm problem. Breaking this problem requires an impractical amount of computational power, making Secp256k1 a secure choice for cryptographic applications, including blockchain networks.

```typescript { name=secp256k1, category=example }
// 1 - Generate a private key
// 1 - Generate a private key.

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

// 2 - Derive public key and address from 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 userAddress = addressUtils.fromPublicKey(publicKey);
const userAddress = addressUtils.fromPublicKey(Buffer.from(publicKey));
console.log('User address:', userAddress);
// User address: 0x...SOME_ADDRESS...

Expand All @@ -66,6 +67,6 @@ const hash = keccak256(messageToSign);
console.log(`Hash: ${hash.toString()}`);

const signature = secp256k1.sign(hash, privateKey);
console.log('Signature:', signature.toString('hex'));
console.log('Signature:', Hex.of(signature));
// Signature: ...SOME_SIGNATURE...
```
5 changes: 4 additions & 1 deletion docs/examples/accounts/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const privateKey = secp256k1.generatePrivateKey();
// 2 - Encrypt/decrypt private key using Ethereum's keystore scheme

const keyStorePassword = 'your password';
const newKeyStore = await keystore.encrypt(privateKey, keyStorePassword);
const newKeyStore = await keystore.encrypt(
Buffer.from(privateKey),
keyStorePassword
);

// 3 - Throw for wrong password

Expand Down
11 changes: 6 additions & 5 deletions docs/examples/certificates/sign_verify.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
type Certificate,
Hex0x,
addressUtils,
blake2b256,
certificate,
secp256k1,
blake2b256,
addressUtils
type Certificate
} from '@vechain/sdk-core';

// START_SNIPPET: SignVerifySnippet
Expand All @@ -12,7 +13,7 @@ import {

const privateKey = secp256k1.generatePrivateKey();
const publicKey = secp256k1.derivePublicKey(privateKey);
const signerAddress = addressUtils.fromPublicKey(publicKey);
const signerAddress = addressUtils.fromPublicKey(Buffer.from(publicKey));

// 2 - Create a certificate

Expand All @@ -33,7 +34,7 @@ const jsonStr = certificate.encode(cert);
const signature = secp256k1.sign(blake2b256(jsonStr), privateKey);

// Add 0x to signature
cert.signature = '0x' + signature.toString('hex');
cert.signature = Hex0x.of(signature);

// Verify certificate
certificate.verify(cert);
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/cryptography/blake2b256.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { blake2b256, type HashInput } from '@vechain/sdk-core';
import { Hex, blake2b256, type HashInput } from '@vechain/sdk-core';
import { expect } from 'expect';

// START_SNIPPET: Blake2b256Snippet
Expand All @@ -10,6 +10,6 @@ const hash = blake2b256(toHash);

// END_SNIPPET: Blake2b256Snippet

expect(hash.toString('hex')).toBe(
expect(Hex.of(hash)).toBe(
'256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610'
);
4 changes: 2 additions & 2 deletions docs/examples/cryptography/keccak256.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { keccak256, type HashInput } from '@vechain/sdk-core';
import { Hex, keccak256, type HashInput } from '@vechain/sdk-core';
import { expect } from 'expect';

// START_SNIPPET: Kekkak256Snippet
Expand All @@ -10,6 +10,6 @@ const hash = keccak256(toHash);

// END_SNIPPET: Kekkak256Snippet

expect(hash.toString('hex')).toBe(
expect(Hex.of(hash)).toBe(
'47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'
);
20 changes: 13 additions & 7 deletions docs/examples/cryptography/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Hex,
keccak256,
secp256k1,
addressUtils,
Expand All @@ -8,16 +9,17 @@ import { expect } from 'expect';

// START_SNIPPET: Secp256k1Snippet

// 1 - Generate a private key
// 1 - Generate a private key.

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

// 2 - Derive public key and address from 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 userAddress = addressUtils.fromPublicKey(publicKey);
const userAddress = addressUtils.fromPublicKey(Buffer.from(publicKey));
console.log('User address:', userAddress);
// User address: 0x...SOME_ADDRESS...

Expand All @@ -28,13 +30,17 @@ const hash = keccak256(messageToSign);
console.log(`Hash: ${hash.toString()}`);

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

// END_SNIPPET: Secp256k1Snippet

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

const recoveredPublicKey = secp256k1.recover(hash, signature);
expect(publicKey.equals(recoveredPublicKey)).toBeTruthy();
expect(publicKey).toStrictEqual(secp256k1.compressPublicKey(recoveredPublicKey));
expect(secp256k1.inflatePublicKey(publicKey)).toStrictEqual(recoveredPublicKey);
// Recovered public key is correct: true
5 changes: 4 additions & 1 deletion docs/examples/transactions/blockref_expiration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ const privateKey = secp256k1.generatePrivateKey();

// 4 - Sign transaction

const signedTransaction = TransactionHandler.sign(body, privateKey);
const signedTransaction = TransactionHandler.sign(
body,
Buffer.from(privateKey)
);

// 5 - Encode transaction

Expand Down
5 changes: 4 additions & 1 deletion docs/examples/transactions/multiple_clauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ const privateKey = secp256k1.generatePrivateKey();

// 4 - Sign transaction

const signedTransaction = TransactionHandler.sign(body, privateKey);
const signedTransaction = TransactionHandler.sign(
body,
Buffer.from(privateKey)
);

// 5 - Encode transaction

Expand Down
5 changes: 4 additions & 1 deletion docs/examples/transactions/sign_decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const privateKey = secp256k1.generatePrivateKey();

// 4 - Sign transaction

const signedTransaction = TransactionHandler.sign(body, privateKey);
const signedTransaction = TransactionHandler.sign(
body,
Buffer.from(privateKey)
);

// 5 - Encode transaction

Expand Down
10 changes: 8 additions & 2 deletions docs/examples/transactions/tx_dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@ const senderPrivateKey = secp256k1.generatePrivateKey();

// 4 - Get Tx A id

const txASigned = TransactionHandler.sign(txABody, senderPrivateKey);
const txASigned = TransactionHandler.sign(
txABody,
Buffer.from(senderPrivateKey)
);

// 5 - Set it inside tx B

txBBody.dependsOn = txASigned.id;

// 6 - Sign Tx B

const txBSigned = TransactionHandler.sign(txBBody, senderPrivateKey);
const txBSigned = TransactionHandler.sign(
txBBody,
Buffer.from(senderPrivateKey)
);

// 7 - encode Tx B

Expand Down
25 changes: 20 additions & 5 deletions docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ const privateKey = secp256k1.generatePrivateKey();

// 4 - Sign transaction

const signedTransaction = TransactionHandler.sign(body, privateKey);
const signedTransaction = TransactionHandler.sign(
body,
Buffer.from(privateKey)
);

// 5 - Encode transaction

Expand Down Expand Up @@ -99,7 +102,10 @@ const privateKey = secp256k1.generatePrivateKey();

// 4 - Sign transaction

const signedTransaction = TransactionHandler.sign(body, privateKey);
const signedTransaction = TransactionHandler.sign(
body,
Buffer.from(privateKey)
);

// 5 - Encode transaction

Expand Down Expand Up @@ -217,7 +223,10 @@ const privateKey = secp256k1.generatePrivateKey();

// 4 - Sign transaction

const signedTransaction = TransactionHandler.sign(body, privateKey);
const signedTransaction = TransactionHandler.sign(
body,
Buffer.from(privateKey)
);

// 5 - Encode transaction

Expand Down Expand Up @@ -284,15 +293,21 @@ const senderPrivateKey = secp256k1.generatePrivateKey();

// 4 - Get Tx A id

const txASigned = TransactionHandler.sign(txABody, senderPrivateKey);
const txASigned = TransactionHandler.sign(
txABody,
Buffer.from(senderPrivateKey)
);

// 5 - Set it inside tx B

txBBody.dependsOn = txASigned.id;

// 6 - Sign Tx B

const txBSigned = TransactionHandler.sign(txBBody, senderPrivateKey);
const txBSigned = TransactionHandler.sign(
txBBody,
Buffer.from(senderPrivateKey)
);

// 7 - encode Tx B

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ function fromPublicKey(publicKey: Buffer): string {
*
*/
function fromPrivateKey(privateKey: Buffer): string {
return addressUtils.fromPublicKey(secp256k1.derivePublicKey(privateKey));
return addressUtils.fromPublicKey(
Buffer.from(secp256k1.derivePublicKey(privateKey))
);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/assertions/keystore/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { assert, SECP256K1 } from '@vechain/sdk-errors';
*/
function assertIsValidPrivateKey(
methodName: string,
privateKey: Buffer,
isValidPrivateKeyFunction: (privateKey: Buffer) => boolean
privateKey: Uint8Array,
isValidPrivateKeyFunction: (privateKey: Uint8Array) => boolean
): void {
assert(
`assertIsValidPrivateKey - ${methodName}`,
Expand All @@ -31,8 +31,8 @@ function assertIsValidPrivateKey(
*/
function assertIsValidSecp256k1MessageHash(
methodName: string,
msgHash: Buffer,
isValidMessageHashFunction: (messageHash: Buffer) => boolean
msgHash: Uint8Array,
isValidMessageHashFunction: (messageHash: Uint8Array) => boolean
): void {
assert(
`assertIsValidSecp256k1MessageHash - ${methodName}`,
Expand Down
Loading

1 comment on commit edabbd3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 100%
100% (2994/2994) 100% (620/620) 100% (626/626)
Title Tests Skipped Failures Errors Time
core 443 0 💤 0 ❌ 0 🔥 59.772s ⏱️
network 266 0 💤 0 ❌ 0 🔥 1m 39s ⏱️
errors 48 0 💤 0 ❌ 0 🔥 14.854s ⏱️

Please sign in to comment.