diff --git a/effects/rpc_known_methods.ts b/effects/rpc_known_methods.ts index f83d55729..88fe85b97 100644 --- a/effects/rpc_known_methods.ts +++ b/effects/rpc_known_methods.ts @@ -31,6 +31,7 @@ export namespace chain { export const getBlockHash = rpcCall<[height?: known.ListOrValue], U.HexHash>( "chain_getBlockHash", ) + export const getBlockHeader = rpcCall<[hash?: U.HexHash], known.Header>("chain_getHeader") } export namespace system { export const accountNextIndex = rpcCall<[accountId: known.AccountId], number>( diff --git a/examples/block_author.ts b/examples/block_author.ts new file mode 100644 index 000000000..bd365f0eb --- /dev/null +++ b/examples/block_author.ts @@ -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) diff --git a/examples/mod.ts b/examples/mod.ts index 051addd5f..242a7e889 100644 --- a/examples/mod.ts +++ b/examples/mod.ts @@ -5,6 +5,7 @@ export * from "./balance.ts" export * from "./batch.ts" +export * from "./block_author.ts" export * from "./derived.ts" export * from "./fee_estimate.ts" export * from "./first_ten_keys.ts" @@ -19,6 +20,7 @@ export * from "./read_era_rewards.ts" export * from "./read_events.ts" export * from "./rpc_call.ts" export * from "./rpc_subscription.ts" +export * from "./smart_contract.ts" export * from "./ticker.ts" export * from "./transfer.ts" export * from "./watch_blocks.ts"