Skip to content

Commit

Permalink
Merge pull request #198 from vechainfoundation/thor-client-accounts
Browse files Browse the repository at this point in the history
feat(Thor Client): Accounts
  • Loading branch information
fabiorigam authored Nov 7, 2023
2 parents f9944f7 + e431e7a commit e3bb8bd
Show file tree
Hide file tree
Showing 42 changed files with 755 additions and 156 deletions.
4 changes: 2 additions & 2 deletions docs/certificates.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
certificate,
secp256k1,
blake2b256,
address
addressUtils
} from '@vechain-sdk/core';

// In this example we create a certificate and
Expand All @@ -48,7 +48,7 @@ import {
// Generate a private key and address for the signer
const privateKey = secp256k1.generatePrivateKey();
const publicKey = secp256k1.derivePublicKey(privateKey);
const signerAddress = address.fromPublicKey(publicKey);
const signerAddress = addressUtils.fromPublicKey(publicKey);

// Create a certificate
const cert: Certificate = {
Expand Down
4 changes: 2 additions & 2 deletions docs/cryptography.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Secp256k1 is mainly used for generating public and private key pairs in cryptogr
import {
keccak256,
secp256k1,
address,
addressUtils,
type HashInput
} from '@vechain-sdk/core';
import { expect } from 'expect';
Expand All @@ -70,7 +70,7 @@ console.log('Private key:', privateKey.toString('hex'));

// Public key and address from private key
const publicKey = secp256k1.derivePublicKey(privateKey);
const userAddress = address.fromPublicKey(publicKey);
const userAddress = addressUtils.fromPublicKey(publicKey);
console.log('User address:', userAddress);
// User address: 0x...SOME_ADDRESS...

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/certificates/sign_verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
certificate,
secp256k1,
blake2b256,
address
addressUtils
} from '@vechain-sdk/core';

// In this example we create a certificate and
Expand All @@ -12,7 +12,7 @@ import {
// Generate a private key and address for the signer
const privateKey = secp256k1.generatePrivateKey();
const publicKey = secp256k1.derivePublicKey(privateKey);
const signerAddress = address.fromPublicKey(publicKey);
const signerAddress = addressUtils.fromPublicKey(publicKey);

// Create a certificate
const cert: Certificate = {
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/cryptography/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
keccak256,
secp256k1,
address,
addressUtils,
type HashInput
} from '@vechain-sdk/core';
import { expect } from 'expect';
Expand All @@ -13,7 +13,7 @@ console.log('Private key:', privateKey.toString('hex'));

// Public key and address from private key
const publicKey = secp256k1.derivePublicKey(privateKey);
const userAddress = address.fromPublicKey(publicKey);
const userAddress = addressUtils.fromPublicKey(publicKey);
console.log('User address:', userAddress);
// User address: 0x...SOME_ADDRESS...

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ function toChecksumed(address: string): string {
return ethers.getAddress(address);
}

export const address = { fromPublicKey, isAddress, toChecksumed };
export const addressUtils = { fromPublicKey, isAddress, toChecksumed };
4 changes: 2 additions & 2 deletions packages/core/src/certificate/certificate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { address } from '../address';
import { addressUtils } from '../address';
import { blake2b256 } from '../hash';
import { secp256k1 } from '../secp256k1';
import fastJsonStableStringify from 'fast-json-stable-stringify';
Expand Down Expand Up @@ -55,7 +55,7 @@ function verify(cert: Certificate): void {
);

// Signature does not match with the signer's public key
if (address.fromPublicKey(pubKey) !== cert.signer) {
if (addressUtils.fromPublicKey(pubKey) !== cert.signer) {
throw buildError(
CERTIFICATE.CERTIFICATE_INVALID_SIGNER,
"Signature does not match with the signer's public key."
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/hdnode/hdnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
X_PUB_PREFIX
} from '../utils';
import { type IHDNode } from './types';
import { address } from '../address';
import { addressUtils } from '../address';
import { sha256 } from '../hash';
import { secp256k1 } from '../secp256k1';
import { type WordlistSizeType } from '../mnemonic';
Expand Down Expand Up @@ -142,7 +142,7 @@ function ethersNodeToOurHDNode(ethersNode: ethers.HDNodeWallet): IHDNode {
)
);
const cc = Buffer.from(ethersNode.chainCode.slice(2), 'hex');
const addr = address.fromPublicKey(pub);
const addr = addressUtils.fromPublicKey(pub);

return {
get publicKey() {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/keystore/keystore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Implements the JSON Keystore v3 Wallet encryption, decryption, and validation functionality.
*/
import { address } from '../address';
import { addressUtils } from '../address';
import { secp256k1 } from '../secp256k1';
import { ethers } from 'ethers';
import { SCRYPT_PARAMS } from '../utils';
Expand All @@ -21,7 +21,7 @@ async function encrypt(
): Promise<Keystore> {
// Public and Address are derived from private key
const derivePublicKey = secp256k1.derivePublicKey(privateKey);
const deriveAddress = address.fromPublicKey(derivePublicKey);
const deriveAddress = addressUtils.fromPublicKey(derivePublicKey);

// Create keystore account compatible with ethers
const keystoreAccount: ethers.KeystoreAccount = {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/transaction/handlers/sign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { address } from '../../address';
import { addressUtils } from '../../address';
import { secp256k1 } from '../../secp256k1';
import { Transaction } from '../transaction';
import { buildError, SECP256K1, TRANSACTION } from '@vechain-sdk/errors';
Expand Down Expand Up @@ -88,7 +88,7 @@ function signWithDelegator(

const transactionHash = transactionToSign.getSignatureHash();
const delegatedHash = transactionToSign.getSignatureHash(
address.fromPublicKey(secp256k1.derivePublicKey(signerPrivateKey))
addressUtils.fromPublicKey(secp256k1.derivePublicKey(signerPrivateKey))
);
const signature = Buffer.concat([
secp256k1.sign(transactionHash, signerPrivateKey),
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/transaction/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { address } from '../address';
import { addressUtils } from '../address';
import { type RLPValidObject } from '../encoding';
import { blake2b256 } from '../hash';
import { secp256k1 } from '../secp256k1';
Expand Down Expand Up @@ -126,7 +126,7 @@ class Transaction {
);

// Address from public key
return address.fromPublicKey(delegatorPublicKey);
return addressUtils.fromPublicKey(delegatorPublicKey);
}

/**
Expand Down Expand Up @@ -188,7 +188,7 @@ class Transaction {
*/
public getSignatureHash(delegateFor?: string): Buffer {
// Correct delegateFor address
if (delegateFor !== undefined && !address.isAddress(delegateFor))
if (delegateFor !== undefined && !addressUtils.isAddress(delegateFor))
throw buildError(
ADDRESS.INVALID_ADDRESS,
'Invalid address given as input as delegateFor parameter.'
Expand Down Expand Up @@ -244,7 +244,7 @@ class Transaction {
);

// Address from public key
return address.fromPublicKey(originPublicKey);
return addressUtils.fromPublicKey(originPublicKey);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/bloom/bloom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { bloom as bloomInstance } from '../../bloom';
import { dataUtils } from '../data';
import { type HexString } from '../types';
import { address } from '../../address';
import { addressUtils } from '../../address';
import { BLOOM_REGEX_LOWERCASE, BLOOM_REGEX_UPPERCASE } from '../const';
import { ADDRESS, BLOOM, buildError, DATA } from '@vechain-sdk/errors';

Expand Down Expand Up @@ -108,7 +108,7 @@ const isAddressInBloom = (
k: number,
addressToCheck: HexString
): boolean => {
if (!address.isAddress(addressToCheck)) {
if (!addressUtils.isAddress(addressToCheck)) {
throw buildError(
ADDRESS.INVALID_ADDRESS,
'Invalid address given as input in Bloom filter.'
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/transaction/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { address } from '../../address';
import { addressUtils } from '../../address';
import { type TransactionClause } from '../../transaction';
import { TRANSACTIONS_GAS_CONSTANTS } from '../const';
import { dataUtils } from '../data';
Expand Down Expand Up @@ -26,7 +26,7 @@ function intrinsicGas(clauses: TransactionClause[]): number {
return clauses.reduce((sum, clause: TransactionClause) => {
if (clause.to !== null) {
// Invalid address
if (!address.isAddress(clause.to)) {
if (!addressUtils.isAddress(clause.to)) {
throw buildError(
DATA.INVALID_DATA_TYPE,
'Invalid data type. Data should be an address.'
Expand Down
10 changes: 5 additions & 5 deletions packages/core/tests/abi/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { address } from '../../src';
import { addressUtils } from '../../src';

/**
* Simple functions fixtures
Expand Down Expand Up @@ -97,21 +97,21 @@ const functions = [
*/
const simpleParametersDataForFunction2 = [
{
master: address.toChecksumed(
master: addressUtils.toChecksumed(
'0x0e8fd586e022f825a109848832d7e552132bc332'
),
endorsor: address.toChecksumed(
endorsor: addressUtils.toChecksumed(
'0x224626926a7a12225a60e127cec119c939db4a5c'
),
identity:
'0xdbf2712e19af00dc4d376728f7cb06cc215c8e7c53b94cb47cefb4a26ada2a6c',
active: false
},
{
master: address.toChecksumed(
master: addressUtils.toChecksumed(
'0x4977d68df97bb313b23238520580d8d3a59939bf'
),
endorsor: address.toChecksumed(
endorsor: addressUtils.toChecksumed(
'0x7ad1d568b3fe5bad3fc264aca70bc7bcd5e4a6ff'
),
identity:
Expand Down
24 changes: 14 additions & 10 deletions packages/core/tests/address/address.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from '@jest/globals';
import { address, secp256k1 } from '../../src';
import { addressUtils, secp256k1 } from '../../src';
import {
checksumedAndUnchecksumedAddresses,
invalidPrivateKey,
Expand All @@ -25,12 +25,16 @@ describe('Address', () => {
* Valid and invalid address check
*/
test('validate address', () => {
expect(address.isAddress('not an address')).toEqual(false);
expect(addressUtils.isAddress('not an address')).toEqual(false);
expect(
address.isAddress('52908400098527886E0F7030069857D2E4169EE7')
addressUtils.isAddress(
'52908400098527886E0F7030069857D2E4169EE7'
)
).toEqual(false);
expect(
address.isAddress('0x52908400098527886E0F7030069857D2E4169EE7')
addressUtils.isAddress(
'0x52908400098527886E0F7030069857D2E4169EE7'
)
).toEqual(true);
});
});
Expand All @@ -47,7 +51,7 @@ describe('Address', () => {
expect(secp256k1.derivePublicKey(simplePrivateKey)).toEqual(
simplePublicKey
);
expect(address.fromPublicKey(simplePublicKey)).toEqual(
expect(addressUtils.fromPublicKey(simplePublicKey)).toEqual(
simpleAddress
);

Expand All @@ -67,10 +71,10 @@ describe('Address', () => {
*/
test('invalid input should throw error', () => {
expect(() => {
address.toChecksumed('invalid data');
addressUtils.toChecksumed('invalid data');
}).toThrowError(InvalidAddressError);
expect(() => {
address.toChecksumed(
addressUtils.toChecksumed(
'52908400098527886E0F7030069857D2E4169EE7'
);
}).toThrowError(InvalidAddressError);
Expand All @@ -81,9 +85,9 @@ describe('Address', () => {
*/
test('valid input', () => {
checksumedAndUnchecksumedAddresses.forEach((addressPair) => {
expect(address.toChecksumed(addressPair.unchecksumed)).toEqual(
addressPair.checksumed
);
expect(
addressUtils.toChecksumed(addressPair.unchecksumed)
).toEqual(addressPair.checksumed);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/core/tests/certificate/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { secp256k1, address, blake2b256, certificate } from '../../src';
import { secp256k1, addressUtils, blake2b256, certificate } from '../../src';

/**
* Private Key used for digital signature during certificate creation
Expand All @@ -19,7 +19,7 @@ const cert = {
},
domain: 'localhost',
timestamp: 1545035330,
signer: address.fromPublicKey(secp256k1.derivePublicKey(privKey))
signer: addressUtils.fromPublicKey(secp256k1.derivePublicKey(privKey))
};

/**
Expand Down
22 changes: 11 additions & 11 deletions packages/core/tests/hdnode/hdnode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
HDNode,
type WordlistSizeType,
ZERO_BUFFER,
address,
addressUtils,
mnemonic,
secp256k1
} from '../../src';
Expand Down Expand Up @@ -32,9 +32,9 @@ describe('Hdnode', () => {
const child = node.derive(i);

// Correct address
expect(address.fromPublicKey(child.publicKey).slice(2)).toEqual(
addresses[i]
);
expect(
addressUtils.fromPublicKey(child.publicKey).slice(2)
).toEqual(addresses[i]);
expect(child.address).toEqual('0x' + addresses[i]);

// Correct public key
Expand All @@ -53,9 +53,9 @@ describe('Hdnode', () => {
for (let i = 0; i < 5; i++) {
const child = xprivNode.derive(i);
// Correct address
expect(address.fromPublicKey(child.publicKey).slice(2)).toEqual(
addresses[i]
);
expect(
addressUtils.fromPublicKey(child.publicKey).slice(2)
).toEqual(addresses[i]);
expect(child.address).toEqual('0x' + addresses[i]);

// Correct public key
Expand All @@ -71,9 +71,9 @@ describe('Hdnode', () => {
for (let i = 0; i < 5; i++) {
const child = xpubNode.derive(i);
// Correct address
expect(address.fromPublicKey(child.publicKey).slice(2)).toEqual(
addresses[i]
);
expect(
addressUtils.fromPublicKey(child.publicKey).slice(2)
).toEqual(addresses[i]);
expect(child.address).toEqual('0x' + addresses[i]);

// Null private key
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('Hdnode', () => {

// Address
expect(currentHdnode.address).toBeDefined();
address.isAddress(currentHdnode.address);
addressUtils.isAddress(currentHdnode.address);
}
);
});
Expand Down
8 changes: 5 additions & 3 deletions packages/core/tests/keystore/keystore.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from '@jest/globals';
import { secp256k1, address, keystore } from '../../src';
import { secp256k1, addressUtils, keystore } from '../../src';
import { type Keystore } from '../../src';
import { encryptionPassword } from './fixture';
import {
Expand Down Expand Up @@ -28,8 +28,10 @@ describe('Keystore', () => {

// Verify keystore
expect(myKeystore.version).toBe(3);
const keyStoreAddress = address.toChecksumed(`0x` + myKeystore.address);
const addressFromPrivateKey = address.fromPublicKey(
const keyStoreAddress = addressUtils.toChecksumed(
`0x` + myKeystore.address
);
const addressFromPrivateKey = addressUtils.fromPublicKey(
secp256k1.derivePublicKey(privateKey)
);
expect(keyStoreAddress).toEqual(addressFromPrivateKey);
Expand Down
Loading

1 comment on commit e3bb8bd

@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% (996/996) 100% (283/283) 100% (174/174)
Title Tests Skipped Failures Errors Time
core 232 0 💤 0 ❌ 0 🔥 1m 2s ⏱️
network 30 0 💤 0 ❌ 0 🔥 8.92s ⏱️
errors 19 0 💤 0 ❌ 0 🔥 9.086s ⏱️

Please sign in to comment.