Make Extrinsic compatible with Method (#335)
* Make Extrinsic compatible with Method * Remove Method -> methodIndex, only use callIndex
This commit is contained in:
@@ -8,6 +8,7 @@ import { AnyNumber, AnyU8a } from './types';
|
||||
import { hexToU8a, isHex, isU8a, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
import { blake2AsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
import Base from './codec/Base';
|
||||
import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact';
|
||||
import Struct from './codec/Struct';
|
||||
import ExtrinsicSignature from './ExtrinsicSignature';
|
||||
@@ -65,6 +66,11 @@ export default class Extrinsic extends Struct {
|
||||
return value as any;
|
||||
}
|
||||
|
||||
// expose args so it is compatible with Method (as constructor value)
|
||||
get args (): Array<Base> {
|
||||
return this.method.args;
|
||||
}
|
||||
|
||||
// the actual [sectionIndex, methodIndex] as used
|
||||
get callIndex (): Uint8Array {
|
||||
return this.method.callIndex;
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/extrinsics authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import Method from './Method';
|
||||
|
||||
describe('Metadata', () => {
|
||||
it('throws when not decodable', () => {
|
||||
expect(() => new Method('foo')).toThrowError(/Method: cannot decode value/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2018 @polkadot/extrinsics authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import extrinsics from '@polkadot/extrinsics/static';
|
||||
|
||||
import Method from './Method';
|
||||
|
||||
describe('Method', () => {
|
||||
beforeEach(() => {
|
||||
Method.injectExtrinsics(extrinsics);
|
||||
});
|
||||
|
||||
it('throws when not decodable', () => {
|
||||
expect(
|
||||
() => new Method('foo')
|
||||
).toThrowError(/Method: cannot decode value/);
|
||||
});
|
||||
|
||||
it('handles decoding correctly (bare)', () => {
|
||||
expect(
|
||||
new Method({
|
||||
args: [],
|
||||
callIndex: [1, 2]
|
||||
}).toU8a()
|
||||
).toEqual(new Uint8Array([1, 2]));
|
||||
});
|
||||
});
|
||||
@@ -23,7 +23,7 @@ interface ArgsDef {
|
||||
|
||||
interface DecodeMethodInput {
|
||||
args: any;
|
||||
methodIndex: MethodIndex | Uint8Array;
|
||||
callIndex: MethodIndex | Uint8Array;
|
||||
}
|
||||
|
||||
interface DecodedMethod extends DecodeMethodInput {
|
||||
@@ -44,7 +44,7 @@ export default class Method extends Struct {
|
||||
const decoded = Method.decodeMethod(value, meta);
|
||||
|
||||
super({
|
||||
methodIndex: MethodIndex,
|
||||
callIndex: MethodIndex,
|
||||
args: Struct.with(decoded.argsDef)
|
||||
}, decoded);
|
||||
|
||||
@@ -74,27 +74,40 @@ export default class Method extends Struct {
|
||||
// Get Struct definition of the arguments
|
||||
const argsDef = Method.getArgsDef(meta);
|
||||
|
||||
return { args: value.subarray(2), argsDef, methodIndex: callIndex, meta };
|
||||
return {
|
||||
args: value.subarray(2),
|
||||
argsDef,
|
||||
callIndex,
|
||||
meta
|
||||
};
|
||||
} else if (
|
||||
isObject(value) &&
|
||||
value.methodIndex &&
|
||||
value.callIndex &&
|
||||
value.args
|
||||
) {
|
||||
// Get the correct callIndex
|
||||
const callIndex = value.methodIndex instanceof MethodIndex
|
||||
? value.methodIndex.callIndex
|
||||
: value.methodIndex;
|
||||
// destructure value, we only pass args/methodsIndex out
|
||||
const { args, callIndex } = value;
|
||||
|
||||
// Get the correct lookupIndex
|
||||
const lookupIndex = callIndex instanceof MethodIndex
|
||||
? callIndex.toU8a()
|
||||
: callIndex;
|
||||
|
||||
// Find metadata with callIndex
|
||||
const meta = _meta || Method.findFunction(callIndex).meta;
|
||||
const meta = _meta || Method.findFunction(lookupIndex).meta;
|
||||
|
||||
// Get Struct definition of the arguments
|
||||
const argsDef = Method.getArgsDef(meta);
|
||||
|
||||
return { ...value, argsDef, meta };
|
||||
return {
|
||||
args,
|
||||
argsDef,
|
||||
meta,
|
||||
callIndex
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Method: cannot decode value "${value}".`);
|
||||
throw new Error(`Method: cannot decode value '${value}' or type ${typeof value}`);
|
||||
}
|
||||
|
||||
// If the extrinsic function has an argument of type `Origin`, we ignore it
|
||||
@@ -116,6 +129,7 @@ export default class Method extends Struct {
|
||||
// which includes the meta, name, section & actual interface for calling
|
||||
static findFunction (callIndex: Uint8Array): ExtrinsicFunction {
|
||||
assert(Object.keys(extrinsicFns).length > 0, 'Calling Method.findFunction before extrinsics have been injected.');
|
||||
|
||||
return extrinsicFns[callIndex.toString()] || FN_UNKNOWN;
|
||||
}
|
||||
|
||||
@@ -156,7 +170,7 @@ export default class Method extends Struct {
|
||||
}
|
||||
|
||||
get callIndex (): Uint8Array {
|
||||
return (this.get('methodIndex') as MethodIndex).callIndex;
|
||||
return (this.get('callIndex') as MethodIndex).toU8a();
|
||||
}
|
||||
|
||||
get data (): Uint8Array {
|
||||
|
||||
@@ -11,15 +11,11 @@ export default class MethodIndex extends U8aFixed {
|
||||
super(value, 16);
|
||||
}
|
||||
|
||||
get callIndex (): Uint8Array {
|
||||
return this.raw;
|
||||
}
|
||||
|
||||
get methodIndex (): number {
|
||||
return this.callIndex[1];
|
||||
return this.raw[1];
|
||||
}
|
||||
|
||||
get sectionIndex (): number {
|
||||
return this.callIndex[0];
|
||||
return this.raw[0];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user