Fix decoding of unsigned extrinsics (#1216)
This commit is contained in:
@@ -11,6 +11,7 @@ import Method from '../../primitive/Generic/Method';
|
||||
import events from '../../json/SignedBlock.002.json';
|
||||
import immortalTxs from '../../json/SignedBlock.004.immortal.json';
|
||||
import mortalTxs from '../../json/SignedBlock.004.mortal.json';
|
||||
import knownMehods from '../../json/SignedBlock.005.json';
|
||||
|
||||
describe('SignedBlock', (): void => {
|
||||
beforeEach((): void => {
|
||||
@@ -36,7 +37,18 @@ describe('SignedBlock', (): void => {
|
||||
]);
|
||||
});
|
||||
|
||||
describe('extrinsics', (): void => {
|
||||
// Test to replicate https://github.com/polkadot-js/api/issues/1212
|
||||
it('decodes to known extrinsics', (): void => {
|
||||
const s = createType('SignedBlock', knownMehods.result);
|
||||
const indexes = s.block.extrinsics.map(({ method: { callIndex } }): Uint8Array => callIndex);
|
||||
|
||||
expect(indexes).toEqual([
|
||||
new Uint8Array([0x02, 0x00]),
|
||||
new Uint8Array([0x0c, 0x00])
|
||||
]);
|
||||
});
|
||||
|
||||
describe('eras', (): void => {
|
||||
it('can decode immortals', (): void => {
|
||||
const s = createType('SignedBlock', immortalTxs.result);
|
||||
const immortalTx = s.block.extrinsics[2];
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"jsonrpc":"2.0","result":{"block":{"extrinsics":["0x280202000b868d92346c01","0x14020c008151"],"header":{"digest":{"logs":["0x0642414245c101a5cb8138000000000000000000000000549baa911a66f7bab6318a6930380bfd015713f77b6775d2df467d37dadd224dda0a80881cfbddc580d45b3b7358f28e4990d950df4508590543d39d635b3402822279397c1d55449b201710c706ce5de3fd3cb2e269bea6ba89c71e4446fd06","0x05424142450101e4e9f32bb0e72821032f752422c6dc23e8acd5388eb17cbe009d7e3764a0625b38c1b90ec6f19bb5d8c093deeddf9df01845352ac6fefad49601be1f726cea09"]},"extrinsicsRoot":"0x794ff5dde3eaa27d56cc760dd18774c883d51ed9206ff93c9a7e7d6a8a6dca11","number":"0x1461","parentHash":"0x28cda71a46766fe3cf64475b566916d367617d14627e895067f4186e65609d0e","stateRoot":"0xc7803b39c1b30a87566ec9d95890db76cf2eee7034faee4c8a0997759e7bcb7e"}},"justification":null},"id":29}
|
||||
@@ -2,13 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Balance, Index, Signature } from '../../../interfaces/runtime';
|
||||
import { Address, Balance, Index, Signature } from '../../../interfaces/runtime';
|
||||
import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
|
||||
import createType, { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
import Struct from '../../../codec/Struct';
|
||||
import Address from '../../Generic/Address';
|
||||
import Method from '../../Generic/Method';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
|
||||
@@ -31,7 +30,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
|
||||
// 1/2 bytes: The Transaction Era
|
||||
public constructor (value?: ExtrinsicSignatureV1 | Uint8Array, { isSigned }: ExtrinsicSignatureV1Options = {}) {
|
||||
super({
|
||||
signer: Address,
|
||||
signer: ClassOf('Address'),
|
||||
signature: ClassOf('Signature'),
|
||||
nonce: ClassOf('Compact<Index>'),
|
||||
era: ExtrinsicEra
|
||||
@@ -50,6 +49,15 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
|
||||
: EMPTY_U8A;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.isSigned
|
||||
? super.encodedLength
|
||||
: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` if the signature is valid
|
||||
*/
|
||||
@@ -135,6 +143,6 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
|
||||
public toU8a (isBare?: boolean): Uint8Array {
|
||||
return this.isSigned
|
||||
? super.toU8a(isBare)
|
||||
: new Uint8Array();
|
||||
: EMPTY_U8A;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Balance, Index, Signature } from '../../../interfaces/runtime';
|
||||
import { Address, Balance, Index, Signature } from '../../../interfaces/runtime';
|
||||
import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
|
||||
import createType, { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
import Struct from '../../../codec/Struct';
|
||||
import Address from '../../Generic/Address';
|
||||
import Method from '../../Generic/Method';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import ExtrinsicPayload from './ExtrinsicPayload';
|
||||
@@ -27,7 +26,7 @@ interface ExtrinsicSignatureV2Options {
|
||||
export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSignature {
|
||||
public constructor (value: ExtrinsicSignatureV2 | Uint8Array | undefined, { isSigned }: ExtrinsicSignatureV2Options = {}) {
|
||||
super({
|
||||
signer: Address,
|
||||
signer: ClassOf('Address'),
|
||||
signature: ClassOf('Signature'),
|
||||
extra: ExtrinsicExtra
|
||||
}, ExtrinsicSignatureV2.decodeExtrinsicSignature(value, isSigned));
|
||||
@@ -45,6 +44,15 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi
|
||||
: EMPTY_U8A;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.isSigned
|
||||
? super.encodedLength
|
||||
: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` if the signature is valid
|
||||
*/
|
||||
@@ -140,6 +148,6 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi
|
||||
public toU8a (isBare?: boolean): Uint8Array {
|
||||
return this.isSigned
|
||||
? super.toU8a(isBare)
|
||||
: new Uint8Array();
|
||||
: EMPTY_U8A;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user