Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge branch '9097-enhance-scripts' of github.com:LiskHQ/lisk-sdk int…
Browse files Browse the repository at this point in the history
…o 9097-enhance-scripts
  • Loading branch information
Tschakki committed Oct 20, 2023
2 parents 423fb3e + b417ecd commit 3b23661
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 120 deletions.
4 changes: 0 additions & 4 deletions commander/src/bootstrapping/commands/transaction/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
getParamsSchema,
} from '../../../utils/transaction';
import { getDefaultPath } from '../../../utils/path';
import { isApplicationRunning } from '../../../utils/application';
import { PromiseResolvedType } from '../../../types';
import { DEFAULT_KEY_DERIVATION_PATH } from '../../../utils/config';
import { deriveKeypair } from '../../../utils/commons';
Expand Down Expand Up @@ -256,9 +255,6 @@ export abstract class SignCommand extends Command {

async finally(error?: Error | string): Promise<void> {
if (error) {
if (this._dataPath && !isApplicationRunning(this._dataPath)) {
throw new Error(`Application at data path ${this._dataPath} is not running.`);
}
this.error(error instanceof Error ? error.message : error);
}
if (this._client) {
Expand Down
8 changes: 4 additions & 4 deletions elements/lisk-cryptography/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const hash = (data: Buffer | string, format?: string): Buffer => {

export const parseKeyDerivationPath = (path: string) => {
if (!path.startsWith('m') || !path.includes('/')) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}

return (
Expand All @@ -58,19 +58,19 @@ export const parseKeyDerivationPath = (path: string) => {
.slice(1)
.map(segment => {
if (!/^[0-9']+$/g.test(segment)) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}

// if segment includes apostrophe add HARDENED_OFFSET
if (segment.includes(`'`)) {
if (parseInt(segment.slice(0, -1), 10) > MAX_UINT32 / 2) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}
return parseInt(segment, 10) + HARDENED_OFFSET;
}

if (parseInt(segment, 10) > MAX_UINT32) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}

return parseInt(segment, 10);
Expand Down
14 changes: 7 additions & 7 deletions elements/lisk-cryptography/test/ed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,19 @@ describe('getPrivateKeyFromPhraseAndPath', () => {

it('should fail for empty string path', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, '')).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it('should fail if path does not start with "m"', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, `/44'/134'/0'`)).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it('should fail if path does not include at least one "/"', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, 'm441340')).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

Expand All @@ -190,24 +190,24 @@ describe('getPrivateKeyFromPhraseAndPath', () => {
passphrase,
`m//134'/0'`, // should be number with or without ' between every back slash
),
).rejects.toThrow('Invalid path format');
).rejects.toThrow('Invalid key derivation path format');
});

it('should fail for path with invalid characters', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, `m/a'/134b'/0'`)).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it('should fail for path with non-sanctioned special characters', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, `m/4a'/#134b'/0'`)).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it(`should fail for path with segment greater than ${MAX_UINT32} / 2`, async () => {
await expect(
getPrivateKeyFromPhraseAndPath(passphrase, `m/44'/134'/${MAX_UINT32}'`),
).rejects.toThrow('Invalid path format');
).rejects.toThrow('Invalid key derivation path format');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,72 @@
"module": "interoperability",
"data": {
"ownChainName": "lisk_mainchain",
"ownChainNonce": 0,
"chainInfos": [],
"terminatedStateAccounts": [],
"terminatedOutboxAccounts": []
"ownChainNonce": "123",
"chainInfos": [
{
"chainID": "04123456",
"chainData": {
"name": "dummy",
"lastCertificate": {
"height": 567467,
"timestamp": 1000,
"stateRoot": "0000000000000000000000000000000000000000000000000000000000000000",
"validatorsHash": "a5a053d50182ea0c33bc03594cf4760f11d67cdba407d16bc0512fabb468253a"
},
"status": 2
},
"channelData": {
"inbox": {
"appendPath": [
"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000"
],
"size": 18,
"root": "9e37ffe87f08f6b7952d82acdb3376046792533ebf2ba8908dda3ebc813dac9a"
},
"outbox": {
"appendPath": [
"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000"
],
"size": 18,
"root": "221a0c844883022e3a54e78eb5dcdc2ed8faa85f648c40659cf5fd2054f36500"
},
"partnerChainOutboxRoot": "851faa36d87411d625fb4416c33c6a44795cb84155637f9991fcef06d1de7155",
"messageFeeTokenID": "0400000000000000",
"minReturnFeePerByte": "1000"
},
"chainValidators": {
"activeValidators": [
{
"blsKey": "3c1e6f29e3434f816cd6697e56cc54bc8d80927bf65a1361b383aa338cd3f63cbf82ce801b752cb32f8ecb3f8cc16835",
"bftWeight": "10"
}
],
"certificateThreshold": "10"
}
}
],
"terminatedStateAccounts": [
{
"chainID": "04123456",
"terminatedStateAccount": {
"stateRoot": "0000000000000000000000000000000000000000000000000000000000000000",
"mainchainStateRoot": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"initialized": true
}
}
],
"terminatedOutboxAccounts": [
{
"chainID": "04123456",
"terminatedOutboxAccount": {
"outboxRoot": "0aed892d544980f5b806dbce4bcb65517acf57a7af012c55c0b2f80b188fa290",
"outboxSize": 1,
"partnerChainInboxSize": 1
}
}
]
},
"schema": {
"$id": "/interoperability/module/genesis",
Expand Down Expand Up @@ -916,11 +978,17 @@
"totalSupply": "10300000000000000"
}
],
"escrowSubstore": [],
"escrowSubstore": [
{
"escrowChainID": "04123456",
"tokenID": "0400000000000000",
"amount": "0"
}
],
"supportedTokensSubstore": [
{
"chainID": "",
"supportedTokenIDs": []
"chainID": "04123456",
"supportedTokenIDs": ["0412345600000000"]
}
]
},
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ import { OwnChainAccountStore } from './stores/own_chain_account';
import { RegisteredNamesStore } from './stores/registered_names';
import { TerminatedOutboxStore } from './stores/terminated_outbox';
import { TerminatedStateStore } from './stores/terminated_state';
import {
ChainInfo,
GenesisInteroperability,
OwnChainAccount,
TerminatedStateAccountWithChainID,
} from './types';
import { ChainInfo, GenesisInteroperability, OwnChainAccount } from './types';
import { computeValidatorsHash, getTokenIDLSK } from './utils';
import { genesisInteroperabilitySchema } from './schemas';
import { CcmProcessedEvent } from './events/ccm_processed';
Expand Down Expand Up @@ -133,21 +128,18 @@ export abstract class BaseInteroperabilityModule extends BaseInteroperableModule
}

