Files
pezkuwi-subxt/subxt/examples/setup_config_signed_extension.rs
T
Tadeo Hepperle 56d0cdae78 Static Decoding of Signed Extensions: Simple Approach (#1235)
* skeleton commit

* signed extension decoding

* fix some minor things

* make api more similar to Extrinsics

* defer decoding of signed extensions

* fix byte slices

* add test for nonce signed extension

* adjust test and extend for tip

* clippy

* support both  ChargeTransactionPayment and ChargeAssetTxPayment

* address PR comments

* Extend lifetimes, expose pub structs, remove as_type

* add signed extensions to block subscribing example

* add Decoded type

* fix merging bug and tests

* add decoded type in CustomSignedExtension

* fix minor issues, extend test

* cargo fmt differences

* remove the `decoded` function

* new as_signed_extra fn, do not expose as_type anymore

* fix Result-Option order, simplify obtaining Nonce

* tx: Remove `wait_for_in_block` helper method (#1237)

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update smoldot to 0.12 (#1212)

* Update lightclient

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Fix typo

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Update cargo.toml

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Add tracing logs to improve debugging

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Add socket buffers module for `PlatformRef`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Update `SubxtPlatform`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cargo: Add lightclient dependencies

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update cargo.lock of wasm tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Add constant for with-buffer module

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Replace rand crate with getrandom

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* example: Update cargo lock file

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Update deps

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Tadeo Hepperle <62739623+tadeohepperle@users.noreply.github.com>

* ChargeAssetTxPayment: support providing u32 or MultiLocation in default impl (#1227)

* Asset Id in Config trait

* add example configuring the config

* fmt

* fix Default trait bound

* merge examples, fix default again

* adjust config in examples

* Update subxt/src/config/mod.rs

Co-authored-by: James Wilson <james@jsdw.me>

---------

Co-authored-by: James Wilson <james@jsdw.me>

* generic AssetId

* fix generics

* fmt

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
2023-11-10 18:32:58 +01:00

104 lines
3.7 KiB
Rust

use codec::Encode;
use subxt::client::OfflineClientT;
use subxt::config::signed_extensions;
use subxt::config::{
Config, DefaultExtrinsicParamsBuilder, ExtrinsicParams, ExtrinsicParamsEncoder,
};
use subxt_signer::sr25519::dev;
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
pub mod runtime {}
// We don't need to construct this at runtime,
// so an empty enum is appropriate:
pub enum CustomConfig {}
impl Config for CustomConfig {
type Hash = subxt::utils::H256;
type AccountId = subxt::utils::AccountId32;
type Address = subxt::utils::MultiAddress<Self::AccountId, ()>;
type Signature = subxt::utils::MultiSignature;
type Hasher = subxt::config::substrate::BlakeTwo256;
type Header = subxt::config::substrate::SubstrateHeader<u32, Self::Hasher>;
type ExtrinsicParams = signed_extensions::AnyOf<
Self,
(
// Load in the existing signed extensions we're interested in
// (if the extension isn't actually needed it'll just be ignored):
signed_extensions::CheckSpecVersion,
signed_extensions::CheckTxVersion,
signed_extensions::CheckNonce,
signed_extensions::CheckGenesis<Self>,
signed_extensions::CheckMortality<Self>,
signed_extensions::ChargeAssetTxPayment<Self>,
signed_extensions::ChargeTransactionPayment,
// And add a new one of our own:
CustomSignedExtension,
),
>;
type AssetId = u32;
}
// Our custom signed extension doesn't do much:
pub struct CustomSignedExtension;
// Give the extension a name; this allows `AnyOf` to look it
// up in the chain metadata in order to know when and if to use it.
impl<T: Config> signed_extensions::SignedExtension<T> for CustomSignedExtension {
const NAME: &'static str = "CustomSignedExtension";
type Decoded = ();
}
// Gather together any params we need for our signed extension, here none.
impl<T: Config> ExtrinsicParams<T> for CustomSignedExtension {
type OtherParams = ();
type Error = std::convert::Infallible;
fn new<Client: OfflineClientT<T>>(
_nonce: u64,
_client: Client,
_other_params: Self::OtherParams,
) -> Result<Self, Self::Error> {
Ok(CustomSignedExtension)
}
}
// Encode whatever the extension needs to provide when asked:
impl ExtrinsicParamsEncoder for CustomSignedExtension {
fn encode_extra_to(&self, v: &mut Vec<u8>) {
"Hello".encode_to(v);
}
fn encode_additional_to(&self, v: &mut Vec<u8>) {
true.encode_to(v)
}
}
// When composing a tuple of signed extensions, the user parameters we need must
// be able to convert `Into` a tuple of corresponding `OtherParams`. Here, we just
// "hijack" the default param builder, but add the `OtherParams` (`()`) for our
// new signed extension at the end, to make the types line up. IN reality you may wish
// to construct an entirely new interface to provide the relevant `OtherParams`.
pub fn custom(
params: DefaultExtrinsicParamsBuilder<CustomConfig>,
) -> <<CustomConfig as Config>::ExtrinsicParams as ExtrinsicParams<CustomConfig>>::OtherParams {
let (a, b, c, d, e, f, g) = params.build();
(a, b, c, d, e, f, g, ())
}
#[tokio::main]
async fn main() {
// With the config defined, it can be handed to Subxt as follows:
let client = subxt::OnlineClient::<CustomConfig>::new().await.unwrap();
let tx_payload = runtime::tx().system().remark(b"Hello".to_vec());
// Configure the tx params:
let tx_config = DefaultExtrinsicParamsBuilder::new().tip(1234);
// And provide them when submitting a transaction:
let _ = client
.tx()
.sign_and_submit_then_watch(&tx_payload, &dev::alice(), custom(tx_config))
.await;
}