Skip to content

Commit

Permalink
refactor: remove backwards compatibility (#1390)
Browse files Browse the repository at this point in the history
* refactor: first commit

* refactor: changes

* refactor: changes

* refactor: changes

* refactor: changes

* refactor: changes

* refactor: changes

* refactor: added abiitem test

* refactor: added abiitem test

* refactor: docs test fixed

* refactor: docs test fixed

* refactor: docs test fixed

* refactor: using parseObjectValues in case we have nested objects

* refactor: removed circular dependencies

* refactor: check if more than 11 occurrences
  • Loading branch information
freemanzMrojo authored Oct 7, 2024
1 parent 07f8f63 commit 506fb25
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 346 deletions.
8 changes: 4 additions & 4 deletions apps/sdk-nextjs-integration/src/app/hash/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useState, useEffect } from 'react';

import { blake2b256, keccak256, sha256 } from '@vechain/sdk-core';
import { Blake2b256, Keccak256, Sha256, Txt } from '@vechain/sdk-core';
import { type HashedContent } from '@/types';
import { Header } from '@/components';

Expand All @@ -24,9 +24,9 @@ export default function HashPage(): JSX.Element {
function hashContent(content: string): void {
try {
setHashedContent({
blake2b256: blake2b256(content, 'hex'),
keccak256: keccak256(content, 'hex'),
sha256: sha256(content, 'hex')
blake2b256: Blake2b256.of(Txt.of(content).bytes).toString(),
keccak256: Keccak256.of(Txt.of(content).bytes).toString(),
sha256: Sha256.of(Txt.of(content).bytes).toString()
});
} catch (error) {
setHashedContent({
Expand Down
5 changes: 2 additions & 3 deletions apps/sdk-nextjs-integration/src/app/transfer-logs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Header } from '@/components';
import { explorerUrl, thorClient } from '@/const';
import { type Transfer } from '@/types';
import { reduceHexStringSize } from '@/utils';
import { addressUtils, FixedPointNumber, Units } from '@vechain/sdk-core';
import { Address, FixedPointNumber, Units } from '@vechain/sdk-core';
import {
type CompressedBlockDetail,
type FilterTransferLogsOptions
Expand Down Expand Up @@ -66,8 +66,7 @@ export default function TransferLogs(): JSX.Element {

// Update the history when the address changes
useEffect(() => {
// Backwards compatibility, from now on use Address.isValid
if (addressUtils.isAddress(address)) {
if (Address.isValid(address)) {
void getHistoryFor(address);
}
}, [address]);
Expand Down
2 changes: 1 addition & 1 deletion docs/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ console.log('Mnemonic words', randomMnemonic);
// 2 - Derive private key from mnemonic words according to BIP32, using the path `m/44'/818'/0'/0`.

// Defined for VET at https://github.com/satoshilabs/slips/blob/master/slip-0044.md
const privateKey = mnemonic.derivePrivateKey(randomMnemonic);
const privateKey = Mnemonic.toPrivateKey(randomMnemonic);

console.log(Hex.of(privateKey).toString());
// ...SOME PRIVATE KEY...
Expand Down
1 change: 0 additions & 1 deletion docs/diagrams/architecture/vcdm.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ classDiagram
}
class ABIContract {
+ABIContract ofAbi(ViemABI abi)$
+ABIContract ofStringAbi(string abi)$
+ABIFunction getFunction(string name)
+ABIEvent getEvent(string name)
+Hex encodeFunctionInput(string functionName, unknown[] functionData)
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/accounts/bip39.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, Mnemonic, mnemonic } from '@vechain/sdk-core';
import { Hex, Mnemonic } from '@vechain/sdk-core';
import { expect } from 'expect';

// START_SNIPPET: Bip39Snippet
Expand All @@ -13,7 +13,7 @@ console.log('Mnemonic words', randomMnemonic);
// 2 - Derive private key from mnemonic words according to BIP32, using the path `m/44'/818'/0'/0`.

// Defined for VET at https://github.com/satoshilabs/slips/blob/master/slip-0044.md
const privateKey = mnemonic.derivePrivateKey(randomMnemonic);
const privateKey = Mnemonic.toPrivateKey(randomMnemonic);

console.log(Hex.of(privateKey).toString());
// ...SOME PRIVATE KEY...
Expand Down
8 changes: 4 additions & 4 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module.exports = {
workerThreads: true,
coverageThreshold: {
global: {
branches: 91,
functions: 92,
lines: 94,
statements: 94
branches: 95,
functions: 97,
lines: 97,
statements: 97
}
}
};
15 changes: 1 addition & 14 deletions packages/core/src/vcdm/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,4 @@ class Address extends HexUInt {
}
}

// Backwards compatibility, remove when it is matured enough #1184

const addressUtils = {
fromPrivateKey: (privateKey: Uint8Array): string =>
Address.ofPrivateKey(privateKey).toString(),
fromPublicKey: (publicKey: Uint8Array): string =>
Address.ofPublicKey(publicKey).toString(),
isAddress: (addressToVerify: string): boolean =>
Address.isValid(addressToVerify),
toERC55Checksum: (address: string): string =>
Address.checksum(HexUInt.of(address)).toString()
};

export { Address, addressUtils };
export { Address };
19 changes: 1 addition & 18 deletions packages/core/src/vcdm/Mnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
InvalidOperation
} from '@vechain/sdk-errors';
import { HDKey } from '../hdkey';
import { Address } from './Address';
import { type VeChainDataModel } from './VeChainDataModel';

/**
Expand Down Expand Up @@ -229,21 +228,5 @@ class Mnemonic implements VeChainDataModel<Mnemonic> {
}
}

// Backwards compatibility, remove in future versions #1184

const mnemonic = {
deriveAddress: (words: string[], path: string = 'm/0'): string =>
Address.ofMnemonic(words, path).toString(),
derivePrivateKey: (words: string[], path: string = 'm/0'): Uint8Array =>
Mnemonic.toPrivateKey(words, path),
generate: (
wordlistSize?: WordlistSizeType,
randomGenerator?: (
numberOfBytes: WordListRandomGeneratorSizeInBytes
) => Uint8Array
): string[] => Mnemonic.of(wordlistSize, randomGenerator),
isValid: (words: string[]): boolean => Mnemonic.isValid(words)
};

export { Mnemonic, mnemonic };
export { Mnemonic };
export type { WordListRandomGeneratorSizeInBytes, WordlistSizeType };
11 changes: 1 addition & 10 deletions packages/core/src/vcdm/Revision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,4 @@ class Revision extends Txt {
}
}

// Backwards compatibility, remove when it is matured enough #1184

const revisionUtils = {
isRevisionAccount: (revision: string | number): boolean =>
Revision.isValid(revision),
isRevisionBlock: (revision: string | number): boolean =>
Revision.isValid(revision)
};

export { Revision, revisionUtils };
export { Revision };
14 changes: 1 addition & 13 deletions packages/core/src/vcdm/abi/ABIContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from '@vechain/sdk-errors';
import {
getAbiItem,
parseAbi,
type AbiEvent,
type AbiFunction,
type DecodeEventLogReturnType,
Expand Down Expand Up @@ -34,15 +33,6 @@ class ABIContract extends ABI {
return new ABIContract(abi);
}

/**
* Creates an ABIContract instance from an ABI string.
* @param {string} abi representation of the contract.
* @returns New instance of ABIContract.
*/
public static ofStringAbi(abi: string): ABIContract {
return new ABIContract(parseAbi([abi]));
}

/**
* Returns the function with the given name.
* @param {string} name The function's name.
Expand Down Expand Up @@ -280,11 +270,9 @@ class ABIContract extends ABI {
const eventLogDecoded = this.parseLog(data, topics);
if (eventLogDecoded.args === undefined) {
return [];
} else if (eventLogDecoded.args instanceof Object) {
return Object.values(eventLogDecoded.args);
}

return eventLogDecoded.args;
return this.parseObjectValues(eventLogDecoded.args);
}
}

Expand Down
20 changes: 5 additions & 15 deletions packages/core/src/vcdm/abi/ABIEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,13 @@ class ABIEvent extends ABIItem {
* @returns {unknown[]} The decoded data as array of values.
*/
public decodeEventLogAsArray(event: ABIEventData): unknown[] {
try {
const rawDecodedData = this.decodeEventLog(event);
const rawDecodedData = this.decodeEventLog(event);

if (rawDecodedData.args === undefined) {
return [];
} else if (rawDecodedData.args instanceof Object) {
return Object.values(rawDecodedData.args);
}
return rawDecodedData.args as unknown[];
} catch (error) {
throw new InvalidAbiDataToEncodeOrDecode(
'ABIEvent.decodeEventLogAsArray',
'Decoding failed: Data must be a valid hex string encoding a compliant ABI type.',
{ data: event },
error
);
if (rawDecodedData.args === undefined) {
return [];
}

return this.parseObjectValues(rawDecodedData.args);
}

/**
Expand Down
29 changes: 17 additions & 12 deletions packages/core/src/vcdm/abi/ABIItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import {
} from 'viem';
import { ABI } from './ABI';

type ABIItemType = AbiFunction | AbiEvent;

/**
* Represents an ABI (Application Binary Interface) item.
* @extends ABI
*/
abstract class ABIItem extends ABI {
public readonly signature: ABIItemType;
public readonly signature: AbiFunction | AbiEvent;
public readonly stringSignature: string;
/**
* ABIItem constructor from item (Event, Function...) signature.
*
* @param {string | ViemABI} signature - The signature of the ABI item (Function, Event...).
* @param {string | AbiFunction | AbiEvent} signature - The signature of the ABI item (Function, Event...).
**/
public constructor(signature: string | ABIItemType) {
public constructor(signature: string | AbiFunction | AbiEvent) {
super();
switch (typeof signature) {
case 'string':
Expand All @@ -45,19 +43,26 @@ abstract class ABIItem extends ABI {
): T;

public static ofSignature<T extends ABIItem>(
ABIItemConstructor: new (signature: ABIItemType) => T,
signature: ABIItemType
ABIItemConstructor: new (signature: AbiFunction) => T,
signature: AbiFunction
): T;

public static ofSignature<T extends ABIItem>(
ABIItemConstructor: new (signature: AbiEvent) => T,
signature: AbiEvent
): T;

/**
* Returns and instance of an ABIItem from a signature.
* @param ABIItemConstructor ABIItem constructor.
* @param {string | ABIItemType} signature Signature of the ABIIItem.
* @param {string | AbiFunction | AbiEvent} signature Signature of the ABIIItem.
* @returns {T} An instance of the ABIItem.
*/
public static ofSignature<T extends ABIItem>(
ABIItemConstructor: new (signature: string | ABIItemType) => T,
signature: string | ABIItemType
ABIItemConstructor: new (
signature: string | AbiFunction | AbiEvent
) => T,
signature: string | AbiFunction | AbiEvent
): T {
return new ABIItemConstructor(signature);
}
Expand Down Expand Up @@ -88,12 +93,12 @@ abstract class ABIItem extends ABI {
* @returns {number} A non-zero number if the current ABIItem is different to the other ABI or zero if they are equal.
* @override {@link VeChainDataModel#compareTo}
**/
public compareTo(that: ABIItem): number {
public override compareTo(that: ABIItem): number {
if (super.compareTo(that) !== 0) {
return -1;
}
return this.stringSignature.localeCompare(that.stringSignature);
}
}

export { ABIItem, type ABIItemType };
export { ABIItem };
22 changes: 1 addition & 21 deletions packages/core/src/vcdm/hash/Blake2b256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { blake2b as nh_blake2b } from '@noble/hashes/blake2b';
import { InvalidOperation } from '@vechain/sdk-errors';
import { Hex } from '../Hex';
import { HexUInt } from '../HexUInt';
import { Txt } from '../Txt';

/**
* Represents the result of an [BLAKE](https://en.wikipedia.org/wiki/BLAKE_(hash_function)) [BlAKE2B 256](https://www.blake2.net/) hash operation.
Expand Down Expand Up @@ -40,23 +39,4 @@ class Blake2b256 extends HexUInt {
}
}

// Backwards compatibility, remove in future release #1184

function blake2b256(
data: string | Uint8Array,
returnType: 'buffer'
): Uint8Array;

function blake2b256(data: string | Uint8Array, returnType: 'hex'): string;

function blake2b256(
data: string | Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
returnType: 'buffer' | 'hex' = 'buffer'
): string | Uint8Array {
return returnType === 'buffer'
? Blake2b256.of(Txt.of(data).bytes).bytes
: Blake2b256.of(Txt.of(data).bytes).toString();
}

export { Blake2b256, blake2b256 };
export { Blake2b256 };
19 changes: 1 addition & 18 deletions packages/core/src/vcdm/hash/Keccak256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { keccak_256 as nh_keccak_256 } from '@noble/hashes/sha3';
import { InvalidOperation } from '@vechain/sdk-errors';
import { Hex } from '../Hex';
import { HexUInt } from '../HexUInt';
import { Txt } from '../Txt';

/**
* Represents the result of an [SHA-3](https://en.wikipedia.org/wiki/SHA-3) [KECCAK 256](https://keccak.team/keccak.html) hash operation.
Expand Down Expand Up @@ -37,20 +36,4 @@ class Keccak256 extends HexUInt {
}
}

// Backwards compatibility, remove in future release #1184

function keccak256(data: string | Uint8Array, returnType: 'buffer'): Uint8Array;

function keccak256(data: string | Uint8Array, returnType: 'hex'): string;

function keccak256(
data: string | Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
returnType: 'buffer' | 'hex' = 'buffer'
): string | Uint8Array {
return returnType === 'buffer'
? Keccak256.of(Txt.of(data).bytes).bytes
: Keccak256.of(Txt.of(data).bytes).toString();
}

export { Keccak256, keccak256 };
export { Keccak256 };
19 changes: 1 addition & 18 deletions packages/core/src/vcdm/hash/Sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as nh_sha256 from '@noble/hashes/sha256';
import { InvalidOperation } from '@vechain/sdk-errors';
import { Hex } from '../Hex';
import { HexUInt } from '../HexUInt';
import { Txt } from '../Txt';

/**
* Represents the result of an [SHA256](https://en.wikipedia.org/wiki/SHA-2) hash operation.
Expand Down Expand Up @@ -36,20 +35,4 @@ class Sha256 extends HexUInt {
}
}

// Backwards compatibility, remove in future release #1184

function sha256(data: string | Uint8Array, returnType: 'buffer'): Uint8Array;

function sha256(data: string | Uint8Array, returnType: 'hex'): string;

function sha256(
data: string | Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
returnType: 'buffer' | 'hex' = 'buffer'
): string | Uint8Array {
return returnType === 'buffer'
? Sha256.of(Txt.of(data).bytes).bytes
: Sha256.of(Txt.of(data).bytes).toString();
}

export { Sha256, sha256 };
export { Sha256 };
Loading

1 comment on commit 506fb25

@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: 99%
99.03% (4196/4237) 97.6% (1384/1418) 98.97% (873/882)
Title Tests Skipped Failures Errors Time
core 799 0 💤 0 ❌ 0 🔥 1m 51s ⏱️
network 733 0 💤 0 ❌ 0 🔥 4m 39s ⏱️
errors 42 0 💤 0 ❌ 0 🔥 16.444s ⏱️

Please sign in to comment.