Compact<Index> in extrinsics (#515)

* Basic compact index

* Nonce is only used in signatures, alias it

* typo

* s/NonceCompact/IndexCompact/

* Update tests
This commit is contained in:
Jaco Greeff
2019-01-08 14:21:14 +01:00
committed by GitHub
parent ca91141931
commit d589a1bbfd
9 changed files with 43 additions and 12 deletions
+15 -4
View File
@@ -24,12 +24,23 @@ describe.skip('e2e transactions', () => {
it('makes a transfer', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address());
await api.tx.balances
api.tx.balances
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
.sign(keyring.dave, nonce)
.send((status) => {
if (status.type === 'Finalised' && status.events && status.events.length) {
done();
.send(({ events, status, type }) => {
console.log('Transaction status:', type);
if (type === 'Finalised') {
console.log('Completed at block hash', status.value.toHex());
console.log('Events:');
events.forEach(({ phase, event: { data, method, section } }) => {
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
});
if (events.length) {
done();
}
}
});
});
+2 -2
View File
@@ -20,7 +20,7 @@ describe('extrinsics', () => {
new Uint8Array()
).toU8a(true)
).toEqual(new Uint8Array([
129, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 210, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213, 157
129, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 19, 0, 0, 0, 213, 157
]));
});
@@ -30,7 +30,7 @@ describe('extrinsics', () => {
extrinsics.balances.transfer(keyring.bob.publicKey(), 6969)
).sign(keyring.alice, 0, '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f').toU8a()
).toEqual(new Uint8Array([
65, 2, 129, 255, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79, 78, 239, 232, 24, 167, 219, 153, 211, 226, 132, 22, 151, 169, 47, 140, 198, 86, 169, 91, 159, 253, 98, 242, 111, 12, 143, 192, 226, 250, 233, 158, 232, 195, 57, 44, 184, 25, 65, 228, 143, 200, 140, 84, 111, 179, 102, 187, 61, 1, 137, 148, 74, 168, 246, 131, 243, 228, 64, 10, 106, 138, 117, 209, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 255, 215, 86, 142, 95, 10, 126, 218, 103, 168, 38, 145, 255, 55, 154, 196, 187, 164, 249, 201, 184, 89, 254, 119, 155, 93, 70, 54, 59, 97, 173, 45, 185, 229, 108
37, 2, 129, 255, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79, 148, 105, 162, 165, 130, 138, 239, 1, 131, 69, 7, 221, 50, 12, 137, 86, 22, 117, 195, 168, 94, 166, 49, 143, 78, 169, 34, 247, 161, 99, 134, 204, 95, 205, 47, 14, 216, 95, 190, 220, 24, 13, 75, 218, 172, 137, 229, 86, 11, 246, 111, 80, 204, 192, 213, 13, 101, 245, 230, 65, 33, 21, 129, 6, 0, 0, 2, 0, 255, 215, 86, 142, 95, 10, 126, 218, 103, 168, 38, 145, 255, 55, 154, 196, 187, 164, 249, 201, 184, 89, 254, 119, 155, 93, 70, 54, 59, 97, 173, 45, 185, 229, 108
]));
});
});
+1 -1
View File
@@ -21,7 +21,7 @@ describe('Extrinsic', () => {
});
it('decodes an actual transaction with indexes', () => {
const extrinsic = new Extrinsic('0x8110f8e1ebdd3cdef7423d24fe68f3863945ea21c190907d7f3394ddf153f633c77b894af36f7a36e7ec767f4593bfd8d084b66d805605905054327a08ff604187080100000000000000270e0200ff0e6422725af0e9aede3bef6eba77bc87afadf60b9735057ae93801f4c472ad7b00407a10f35a00000000000000000000');
const extrinsic = new Extrinsic('0x8110f8e1ebdd3cdef7423d24fe68f3863945ea21c190907d7f3394ddf153f633c77b894af36f7a36e7ec767f4593bfd8d084b66d805605905054327a08ff6041870804270e0200ff0e6422725af0e9aede3bef6eba77bc87afadf60b9735057ae93801f4c472ad7b00407a10f35a00000000000000000000');
expect(extrinsic.isSigned).toEqual(true);
expect(extrinsic.signature.signer.toU8a()).toEqual(new Uint8Array([0x10]));
+1 -1
View File
@@ -11,7 +11,7 @@ import Struct from './codec/Struct';
import Address from './Address';
import ExtrinsicEra from './ExtrinsicEra';
import Method from './Method';
import Nonce from './Nonce';
import Nonce from './NonceCompact';
import Signature from './Signature';
import SignaturePayload from './SignaturePayload';
+15
View File
@@ -0,0 +1,15 @@
// Copyright 2017-2018 @polkadot/types 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 Compact from './codec/Compact';
import Nonce from './Nonce';
/**
* @name Nonce
* @description
* The Compact<Nonce> or number of transactions sent by a specific account. Generally used
* with extrinsics to determine the order of execution.
*/
export default class NonceCompact extends Compact.with(Nonce) {
}
+1 -1
View File
@@ -8,7 +8,7 @@ import { AnyNumber, AnyU8a } from './types';
import Struct from './codec/Struct';
import Method from './Method';
import Hash from './Hash';
import Nonce from './Nonce';
import Nonce from './NonceCompact';
import ExtrinsicEra from './ExtrinsicEra';
type SignaturePayloadValue = {
+2
View File
@@ -52,6 +52,8 @@ export default class Type extends Text {
// alias () -> Null
Type._alias('\\\(\\\)', 'Null'),
// alias Vec<u8> -> Bytes
Type._alias('Compact<Index>', 'IndexCompact'),
// alias Vec<u8> -> Bytes
Type._alias('Vec<u8>', 'Bytes'),
// alias RawAddress -> Address
Type._alias('RawAddress', 'Address'),
+5 -3
View File
@@ -21,7 +21,7 @@ import Moment from '../Moment';
* a number and making the compact representation thereof
*/
export default class Compact extends Base<UInt | Moment> implements Codec {
constructor (Type: Constructor<UInt | Moment>, value: AnyNumber = 0) {
constructor (Type: Constructor<UInt | Moment>, value: Compact | AnyNumber = 0) {
super(Compact.decodeCompact(Type, value));
}
@@ -48,8 +48,10 @@ export default class Compact extends Base<UInt | Moment> implements Codec {
return value;
}
static decodeCompact (Type: Constructor<UInt | Moment>, value: AnyNumber): Moment | UInt {
if (isString(value)) {
static decodeCompact (Type: Constructor<UInt | Moment>, value: Compact | AnyNumber): Moment | UInt {
if (value instanceof Compact) {
return new Type(value.raw);
} else if (isString(value)) {
return new Type(
isHex(value)
? hexToBn(value)
+1
View File
@@ -60,6 +60,7 @@ export { default as NewAccountOutcome } from './NewAccountOutcome';
export { default as Null } from './Null';
// NOTE Nonce is renamed to Index
export { default as Index } from './Nonce';
export { default as IndexCompact } from './NonceCompact';
export { default as Justification } from './Justification';
export { default as Origin } from './Origin';
export { default as ParaId } from './ParaId';