Skip to content
This repository was archived by the owner on Sep 14, 2023. It is now read-only.

chore: misc cleanup #672

Merged
merged 7 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions _tasks/downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { chain as kusama } from "kusama/mod.ts"
import { chain as polkadot } from "polkadot/mod.ts"
import { chain as rococo } from "rococo/mod.ts"
import { chain as westend } from "westend/mod.ts"
import { hex } from "../crypto/mod.ts"
import { emptyDir } from "../deps/std/fs.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }
import { hex } from "../util/mod.ts"

const GENERATION_NOTICE = `// This file was generated by \`_tasks/downloads.ts\``

Expand All @@ -18,11 +18,11 @@ await Deno.writeTextFile(
`${GENERATION_NOTICE}
import { createFromBuffer } from "../deps/dprint.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }
import { decode } from "./hex.ts"
import { hex } from "../crypto/mod.ts"

const wasm = "${wasm}"

export const tsFormatter = createFromBuffer(decode(wasm))
export const tsFormatter = createFromBuffer(hex.decode(wasm))
const { indentWidth, lineWidth, typescript: config } = dprintConfig
tsFormatter.setConfig({ indentWidth, lineWidth }, config)
`,
Expand Down
4 changes: 2 additions & 2 deletions codegen/frame/pallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hex } from "../../crypto/mod.ts"
import { Pallet } from "../../frame_metadata/mod.ts"
import { Ty, UnionTyDef } from "../../scale_info/mod.ts"
import { normalizeIdent } from "../../util/case.ts"
import { hex } from "../../util/mod.ts"
import { File } from "./File.ts"
import { FrameCodegen } from "./FrameCodegen.ts"
import { getRawCodecPath, makeDocComment, S } from "./util.ts"
Expand Down Expand Up @@ -52,7 +52,7 @@ export function pallet(ctx: FrameCodegen, pallet: Pallet) {
ctx.typeVisitor.visit(constant.ty)
} = codecs.$${constant.ty.id}.decode(C.hex.decode(${
S.string(hex.encode(constant.value))
} as C.Hex))`,
}))`,
)
constant.value
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 4 additions & 6 deletions util/hex.ts → crypto/hex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Hex } from "./branded.ts"

export { decode as decodeBuf, encode as encodeBuf } from "../deps/std/encoding/hex.ts"

