Skip to content

*: migrate eip-712 support to ethers-rs #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,3 @@ members = [
"tests/e2e",
"spaces-cli",
]

[patch.crates-io]
# TODO: replace
eip-712 = {git="https://github.com/darioush/EIP-712.git"}
1 change: 1 addition & 0 deletions spaces-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ homepage = "https://avax.network"
[dependencies]
avalanche-types = { version = "0.0.138", features = ["subnet"] }
clap = { version = "4.0", features = ["derive"] }
ethers-core = { version = "1.0.0", default-features = false, features = ["eip712", "macros"] }
hex = "0.4.3"
jsonrpc-core = "18.0.0"
jsonrpc-core-client = { version = "18.0.0" }
Expand Down
7 changes: 4 additions & 3 deletions spaces-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use std::{

use avalanche_types::key;
use clap::{Parser, Subcommand};
use ethers_core::types::transaction::eip712::Eip712;
use jsonrpc_client_transports::{transports, RpcError};
use jsonrpc_core::futures;
use spacesvm::{
api::{
DecodeTxArgs, IssueTxArgs, IssueTxResponse, PingResponse, ResolveArgs, ResolveResponse,
ServiceClient as Client,
},
chain::tx::{decoder, tx::TransactionType, unsigned::TransactionData},
chain::tx::{tx::TransactionType, unsigned::TransactionData},
};

#[derive(Parser)]
Expand Down Expand Up @@ -167,8 +168,8 @@ async fn sign_and_submit(

let typed_data = &resp.typed_data;

let dh = decoder::hash_structured_data(typed_data)?;
let sig = pk.sign_digest(&dh.as_bytes())?;
let dh = typed_data.struct_hash().unwrap();
let sig = pk.sign_digest(&dh)?;

client
.issue_tx(IssueTxArgs {
Expand Down
13 changes: 6 additions & 7 deletions spacesvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "spacesvm"
version = "0.0.1"
version = "0.0.0"
edition = "2021"
rust-version = "1.65"
publish = true
description = "Authenticated, hierarchical key-value store w/EIP-712 compatibility"
publish = false
description = "Authenticated hierarchical key-value store w/EIP-712 compatibility"
license = "BSD-3-Clause"
homepage = "https://avax.network"
repository = "https://github.com/ava-labs/spacesvm-rs"
readme = "README.md"
readme = "../README.md"

[[bin]]
name = "spacesvm"
Expand All @@ -18,12 +18,11 @@ path = "src/bin/spaces/main.rs"
avalanche-types = { version = "0.0.138", features = ["subnet"] }
byteorder = "1.4.3"
chrono = "0.4.22"
clap = { version = "4.0.23", features = ["cargo", "derive"] }
crossbeam-channel = "0.5.6"
ethers-core = { version = "1.0.0", default-features = false, features = ["eip712", "macros"] }
derivative = "2.2.0"
dyn-clone = "1.0.9"
ethereum-types = { version = "0.14.0" }
clap = { version = "4.0.22", features = ["cargo", "derive"] }
eip-712 = "0.1.0"
env_logger = "0.9.3"
hex = "0.4.3"
http = "0.2.8"
Expand Down
2 changes: 1 addition & 1 deletion spacesvm/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub mod service;

use avalanche_types::ids;
use ethers_core::types::transaction::eip712::TypedData;
use jsonrpc_core::{BoxFuture, Error, ErrorCode, Result};
use jsonrpc_derive::rpc;
use serde::{Deserialize, Serialize};

use crate::chain::{
storage::ValueMeta,
tx::decoder::TypedData,
tx::{self},
};

Expand Down
12 changes: 6 additions & 6 deletions spacesvm/src/api/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::sync::Arc;

use crate::{
api::*,
chain::{self, storage, tx::Transaction},
chain::{
self, storage,
tx::{decoder::parse_typed_data, Transaction},
},
vm::inner::Inner,
};

Expand Down Expand Up @@ -34,10 +37,7 @@ impl crate::api::Service for Service {
Box::pin(async move {
let mut inner = vm.write().await;

let unsigned_tx = params
.typed_data
.parse_typed_data()
.map_err(create_jsonrpc_error)?;
let unsigned_tx = parse_typed_data(&params.typed_data).map_err(create_jsonrpc_error)?;

let mut tx = chain::tx::tx::Transaction::new(unsigned_tx, params.signature);
tx.init().await.map_err(create_jsonrpc_error)?;
Expand Down Expand Up @@ -82,7 +82,7 @@ impl crate::api::Service for Service {
.map_err(create_jsonrpc_error)?;

utx.set_block_id(*last_accepted).await;
let typed_data = utx.typed_data().await;
let typed_data = utx.typed_data().await.map_err(create_jsonrpc_error)?;

let string = serde_json::to_string(&typed_data).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion spacesvm/src/chain/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ async fn test_raw_space() {
#[tokio::test]
async fn test_space_info_rt() {
use super::tx::claim::Info;
use ethereum_types::H160;
use ethers_core::types::H160;

env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "debug"),
Expand Down
36 changes: 19 additions & 17 deletions spacesvm/src/chain/tx/claim.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::{
collections::HashMap,
io::{Error, ErrorKind, Result},
};
use std::io::{Error, ErrorKind, Result};

use avalanche_types::ids;
use ethers_core::types::{
transaction::eip712::{Eip712DomainType as Type, TypedData},
H160,
};

use serde::{Deserialize, Serialize};

use crate::chain::{
storage::{has_space, put_space_info},
tx::decoder::{create_typed_data, MessageValue, Type, TypedData},
tx::decoder::{create_typed_data, TypedDataMessage},
};

use super::{
Expand All @@ -26,7 +28,7 @@ pub struct Info {
#[serde(deserialize_with = "ids::short::must_deserialize_id")]
pub raw_space: ids::short::Id,

pub owner: ethereum_types::H160,
pub owner: H160,
}

/// Creates a space, which acts as a logical key-space root.
Expand Down Expand Up @@ -86,31 +88,31 @@ impl unsigned::Transaction for Tx {
return put_space_info(&mut db, self.space.as_bytes(), new_info, 0).await;
}

async fn typed_data(&self) -> TypedData {
async fn typed_data(&self) -> Result<TypedData> {
let mut tx_fields: Vec<Type> = Vec::new();
tx_fields.push(Type {
name: TD_SPACE.to_owned(),
type_: TD_STRING.to_owned(),
r#type: TD_STRING.to_owned(),
});
tx_fields.push(Type {
name: TD_BLOCK_ID.to_owned(),
type_: TD_STRING.to_owned(),
r#type: TD_STRING.to_owned(),
});

let mut message: HashMap<String, MessageValue> = HashMap::with_capacity(1);
let mut message = TypedDataMessage::new();
message.insert(
TD_SPACE.to_owned(),
MessageValue::Vec(self.space.as_bytes().to_vec()),
serde_json::Value::String(self.space.clone()),
);
let value = MessageValue::Vec(self.base_tx.block_id.to_vec());
log::debug!("typed_data: message value: {:?}", value);
log::debug!("typed_data: id vec: {:?}", self.base_tx.block_id.to_vec());
log::debug!("typed_data: id: {}", self.base_tx.block_id);
message.insert(
TD_BLOCK_ID.to_owned(),
MessageValue::Vec(self.base_tx.block_id.to_vec()),
serde_json::Value::String(self.base_tx.block_id.to_string()),
);

return create_typed_data(super::tx::TransactionType::Claim, tx_fields, message);
Ok(create_typed_data(
super::tx::TransactionType::Claim,
tx_fields,
message,
))
}
}
Loading