Skip to content
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

Move Polkadot implementation to Rust #3857

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ members = [
"chains/tw_binance",
"chains/tw_cosmos",
"chains/tw_ethereum",
"chains/tw_internet_computer",
"chains/tw_greenfield",
"chains/tw_internet_computer",
"chains/tw_native_evmos",
"chains/tw_native_injective",
"chains/tw_polkadot",
"chains/tw_ronin",
"chains/tw_solana",
"chains/tw_sui",
Expand All @@ -26,6 +27,7 @@ members = [
"tw_misc",
"tw_number",
"tw_proto",
"tw_ss58_address",
"tw_utxo",
"wallet_core_rs",
]
Expand Down
11 changes: 11 additions & 0 deletions rust/chains/tw_polkadot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "tw_polkadot"
version = "0.1.0"
edition = "2021"

[dependencies]
tw_coin_entry = { path = "../../tw_coin_entry" }
tw_hash = { path = "../../tw_hash" }
tw_keypair = { path = "../../tw_keypair" }
tw_memory = { path = "../../tw_memory" }
tw_proto = { path = "../../tw_proto" }
35 changes: 35 additions & 0 deletions rust/chains/tw_polkadot/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

use std::fmt;
use std::str::FromStr;
use tw_coin_entry::coin_entry::CoinAddress;
use tw_coin_entry::error::prelude::*;
use tw_memory::Data;

pub struct PolkadotAddress {
// bytes:
// TODO add necessary fields.
}

impl CoinAddress for PolkadotAddress {
#[inline]
fn data(&self) -> Data {
todo!()
}
}

impl FromStr for PolkadotAddress {
type Err = AddressError;

fn from_str(_s: &str) -> Result<Self, Self::Err> {
todo!()
}
}

impl fmt::Display for PolkadotAddress {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
todo!()
}
}
50 changes: 50 additions & 0 deletions rust/chains/tw_polkadot/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::coin_entry::{PublicKeyBytes, SignatureBytes};
use tw_coin_entry::error::prelude::*;
use tw_coin_entry::signing_output_error;
use tw_proto::Polkadot::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;

pub struct PolkadotCompiler;

impl PolkadotCompiler {
#[inline]
pub fn preimage_hashes(
coin: &dyn CoinContext,
input: Proto::SigningInput<'_>,
) -> CompilerProto::PreSigningOutput<'static> {
Self::preimage_hashes_impl(coin, input)
.unwrap_or_else(|e| signing_output_error!(CompilerProto::PreSigningOutput, e))
}

fn preimage_hashes_impl(
_coin: &dyn CoinContext,
_input: Proto::SigningInput<'_>,
) -> SigningResult<CompilerProto::PreSigningOutput<'static>> {
todo!()
}

#[inline]
pub fn compile(
coin: &dyn CoinContext,
input: Proto::SigningInput<'_>,
signatures: Vec<SignatureBytes>,
public_keys: Vec<PublicKeyBytes>,
) -> Proto::SigningOutput<'static> {
Self::compile_impl(coin, input, signatures, public_keys)
.unwrap_or_else(|e| signing_output_error!(Proto::SigningOutput, e))
}

fn compile_impl(
_coin: &dyn CoinContext,
_input: Proto::SigningInput<'_>,
_signatures: Vec<SignatureBytes>,
_public_keys: Vec<PublicKeyBytes>,
) -> SigningResult<Proto::SigningOutput<'static>> {
todo!()
}
}
93 changes: 93 additions & 0 deletions rust/chains/tw_polkadot/src/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

use crate::address::PolkadotAddress;
use crate::compiler::PolkadotCompiler;
use crate::signer::PolkadotSigner;
use std::str::FromStr;
use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::coin_entry::{CoinEntry, PublicKeyBytes, SignatureBytes};
use tw_coin_entry::derivation::Derivation;
use tw_coin_entry::error::prelude::*;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::transaction_decoder::NoTransactionDecoder;
use tw_coin_entry::modules::wallet_connector::NoWalletConnector;
use tw_coin_entry::prefix::NoPrefix;
use tw_keypair::tw::PublicKey;
use tw_proto::Polkadot::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;

pub struct PolkadotEntry;

impl CoinEntry for PolkadotEntry {
type AddressPrefix = NoPrefix;
type Address = PolkadotAddress;
type SigningInput<'a> = Proto::SigningInput<'a>;
type SigningOutput = Proto::SigningOutput<'static>;
type PreSigningOutput = CompilerProto::PreSigningOutput<'static>;

// Optional modules:
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnector = NoWalletConnector;
type TransactionDecoder = NoTransactionDecoder;

#[inline]
fn parse_address(
&self,
_coin: &dyn CoinContext,
_address: &str,
_prefix: Option<Self::AddressPrefix>,
) -> AddressResult<Self::Address> {
todo!()
}

#[inline]
fn parse_address_unchecked(
&self,
_coin: &dyn CoinContext,
address: &str,
) -> AddressResult<Self::Address> {
PolkadotAddress::from_str(address)
}

#[inline]
fn derive_address(
&self,
_coin: &dyn CoinContext,
_public_key: PublicKey,
_derivation: Derivation,
_prefix: Option<Self::AddressPrefix>,
) -> AddressResult<Self::Address> {
todo!()
}

#[inline]
fn sign(&self, coin: &dyn CoinContext, input: Self::SigningInput<'_>) -> Self::SigningOutput {
PolkadotSigner::sign(coin, input)
}

#[inline]
fn preimage_hashes(
&self,
coin: &dyn CoinContext,
input: Self::SigningInput<'_>,
) -> Self::PreSigningOutput {
PolkadotCompiler::preimage_hashes(coin, input)
}

#[inline]
fn compile(
&self,
coin: &dyn CoinContext,
input: Self::SigningInput<'_>,
signatures: Vec<SignatureBytes>,
public_keys: Vec<PublicKeyBytes>,
) -> Self::SigningOutput {
PolkadotCompiler::compile(coin, input, signatures, public_keys)
}
}
13 changes: 13 additions & 0 deletions rust/chains/tw_polkadot/src/extrinsic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use tw_hash::H32;
use tw_proto::Polkadot::Proto;

