Skip to content

Commit

Permalink
feat: polkadot support completion
Browse files Browse the repository at this point in the history
  • Loading branch information
aya015757881 committed Jan 2, 2024
1 parent fbfc6af commit c946d46
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 53 deletions.
22 changes: 8 additions & 14 deletions anychain-polkadot/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl<N: PolkadotNetwork> Address for PolkadotAddress<N> {
public_key: &Self::PublicKey,
_format: &Self::Format,
) -> Result<Self, anychain_core::AddressError> {
Self::from_payload(&hex::encode(&public_key.address_payload()))
Self::from_payload(&hex::encode(public_key.address_payload()))
}
}

impl<N: PolkadotNetwork> PolkadotAddress<N> {
pub fn from_payload(payload: &str) -> Result<Self, AddressError> {
let payload = hex::decode(payload).unwrap();
let payload = [vec![N::version()], payload].concat();
let payload = [vec![N::VERSION], payload].concat();

let ss_prefix = vec![0x53u8, 0x53, 0x35, 0x38, 0x50, 0x52, 0x45];

Expand All @@ -58,7 +58,7 @@ impl<N: PolkadotNetwork> FromStr for PolkadotAddress<N> {
type Err = TransactionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = s.from_base58()?;
if N::version() != bytes[0] {
if N::VERSION != bytes[0] {
return Err(TransactionError::Message(format!(
"Invalid version byte {} for polkadot network {}",
bytes[0],
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<N: PolkadotNetwork> Display for PolkadotAddress<N> {
mod tests {
use std::str::FromStr;

use crate::{Polkadot, PolkadotAddress, PolkadotFormat, PolkadotSecretKey, Substrate};
use crate::{PolkadotAddress, PolkadotFormat, PolkadotSecretKey, Westend};
use anychain_core::{hex, Address};
use ed25519_dalek_fiat::SecretKey;

Expand All @@ -102,37 +102,31 @@ mod tests {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32,
];
let h = hex::encode(&sk);
let h = hex::encode(sk);
println!("{}", h);

let sk = SecretKey::from_bytes(&sk).unwrap();
let sk = PolkadotSecretKey::Ed25519(sk);

let address =
PolkadotAddress::<Substrate>::from_secret_key(&sk, &PolkadotFormat::Standard).unwrap();
PolkadotAddress::<Westend>::from_secret_key(&sk, &PolkadotFormat::Standard).unwrap();

println!("address = {}", address);
}

#[test]
fn test_address_2() {
let hash = "8ee504148e75c34e8f051899b3c6e4241ff18dc1c9211260b6a6a434bedb485f";
let address = PolkadotAddress::<Substrate>::from_payload(hash).unwrap();
let address = PolkadotAddress::<Westend>::from_payload(hash).unwrap();
println!("address = {}", address);
}

#[test]
fn test_address_3() {
let addr = "5DoW9HHuqSfpf55Ux5pLdJbHFWvbngeg8Ynhub9DrdtxmZeV";
let addr = PolkadotAddress::<Substrate>::from_str(addr).unwrap();
let addr = PolkadotAddress::<Westend>::from_str(addr).unwrap();
let payload = addr.to_payload().unwrap();
let payload = hex::encode(payload);
println!("{}", payload);
}

#[test]
fn f() {
let s = "012eaaeadc3e26dcb6ce0479ff94b009fdb8f81d9c6cf43ba2a4595496564b7d10c8498f90139462d5ce300599a85335515ef970bf3645f7ced5e22181231d638c";
println!("len = {}", s.len());
}
}
7 changes: 4 additions & 3 deletions anychain-polkadot/src/network/kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ impl Network for Kusama {
}

impl PolkadotNetwork for Kusama {
fn version() -> u8 {
0x02
}
const VERSION: u8 = 0x02;
const PALLET_ASSET: u8 = 4;
const TRANSFER_ALLOW_DEATH: u8 = 0;
const TRANSFER_KEEP_ALIVE: u8 = 3;
}

impl FromStr for Kusama {
Expand Down
12 changes: 9 additions & 3 deletions anychain-polkadot/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ pub use polkadot::*;
mod kusama;
pub use kusama::*;

mod substrate;
pub use substrate::*;
mod westend;
pub use westend::*;

mod rococo;
pub use rococo::*;

use anychain_core::Network;

pub trait PolkadotNetwork: Network {
fn version() -> u8;
const VERSION: u8;
const PALLET_ASSET: u8;
const TRANSFER_ALLOW_DEATH: u8;
const TRANSFER_KEEP_ALIVE: u8;
}
7 changes: 4 additions & 3 deletions anychain-polkadot/src/network/polkadot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ impl Network for Polkadot {
}

impl PolkadotNetwork for Polkadot {
fn version() -> u8 {
0x00
}
const VERSION: u8 = 0x00;
const PALLET_ASSET: u8 = 5;
const TRANSFER_ALLOW_DEATH: u8 = 0;
const TRANSFER_KEEP_ALIVE: u8 = 3;
}

impl FromStr for Polkadot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ use crate::PolkadotNetwork;
use anychain_core::{Network, NetworkError};

#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub struct Substrate;
pub struct Rococo;

impl Network for Substrate {
const NAME: &'static str = "substrate";
impl Network for Rococo {
const NAME: &'static str = "rococo";
}

impl PolkadotNetwork for Substrate {
fn version() -> u8 {
0x2a
}
impl PolkadotNetwork for Rococo {
const VERSION: u8 = 0x2a;
const PALLET_ASSET: u8 = 4;
const TRANSFER_ALLOW_DEATH: u8 = 0;
const TRANSFER_KEEP_ALIVE: u8 = 3;
}

impl FromStr for Substrate {
impl FromStr for Rococo {
type Err = NetworkError;
fn from_str(_s: &str) -> Result<Self, Self::Err> {
todo!()
}
}

impl Display for Substrate {
impl Display for Rococo {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
Expand Down
31 changes: 31 additions & 0 deletions anychain-polkadot/src/network/westend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{fmt::Display, str::FromStr};

use crate::PolkadotNetwork;
use anychain_core::{Network, NetworkError};

#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub struct Westend;

impl Network for Westend {
const NAME: &'static str = "westend";
}

impl PolkadotNetwork for Westend {
const VERSION: u8 = 0x2a;
const PALLET_ASSET: u8 = 4;
const TRANSFER_ALLOW_DEATH: u8 = 0;
const TRANSFER_KEEP_ALIVE: u8 = 3;
}

impl FromStr for Westend {
type Err = NetworkError;
fn from_str(_s: &str) -> Result<Self, Self::Err> {
todo!()
}
}

impl Display for Westend {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
2 changes: 1 addition & 1 deletion anychain-polkadot/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<N: PolkadotNetwork> PublicKey for PolkadotPublicKey<N> {
&self,
format: &Self::Format,
) -> Result<Self::Address, anychain_core::AddressError> {
Ok(Self::Address::from_public_key(self, format)?)
Self::Address::from_public_key(self, format)
}
}

Expand Down
Loading

0 comments on commit c946d46

Please sign in to comment.