Add derive.chain.{getHeader,subscribeNewHead} with author (#602)
* Add derive.chain.{getHeader,subscribeNewHead} with author
* tslint cast, ignore
This commit is contained in:
@@ -9,6 +9,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
|
||||
import Compact from './codec/Compact';
|
||||
import Struct from './codec/Struct';
|
||||
|
||||
import AccountId from './AccountId';
|
||||
import BlockNumber from './BlockNumber';
|
||||
import Digest, { DigestItem } from './Digest';
|
||||
import Hash from './Hash';
|
||||
@@ -81,3 +82,30 @@ export default class Header extends Struct {
|
||||
return this.get('stateRoot') as Hash;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name HeaderExtended
|
||||
* @description
|
||||
* A [[Block]] header with an additional `author` field that indicates the block author
|
||||
*/
|
||||
export class HeaderExtended extends Header {
|
||||
private _author?: AccountId;
|
||||
|
||||
constructor (header: Header, sessionValidators: Array<AccountId>) {
|
||||
super(header);
|
||||
|
||||
const { digest: { logs } } = header;
|
||||
const digestItem = logs.find(({ type }) => type === 'Seal');
|
||||
|
||||
this._author = digestItem && sessionValidators.length
|
||||
? sessionValidators[digestItem.asSeal.slot.toNumber() % sessionValidators.length]
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Convenience method, returns the author for the block
|
||||
*/
|
||||
get author (): AccountId | undefined {
|
||||
return this._author;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user