#[derive(Debug, Clone)]
pub struct Extrinsic;

impl Extrinsic {
pub fn from_input(input: Proto::SigningInput<'_>) -> Self {
// let x = H32::from(input.block_hash);

Self {}
}
}
9 changes: 9 additions & 0 deletions rust/chains/tw_polkadot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

pub mod address;
pub mod compiler;
pub mod entry;
mod extrinsic;
pub mod signer;
27 changes: 27 additions & 0 deletions rust/chains/tw_polkadot/src/signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::error::prelude::*;
use tw_coin_entry::signing_output_error;
use tw_proto::Polkadot::Proto;

pub struct PolkadotSigner;

impl PolkadotSigner {
pub fn sign(
coin: &dyn CoinContext,
input: Proto::SigningInput<'_>,
) -> Proto::SigningOutput<'static> {
Self::sign_impl(coin, input)
.unwrap_or_else(|e| signing_output_error!(Proto::SigningOutput, e))
}

fn sign_impl(
_coin: &dyn CoinContext,
_input: Proto::SigningInput<'_>,
) -> SigningResult<Proto::SigningOutput<'static>> {
todo!()
}
}
1 change: 1 addition & 0 deletions rust/tw_any_coin/tests/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod greenfield;
mod internet_computer;
mod native_evmos;
mod native_injective;
mod polkadot;
mod solana;
mod sui;
mod tbinance;
Expand Down
7 changes: 7 additions & 0 deletions rust/tw_any_coin/tests/chains/polkadot/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

mod polkadot_address;
mod polkadot_compile;
mod polkadot_sign;
28 changes: 28 additions & 0 deletions rust/tw_any_coin/tests/chains/polkadot/polkadot_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

use tw_any_coin::test_utils::address_utils::{
test_address_get_data, test_address_invalid, test_address_normalization, test_address_valid,
};
use tw_coin_registry::coin_type::CoinType;

#[test]
fn test_polkadot_address_normalization() {
test_address_normalization(CoinType::Polkadot, "DENORMALIZED", "EXPECTED");
}

#[test]
fn test_polkadot_address_is_valid() {
test_address_valid(CoinType::Polkadot, "VALID ADDRESS");
}

#[test]
fn test_polkadot_address_invalid() {
test_address_invalid(CoinType::Polkadot, "INVALID ADDRESS");
}

#[test]
fn test_polkadot_address_get_data() {
test_address_get_data(CoinType::Polkadot, "ADDRESS", "HEX(DATA)");
}
8 changes: 8 additions & 0 deletions rust/tw_any_coin/tests/chains/polkadot/polkadot_compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#[test]
fn test_polkadot_compile() {
todo!()
}
8 changes: 8 additions & 0 deletions rust/tw_any_coin/tests/chains/polkadot/polkadot_sign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#[test]
fn test_polkadot_sign() {
todo!()
}
1 change: 1 addition & 0 deletions rust/tw_any_coin/tests/coin_address_derivation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn test_coin_address_derivation() {
CoinType::Dydx => "dydx1ten42eesehw0ktddcp0fws7d3ycsqez3kaamq3",
CoinType::Solana => "5sn9QYhDaq61jLXJ8Li5BKqGL4DDMJQvU1rdN8XgVuwC",
CoinType::Sui => "0x01a5c6c1b74cec4fbd12b3e17252b83448136065afcdf24954dc3a9c26df4905",
CoinType::Polkadot => todo!(),
// end_of_coin_address_derivation_tests_marker_do_not_modify
_ => panic!("{:?} must be covered", coin),
};
Expand Down
1 change: 1 addition & 0 deletions rust/tw_coin_registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tw_memory = { path = "../tw_memory" }
tw_misc = { path = "../tw_misc" }
tw_native_evmos = { path = "../chains/tw_native_evmos" }
tw_native_injective = { path = "../chains/tw_native_injective" }
tw_polkadot = { path = "../chains/tw_polkadot" }
tw_ronin = { path = "../chains/tw_ronin" }
tw_solana = { path = "../chains/tw_solana" }
tw_sui = { path = "../chains/tw_sui" }
Expand Down
1 change: 1 addition & 0 deletions rust/tw_coin_registry/src/blockchain_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum BlockchainType {
InternetComputer,
NativeEvmos,
NativeInjective,
Polkadot,
Ronin,
Solana,
Sui,
Expand Down
Loading