// activeValidators must be ordered lexicographically by blsKey property
const sortedByBlsKeys = [...activeValidators].sort((a, b) => a.blsKey.compare(b.blsKey));
for (let i = 0; i < activeValidators.length; i += 1) {
if (!activeValidators[i].blsKey.equals(sortedByBlsKeys[i].blsKey)) {
throw new Error('activeValidators must be ordered lexicographically by blsKey property.');
}
const blsKeys = activeValidators.map(v => v.blsKey);
if (!objectUtils.isBufferArrayOrdered(blsKeys)) {
throw new Error('activeValidators must be ordered lexicographically by blsKey property.');
}

// all blsKey properties must be pairwise distinct
const blsKeys = activeValidators.map(v => v.blsKey);
if (!objectUtils.bufferArrayUniqueItems(blsKeys)) {
throw new Error(`All blsKey properties must be pairwise distinct.`);
}

// for each validator in activeValidators, validator.bftWeight > 0 must hold
if (activeValidators.filter(v => v.bftWeight <= 0).length > 0) {
if (activeValidators.filter(v => v.bftWeight <= BigInt(0)).length > 0) {
throw new Error(`validator.bftWeight must be > 0.`);
}

Expand Down Expand Up @@ -189,28 +181,15 @@ export abstract class BaseInteroperabilityModule extends BaseInteroperableModule
}
}

protected _verifyTerminatedStateAccountsCommon(
terminatedStateAccounts: TerminatedStateAccountWithChainID[],
mainchainID: Buffer,
) {
protected _verifyTerminatedStateAccountsIDs(chainIDs: Buffer[]) {
// Each entry stateAccount in terminatedStateAccounts has a unique stateAccount.chainID
const chainIDs = terminatedStateAccounts.map(a => a.chainID);
if (!objectUtils.bufferArrayUniqueItems(chainIDs)) {
throw new Error(`terminatedStateAccounts don't hold unique chainID.`);
}

// terminatedStateAccounts is ordered lexicographically by stateAccount.chainID
const sortedByChainID = [...terminatedStateAccounts].sort((a, b) =>
a.chainID.compare(b.chainID),
);

for (let i = 0; i < terminatedStateAccounts.length; i += 1) {
const stateAccountWithChainID = terminatedStateAccounts[i];
if (!stateAccountWithChainID.chainID.equals(sortedByChainID[i].chainID)) {
throw new Error('terminatedStateAccounts must be ordered lexicographically by chainID.');
}

this._verifyChainID(stateAccountWithChainID.chainID, mainchainID, 'stateAccount.');
if (!objectUtils.isBufferArrayOrdered(chainIDs)) {
throw new Error('terminatedStateAccounts must be ordered lexicographically by chainID.');
}
}

Expand Down
Loading

0 comments on commit 3b23661

Please sign in to comment.