Payload Call as Bytes, test serialization (#1304)

* Payload Call as Bytes, test serialization

* Strip hex prefixes to method (signing & payload)
This commit is contained in:
Jaco Greeff
2019-08-13 18:21:46 +02:00
committed by YJ
parent f5da2f8b0e
commit 677e63b66e
8 changed files with 57 additions and 27 deletions
@@ -0,0 +1,33 @@
// Copyright 2017-2019 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import ExtrinsicPayload from './ExtrinsicPayload';
describe('ExtrinsicPayload', (): void => {
const TEST = {
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
era: '0x0703',
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
method: '0x0500ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
nonce: '0x00001234',
tip: '0x00000000000000000000000000005678'
};
it('creates and can re-create from itself (U8a)', (): void => {
const a = new ExtrinsicPayload(TEST, { version: 3 });
const b = new ExtrinsicPayload(a.toU8a(), { version: 3 });
expect(a).toEqual(b);
});
it('creates and can re-create from itself (hex)', (): void => {
const a = new ExtrinsicPayload(TEST, { version: 3 });
const b = new ExtrinsicPayload(a.toHex(), { version: 3 });
expect(a).toEqual(b);
});
});
@@ -7,7 +7,7 @@ import { ExtrinsicPayloadValue, IKeyringPair } from '../../../types';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import U8a from '../../../codec/U8a';
import Bytes from '../../../primitive/Bytes';
import ExtrinsicEra from '../ExtrinsicEra';
import { sign } from '../util';
@@ -26,7 +26,7 @@ export default class ExtrinsicPayloadV1 extends Struct {
public constructor (value?: ExtrinsicPayloadValue | Uint8Array | string) {
super({
nonce: 'Compact<Index>',
method: U8a,
method: 'Bytes',
era: ExtrinsicEra,
blockHash: 'Hash'
}, value);
@@ -40,10 +40,10 @@ export default class ExtrinsicPayloadV1 extends Struct {
}
/**
* @description The [[U8a]] contained in the payload
* @description The [[Bytes]] contained in the payload
*/
public get method (): U8a {
return this.get('method') as U8a;
public get method (): Bytes {
return this.get('method') as Bytes;
}
/**
@@ -63,8 +63,7 @@ export default class ExtrinsicPayloadV1 extends Struct {
/**
* @description Sign the payload with the keypair
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public sign (signerPair: IKeyringPair): Uint8Array {
return sign(signerPair, this.toU8a());
return sign(signerPair, this.toU8a(true));
}
}
@@ -126,7 +126,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
blockHash,
era: era || IMMORTAL_ERA,
genesisHash,
method: method.toU8a(),
method: method.toHex(),
nonce,
tip: 0
});
@@ -7,7 +7,7 @@ import { ExtrinsicPayloadValue, IKeyringPair } from '../../../types';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import U8a from '../../../codec/U8a';
import Bytes from '../../../primitive/Bytes';
import ExtrinsicEra from '../ExtrinsicEra';
import { sign } from '../util';
@@ -20,7 +20,7 @@ import { sign } from '../util';
export default class ExtrinsicPayloadV2 extends Struct {
public constructor (value?: ExtrinsicPayloadValue | Uint8Array | string) {
super({
method: U8a,
method: 'Bytes',
era: ExtrinsicEra,
nonce: 'Compact<Index>',
tip: 'Compact<Balance>',
@@ -43,10 +43,10 @@ export default class ExtrinsicPayloadV2 extends Struct {
}
/**
* @description The [[U8a]] contained in the payload
* @description The [[Bytes]] contained in the payload
*/
public get method (): U8a {
return this.get('method') as U8a;
public get method (): Bytes {
return this.get('method') as Bytes;
}
/**
@@ -66,8 +66,7 @@ export default class ExtrinsicPayloadV2 extends Struct {
/**
* @description Sign the payload with the keypair
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public sign (signerPair: IKeyringPair): Uint8Array {
return sign(signerPair, this.toU8a());
return sign(signerPair, this.toU8a(true));
}
}
@@ -130,7 +130,7 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi
blockHash,
era: era || IMMORTAL_ERA,
genesisHash,
method: method.toU8a(),
method: method.toHex(),
nonce,
tip: tip || 0
});
@@ -7,7 +7,7 @@ import { ExtrinsicPayloadValue, IKeyringPair } from '../../../types';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import U8a from '../../../codec/U8a';
import Bytes from '../../../primitive/Bytes';
import ExtrinsicEra from '../ExtrinsicEra';
import { sign } from '../util';
@@ -20,7 +20,7 @@ import { sign } from '../util';
export default class ExtrinsicPayloadV3 extends Struct {
public constructor (value?: ExtrinsicPayloadValue | Uint8Array | string) {
super({
method: U8a,
method: 'Bytes',
era: ExtrinsicEra,
nonce: 'Compact<Index>',
tip: 'Compact<Balance>',
@@ -51,10 +51,10 @@ export default class ExtrinsicPayloadV3 extends Struct {
}
/**
* @description The [[U8a]] contained in the payload
* @description The [[Bytes]] contained in the payload
*/
public get method (): U8a {
return this.get('method') as U8a;
public get method (): Bytes {
return this.get('method') as Bytes;
}
/**
@@ -74,8 +74,7 @@ export default class ExtrinsicPayloadV3 extends Struct {
/**
* @description Sign the payload with the keypair
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public sign (signerPair: IKeyringPair): Uint8Array {
return sign(signerPair, this.toU8a());
return sign(signerPair, this.toU8a(true));
}
}
@@ -130,7 +130,7 @@ export default class ExtrinsicSignatureV3 extends Struct implements IExtrinsicSi
blockHash,
era: era || IMMORTAL_ERA,
genesisHash,
method: method.toU8a(),
method: method.toHex(),
nonce,
tip: tip || 0
});