Override subarray, returning via Uint8Array.from (#395)
* Override subarray, returning via Uint8Array.from * Import order * Use .from in subarray test
This commit is contained in:
@@ -2,8 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import AccountId from './AccountId';
|
||||
import { setAddressPrefix } from '@polkadot/keyring';
|
||||
import U8a from './codec/U8a';
|
||||
import Vector from './codec/Vector';
|
||||
import jsonVec from './json/AccountIdVec.001.json';
|
||||
import AccountId from './AccountId';
|
||||
import StorageData from './StorageData';
|
||||
|
||||
describe('AccountId', () => {
|
||||
describe('decoding', () => {
|
||||
@@ -54,4 +58,24 @@ describe('AccountId', () => {
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8
|
||||
]));
|
||||
});
|
||||
|
||||
describe('storage decoding', () => {
|
||||
it('has the correct entries', () => {
|
||||
setAddressPrefix(68);
|
||||
|
||||
const data = new StorageData(jsonVec.params.result.changes[0][1]);
|
||||
const list = new Vector(AccountId, data).map((accountId) => accountId.toString());
|
||||
|
||||
expect(list).toEqual([
|
||||
'7qVJujLF3EDbZt5WfQXWvueFedMS4Vfk2Hb4GyR8jwksTZQ5',
|
||||
'7pHyqeYaJjJPgxQgCXoS2EZMhBhtpm6BLCqQ4jJZTQB2kEDM',
|
||||
'7pYLWV6PTUmLTMQfHmmuBwBNLkhcKhRAnkM36CSJtjat9Cxe',
|
||||
'7qT1BvpawNbqb3BZaBTMFMMAKrpJKLPf1LmEHR1JyarWJpvA',
|
||||
'7rADc9JW5EUGFPWLjPMipH4c3bJ2GyAUedmqQHiaGucWVifb',
|
||||
'7oK5KRH6jt4p8auipnru9ptqeuRwbLMHA2tgCViZzhmW4Mo2',
|
||||
'7ndAVsHvonnzTg4AvRhpraNCKj9g4CGQXKoLrgkTZ91NaLgJ',
|
||||
'7oL7VfXgLA8L3pJJwi11v3sBYc1b5R3tLrweHwzMNxgEpWuv'
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -25,6 +25,10 @@ export default class Bytes extends U8a {
|
||||
return Bytes.decodeBytes(
|
||||
Compact.addLengthPrefix(u8a)
|
||||
);
|
||||
} else if (value instanceof U8a) {
|
||||
// This is required. In the case of a U8a we already have gottent rid of the length,
|
||||
// i.e. new Bytes(new Bytes(...)) will work as expected
|
||||
return value;
|
||||
} else if (isU8a(value)) {
|
||||
const [offset, length] = Compact.decodeU8a(value, DEFAULT_LENGTH_BITS);
|
||||
|
||||
|
||||
@@ -49,6 +49,6 @@ describe('U8a', () => {
|
||||
});
|
||||
|
||||
it('implements subarray correctly', () => {
|
||||
expect(u8a.subarray(1, 3).toString()).toEqual('0x0203');
|
||||
expect(u8a.subarray(1, 3)).toEqual(Uint8Array.from([2, 3]));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,19 +11,13 @@ import { AnyU8a, Codec } from '../types';
|
||||
// consume the full u8a as passed to it in U8a. As such it is meant to be
|
||||
// subclassed where the wrapper takes care of the actual lengths.
|
||||
export default class U8a extends Uint8Array implements Codec {
|
||||
public raw: Uint8Array; // FIXME Remove this once we convert all types out of Base
|
||||
// FIXME Remove this once we convert all types out of Base
|
||||
public raw: Uint8Array;
|
||||
|
||||
constructor (value: AnyU8a | ArrayBuffer, byteOffset: number = 0, length?: number) {
|
||||
// Uint8Array can be constructed with ArrayBuffer, in which case it has 1
|
||||
// or 2 more optional arguments. We handle this special case here.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
|
||||
if (value instanceof ArrayBuffer) {
|
||||
super(value, byteOffset, length);
|
||||
} else {
|
||||
super(
|
||||
U8a.decodeU8a(value)
|
||||
);
|
||||
}
|
||||
constructor (value: AnyU8a) {
|
||||
super(
|
||||
U8a.decodeU8a(value)
|
||||
);
|
||||
|
||||
this.raw = this;
|
||||
}
|
||||
@@ -40,6 +34,12 @@ export default class U8a extends Uint8Array implements Codec {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
// Create a new subarray from the actual buffer. This is needed
|
||||
// for compat reasons since a new Uint8Array gets returned here
|
||||
subarray (begin: number, end?: number): Uint8Array {
|
||||
return Uint8Array.from(this).subarray(begin, end);
|
||||
}
|
||||
|
||||
toHex (): string {
|
||||
return u8aToHex(this);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "state_storage",
|
||||
"params": {
|
||||
"result": {
|
||||
"block": "0x3265584ff8e6a66cb6c334cdaf8bb6938b49b96e644b5bacc0d06f2b13f75230",
|
||||
"changes": [
|
||||
[
|
||||
"0x329586a7b97f2ac15f6c26a02c3c5621",
|
||||
"0x2082c39b31a2b79a90f8e66e7a77fdb85a4ed5517f2ae39f6a80565e8ecae85cf54de37a07567ebcbf8c64568428a835269a566723687058e017b6d69db00a77e758d67e0f9be62dce75adbb005e8097de5c45f32b1ba7264717df2db4ae9f276e8101764f45778d4980dadaceee6e8af2517d3ab91ac9bec9cd1714fa5994081ca070532405ebf20fba389cbabfe1885cc134ee18028d488714eae621b47baf9d227cee94fa4e88d8d25abe706f15aca03b1d753d433f5ef9aa9ad1bcf5e5b81e040c8dc048a6d129803caa851c4c9633610068e4ef9eaa0bfbf40dfbfd43d9222347812ef77d9fd3cca1673e1b2bde54da96fddcf79d92832b1e2a819724f140"
|
||||
]
|
||||
]
|
||||
},
|
||||
"subscription": 43614
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user