A dialog that previews an EIP-712 typed-data signature before the wallet asks for it: who is asking, on which chain and contract, and every field of the message with amounts shown in their token's units and full addresses. Reject and Sign are the host's handlers.
Loading GPUI…
use gpui::{div, prelude::*, px, AnyElement, Context, FontWeight, SharedString, Window};
use crate::icons::{icon, CIRCLE_CHECK, CIRCLE_X, PENCIL, TRIANGLE_ALERT};
use crate::theme::ActiveTheme;
use crate::ui::badge::{Badge, BadgeVariant};
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::dialog::Dialog;
use crate::ui::onchain::{format_units, Address};
use crate::ui::scroll_area::ScrollArea;
const MONO: &str = "Geist Mono";
/// The EIP-712 domain of the sample request. The verifying contract is a fake address.
const DOMAIN_NAME: &str = "Sample Exchange";
const DOMAIN_VERSION: &str = "1";
const CHAIN: &str = "Mainnet";
const VERIFYING_CONTRACT: &str = "0x000000000000000000000000000000000000c0DE";
/// A message field: name, Solidity type and how to show its value.
enum Value {
Address(&'static str),
/// Base units, the token's decimals and symbol.
Amount(u128, u8, &'static str),
Text(&'static str),
}
/// The sample `Order` message.
const MESSAGE: [(&str, &str, Value); 6] = [
("maker", "address", Value::Address("0x00000000000000000000000000000000000A11cE")),
("sellToken", "address", Value::Address("0x0000000000000000000000000000000000001006")),
("sellAmount", "uint256", Value::Amount(1_500_000_000, 6, "USDC")),
("buyToken", "address", Value::Address("0x0000000000000000000000000000000000001001")),
("buyAmount", "uint256", Value::Amount(499_000_000_000_000_000, 18, "ETH")),
("expiry", "uint256", Value::Text("1798761600 (Jan 1, 2027 00:00 UTC)")),
];
#[derive(Clone, Copy, Default, PartialEq)]
enum Outcome {
#[default]
None,
Signed,
Rejected,
}
#[derive(Default)]
pub struct SigningRequestPattern {
open: bool,
outcome: Outcome,
}
impl SigningRequestPattern {
/// Sign asks the host to have the wallet sign; Reject tells the dApp the user declined.
fn decide(&mut self, outcome: Outcome, cx: &mut Context<Self>) {
self.outcome = outcome;
self.open = false;
cx.notify();
}
}
impl Render for SigningRequestPattern {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme().clone();
let kv = |label: &'static str, value: AnyElement| {
div()
.flex()
.items_start()
.justify_between()
.gap_4()
.py_1p5()
.text_xs()
.child(div().flex_none().text_color(theme.muted_foreground).child(label))
.child(div().min_w_0().text_right().child(value))
};
let mono = |text: String| div().font_family(MONO).child(text).into_any_element();
let address = |text: &str| match Address::parse(text) {
Ok(address) => mono(address.to_string()),
// A malformed address in a request is itself a warning sign.
Err(error) => div().text_color(theme.destructive).child(error.to_string()).into_any_element(),
};
let fields = MESSAGE.iter().map(|(name, ty, value)| {
let shown: AnyElement = match value {
Value::Address(text) => address(text),
Value::Amount(units, decimals, symbol) => div()
.flex()
.flex_col()
.items_end()
.child(div().font_weight(FontWeight::MEDIUM).child(format!("{} {symbol}", format_units(*units, *decimals))))
.child(div().font_family(MONO).text_color(theme.muted_foreground).child(units.to_string()))
.into_any_element(),
Value::Text(text) => div().child(*text).into_any_element(),
};
div()
.flex()
.flex_col()
.gap_0p5()
.py_2()
.border_b_1()
.border_color(theme.border)
.child(
div()
.flex()
.items_center()
.gap_2()
.text_xs()
.child(div().font_weight(FontWeight::MEDIUM).child(*name))
.child(div().font_family(MONO).text_color(theme.muted_foreground).child(*ty)),
)
.child(div().text_xs().child(shown))
});
let section = |title: &'static str| {
div()
.pt_1()
.text_xs()
.font_weight(FontWeight::MEDIUM)
.text_color(theme.muted_foreground)
.child(SharedString::from(title))
};
let dialog = Dialog::new("signing", self.open)
.trigger(
Button::new("review", "Review signature request")
.variant(ButtonVariant::Outline)
.leading(icon(PENCIL).size(px(14.0)).text_color(theme.foreground)),
)
.title("Signature request")
.description("app.example wants you to sign this message. Signing is free, but it can authorize a trade.")
.on_open_change(cx.listener(|this, open: &bool, _, cx| {
this.open = *open;
cx.notify();
}))
.child(
ScrollArea::new("typed-data").max_h(px(330.0)).child(
div()
.flex()
.flex_col()
.pr_3()
.child(section("Domain"))
.child(kv("Name", div().child(DOMAIN_NAME).into_any_element()))
.child(kv("Version", div().child(DOMAIN_VERSION).into_any_element()))
.child(kv("Chain", div().child(CHAIN).into_any_element()))
// A full address doesn't fit beside its label, so it gets its own line.
.child(
div()
.flex()
.flex_col()
.gap_0p5()
.py_1p5()
.text_xs()
.child(div().text_color(theme.muted_foreground).child("Verifying contract"))
.child(address(VERIFYING_CONTRACT)),
)
.child(
div()
.flex()
.items_center()
.gap_2()
.pt_3()
.child(section("Message"))
.child(Badge::new("Order").variant(BadgeVariant::Secondary)),
)
.children(fields),
),
)
.child(
div()
.flex()
.items_start()
.gap_2()
.p_3()
.rounded(theme.radius)
.bg(theme.muted)
.text_xs()
.child(icon(TRIANGLE_ALERT).flex_none().size(px(14.0)).text_color(theme.foreground))
.child(div().flex_1().min_w_0().child(
"Check the contract and amounts. Anyone holding this signature can submit the order until it expires.",
)),
)
.footer(
div()
.flex()
.gap_2()
.child(
Button::new("reject", "Reject")
.variant(ButtonVariant::Outline)
.on_click(cx.listener(|this, _, _, cx| this.decide(Outcome::Rejected, cx))),
)
.child(Button::new("sign", "Sign").on_click(cx.listener(|this, _, _, cx| this.decide(Outcome::Signed, cx)))),
);
let note: Option<AnyElement> = match self.outcome {
Outcome::None => None,
Outcome::Signed => Some(
div()
.flex()
.items_center()
.gap_1p5()
.child(icon(CIRCLE_CHECK).size(px(14.0)).text_color(theme.foreground))
.child("Signed. The host sends the signature to the app.")
.into_any_element(),
),
Outcome::Rejected => Some(
div()
.flex()
.items_center()
.gap_1p5()
.child(icon(CIRCLE_X).size(px(14.0)).text_color(theme.muted_foreground))
.child("Rejected. Nothing was signed.")
.into_any_element(),
),
};
div()
.flex()
.flex_col()
.items_center()
.gap_3()
.child(dialog)
.child(
div()
.text_xs()
.text_color(theme.muted_foreground)
.child(note.unwrap_or_else(|| "Sample EIP-712 order with fake addresses.".into_any_element())),
)
}
}Installation
Add the components
$ cargo hyuskui add badge button dialog onchain scroll-area iconsUses Badge, Button, Dialog, Onchain and Scroll Area.
Add the crates
$ cargo add sha3@0.10onchain.rshashes addresses with Keccak-256 for their EIP-55 checksum.cargo hyuskui addruns this for you; you need it only when copying the files by hand.Copy the pattern
Paste the code into
src/crypto/signing_request.rsand declare the module. The demo runs on sample data and fake addresses; feed it your wallet connection, balances and quotes, and send transactions from the handlers.src/crypto/signing_request.rs use gpui::{div, prelude::*, px, AnyElement, Context, FontWeight, SharedString, Window}; use crate::icons::{icon, CIRCLE_CHECK, CIRCLE_X, PENCIL, TRIANGLE_ALERT}; use crate::theme::ActiveTheme; use crate::ui::badge::{Badge, BadgeVariant}; use crate::ui::button::{Button, ButtonVariant}; use crate::ui::dialog::Dialog; use crate::ui::onchain::{format_units, Address}; use crate::ui::scroll_area::ScrollArea; const MONO: &str = "Geist Mono"; /// The EIP-712 domain of the sample request. The verifying contract is a fake address. const DOMAIN_NAME: &str = "Sample Exchange"; const DOMAIN_VERSION: &str = "1"; const CHAIN: &str = "Mainnet"; const VERIFYING_CONTRACT: &str = "0x000000000000000000000000000000000000c0DE"; /// A message field: name, Solidity type and how to show its value. enum Value { Address(&'static str), /// Base units, the token's decimals and symbol. Amount(u128, u8, &'static str), Text(&'static str), } /// The sample `Order` message. const MESSAGE: [(&str, &str, Value); 6] = [ ("maker", "address", Value::Address("0x00000000000000000000000000000000000A11cE")), ("sellToken", "address", Value::Address("0x0000000000000000000000000000000000001006")), ("sellAmount", "uint256", Value::Amount(1_500_000_000, 6, "USDC")), ("buyToken", "address", Value::Address("0x0000000000000000000000000000000000001001")), ("buyAmount", "uint256", Value::Amount(499_000_000_000_000_000, 18, "ETH")), ("expiry", "uint256", Value::Text("1798761600 (Jan 1, 2027 00:00 UTC)")), ]; #[derive(Clone, Copy, Default, PartialEq)] enum Outcome { #[default] None, Signed, Rejected, } #[derive(Default)] pub struct SigningRequestPattern { open: bool, outcome: Outcome, } impl SigningRequestPattern { /// Sign asks the host to have the wallet sign; Reject tells the dApp the user declined. fn decide(&mut self, outcome: Outcome, cx: &mut Context<Self>) { self.outcome = outcome; self.open = false; cx.notify(); } } impl Render for SigningRequestPattern { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { let theme = cx.theme().clone(); let kv = |label: &'static str, value: AnyElement| { div() .flex() .items_start() .justify_between() .gap_4() .py_1p5() .text_xs() .child(div().flex_none().text_color(theme.muted_foreground).child(label)) .child(div().min_w_0().text_right().child(value)) }; let mono = |text: String| div().font_family(MONO).child(text).into_any_element(); let address = |text: &str| match Address::parse(text) { Ok(address) => mono(address.to_string()), // A malformed address in a request is itself a warning sign. Err(error) => div().text_color(theme.destructive).child(error.to_string()).into_any_element(), }; let fields = MESSAGE.iter().map(|(name, ty, value)| { let shown: AnyElement = match value { Value::Address(text) => address(text), Value::Amount(units, decimals, symbol) => div() .flex() .flex_col() .items_end() .child(div().font_weight(FontWeight::MEDIUM).child(format!("{} {symbol}", format_units(*units, *decimals)))) .child(div().font_family(MONO).text_color(theme.muted_foreground).child(units.to_string())) .into_any_element(), Value::Text(text) => div().child(*text).into_any_element(), }; div() .flex() .flex_col() .gap_0p5() .py_2() .border_b_1() .border_color(theme.border) .child( div() .flex() .items_center() .gap_2() .text_xs() .child(div().font_weight(FontWeight::MEDIUM).child(*name)) .child(div().font_family(MONO).text_color(theme.muted_foreground).child(*ty)), ) .child(div().text_xs().child(shown)) }); let section = |title: &'static str| { div() .pt_1() .text_xs() .font_weight(FontWeight::MEDIUM) .text_color(theme.muted_foreground) .child(SharedString::from(title)) }; let dialog = Dialog::new("signing", self.open) .trigger( Button::new("review", "Review signature request") .variant(ButtonVariant::Outline) .leading(icon(PENCIL).size(px(14.0)).text_color(theme.foreground)), ) .title("Signature request") .description("app.example wants you to sign this message. Signing is free, but it can authorize a trade.") .on_open_change(cx.listener(|this, open: &bool, _, cx| { this.open = *open; cx.notify(); })) .child( ScrollArea::new("typed-data").max_h(px(330.0)).child( div() .flex() .flex_col() .pr_3() .child(section("Domain")) .child(kv("Name", div().child(DOMAIN_NAME).into_any_element())) .child(kv("Version", div().child(DOMAIN_VERSION).into_any_element())) .child(kv("Chain", div().child(CHAIN).into_any_element())) // A full address doesn't fit beside its label, so it gets its own line. .child( div() .flex() .flex_col() .gap_0p5() .py_1p5() .text_xs() .child(div().text_color(theme.muted_foreground).child("Verifying contract")) .child(address(VERIFYING_CONTRACT)), ) .child( div() .flex() .items_center() .gap_2() .pt_3() .child(section("Message")) .child(Badge::new("Order").variant(BadgeVariant::Secondary)), ) .children(fields), ), ) .child( div() .flex() .items_start() .gap_2() .p_3() .rounded(theme.radius) .bg(theme.muted) .text_xs() .child(icon(TRIANGLE_ALERT).flex_none().size(px(14.0)).text_color(theme.foreground)) .child(div().flex_1().min_w_0().child( "Check the contract and amounts. Anyone holding this signature can submit the order until it expires.", )), ) .footer( div() .flex() .gap_2() .child( Button::new("reject", "Reject") .variant(ButtonVariant::Outline) .on_click(cx.listener(|this, _, _, cx| this.decide(Outcome::Rejected, cx))), ) .child(Button::new("sign", "Sign").on_click(cx.listener(|this, _, _, cx| this.decide(Outcome::Signed, cx)))), ); let note: Option<AnyElement> = match self.outcome { Outcome::None => None, Outcome::Signed => Some( div() .flex() .items_center() .gap_1p5() .child(icon(CIRCLE_CHECK).size(px(14.0)).text_color(theme.foreground)) .child("Signed. The host sends the signature to the app.") .into_any_element(), ), Outcome::Rejected => Some( div() .flex() .items_center() .gap_1p5() .child(icon(CIRCLE_X).size(px(14.0)).text_color(theme.muted_foreground)) .child("Rejected. Nothing was signed.") .into_any_element(), ), }; div() .flex() .flex_col() .items_center() .gap_3() .child(dialog) .child( div() .text_xs() .text_color(theme.muted_foreground) .child(note.unwrap_or_else(|| "Sample EIP-712 order with fake addresses.".into_any_element())), ) } }