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

Commit

Permalink
Merge pull request #7410 from LiskHQ/7381-update_transaction_schema
Browse files Browse the repository at this point in the history
Update transaction schema - Closes #7381
  • Loading branch information
ishantiw authored Aug 18, 2022
2 parents 2aa7c0a + 2ce4a09 commit f58e6f5
Show file tree
Hide file tree
Showing 162 changed files with 2,948 additions and 3,074 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export abstract class BaseGenesisBlockCommand extends Command {

const genesisBlock = await app.generateGenesisBlock({
assets: assetsJSON.assets.map(a => ({
moduleID: a.moduleID,
module: a.module,
data: a.data,
schema: a.schema,
})),
Expand Down
42 changes: 17 additions & 25 deletions commander/src/bootstrapping/commands/transaction/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import {
} from '../../../utils/transaction';

interface Args {
readonly moduleID: number;
readonly commandID: number;
readonly module: string;
readonly command: string;
readonly fee: string;
}

Expand All @@ -63,8 +63,8 @@ interface CreateFlags {
}

interface Transaction {
moduleID: string;
commandID: string;
module: string;
command: string;
nonce: string;
fee: string;
senderPublicKey: string;
Expand All @@ -75,11 +75,7 @@ interface Transaction {
const getParamsObject = async (metadata: ModuleMetadataJSON[], flags: CreateFlags, args: Args) => {
let params: Record<string, unknown>;

const paramsSchema = getParamsSchema(
metadata,
cryptography.utils.intToBuffer(args.moduleID, 4).toString('hex'),
cryptography.utils.intToBuffer(args.commandID, 4).toString('hex'),
) as Schema;
const paramsSchema = getParamsSchema(metadata, args.module, args.command) as Schema;

if (flags.file) {
params = JSON.parse(getFileParams(flags.file));
Expand Down Expand Up @@ -119,11 +115,7 @@ const validateAndSignTransaction = async (
noSignature: boolean,
) => {
const { params, ...transactionWithoutParams } = transaction;
const paramsSchema = getParamsSchema(
metadata,
transaction.moduleID,
transaction.commandID,
) as Schema;
const paramsSchema = getParamsSchema(metadata, transaction.module, transaction.command) as Schema;

const txObject = codec.fromJSON(schema.transaction, { ...transactionWithoutParams, params: '' });
validator.validate(schema.transaction, txObject);
Expand Down Expand Up @@ -220,14 +212,14 @@ export abstract class CreateCommand extends Command {

static args = [
{
name: 'moduleID',
name: 'module',
required: true,
description: 'Registered transaction module id.',
description: 'Registered transaction module.',
},
{
name: 'commandID',
name: 'command',
required: true,
description: 'Registered transaction command id.',
description: 'Registered transaction command.',
},
{
name: 'fee',
Expand All @@ -237,11 +229,11 @@ export abstract class CreateCommand extends Command {
];

static examples = [
'transaction:create 2 0 100000000 --params=\'{"amount":100000000,"recipientAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","data":"send token"}\'',
'transaction:create 2 0 100000000 --params=\'{"amount":100000000,"recipientAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","data":"send token"}\' --json',
'transaction:create 2 0 100000000 --offline --network mainnet --network-identifier 873da85a2cee70da631d90b0f17fada8c3ac9b83b2613f4ca5fddd374d1034b3 --nonce 1 --params=\'{"amount":100000000,"recipientAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","data":"send token"}\'',
'transaction:create 2 0 100000000 --file=/txn_params.json',
'transaction:create 2 0 100000000 --file=/txn_params.json --json',
'transaction:create token transfer 100000000 --params=\'{"amount":100000000,"recipientAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","data":"send token"}\'',
'transaction:create token transfer 100000000 --params=\'{"amount":100000000,"recipientAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","data":"send token"}\' --json',
'transaction:create token transfer 100000000 --offline --network mainnet --network-identifier 873da85a2cee70da631d90b0f17fada8c3ac9b83b2613f4ca5fddd374d1034b3 --nonce 1 --params=\'{"amount":100000000,"recipientAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","data":"send token"}\'',
'transaction:create token transfer 100000000 --file=/txn_params.json',
'transaction:create token transfer 100000000 --file=/txn_params.json --json',
];

static flags = {
Expand Down Expand Up @@ -290,8 +282,8 @@ export abstract class CreateCommand extends Command {
const { args, flags } = this.parse(CreateCommand);

const incompleteTransaction = {
moduleID: cryptography.utils.intToBuffer(args.moduleID, 4).toString('hex'),
commandID: cryptography.utils.intToBuffer(args.commandID, 4).toString('hex'),
module: args.module,
command: args.command,
fee: args.fee,
nonce: '0',
senderPublicKey: '',
Expand Down
4 changes: 2 additions & 2 deletions commander/src/bootstrapping/commands/transaction/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ const signTransaction = async (
// eslint-disable-next-line @typescript-eslint/ban-types
const paramsSchema = getParamsSchema(
metadata,
transactionObject.moduleID,
transactionObject.commandID,
transactionObject.module,
transactionObject.command,
);
const networkIdentifierBuffer = Buffer.from(networkIdentifier as string, 'hex');
const passphrase = flags.passphrase ?? (await getPassphraseFromPrompt('passphrase', true));
Expand Down
6 changes: 3 additions & 3 deletions commander/src/utils/genesis_creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const genesisAssetsSchema = {

export interface GenesisAssetsInput {
assets: {
moduleID: Buffer;
module: string;
data: Record<string, unknown>;
schema: Schema;
}[];
Expand Down Expand Up @@ -96,7 +96,7 @@ export const generateGenesisBlockDefaultDPoSAssets = (input: GenesisBlockDefault

const genesisAssets = [
{
moduleID: new TokenModule().id,
module: new TokenModule().name,
data: {
userSubstore: accountList.map(a => ({
address: a.address,
Expand All @@ -111,7 +111,7 @@ export const generateGenesisBlockDefaultDPoSAssets = (input: GenesisBlockDefault
schema: tokenGenesisStoreSchema,
},
{
moduleID: new DPoSModule().id,
module: new DPoSModule().name,
data: {
validators: validatorList.map(v => ({
address: v.address,
Expand Down
22 changes: 11 additions & 11 deletions commander/src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import { isApplicationRunning } from './application';

export const getParamsSchema = (
metadata: ModuleMetadataJSON[],
moduleID: string,
commandID: string,
module: string,
command: string,
): Schema | undefined => {
const moduleMeta = metadata.find(meta => meta.id === moduleID);
const moduleMeta = metadata.find(meta => meta.name === module);
if (!moduleMeta) {
throw new Error(`ModuleID: ${moduleID} is not registered.`);
throw new Error(`Module: ${module} is not registered.`);
}
const commandMeta = moduleMeta.commands.find(meta => meta.id === commandID);
const commandMeta = moduleMeta.commands.find(meta => meta.name === command);
if (!commandMeta) {
throw new Error(`ModuleID: ${moduleID} CommandID: ${commandID} is not registered.`);
throw new Error(`Module: ${module} Command: ${command} is not registered.`);
}
return commandMeta.params;
};
Expand All @@ -46,7 +46,7 @@ export const decodeTransaction = (
const transactionBytes = Buffer.from(transactionHexStr, 'hex');
const id = cryptography.utils.hash(transactionBytes);
const transaction = codec.decodeJSON<TransactionJSON>(schema.transaction, transactionBytes);
const paramsSchema = getParamsSchema(metadata, transaction.moduleID, transaction.commandID);
const paramsSchema = getParamsSchema(metadata, transaction.module, transaction.command);
const params = codec.decodeJSON<Record<string, unknown>>(
paramsSchema as Schema,
Buffer.from(transaction.params, 'hex'),
Expand All @@ -69,8 +69,8 @@ export const encodeTransaction = (
}
const paramsSchema = getParamsSchema(
metadata,
(transaction.moduleID as Buffer).toString('hex'),
(transaction.commandID as Buffer).toString('hex'),
transaction.module as string,
transaction.command as string,
);
const paramsBytes = codec.encode(paramsSchema as Schema, transaction.params as object);
const txBytes = codec.encode(schema.transaction, { ...transaction, params: paramsBytes });
Expand All @@ -88,8 +88,8 @@ export const transactionToJSON = (
}
const paramsSchema = getParamsSchema(
metadata,
(transaction.moduleID as Buffer).toString('hex'),
(transaction.commandID as Buffer).toString('hex'),
transaction.module as string,
transaction.command as string,
);
const paramsJSON = codec.toJSON(paramsSchema as Schema, transaction.params as object);
const { id, params, ...txWithoutParams } = transaction;
Expand Down
Loading

0 comments on commit f58e6f5

Please sign in to comment.