1 | import { U8aFixed } from '@polkadot/types-codec';
|
2 | import { BN, bnToU8a, isNumber, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
|
3 | export const CID_AURA = stringToU8a('aura');
|
4 | export const CID_BABE = stringToU8a('BABE');
|
5 | export const CID_GRPA = stringToU8a('FRNK');
|
6 | export const CID_POW = stringToU8a('pow_');
|
7 | export const CID_NMBS = stringToU8a('nmbs');
|
8 | function getAuraAuthor(registry, bytes, sessionValidators) {
|
9 | return sessionValidators[registry.createTypeUnsafe('RawAuraPreDigest', [bytes.toU8a(true)])
|
10 | .slotNumber
|
11 | .mod(new BN(sessionValidators.length))
|
12 | .toNumber()];
|
13 | }
|
14 | function getBabeAuthor(registry, bytes, sessionValidators) {
|
15 | const digest = registry.createTypeUnsafe('RawBabePreDigestCompat', [bytes.toU8a(true)]);
|
16 | return sessionValidators[digest.value.toNumber()];
|
17 | }
|
18 | function getBytesAsAuthor(registry, bytes) {
|
19 | return registry.createTypeUnsafe('AccountId', [bytes]);
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export class GenericConsensusEngineId extends U8aFixed {
|
27 | constructor(registry, value) {
|
28 | super(registry, isNumber(value)
|
29 | ? bnToU8a(value, { isLe: false })
|
30 | : value, 32);
|
31 | }
|
32 | |
33 |
|
34 |
|
35 | get isAura() {
|
36 | return this.eq(CID_AURA);
|
37 | }
|
38 | |
39 |
|
40 |
|
41 | get isBabe() {
|
42 | return this.eq(CID_BABE);
|
43 | }
|
44 | |
45 |
|
46 |
|
47 | get isGrandpa() {
|
48 | return this.eq(CID_GRPA);
|
49 | }
|
50 | |
51 |
|
52 |
|
53 | get isPow() {
|
54 | return this.eq(CID_POW);
|
55 | }
|
56 | |
57 |
|
58 |
|
59 | get isNimbus() {
|
60 | return this.eq(CID_NMBS);
|
61 | }
|
62 | |
63 |
|
64 |
|
65 | extractAuthor(bytes, sessionValidators) {
|
66 | if (sessionValidators?.length) {
|
67 | if (this.isAura) {
|
68 | return getAuraAuthor(this.registry, bytes, sessionValidators);
|
69 | }
|
70 | else if (this.isBabe) {
|
71 | return getBabeAuthor(this.registry, bytes, sessionValidators);
|
72 | }
|
73 | }
|
74 |
|
75 | if (this.isPow || this.isNimbus) {
|
76 | return getBytesAsAuthor(this.registry, bytes);
|
77 | }
|
78 | return undefined;
|
79 | }
|
80 | |
81 |
|
82 |
|
83 | toHuman() {
|
84 | return this.toString();
|
85 | }
|
86 | |
87 |
|
88 |
|
89 | toRawType() {
|
90 | return 'ConsensusEngineId';
|
91 | }
|
92 | |
93 |
|
94 |
|
95 | toString() {
|
96 | return this.isAscii
|
97 | ? u8aToString(this)
|
98 | : u8aToHex(this);
|
99 | }
|
100 | }
|