Cleanup extrinsic encoding (#150)
* Cleanup extrinsic encoding * Removed now-defunct type * Add uncheckedLength encoding
This commit is contained in:
@@ -13,7 +13,8 @@ const keyring = testingPairs();
|
||||
describe('encode', () => {
|
||||
it('encodes extrinsic correctly', () => {
|
||||
expect(
|
||||
encode(keyring.nobody.publicKey(), 1234)(
|
||||
encode(
|
||||
keyring.nobody.publicKey(), 1234,
|
||||
extrinsics.timestamp.public.set,
|
||||
[10101],
|
||||
'latest'
|
||||
|
||||
@@ -10,14 +10,11 @@ import { ExtrinsicWithAccount } from '../types';
|
||||
import encodeCall from './call';
|
||||
import encodeExtrinsic from './extrinsic';
|
||||
|
||||
type Encoder = (extrinsic: SectionItem<Extrinsics>, values: Array<any>, version: EncodingVersions) => ExtrinsicWithAccount;
|
||||
|
||||
export default function encode (publicKey: Uint8Array, index: number | BN): Encoder {
|
||||
return (extrinsic: SectionItem<Extrinsics>, values: Array<any>, version: EncodingVersions): ExtrinsicWithAccount =>
|
||||
encodeCall(
|
||||
publicKey,
|
||||
index,
|
||||
encodeExtrinsic(extrinsic, values, version),
|
||||
version
|
||||
);
|
||||
export default function encode (publicKey: Uint8Array, index: number | BN, extrinsic: SectionItem<Extrinsics>, values: Array<any>, version: EncodingVersions): ExtrinsicWithAccount {
|
||||
return encodeCall(
|
||||
publicKey,
|
||||
index,
|
||||
encodeExtrinsic(extrinsic, values, version),
|
||||
version
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@ const keyring = testingPairs();
|
||||
describe('unchecked', () => {
|
||||
it('encodes extrinsic correctly (nobody)', () => {
|
||||
expect(
|
||||
encode(keyring.nobody, 1234)(
|
||||
encode(
|
||||
keyring.nobody, 1234,
|
||||
extrinsics.timestamp.public.set,
|
||||
[10101]
|
||||
)
|
||||
@@ -37,9 +38,11 @@ describe('unchecked', () => {
|
||||
|
||||
it('encodes extrinsic correctly (one)', () => {
|
||||
expect(
|
||||
encode(keyring.one, 258, 'poc-1')(
|
||||
encode(
|
||||
keyring.one, 258,
|
||||
extrinsics.staking.public.transfer,
|
||||
[keyring.two.publicKey(), 69]
|
||||
[keyring.two.publicKey(), 69],
|
||||
'poc-1'
|
||||
)
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
@@ -62,7 +65,8 @@ describe('unchecked', () => {
|
||||
|
||||
it('encodes extrinsic correctly (one + latest)', () => {
|
||||
expect(
|
||||
encode(keyring.one, 258)(
|
||||
encode(
|
||||
keyring.one, 258,
|
||||
extrinsics.staking.public.transfer,
|
||||
[keyring.two.publicKey(), 69]
|
||||
)
|
||||
@@ -89,7 +93,8 @@ describe('unchecked', () => {
|
||||
|
||||
it('encodes timetamp.set extrinsic correctly (actual values)', () => {
|
||||
expect(
|
||||
encode(keyring.nobody, 0)(
|
||||
encode(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.timestamp.public.set,
|
||||
[0x5b13c37b]
|
||||
)
|
||||
@@ -102,7 +107,8 @@ describe('unchecked', () => {
|
||||
|
||||
it('encoded parachains.setHeads correctly (actual values)', () => {
|
||||
expect(
|
||||
encode(keyring.nobody, 0)(
|
||||
encode(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.parachains.public.setHeads,
|
||||
[[]]
|
||||
)
|
||||
|
||||
@@ -13,21 +13,15 @@ import u8aConcat from '@polkadot/util/u8a/concat';
|
||||
import encode from './index';
|
||||
import prefixes from './prefixes';
|
||||
|
||||
type Encoder = (extrinsic: SectionItem<Extrinsics>, values: Array<any>) => UncheckedRaw;
|
||||
export default function unchecked (pair: KeyringPair, index: number | BN, extrinsic: SectionItem<Extrinsics>, values: Array<any>, version: EncodingVersions = 'latest'): UncheckedRaw {
|
||||
const message = encode(pair.publicKey(), index, extrinsic, values, version);
|
||||
const signature = pair.sign(message);
|
||||
|
||||
export default function unchecked (pair: KeyringPair, index: number | BN, version: EncodingVersions = 'latest'): Encoder {
|
||||
const encoder = encode(pair.publicKey(), index);
|
||||
|
||||
return (extrinsic: SectionItem<Extrinsics>, values: Array<any>): UncheckedRaw => {
|
||||
const message = encoder(extrinsic, values, version);
|
||||
const signature = pair.sign(message);
|
||||
|
||||
return u8aConcat(
|
||||
version === 'poc-1'
|
||||
? prefixes.none
|
||||
: prefixes.publicKey,
|
||||
message,
|
||||
signature
|
||||
);
|
||||
};
|
||||
return u8aConcat(
|
||||
version === 'poc-1'
|
||||
? prefixes.none
|
||||
: prefixes.publicKey,
|
||||
message,
|
||||
signature
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { EncodingVersions, SectionItem } from '@polkadot/params/types';
|
||||
import { UncheckedRaw } from '@polkadot/primitives/extrinsic';
|
||||
import { KeyringPair } from '@polkadot/util-keyring/types';
|
||||
import { Extrinsics } from '../../types';
|
||||
|
||||
import encodeUnchecked from './unchecked';
|
||||
import encodeLength from './length';
|
||||
|
||||
export default function uncheckedLength (pair: KeyringPair, index: number | BN, extrinsic: SectionItem<Extrinsics>, values: Array<any>, version: EncodingVersions = 'latest'): UncheckedRaw {
|
||||
return encodeLength(
|
||||
encodeUnchecked(
|
||||
pair,
|
||||
index,
|
||||
extrinsic,
|
||||
values,
|
||||
version
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -44,7 +44,8 @@ describe('decodeRaw', () => {
|
||||
)
|
||||
).toMatchObject({
|
||||
extrinsics: [
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.timestamp.public.set,
|
||||
[10101]
|
||||
)
|
||||
|
||||
@@ -23,11 +23,13 @@ describe('encodeBlock', () => {
|
||||
stateRoot: hexToU8a('0xf707774183723f201b875024069f30e7089b7f83c3c3cdc534b10d15fca91262')
|
||||
},
|
||||
extrinsics: [
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked
|
||||
(keyring.nobody, 0,
|
||||
extrinsics.timestamp.public.set,
|
||||
[0x5b13c3a4]
|
||||
),
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.parachains.public.setHeads,
|
||||
[[]]
|
||||
)
|
||||
|
||||
@@ -34,11 +34,13 @@ describe('block', () => {
|
||||
|
||||
it('creates a valid block, with defaults submitted', () => {
|
||||
const transactions = [
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.timestamp.public.set,
|
||||
[0x5b13c3a4]
|
||||
),
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.parachains.public.setHeads,
|
||||
[[]]
|
||||
)
|
||||
|
||||
@@ -25,11 +25,13 @@ describe('root', () => {
|
||||
it('calculates the root correctly (actual)', () => {
|
||||
expect(
|
||||
root([
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.timestamp.public.set,
|
||||
[0x5b13c3a4]
|
||||
),
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.parachains.public.setHeads,
|
||||
[[]]
|
||||
)
|
||||
|
||||
@@ -22,11 +22,13 @@ describe('header', () => {
|
||||
|
||||
it('applies the all transactions (extrinsicsRoot)', () => {
|
||||
const transactions = [
|
||||
encodeUnchecked(keyring.nobody, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.nobody, 0,
|
||||
extrinsics.timestamp.public.set,
|
||||
[54321]
|
||||
),
|
||||
encodeUnchecked(keyring.one, 0)(
|
||||
encodeUnchecked(
|
||||
keyring.one, 0,
|
||||
extrinsics.timestamp.public.set,
|
||||
[12345]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user