Expose a hasOrigin getter (public vs private) (#437)

* Expose a hasOrigin getter (public vs private)

* Add tests, fix hex creation
This commit is contained in:
Jaco Greeff
2018-12-04 13:49:37 +01:00
committed by GitHub
parent 929da62719
commit 101f869423
2 changed files with 42 additions and 2 deletions
+31
View File
@@ -25,4 +25,35 @@ describe('Method', () => {
}).toU8a()
).toEqual(new Uint8Array([1, 2]));
});
it('handles creation from a hex value properly', () => {
expect(
new Method('0x0102').toU8a()
).toEqual(new Uint8Array([1, 2]));
});
describe('hasOrigin', () => {
const test = {
args: [],
callIndex: [1, 2]
};
it('is false with no arguments', () => {
expect(
new Method(test, { arguments: [] } as any).hasOrigin
).toEqual(false);
});
it('is false with first argument as non-Origin', () => {
expect(
new Method(test, { arguments: [{ name: 'a', type: 'u32' }] } as any).hasOrigin
).toEqual(false);
});
it('is false with first argument as non-Origin', () => {
expect(
new Method(test, { arguments: [{ name: 'a', type: 'Origin' }] } as any).hasOrigin
).toEqual(true);
});
});
});
+11 -2
View File
@@ -4,7 +4,7 @@
import { AnyU8a, Codec, Constructor } from './types';
import { assert, isHex, isObject, isU8a } from '@polkadot/util';
import { assert, isHex, isObject, isU8a, hexToU8a } from '@polkadot/util';
import { getTypeDef, getTypeClass } from './codec/createType';
import Struct from './codec/Struct';
@@ -93,7 +93,7 @@ export default class Method extends Struct {
*/
private static decodeMethod (value: DecodedMethod | Uint8Array | string, _meta?: FunctionMetadata): DecodedMethod {
if (isHex(value)) {
return Method.decodeMethod(value, _meta);
return Method.decodeMethod(hexToU8a(value), _meta);
} else if (isU8a(value)) {
// The first 2 bytes are the callIndex
const callIndex = value.subarray(0, 2);
@@ -219,6 +219,15 @@ export default class Method extends Struct {
return (this.get('args') as Struct).toU8a();
}
/**
* @description `true` if the `Origin` type is on the method (extrinsic method)
*/
get hasOrigin (): boolean {
const firstArg = this.meta.arguments[0];
return !!firstArg && firstArg.type.toString() === 'Origin';
}
/**
* @description The [[FunctionMetadata]]
*/