Skip to content

Commit

Permalink
86 transaction online (#228)
Browse files Browse the repository at this point in the history
* fix: change endpoint structure in order to improve readability

* fix: adapt to new urls

* fix: move simulateTransactionCall to transaction-client.ts

* fix: add transaction client to merge

* fix: add missing exports

* fix: remove redundant

* fix: remove unuseful

* fix: change standard accounts names (group by tests) and create accounts for transaction tests

* feat: transaction client

* feat: tests. Missing simulate transaction call

* fix: yarn.lock update and format better errorTypes.ts

* fix: simulate transaction call in a separated task

* fix: add check on result of transaction send

* feat: test sending of delegated transactions and multiclauses

* refactor: linting

* refactor: restored linting due to prettier update

* fix: typo

* fix: typo 2

* fix: typo for block_ref

* feat: transaction utils for check transaction data

* fix: typo

* fix: types

* fix: import

* fix: check transaction

* fix: change approach with using isThorId

* fix: 100% coverage

* fix: last version

* fix: tsdoc and handle null

* fix: handle null for block

* fix: missing types inference

* fix: tests

* fix: add data into errors

---------

Co-authored-by: Piero Bassa <[email protected]>
  • Loading branch information
rodolfopietro97 and pierobassa authored Nov 14, 2023
1 parent dcfbc60 commit dd88b92
Show file tree
Hide file tree
Showing 27 changed files with 1,252 additions and 387 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type RLPValidObject } from '../encoding';
import { blake2b256 } from '../hash';
import { secp256k1 } from '../secp256k1';
import {
BLOCKREF_LENGTH,
BLOCK_REF_LENGTH,
dataUtils,
SIGNATURE_LENGTH,
SIGNED_TRANSACTION_RLP,
Expand Down Expand Up @@ -417,7 +417,7 @@ class Transaction {
body.blockRef !== undefined &&
dataUtils.isHexString(body.blockRef) &&
Buffer.from(body.blockRef.slice(2), 'hex').length ===
BLOCKREF_LENGTH &&
BLOCK_REF_LENGTH &&
// Expiration
body.expiration !== undefined &&
// Clauses
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/utils/const/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ const DECIMAL_INTEGER_REGEX = /^\d+$/;
*/
const NUMERIC_REGEX = /^-?\d*(\.\d+)?$/;

/**
* Default length of thor id hex string.
* Thor id is a 64 characters long hexadecimal string.
* This is used to validate thor id strings (block ids, transaction ids, ...).
*/
const THOR_ID_LENGTH = 64;

export {
ZERO_BUFFER,
HEX_REGEX,
HEX_ADDRESS_REGEX,
HEX_REGEX_OPTIONAL_PREFIX,
DECIMAL_INTEGER_REGEX,
NUMERIC_REGEX
NUMERIC_REGEX,
THOR_ID_LENGTH
};
6 changes: 3 additions & 3 deletions packages/core/src/utils/const/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ const SIGNED_TRANSACTION_RLP = new RLP.Profiler({
const SIGNATURE_LENGTH = 65;

/**
* Blockref field length
* Block ref field length
*/
const BLOCKREF_LENGTH = 8;
const BLOCK_REF_LENGTH = 8;

export {
TRANSACTIONS_GAS_CONSTANTS,
Expand All @@ -148,5 +148,5 @@ export {
TRANSACTION_FEATURES_KIND,
TRANSACTION_SIGNATURE_KIND,
SIGNATURE_LENGTH,
BLOCKREF_LENGTH
BLOCK_REF_LENGTH
};
27 changes: 25 additions & 2 deletions packages/core/src/utils/data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
DECIMAL_INTEGER_REGEX,
HEX_REGEX,
HEX_REGEX_OPTIONAL_PREFIX,
NUMERIC_REGEX
NUMERIC_REGEX,
THOR_ID_LENGTH
} from '../const';
import { type HexString } from '../types';
import { type HexConfig } from './types';
Expand Down Expand Up @@ -83,10 +84,32 @@ const isNumeric = (value: string): boolean => {
return NUMERIC_REGEX.test(value);
};

/**
* Checks whether the provided data is a valid transaction thor id.
* Thor id is a 64 characters long hexadecimal string.
* It is used to identify a transaction id, a block id, ....
*
* @remarks
* The check can optionally validate the presence of a '0x' prefix.
*
* @param data - The string data to check.
* @param checkPrefix - A boolean determining whether to validate the '0x' prefix (default: false).
* @returns A boolean indicating whether the input is a valid hexadecimal string.
*/
const isThorId = (data: string, checkPrefix: boolean = false): boolean => {
return (
isHexString(data, checkPrefix) &&
(checkPrefix
? data.length === THOR_ID_LENGTH + 2 // +2 for '0x'
: data.length === THOR_ID_LENGTH)
);
};

export const dataUtils = {
toHexString,
isHexString,
removePrefix,
isDecimalString,
isNumeric
isNumeric,
isThorId
};
1 change: 1 addition & 0 deletions packages/core/src/utils/data/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './data';
export * from './types.d';
23 changes: 22 additions & 1 deletion packages/core/tests/utils/data/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { describe, expect, test } from '@jest/globals';
import { dataUtils } from '../../../src';
import {
invalidHexStrings,
invalidThorIDs,
isNumericTestCases,
prefixedAndUnprefixedStrings,
validHexStrings
validHexStrings,
validThorIDs
} from './fixture';

/**
Expand Down Expand Up @@ -61,6 +63,25 @@ describe('utils/hex', () => {
});
});

/**
* Thor id verification
*/
describe('Thor id verification', () => {
validThorIDs.forEach((id) => {
test(`Should return true for valid thor id string: ${id.value}`, () => {
expect(dataUtils.isThorId(id.value, id.checkPrefix)).toBe(true);
});
});

invalidThorIDs.forEach((id) => {
test(`Should return false for valid thor id string: ${id.value}`, () => {
expect(dataUtils.isThorId(id.value, id.checkPrefix)).toBe(
false
);
});
});
});

/**
* Hex prefixes
*/
Expand Down
36 changes: 36 additions & 0 deletions packages/core/tests/utils/data/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ const invalidHexStrings = [
'0x48656c6c6fz'
];

/**
* Valid thor IDs.
*/
const validThorIDs = [
{
value: '0x271f7db20141001975f71deb8fca90d6b22b8d6610dfb5a3e0bbeaf78b5a4891',
checkPrefix: true
},
{
value: '271f7db20141001975f71deb8fca90d6b22b8d6610dfb5a3e0bbeaf78b5a4891'
},
{
value: '271f7db20141001975f71deb8fca90d6b22b8d6610dfb5a3e0bbeaf78b5a4891',
checkPrefix: false
}
];

/**
* Invalid thor IDs.
*/
const invalidThorIDs = [
{
value: '0x271f7db20141001975f71deb8fca90d6b22b8d6610d',
checkPrefix: true
},
{
value: '0xInvalidThorID',
checkPrefix: false
},
{
value: '0xInvalidThorID'
}
];

/**
* Prefixd and unprefixed strings
*/
Expand Down Expand Up @@ -132,6 +166,8 @@ const isNumericTestCases = [
export {
validHexStrings,
invalidHexStrings,
validThorIDs,
invalidThorIDs,
prefixedAndUnprefixedStrings,
isNumericTestCases
};
124 changes: 62 additions & 62 deletions packages/errors/src/types/errorTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ type ErrorCode =
type DataType<ErrorCodeT extends ErrorCode> = ErrorCodeT extends RLP.INVALID_RLP
? InvalidRLPErrorData
: ErrorCodeT extends HTTP_CLIENT.INVALID_HTTP_REQUEST
? HTTPClientErrorData
: DefaultErrorData;
? HTTPClientErrorData
: DefaultErrorData;

/**
* Default error codes.
Expand Down Expand Up @@ -122,66 +122,66 @@ type ErrorType<ErrorCodeT> =
ErrorCodeT extends SECP256K1.INVALID_SECP256k1_PRIVATE_KEY
? InvalidSecp256k1PrivateKeyError
: ErrorCodeT extends SECP256K1.INVALID_SECP256k1_MESSAGE_HASH
? InvalidSecp256k1MessageHashError
: ErrorCodeT extends SECP256K1.INVALID_SECP256k1_SIGNATURE
? InvalidSecp256k1SignatureError
: ErrorCodeT extends SECP256K1.INVALID_SECP256k1_SIGNATURE_RECOVERY
? InvalidSecp256k1SignatureRecoveryError
: ErrorCodeT extends ADDRESS.INVALID_ADDRESS
? InvalidAddressError
: ErrorCodeT extends KEYSTORE.INVALID_KEYSTORE
? InvalidKeystoreError
: ErrorCodeT extends KEYSTORE.INVALID_PASSWORD
? InvalidKeystorePasswordError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_CHAIN_CODE
? InvalidHDNodeChaincodeError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_MNEMONICS
? InvalidHDNodeMnemonicsError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_PRIVATE_KEY
? InvalidHDNodePrivateKeyError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_PUBLIC_KEY
? InvalidHDNodePublicKeyError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_DERIVATION_PATH
? InvalidHDNodeDerivationPathError
: ErrorCodeT extends BLOOM.INVALID_BLOOM
? InvalidBloomError
: ErrorCodeT extends BLOOM.INVALID_K
? InvalidKError
: ErrorCodeT extends CERTIFICATE.CERTIFICATE_NOT_SIGNED
? CertificateNotSignedError
: ErrorCodeT extends CERTIFICATE.CERTIFICATE_INVALID_SIGNATURE_FORMAT
? CertificateInvalidSignatureFormatError
: ErrorCodeT extends CERTIFICATE.CERTIFICATE_INVALID_SIGNER
? CertificateInvalidSignerError
: ErrorCodeT extends ABI.INVALID_EVENT
? InvalidAbiEventError
: ErrorCodeT extends ABI.INVALID_DATA_TO_DECODE
? InvalidAbiDataToDecodeError
: ErrorCodeT extends ABI.INVALID_DATA_TO_ENCODE
? InvalidAbiDataToEncodeError
: ErrorCodeT extends ABI.INVALID_FORMAT_TYPE
? InvalidAbiFormatTypeError
: ErrorCodeT extends ABI.INVALID_FUNCTION
? InvalidAbiFunctionError
: ErrorCodeT extends ABI.CONTRACT_INTERFACE_ERROR
? ContractInterfaceError
: ErrorCodeT extends RLP.INVALID_RLP
? InvalidRLPError
: ErrorCodeT extends DATA.INVALID_DATA_TYPE
? InvalidDataTypeError
: ErrorCodeT extends DATA.INVALID_DATA_RETURN_TYPE
? InvalidDataReturnTypeError
: ErrorCodeT extends TRANSACTION.ALREADY_SIGNED
? TransactionAlreadySignedError
: ErrorCodeT extends TRANSACTION.NOT_SIGNED
? TransactionNotSignedError
: ErrorCodeT extends TRANSACTION.INVALID_TRANSACTION_BODY
? TransactionBodyError
: ErrorCodeT extends TRANSACTION.INVALID_DELEGATION
? TransactionDelegationError
: ErrorCodeT extends HTTP_CLIENT.INVALID_HTTP_REQUEST
? HTTPClientError
: never;
? InvalidSecp256k1MessageHashError
: ErrorCodeT extends SECP256K1.INVALID_SECP256k1_SIGNATURE
? InvalidSecp256k1SignatureError
: ErrorCodeT extends SECP256K1.INVALID_SECP256k1_SIGNATURE_RECOVERY
? InvalidSecp256k1SignatureRecoveryError
: ErrorCodeT extends ADDRESS.INVALID_ADDRESS
? InvalidAddressError
: ErrorCodeT extends KEYSTORE.INVALID_KEYSTORE
? InvalidKeystoreError
: ErrorCodeT extends KEYSTORE.INVALID_PASSWORD
? InvalidKeystorePasswordError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_CHAIN_CODE
? InvalidHDNodeChaincodeError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_MNEMONICS
? InvalidHDNodeMnemonicsError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_PRIVATE_KEY
? InvalidHDNodePrivateKeyError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_PUBLIC_KEY
? InvalidHDNodePublicKeyError
: ErrorCodeT extends HDNODE.INVALID_HDNODE_DERIVATION_PATH
? InvalidHDNodeDerivationPathError
: ErrorCodeT extends BLOOM.INVALID_BLOOM
? InvalidBloomError
: ErrorCodeT extends BLOOM.INVALID_K
? InvalidKError
: ErrorCodeT extends CERTIFICATE.CERTIFICATE_NOT_SIGNED
? CertificateNotSignedError
: ErrorCodeT extends CERTIFICATE.CERTIFICATE_INVALID_SIGNATURE_FORMAT
? CertificateInvalidSignatureFormatError
: ErrorCodeT extends CERTIFICATE.CERTIFICATE_INVALID_SIGNER
? CertificateInvalidSignerError
: ErrorCodeT extends ABI.INVALID_EVENT
? InvalidAbiEventError
: ErrorCodeT extends ABI.INVALID_DATA_TO_DECODE
? InvalidAbiDataToDecodeError
: ErrorCodeT extends ABI.INVALID_DATA_TO_ENCODE
? InvalidAbiDataToEncodeError
: ErrorCodeT extends ABI.INVALID_FORMAT_TYPE
? InvalidAbiFormatTypeError
: ErrorCodeT extends ABI.INVALID_FUNCTION
? InvalidAbiFunctionError
: ErrorCodeT extends ABI.CONTRACT_INTERFACE_ERROR
? ContractInterfaceError
: ErrorCodeT extends RLP.INVALID_RLP
? InvalidRLPError
: ErrorCodeT extends DATA.INVALID_DATA_TYPE
? InvalidDataTypeError
: ErrorCodeT extends DATA.INVALID_DATA_RETURN_TYPE
? InvalidDataReturnTypeError
: ErrorCodeT extends TRANSACTION.ALREADY_SIGNED
? TransactionAlreadySignedError
: ErrorCodeT extends TRANSACTION.NOT_SIGNED
? TransactionNotSignedError
: ErrorCodeT extends TRANSACTION.INVALID_TRANSACTION_BODY
? TransactionBodyError
: ErrorCodeT extends TRANSACTION.INVALID_DELEGATION
? TransactionDelegationError
: ErrorCodeT extends HTTP_CLIENT.INVALID_HTTP_REQUEST
? HTTPClientError
: never;

/**
* Map to get the error class from the error code.
Expand Down
6 changes: 3 additions & 3 deletions packages/network/src/client/thor/accounts/account-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AccountClient {

return (await this.httpClient.http(
'GET',
thorest.account.ACCOUNT_DETAIL(address),
thorest.accounts.get.ACCOUNT_DETAIL(address),
{
query: buildQuery({ revision })
}
Expand Down Expand Up @@ -86,7 +86,7 @@ class AccountClient {

const result = (await this.httpClient.http(
'GET',
thorest.account.ACCOUNT_BYTECODE(address),
thorest.accounts.get.ACCOUNT_BYTECODE(address),
{
query: buildQuery({ revision })
}
Expand Down Expand Up @@ -133,7 +133,7 @@ class AccountClient {

const result = (await this.httpClient.http(
'GET',
thorest.account.STORAGE_AT(address, position),
thorest.accounts.get.STORAGE_AT(address, position),
{
query: buildQuery({ position, revision })
}
Expand Down
Loading

1 comment on commit dd88b92

@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% (1101/1101) 100% (311/311) 100% (200/200)
Title Tests Skipped Failures Errors Time
core 303 0 💤 0 ❌ 0 🔥 1m 33s ⏱️
network 55 0 💤 0 ❌ 0 🔥 38.791s ⏱️
errors 19 0 💤 0 ❌ 0 🔥 11.11s ⏱️

Please sign in to comment.