Skip to content

Commit e4d9db8

Browse files
committed
fix: allow dynamic choosing of networks
1 parent 9345eee commit e4d9db8

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

src/lib.rs

+48-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use iop_sdk::ciphersuite::secp256k1::Secp256k1;
12
use iop_sdk::hydra::txtype::{
23
hyd_core::Transaction, Aip29Transaction, CommonTransactionFields, OptionalTransactionFields,
34
};
45
use iop_sdk::vault::hydra::HydraSigner;
56
use iop_sdk::{
6-
ciphersuite::secp256k1::{hyd, SecpKeyId, SecpPrivateKey, SecpPublicKey},
7+
ciphersuite::secp256k1::{hyd,SecpKeyId, SecpPrivateKey, SecpPublicKey},
78
vault::{hydra, morpheus, Bip39, Vault},
89
};
910
use iop_sdk::{
@@ -16,18 +17,23 @@ use pyo3::prelude::*;
1617
use serde::{Deserialize, Serialize};
1718
use std::{error::Error, fmt::Display};
1819

20+
1921
//use pyo3::exceptions;
2022
use pyo3::panic::PanicException;
2123
//use iop_sdk::multicipher::MKeyId;
22-
//use iop_sdk::vault::{PrivateKey,Network,};
24+
//use iop_sdk::vault::{PrivateKey,Network as HydNetwork};
25+
use iop_sdk::vault::Network as HydNetwork;
2326
use iop_sdk::morpheus::crypto::{sign::PrivateKeySigner, Signed};
2427
use iop_sdk::morpheus::data::{Did, WitnessStatement};
2528
use iop_sdk::multicipher::MPublicKey;
2629
use iop_sdk::vault::morpheus::Private;
30+
use std::str::FromStr;
31+
use iop_sdk::ciphersuite::secp256k1::hyd::{Testnet,Mainnet};
2732

2833
#[derive(Debug)]
2934
pub enum Err {
30-
CouldNotSendTrsansaction,
35+
CouldNotSendTransaction,
36+
CouldNotMatchNetwork
3137
}
3238

3339

@@ -64,12 +70,33 @@ struct SendTxnsReq<'a> {
6470
transactions: Vec<&'a TransactionData>,
6571
}
6672

73+
74+
pub struct Network {
75+
network: Box< dyn HydNetwork<Suite=Secp256k1>>
76+
}
77+
78+
impl FromStr for Network {
79+
type Err = Err;
80+
fn from_str(s:&str) -> Result<Self, Self::Err> {
81+
match s.to_lowercase().as_str()
82+
{
83+
"testnet" => Ok(Self{network: Box::new(Testnet)}),
84+
"mainnet" => Ok(Self{network: Box::new(Mainnet)}),
85+
"devnet" => Ok(Self{network: Box::new(hyd::Devnet)}),
86+
_ => Err(Err::CouldNotMatchNetwork),
87+
}
88+
}
89+
}
90+
91+
92+
6793
#[pyfunction]
6894
#[allow(unused_variables)]
69-
pub fn get_hyd_vault(phrase: String, password: String) -> PyResult<String> {
95+
pub fn get_hyd_vault(phrase: String, password: String, network: String) -> PyResult<String> {
7096
let mut vault =
7197
Vault::create(None, phrase, &password, &password).expect("Vault could not be initialised");
72-
let params = hydra::Parameters::new(&hyd::Testnet, 0);
98+
let network = network.parse::<Network>().unwrap().network;
99+
let params = hydra::Parameters::new(&*network, 0);
73100
hydra::Plugin::init(&mut vault, &password, &params).expect("plugin could not be initialised");
74101
let wallet = hydra::Plugin::get(&vault, &params).expect("wallet could not be initialized");
75102
let admin = serde_json::to_string_pretty(&vault).unwrap();
@@ -102,22 +129,26 @@ pub fn get_new_acc_on_vault(
102129
data: String,
103130
unlock_password: String,
104131
account: i32,
132+
network: String
105133
) -> PyResult<String> {
106134
let mut vault: Vault = serde_json::from_str(&data).unwrap();
107-
let params = hydra::Parameters::new(&hyd::Testnet, account);
135+
let network = Network::from_str(&network).unwrap().network;
136+
let params = hydra::Parameters::new(&*network, account);
108137
Plugin::create(&mut vault, unlock_password, &params).expect("vault could not be created");
109138
let admin = serde_json::to_string_pretty(&vault).unwrap();
110139
Ok(admin)
111140
}
112141

113142
#[allow(unused)]
114-
fn deserialize_hydra(
143+
fn deserialize_hydra<'a>(
115144
data: String,
116145
unlock_password: String,
117146
account: i32,
147+
network: &'a str
118148
) -> Result<(SecpPrivateKey, SecpPublicKey, SecpKeyId), ()> {
119149
let vault: Vault = serde_json::from_str(&data).unwrap();
120-
let params = hydra::Parameters::new(&hyd::Testnet, account);
150+
let network = Network::from_str(network).unwrap().network;
151+
let params = hydra::Parameters::new(&*network, account);
121152
let wallet = hydra::Plugin::get(&vault, &params).expect("wallet could not be gotten");
122153
let wallet_private = wallet
123154
.private(&unlock_password)
@@ -223,18 +254,20 @@ pub fn generate_transaction<'a>(
223254
nonce: u64,
224255
password: String,
225256
account: i32,
257+
network: &'a str
226258
) -> PyResult<String> {
227259
let mut transactions = Vec::new();
228-
let signer = deserialize_hydra(data, password, account).unwrap();
229-
let recipient_id = SecpKeyId::from_p2pkh_addr(receiver.as_str(), &hyd::Testnet).unwrap();
260+
let signer = deserialize_hydra(data, password, account,network).unwrap();
261+
let network = network.parse::<Network>().unwrap().network;
262+
let recipient_id = SecpKeyId::from_p2pkh_addr(receiver.as_str(), &*network).unwrap();
230263
let nonce = nonce + 1;
231264
let optional = OptionalTransactionFields {
232265
amount,
233266
manual_fee: None,
234267
vendor_field: None,
235268
};
236269
let common_fields = CommonTransactionFields {
237-
network: &hyd::Testnet,
270+
network: &*network,
238271
sender_public_key: signer.1,
239272
nonce,
240273
optional,
@@ -249,9 +282,10 @@ pub fn generate_transaction<'a>(
249282

250283
#[pyfunction]
251284
#[allow(unused_variables)]
252-
pub fn get_wallet(data: String, account: i32) -> PyResult<String> {
285+
pub fn get_wallet<'a>(data: String, account: i32, network: &'a str) -> PyResult<String> {
253286
let vault: Vault = serde_json::from_str(&data).unwrap();
254-
let params = hydra::Parameters::new(&hyd::Testnet, account);
287+
let network = network.parse::<Network>().unwrap().network;
288+
let params = hydra::Parameters::new(&*network, account);
255289
let wallet = hydra::Plugin::get(&vault, &params).expect("wallet could not be gotten");
256290
let wallet_address = wallet.public().unwrap().key_mut(0).unwrap().to_p2pkh_addr();
257291
Ok(wallet_address)
@@ -287,4 +321,4 @@ fn iop_python(_py: Python, m: &PyModule) -> PyResult<()> {
287321

288322

289323
Ok(())
290-
}
324+
}

0 commit comments

Comments
 (0)