Update Keys, add rotateKeys RPC (#1280)
* author_rotateKeys * Update Keys encoding
This commit is contained in:
@@ -22,6 +22,10 @@ import { eraLength } from '../session/eraLength';
|
||||
import { recentlyOffline } from './recentlyOffline';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
const EMPTY_KEYS = {
|
||||
toU8a: (): Uint8Array => new Uint8Array()
|
||||
};
|
||||
|
||||
type MultiResult = [Option<Keys>, [
|
||||
Option<AccountId> | Vec<[AccountId, Keys] & Codec>, Option<StakingLedger>, [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs]
|
||||
]];
|
||||
@@ -84,13 +88,27 @@ function redeemableSum (stakingLedger: StakingLedger | undefined, eraLength: BN,
|
||||
);
|
||||
}
|
||||
|
||||
function keysToAccountIds (keys: Uint8Array): AccountId[] {
|
||||
const result: AccountId[] = [];
|
||||
let offset = 0;
|
||||
|
||||
while (offset < keys.length) {
|
||||
result.push(createType('AccountId', keys.subarray(offset, offset + 32)));
|
||||
offset += 32;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function unwrapSessionIds (stashId: AccountId, queuedKeys: Option<AccountId> | Vec<[AccountId, Keys] & Codec>, nextKeys: Option<Keys>): { nextSessionIds: AccountId[]; nextSessionId?: AccountId; sessionIds: AccountId[]; sessionId?: AccountId } {
|
||||
// for 2.x we have a Vec<(ValidatorId,Keys)> of the keys
|
||||
if (Array.isArray(queuedKeys)) {
|
||||
const sessionIds = (queuedKeys.find(([currentId]): boolean => currentId.eq(stashId)) || [undefined, { toArray: (): AccountId[] => [] }])[1].toArray() as AccountId[];
|
||||
const nextSessionIds = nextKeys.isSome
|
||||
? nextKeys.unwrap().toArray() as AccountId[]
|
||||
: [];
|
||||
const sessionIds = keysToAccountIds(
|
||||
(queuedKeys.find(([currentId]): boolean =>
|
||||
currentId.eq(stashId)
|
||||
) || [undefined, EMPTY_KEYS])[1].toU8a(true)
|
||||
);
|
||||
const nextSessionIds = keysToAccountIds(nextKeys.unwrapOr(EMPTY_KEYS).toU8a(true));
|
||||
|
||||
return {
|
||||
nextSessionId: nextSessionIds[0],
|
||||
|
||||
@@ -9,7 +9,9 @@ import { Codec, IExtrinsic } from '@polkadot/types/types';
|
||||
|
||||
export interface RpcInterface {
|
||||
author: {
|
||||
insertKey(keyType: Text | string, suri: Text | string, maybePublic?: Bytes | Uint8Array | string): Observable<Bytes>;
|
||||
removeExtrinsic(bytesOrHash: (ExtrinsicOrHash)[]): Observable<Vec<Hash>>;
|
||||
rotateKeys(): Observable<Bytes>;
|
||||
pendingExtrinsics(): Observable<Vec<Extrinsic>>;
|
||||
submitExtrinsic(extrinsic: IExtrinsic): Observable<Hash>;
|
||||
submitAndWatchExtrinsic(extrinsic: IExtrinsic): Observable<ExtrinsicStatus>;
|
||||
|
||||
@@ -15,6 +15,22 @@ const removeExtrinsic: RpcMethodOpt = {
|
||||
type: 'Vec<Hash>'
|
||||
};
|
||||
|
||||
const insertKey: RpcMethodOpt = {
|
||||
description: 'Insert a key into the keystore.',
|
||||
params: [
|
||||
createParam('keyType', 'Text'),
|
||||
createParam('suri', 'Text'),
|
||||
createParam('maybePublic', 'Bytes', { isOptional: true })
|
||||
],
|
||||
type: 'Bytes'
|
||||
};
|
||||
|
||||
const rotateKeys: RpcMethodOpt = {
|
||||
description: 'Generate new session keys and returns the corresponding public keys',
|
||||
params: [],
|
||||
type: 'Bytes'
|
||||
};
|
||||
|
||||
const pendingExtrinsics: RpcMethodOpt = {
|
||||
description: 'Returns all pending extrinsics, potentially grouped by sender',
|
||||
params: [],
|
||||
@@ -55,7 +71,9 @@ export default {
|
||||
description: 'Authoring of network items',
|
||||
section,
|
||||
methods: {
|
||||
insertKey: createMethod(section, 'insertKey', insertKey),
|
||||
removeExtrinsic: createMethod(section, 'removeExtrinsic', removeExtrinsic),
|
||||
rotateKeys: createMethod(section, 'rotateKeys', rotateKeys),
|
||||
pendingExtrinsics: createMethod(section, 'pendingExtrinsics', pendingExtrinsics),
|
||||
submitExtrinsic: createMethod(section, 'submitExtrinsic', submitExtrinsic),
|
||||
submitAndWatchExtrinsic: createMethod(section, 'submitAndWatchExtrinsic', submitAndWatchExtrinsic)
|
||||
|
||||
Reference in New Issue
Block a user