This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3e4828
commit 428c0b3
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as C from "http://localhost:5646/@local/mod.ts" | ||
import { | ||
Session, | ||
System, | ||
} from "http://localhost:5646/@local/proxy/wss:rpc.polkadot.io/pallets/mod.ts" | ||
import { $preDigest } from "http://localhost:5646/@local/proxy/wss:rpc.polkadot.io/types/sp_consensus_babe/digests.ts" | ||
import { | ||
$digestItem, | ||
DigestItem, | ||
} from "http://localhost:5646/@local/proxy/wss:rpc.polkadot.io/types/sp_runtime/generic/digest.ts" | ||
import * as U from "http://localhost:5646/@local/util/mod.ts" | ||
|
||
const blockHeader = C.chain.getBlockHeader(C.polkadot)() | ||
const header = U.throwIfError(await blockHeader.run()) | ||
const digestedLogs = header.digest.logs.map(C.hex.decode).map($digestItem.decode.bind($digestItem)) | ||
|
||
function preRuntimeGuard(digestItem: DigestItem): digestItem is DigestItem.PreRuntime { | ||
return digestItem.type === "PreRuntime" | ||
} | ||
|
||
const digest = digestedLogs.find(preRuntimeGuard) | ||
if (!digest) { | ||
throw new Error("Missing PreRuntime log") | ||
} | ||
|
||
const utf8decoder = new TextDecoder() | ||
const consensusEngineId = utf8decoder.decode(digest.value[0]) | ||
|
||
if (consensusEngineId !== "BABE") { | ||
throw new Error(`Unsupported consensus engine id: ${consensusEngineId}. Only BABE is supported.`) | ||
} | ||
|
||
const preDigest = $preDigest.decode(digest.value[1]) | ||
const validators = U.throwIfError(await Session.Validators.entry().read().run()) | ||
const pubKey = validators.value[preDigest.value.authorityIndex]! | ||
|
||
const ss58EncodedPubKey = U.ss58.encode( | ||
System.SS58Prefix, | ||
pubKey, | ||
) | ||
|
||
console.log(ss58EncodedPubKey) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters