Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Transaction parser #754

Merged
merged 3 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 71 additions & 115 deletions src/modules/sign/components/PayloadDetailsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { GenericExtrinsicPayload } from '@polkadot/types';
import { GenericExtrinsicPayload, GenericCall } from '@polkadot/types';
import type { Call, ExtrinsicEra } from '@polkadot/types/interfaces';
import { AnyJson, AnyU8a, IExtrinsicEra, IMethod } from '@polkadot/types/types';
import { formatBalance } from '@polkadot/util';
Expand Down Expand Up @@ -62,65 +62,62 @@ const ExtrinsicPart = withRegistriesStore<ExtrinsicPartProps>(
const prefix = networkParams.prefix;
const typeRegistry = getTypeRegistry(networkKey)!;

useEffect(() => {
if (label === 'Method' && !fallback) {
try {
const call = typeRegistry.createType('Call', value);
const methodArgs = {};

function formatArgs(
callInstance: Call,
callMethodArgs: any,
depth: number
): void {
const { args, meta } = callInstance;
const paramArgKvArray = [];
if (!meta.args.length) {
const sectionMethod = `${call.method}.${call.section}`;
callMethodArgs[sectionMethod] = null;
return;
}

for (let i = 0; i < meta.args.length; i++) {
let argument;
if (
args[i].toRawType() === 'Balance' ||
args[i].toRawType() === 'Compact<Balance>'
) {
argument = formatBalance(args[i].toString());
} else if (
args[i].toRawType() === 'Address' ||
args[i].toRawType() === 'AccountId'
) {
// encode Address and AccountId to the appropriate prefix
argument = recodeAddress(args[i].toString(), prefix);
} else if ((args[i] as Call).section) {
argument = formatArgs(args[i] as Call, callMethodArgs, depth++); // go deeper into the nested calls
} else if (
args[i].toRawType() === 'Vec<AccountId>' ||
args[i].toRawType() === 'Vec<Address>'
) {
argument = (args[i] as any).map((v: any) =>
recodeAddress(v.toString(), prefix)
);
} else {
argument = args[i].toString();
}
const param = meta.args[i].name.toString();
const sectionMethod = `${call.method}.${call.section}`;
paramArgKvArray.push([param, argument]);
callMethodArgs[sectionMethod] = paramArgKvArray;
function parseArrayGenericCalls(
argsArray: Codec[],
): (Codec | ISanitizedCall)[] {
return argsArray.map((argument) => {
if (argument instanceof GenericCall) {
return parseGenericCall(argument);
}

return argument;
});
}

function parseGenericCall(
genericCall: GenericCall,
): ISanitizedCall {
const newArgs = {};

// Pull out the struct of arguments to this call
const callArgs = genericCall.get('args') as Struct;

// Make sure callArgs exists and we can access its keys
if (callArgs && callArgs.defKeys) {
// paramName is a string
for (const paramName of callArgs.defKeys) {
const argument = callArgs.get(paramName);

if (Array.isArray(argument)) {
newArgs[paramName] = parseArrayGenericCalls(argument);
} else if (argument instanceof GenericCall) {
newArgs[paramName] = parseGenericCall(argument);
} else if (paramName === 'call' && argument?.toRawType() === 'Bytes') {
// multiSig.asMulti.args.call is an OpaqueCall (Vec<u8>) that we
// serialize to a polkadot-js Call and parse so it is not a hex blob.
try {
const call = typeRegistry.createType('Call', argument.toHex());
newArgs[paramName] = parseGenericCall(call);
} catch {
newArgs[paramName] = argument;
}
} else {
newArgs[paramName] = argument;
}

formatArgs(call, methodArgs, 0);
setFormattedCallArgs(methodArgs);
} catch (e) {
alertDecodeError(setAlert);
setUseFallBack(true);
}
}

return {
method: {
pallet: genericCall.section,
method: genericCall.method,
},
args: newArgs,
};
}


useEffect(() => {
if (label === 'Era' && !fallback) {
if ((value as ExtrinsicEra).isMortalEra) {
setPeriod((value as ExtrinsicEra).asMortalEra.period.toString());
Expand All @@ -132,17 +129,18 @@ const ExtrinsicPart = withRegistriesStore<ExtrinsicPartProps>(
setTip(formatBalance(value as any));
}
}, [
fallback, //good
label, //good
prefix, //good
//value, //bad
networkKey, //good
//registriesStore, //bad
//setAlert, //bad
typeRegistry, //good
networks //good
fallback,
label,
prefix,
value,
networkKey,
registriesStore,
setAlert,
typeRegistry,
networks
]);


const renderEraDetails = (): React.ReactElement => {
if (period && phase) {
return (
Expand Down Expand Up @@ -175,58 +173,16 @@ const ExtrinsicPart = withRegistriesStore<ExtrinsicPartProps>(
}
};

type ArgsList = Array<[string, any]>;
type MethodCall = [string, ArgsList];
type FormattedArgs = Array<MethodCall>;

const renderMethodDetails = (): React.ReactNode => {
if (formattedCallArgs) {
const formattedArgs: FormattedArgs = Object.entries(formattedCallArgs);

// HACK: if there's a sudo method just put it to the front. Better way would be to order by depth but currently this is only relevant for a single extrinsic, so seems like overkill.
for (let i = 1; i < formattedArgs.length; i++) {
if (formattedArgs[i][0].includes('sudo')) {
const tmp = formattedArgs[i];
formattedArgs.splice(i, 1);
formattedArgs.unshift(tmp);
break;
}
}

return formattedArgs.map((entry, index) => {
const sectionMethod = entry[0];
const paramArgs: Array<[any, any]> = entry[1];

return (
<View key={index} style={styles.callDetails}>
<Text style={styles.secondaryText}>
Call <Text style={styles.titleText}>{sectionMethod}</Text> with
the following arguments:
</Text>
{paramArgs ? (
paramArgs.map(([param, arg]) => (
<View key={param} style={styles.callDetails}>
<Text style={styles.titleText}>
{' { '}
{param}:{' '}
{arg && arg.length > 50
? shortString(arg)
Copy link
Contributor

@Tbaut Tbaut Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some time achieve the same on Stylo, although to be able to show the whole decoded payload, all I needed was to ditch this shortString, which was effectively shortening valuable information.

: arg instanceof Array
? arg.join(', ')
: arg}{' '}
{'}'}
</Text>
</View>
))
) : (
<Text style={styles.secondaryText}>
This method takes 0 arguments.
</Text>
)}
</View>
);
});
}
const call = typeRegistry.createType('Call', value);
const parsed = JSON.stringify(parseGenericCall(call), null,2);
return (
<View style={styles.callDetails}>
<Text style={styles.titleText}>
{parsed}
</Text>
</View>
);
};

const renderTipDetails = (): React.ReactElement => {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import testIDs from 'e2e/testIDs';
import { testTap, testVisible } from 'e2e/utils';

// Set the default timeout
jest.setTimeout(120000);
jest.setTimeout(360000);
jasmine.getEnv().addReporter(adapter);

// This takes care of generating status logs on a per-spec basis. By default, jest only reports at file-level.
Expand Down