Allow rawStorage queries via Uint8Array keys (#378)
* Allow rawStorage queries via Uint8Array keys * Data fallback
This commit is contained in:
@@ -15,7 +15,7 @@ import { Vector } from '@polkadot/types/codec';
|
||||
import { Hash, Method } from '@polkadot/types/index';
|
||||
import Event from '@polkadot/types/Event';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
import { assert, isUndefined } from '@polkadot/util';
|
||||
import { assert, isUndefined, isU8a } from '@polkadot/util';
|
||||
|
||||
type MapFn<R, T> = (combined: R) => T;
|
||||
|
||||
@@ -104,9 +104,13 @@ export default class ApiBase {
|
||||
}
|
||||
|
||||
// FIXME Remove when extending from api-rx
|
||||
rawStorage = <T> (key: StorageFunction, ...params: Array<any>): Observable<T | undefined> => {
|
||||
rawStorage = <T> (key: Uint8Array | StorageFunction, ...params: Array<any>): Observable<T | undefined> => {
|
||||
return this
|
||||
.rawStorageMulti([key, ...params] as [StorageFunction, any])
|
||||
.rawStorageMulti(
|
||||
isU8a(key)
|
||||
? key
|
||||
: [key, ...params] as [StorageFunction, any]
|
||||
)
|
||||
.pipe(
|
||||
map((result: Array<T>): T | undefined =>
|
||||
result
|
||||
@@ -117,7 +121,7 @@ export default class ApiBase {
|
||||
}
|
||||
|
||||
// FIXME Remove when extending from api-rx
|
||||
rawStorageMulti = <T extends []> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
|
||||
rawStorageMulti = <T extends []> (...keys: Array<Uint8Array | [StorageFunction] | [StorageFunction, any]>): Observable<T> => {
|
||||
let observable;
|
||||
|
||||
try {
|
||||
|
||||
@@ -179,12 +179,10 @@ export default class Rpc implements RpcInterface {
|
||||
|
||||
if (method.type === 'StorageData') {
|
||||
// single return value (via state.getStorage), decode the value based on the
|
||||
// outputType that we have specified
|
||||
const type = (params[0] as StorageKey).outputType;
|
||||
// outputType that we have specified. Fallback to Data on nothing
|
||||
const type = (params[0] as StorageKey).outputType || 'Data';
|
||||
|
||||
if (type) {
|
||||
return createType(type, base.raw);
|
||||
}
|
||||
return createType(type, base.raw);
|
||||
} else if (method.type === 'StorageChangeSet') {
|
||||
// multiple return values (via state.storage subscription), decode the values
|
||||
// one at a time, all based on the query types. Three values can be returned -
|
||||
@@ -192,11 +190,8 @@ export default class Rpc implements RpcInterface {
|
||||
// - null - The storage key is empty (but in the resultset)
|
||||
// - undefined - The storage value is not in the resultset
|
||||
return (params[0] as Vector<StorageKey>).reduce((result, _key: StorageKey) => {
|
||||
const type = _key.outputType;
|
||||
|
||||
if (!type) {
|
||||
throw new Error('Cannot format StorageChangeSet, output type missing for key');
|
||||
}
|
||||
// Fallback to Data (i.e. just the encoding) if we don't have a specific type
|
||||
const type = _key.outputType || 'Data';
|
||||
|
||||
// see if we have a result value for this specific key
|
||||
const key = _key.toHex();
|
||||
|
||||
@@ -38,6 +38,7 @@ export default class StorageKey extends Bytes {
|
||||
return fn(arg);
|
||||
}
|
||||
}
|
||||
|
||||
return value as Uint8Array;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user