Support KeyValue encoding/decoding (storage) (#164)
* consensus.setStorage takes KeyValueStorage[] * encode KeyValue & KeyValueStorage
This commit is contained in:
@@ -27,7 +27,7 @@ const setCode: CreateItemOptions = {
|
||||
const setStorage: CreateItemOptions = {
|
||||
description: 'Set storage entries',
|
||||
params: [
|
||||
param('entries', ['KeyValue'])
|
||||
param('entries', ['KeyValueStorage'])
|
||||
],
|
||||
type: []
|
||||
};
|
||||
|
||||
@@ -12,6 +12,7 @@ import u8aToU8a from '@polkadot/util/u8a/toU8a';
|
||||
import addressDecode from '@polkadot/util-keyring/address/decode';
|
||||
|
||||
import sizes from '../../sizes';
|
||||
import keyValue from './keyValue';
|
||||
|
||||
export default function encodeType (type: Param$Type, value: any, version: EncodingVersions): Uint8Array {
|
||||
try {
|
||||
@@ -44,8 +45,6 @@ export default function encodeType (type: Param$Type, value: any, version: Encod
|
||||
case 'CandidateReceipt':
|
||||
case 'Digest':
|
||||
case 'Header':
|
||||
case 'KeyValue':
|
||||
case 'KeyValueStorage':
|
||||
case 'MisbehaviorReport':
|
||||
case 'Proposal':
|
||||
return u8aToU8a(value);
|
||||
@@ -58,6 +57,10 @@ export default function encodeType (type: Param$Type, value: any, version: Encod
|
||||
case 'Index':
|
||||
return bnToU8a(value, sizes.Index.get(version), true);
|
||||
|
||||
case 'KeyValue':
|
||||
case 'KeyValueStorage':
|
||||
return keyValue(value);
|
||||
|
||||
case 'ParachainId':
|
||||
case 'PropIndex':
|
||||
case 'ReferendumIndex':
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2017-2018 @polkadot/params authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
import encode from './index';
|
||||
|
||||
describe('keyValue', () => {
|
||||
it('encodes KeyValueStorage -> Uint8Array properly', () => {
|
||||
expect(
|
||||
encode('KeyValueStorage', {
|
||||
key: toU8a('0x11223344'),
|
||||
value: toU8a('0x998877665544332211')
|
||||
})
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
4, 0, 0, 0,
|
||||
0x11, 0x22, 0x33, 0x44,
|
||||
9, 0, 0, 0,
|
||||
0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11
|
||||
])
|
||||
)
|
||||
});
|
||||
|
||||
it('encodes KeyValue -> Uint8Array properly', () => {
|
||||
expect(
|
||||
encode('KeyValue', {
|
||||
key: toU8a('0x11'),
|
||||
value: toU8a('0x9988')
|
||||
})
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
1, 0, 0, 0,
|
||||
0x11,
|
||||
2, 0, 0, 0,
|
||||
0x99, 0x88
|
||||
])
|
||||
)
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 @polkadot/params authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { KeyValue } from '../../types';
|
||||
|
||||
import bnToU8a from '@polkadot/util/bn/toU8a';
|
||||
import u8aConcat from '@polkadot/util/u8a/concat';
|
||||
|
||||
// NOTE reverse of ../../decode/value/keyValue
|
||||
|
||||
export default function keyValue ({ key, value }: KeyValue): Uint8Array {
|
||||
return u8aConcat(
|
||||
bnToU8a(key.length, 32, true),
|
||||
key,
|
||||
bnToU8a(value.length, 32, true),
|
||||
value
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user