export function decode(hex: string): Uint8Array {
Expand All @@ -12,14 +10,14 @@ export function decode(hex: string): Uint8Array {
return array
}

export function encode(bytes: Uint8Array): Hex {
export function encode(bytes: Uint8Array): string {
let str = ""
for (let i = 0; i < bytes.length; i++) {
str += bytes[i]!.toString(16).padStart(2, "0")
}
return str as Hex
return str
}

export function encodePrefixed(bytes: Uint8Array): Hex {
return ("0x" + encode(bytes)) as Hex
export function encodePrefixed(bytes: Uint8Array): string {
return "0x" + encode(bytes)
}
8 changes: 8 additions & 0 deletions crypto/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * as hashers from "./hashers.ts"
export * as hex from "./hex.ts"
export * as ss58 from "./ss58.ts"

// moderate --exclude hashers.ts hex.ts ss58.ts

export * from "./Sr25519.ts"
export * from "./test_pairs.ts"
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion fluent/AddressRune.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ss58 } from "../crypto/mod.ts"
import { Rune } from "../rune/mod.ts"
import { ValueRune } from "../rune/ValueRune.ts"
import { ss58 } from "../util/mod.ts"
import { Chain, ChainRune } from "./ChainRune.ts"
import { PublicKeyRune } from "./PublicKeyRune.ts"

Expand Down
4 changes: 2 additions & 2 deletions fluent/BlockRune.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { hex } from "../crypto/mod.ts"
import * as M from "../frame_metadata/mod.ts"
import { known } from "../rpc/mod.ts"
import { ArrayRune, Rune } from "../rune/mod.ts"
import { ValueRune } from "../rune/ValueRune.ts"
import { hex, HexHash } from "../util/mod.ts"
import { Chain, ChainRune } from "./ChainRune.ts"
import { CodecRune } from "./CodecRune.ts"

export class BlockRune<out U, out C extends Chain = Chain> extends Rune<known.SignedBlock, U> {
constructor(
_prime: BlockRune<U, C>["_prime"],
readonly chain: ChainRune<U, C>,
readonly hash: Rune<HexHash, U>,
readonly hash: Rune<string, U>,
) {
super(_prime)
}
Expand Down
7 changes: 3 additions & 4 deletions fluent/ChainRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as M from "../frame_metadata/mod.ts"
import { Event } from "../primitives/mod.ts"
import { Connection } from "../rpc/mod.ts"
import { Rune, RunicArgs, ValueRune } from "../rune/mod.ts"
import { HexHash } from "../util/mod.ts"
import { BlockRune } from "./BlockRune.ts"
import { ConnectionRune } from "./ConnectionRune.ts"
import { ExtrinsicRune } from "./ExtrinsicRune.ts"
Expand Down Expand Up @@ -32,16 +31,16 @@ export class ChainRune<out U, out C extends Chain = Chain> extends Rune<C, U> {
.subscribe("chain_subscribeNewHeads", "chain_unsubscribeNewHeads")
.access("number"),
)
.unsafeAs<HexHash>(),
.unsafeAs<string>(),
)

block<X>(...[blockHash]: RunicArgs<X, [blockHash: HexHash]>) {
block<X>(...[blockHash]: RunicArgs<X, [blockHash: string]>) {
return this.connection.call("chain_getBlock", blockHash)
.unhandle(null)
.into(BlockRune, this, Rune.resolve(blockHash))
}

metadata<X>(...[blockHash]: RunicArgs<X, [blockHash?: HexHash]>) {
metadata<X>(...[blockHash]: RunicArgs<X, [blockHash?: string]>) {
return this.connection.call("state_getMetadata", blockHash)
.map(M.fromPrefixedHex)
.throws($.ScaleError)
Expand Down
5 changes: 2 additions & 3 deletions fluent/ConnectionRune.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Calls, Subscription, Subscriptions } from "../rpc/known/mod.ts"
import { Connection, ConnectionError, RpcSubscriptionMessage, ServerError } from "../rpc/mod.ts"
import { Batch, MetaRune, Run, Rune, RunicArgs, RunStream, ValueRune } from "../rune/mod.ts"
import { PromiseOr } from "../util/mod.ts"
import { ChainRune } from "./ChainRune.ts"

class RunConnection extends Run<Connection, never> {
constructor(ctx: Batch, readonly initConnection: (signal: AbortSignal) => PromiseOr<Connection>) {
constructor(ctx: Batch, readonly initConnection: (signal: AbortSignal) => Promise<Connection>) {
super(ctx)
}

Expand All @@ -15,7 +14,7 @@ class RunConnection extends Run<Connection, never> {
}
}

export function connection(init: (signal: AbortSignal) => PromiseOr<Connection>) {
export function connection(init: (signal: AbortSignal) => Promise<Connection>) {
return Rune.new(RunConnection, init).into(ConnectionRune)
}

Expand Down
20 changes: 9 additions & 11 deletions fluent/ExtrinsicRune.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { hashers, hex, ss58 } from "../crypto/mod.ts"
import * as M from "../frame_metadata/mod.ts"
import { MultiAddress, Signer } from "../primitives/mod.ts"
import { Rune, RunicArgs, ValueRune } from "../rune/mod.ts"
import { Era, era } from "../scale_info/mod.ts"
import { Blake2_256 } from "../util/hashers.ts"
import * as U from "../util/mod.ts"
import { HexHash } from "../util/mod.ts"
import { Chain, ChainRune } from "./ChainRune.ts"
import { CodecRune } from "./CodecRune.ts"
import { SignedExtrinsicRune } from "./SignedExtrinsicRune.ts"
Expand All @@ -16,7 +14,7 @@ export interface ExtrinsicSender {

export interface SignedExtrinsicProps {
sender: ExtrinsicSender
checkpoint?: U.HexHash
checkpoint?: string
mortality?: Era
nonce?: number
tip?: bigint
Expand All @@ -29,7 +27,7 @@ export class ExtrinsicRune<out U, out C extends Chain = Chain> extends Rune<Chai
super(_prime)
const metadata = this.chain.metadata()
this.hash = Rune.rec({ metadata, deriveCodec: metadata.deriveCodec })
.map((x) => Blake2_256.$hash(M.$call(x)))
.map((x) => hashers.Blake2_256.$hash(M.$call(x)))
.into(CodecRune)
.encoded(this)
}
Expand Down Expand Up @@ -57,21 +55,21 @@ export class ExtrinsicRune<out U, out C extends Chain = Chain> extends Rune<Chai
.map(([addrPrefix, sender]) => {
switch (sender.address.type) {
case "Id": {
return U.ss58.encode(addrPrefix, sender.address.value)
return ss58.encode(addrPrefix, sender.address.value)
}
default: {
throw new Error("unimplemented")
}
}
})
.throws(U.ss58.InvalidPublicKeyLengthError, U.ss58.InvalidNetworkPrefixError)
.throws(ss58.InvalidPublicKeyLengthError, ss58.InvalidNetworkPrefixError)
// TODO: handle props.nonce resolving to undefined
const nonce = props.nonce ?? this.chain.connection.call("system_accountNextIndex", senderSs58)
const genesisHashHex = this.chain.connection.call("chain_getBlockHash", 0).unsafeAs<HexHash>()
const genesisHashHex = this.chain.connection.call("chain_getBlockHash", 0).unsafeAs<string>()
.into(ValueRune)
const genesisHash = genesisHashHex.map(U.hex.decode)
const genesisHash = genesisHashHex.map(hex.decode)
const checkpointHash = Rune.tuple([props.checkpoint, genesisHashHex]).map(([a, b]) => a ?? b)
.map(U.hex.decode)
.map(hex.decode)
const mortality = Rune.resolve(props.mortality).map((x) => x ?? era.immortal)
const tip = Rune.resolve(props.tip).map((x) => x ?? 0n)
const extra = Rune.tuple([mortality, nonce, tip])
Expand Down Expand Up @@ -106,7 +104,7 @@ export class ExtrinsicRune<out U, out C extends Chain = Chain> extends Rune<Chai
}

feeEstimate() {
const extrinsicHex = this.encoded().map(U.hex.encodePrefixed)
const extrinsicHex = this.encoded().map(hex.encodePrefixed)
return this.chain.connection.call("payment_queryInfo", extrinsicHex)
.map(({ weight, ...rest }) => ({
...rest,
Expand Down
3 changes: 1 addition & 2 deletions fluent/ExtrinsicStatusRune.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { known } from "../rpc/mod.ts"
import { MetaRune, Rune, RunicArgs, ValueRune } from "../rune/mod.ts"
import { Hex } from "../util/mod.ts"
import { BlockRune } from "./BlockRune.ts"
import { Chain } from "./ChainRune.ts"
import { SignedExtrinsicRune } from "./SignedExtrinsicRune.ts"
Expand Down Expand Up @@ -67,7 +66,7 @@ export class ExtrinsicStatusRune<out U1, out U2, out C extends Chain = Chain>
const txI = Rune
.tuple([block.into(ValueRune).access("block", "extrinsics"), this.extrinsic.hex()])
.map(([hexes, hex]) => {
const i = hexes.indexOf(("0x" + hex) as Hex)
const i = hexes.indexOf("0x" + hex)
return i === -1 ? undefined : i
})
return Rune
Expand Down
2 changes: 1 addition & 1 deletion fluent/PublicKeyRune.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ss58 } from "../crypto/mod.ts"
import { Rune, ValueRune } from "../rune/mod.ts"
import { ss58 } from "../util/mod.ts"
import { AddressRune } from "./AddressRune.ts"
import { Chain, ChainRune } from "./ChainRune.ts"

Expand Down
2 changes: 1 addition & 1 deletion fluent/SignedExtrinsicRune.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hex } from "../crypto/mod.ts"
import { Rune, ValueRune } from "../rune/mod.ts"
import { hex } from "../util/mod.ts"
import { Chain, ChainRune } from "./ChainRune.ts"
import { ExtrinsicStatusRune } from "./ExtrinsicStatusRune.ts"

Expand Down
16 changes: 8 additions & 8 deletions fluent/StorageRune.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hex } from "../crypto/mod.ts"
import * as $ from "../deps/scale.ts"
import * as M from "../frame_metadata/mod.ts"
import { Rune, RunicArgs, ValueRune } from "../rune/mod.ts"
import { hex, HexHash } from "../util/mod.ts"
import { CodecRune } from "./CodecRune.ts"
import { PalletRune } from "./PalletRune.ts"

Expand All @@ -19,7 +19,7 @@ export class StorageRune<in out K extends unknown[], out V, out U> extends Rune<
this.$value = this.pallet.metadata.codec(this.into(ValueRune).access("value"))
}

size<X>(...[partialKey, blockHash]: RunicArgs<X, [partialKey?: unknown[], blockHash?: HexHash]>) {
size<X>(...[partialKey, blockHash]: RunicArgs<X, [partialKey?: unknown[], blockHash?: string]>) {
return this.pallet.metadata.chain.connection
.call(
"state_getStorageSize",
Expand All @@ -35,7 +35,7 @@ export class StorageRune<in out K extends unknown[], out V, out U> extends Rune<
count: number,
partialKey?: unknown[],
start?: unknown[],
blockHash?: HexHash,
blockHash?: string,
]>
) {
const storageKeys = this.keyPageRaw(count, partialKey, start, blockHash)
Expand All @@ -51,7 +51,7 @@ export class StorageRune<in out K extends unknown[], out V, out U> extends Rune<
count: number,
partialKey?: unknown[],
start?: unknown[],
blockHash?: HexHash,
blockHash?: string,
]>
) {
return Rune
Expand All @@ -69,15 +69,15 @@ export class StorageRune<in out K extends unknown[], out V, out U> extends Rune<
.unsafeAs<[K, V][]>()
}

entryRaw<X>(...[key, blockHash]: RunicArgs<X, [key: K, blockHash?: HexHash]>) {
entryRaw<X>(...[key, blockHash]: RunicArgs<X, [key: K, blockHash?: string]>) {
const storageKey = this.$key.encoded(key).map(hex.encode)
return this.pallet.metadata.chain.connection
.call("state_getStorage", storageKey, blockHash)
.unhandle(null)
.rehandle(null, () => Rune.constant(undefined))
}

entry<X>(...[key, blockHash]: RunicArgs<X, [key: K, blockHash?: HexHash]>) {
entry<X>(...[key, blockHash]: RunicArgs<X, [key: K, blockHash?: string]>) {
return this.$value
.decoded(this.entryRaw(key, blockHash).unhandle(undefined).map(hex.decode))
.unsafeAs<V>()
Expand All @@ -90,7 +90,7 @@ export class StorageRune<in out K extends unknown[], out V, out U> extends Rune<
count: number,
partialKey?: unknown[],
start?: unknown[],
blockHash?: HexHash,
blockHash?: string,
]>
) {
const storageKey = this.$key
Expand Down Expand Up @@ -118,7 +118,7 @@ export class StorageRune<in out K extends unknown[], out V, out U> extends Rune<
count: number,
partialKey?: unknown[],
start?: unknown[],
blockHash?: HexHash,
blockHash?: string,
]>
) {
const raw = this.keyPageRaw(count, partialKey, start, blockHash)
Expand Down
4 changes: 2 additions & 2 deletions frame_metadata/Extrinsic.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hashers, hex, ss58 } from "../crypto/mod.ts"
import * as $ from "../deps/scale.ts"
import { assert } from "../deps/std/testing/asserts.ts"
import {
Expand All @@ -8,7 +9,6 @@ import {
Signer,
} from "../primitives/mod.ts"
import { $null, DeriveCodec } from "../scale_info/Codec.ts"
import { hashers, Hex, hex, ss58 } from "../util/mod.ts"
import { Metadata } from "./Metadata.ts"

// TODO: make generic over chain
Expand Down Expand Up @@ -90,7 +90,7 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
const signer = props.sign
buffer.writeAsync(0, async (buffer) => {
const { signature } = await signer.signPayload(payload)
buffer.insertArray(hex.decode(signature as Hex))
buffer.insertArray(hex.decode(signature))
})
buffer.insertArray(extraEncoded)
buffer.insertArray(callEncoded)
Expand Down
Loading