Skip to content

Instantly share code, notes, and snippets.

@jsdw
jsdw / Cargo.toml
Created March 7, 2024 17:40
Subxt: An Ethereum compatible signer
[package]
name = "eth_signer_example"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = { version = "1.0.80", features = ["backtrace"] }
subxt = "0.34.0"
tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] }
codec = { package = "parity-scale-codec", version = "3.6.9", features = ["derive"] }
@jsdw
jsdw / substrate_extrinsics.md
Last active October 23, 2023 12:31
Constructing and Signing Extrinsics in Substrate

Constructing and Signing Extrinsics

Substrate is configurable enough that extrinsics can take any format, in theory. In practice, runtimes tend to use our UncheckedExtrinsic type to represent extrinsics. In Polkadot, this is configured here at the time of writing.

What follows is a description of how extrinsics based on this type are encoded into bytes. These bytes can then be submitted to a chain for inclusion in a block (this is how we make changes to the state of a chain), and are ultimately stored in the body of a block.

Encoding an Extrinsic

At a high level, all extrinsics are formed from concatenating some details together, ie:

@jsdw
jsdw / polkadot-js-extension-signing.js
Created July 11, 2023 14:35
Signing a transaction with polkadot JS browser extension
import * as dapp from "@polkadot/extension-dapp";
import { Keyring } from "@polkadot/ui-keyring";
import {
mnemonicGenerate,
cryptoWaitReady,
signatureVerify,
decodeAddress,
} from "@polkadot/util-crypto";
import { stringToU8a, u8aToHex, stringToHex, hexToU8a } from "@polkadot/util";
@jsdw
jsdw / aggregateAndDebounce.ts
Last active February 24, 2023 14:20
A function to aggregate values, waiting at least waitMs times before sending off
function aggregateAndDebounce<A, B>(
// Take some val and the current aggregated vals in and return next aggregated vals
aggregate: (val: A, vals: B) => B,
// This is called waitMs after the some values are given
debounced: (val: B) => void,
// This produces an initial state for the aggregated vals, to start from/
initialVals: () => B,
// How long to wait until calling the debounce
waitMs: number
): (val: A) => void {
@jsdw
jsdw / PromiseQueue.ts
Created June 13, 2022 09:08
A JS/TS Promise Queue implementation (run N tasks concurrently)
/**
* A PromiseQueue, enforcing that no more than `maxTasks` number of tasks
* are running at a given time.
*/
class PromiseQueue<T> {
// How many tasks are allowed to run concurrently?
#maxTasks: number;
// How many tasks are currently running concurrently?
#runningTasks: number = 0;
// The queued tasks waiting to run
{-# LANGUAGE DataKinds, TypeOperators, FlexibleContexts, GeneralizedNewtypeDeriving, TypeFamilies #-}
module Main where
import qualified Database as Database
import qualified Data.Map as Map
import Network.Wai (Request, requestHeaders)
import Network.Wai.Handler.Warp (run)
import Control.Applicative ((<$>), (<*>))