1
+ use iop_sdk:: ciphersuite:: secp256k1:: Secp256k1 ;
1
2
use iop_sdk:: hydra:: txtype:: {
2
3
hyd_core:: Transaction , Aip29Transaction , CommonTransactionFields , OptionalTransactionFields ,
3
4
} ;
4
5
use iop_sdk:: vault:: hydra:: HydraSigner ;
5
6
use iop_sdk:: {
6
- ciphersuite:: secp256k1:: { hyd, SecpKeyId , SecpPrivateKey , SecpPublicKey } ,
7
+ ciphersuite:: secp256k1:: { hyd, SecpKeyId , SecpPrivateKey , SecpPublicKey } ,
7
8
vault:: { hydra, morpheus, Bip39 , Vault } ,
8
9
} ;
9
10
use iop_sdk:: {
@@ -16,18 +17,23 @@ use pyo3::prelude::*;
16
17
use serde:: { Deserialize , Serialize } ;
17
18
use std:: { error:: Error , fmt:: Display } ;
18
19
20
+
19
21
//use pyo3::exceptions;
20
22
use pyo3:: panic:: PanicException ;
21
23
//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 ;
23
26
use iop_sdk:: morpheus:: crypto:: { sign:: PrivateKeySigner , Signed } ;
24
27
use iop_sdk:: morpheus:: data:: { Did , WitnessStatement } ;
25
28
use iop_sdk:: multicipher:: MPublicKey ;
26
29
use iop_sdk:: vault:: morpheus:: Private ;
30
+ use std:: str:: FromStr ;
31
+ use iop_sdk:: ciphersuite:: secp256k1:: hyd:: { Testnet , Mainnet } ;
27
32
28
33
#[ derive( Debug ) ]
29
34
pub enum Err {
30
- CouldNotSendTrsansaction ,
35
+ CouldNotSendTransaction ,
36
+ CouldNotMatchNetwork
31
37
}
32
38
33
39
@@ -64,12 +70,33 @@ struct SendTxnsReq<'a> {
64
70
transactions : Vec < & ' a TransactionData > ,
65
71
}
66
72
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
+
67
93
#[ pyfunction]
68
94
#[ 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 > {
70
96
let mut vault =
71
97
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 ) ;
73
100
hydra:: Plugin :: init ( & mut vault, & password, & params) . expect ( "plugin could not be initialised" ) ;
74
101
let wallet = hydra:: Plugin :: get ( & vault, & params) . expect ( "wallet could not be initialized" ) ;
75
102
let admin = serde_json:: to_string_pretty ( & vault) . unwrap ( ) ;
@@ -102,22 +129,26 @@ pub fn get_new_acc_on_vault(
102
129
data : String ,
103
130
unlock_password : String ,
104
131
account : i32 ,
132
+ network : String
105
133
) -> PyResult < String > {
106
134
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) ;
108
137
Plugin :: create ( & mut vault, unlock_password, & params) . expect ( "vault could not be created" ) ;
109
138
let admin = serde_json:: to_string_pretty ( & vault) . unwrap ( ) ;
110
139
Ok ( admin)
111
140
}
112
141
113
142
#[ allow( unused) ]
114
- fn deserialize_hydra (
143
+ fn deserialize_hydra < ' a > (
115
144
data : String ,
116
145
unlock_password : String ,
117
146
account : i32 ,
147
+ network : & ' a str
118
148
) -> Result < ( SecpPrivateKey , SecpPublicKey , SecpKeyId ) , ( ) > {
119
149
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) ;
121
152
let wallet = hydra:: Plugin :: get ( & vault, & params) . expect ( "wallet could not be gotten" ) ;
122
153
let wallet_private = wallet
123
154
. private ( & unlock_password)
@@ -223,18 +254,20 @@ pub fn generate_transaction<'a>(
223
254
nonce : u64 ,
224
255
password : String ,
225
256
account : i32 ,
257
+ network : & ' a str
226
258
) -> PyResult < String > {
227
259
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 ( ) ;
230
263
let nonce = nonce + 1 ;
231
264
let optional = OptionalTransactionFields {
232
265
amount,
233
266
manual_fee : None ,
234
267
vendor_field : None ,
235
268
} ;
236
269
let common_fields = CommonTransactionFields {
237
- network : & hyd :: Testnet ,
270
+ network : & * network ,
238
271
sender_public_key : signer. 1 ,
239
272
nonce,
240
273
optional,
@@ -249,9 +282,10 @@ pub fn generate_transaction<'a>(
249
282
250
283
#[ pyfunction]
251
284
#[ 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 > {
253
286
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) ;
255
289
let wallet = hydra:: Plugin :: get ( & vault, & params) . expect ( "wallet could not be gotten" ) ;
256
290
let wallet_address = wallet. public ( ) . unwrap ( ) . key_mut ( 0 ) . unwrap ( ) . to_p2pkh_addr ( ) ;
257
291
Ok ( wallet_address)
@@ -287,4 +321,4 @@ fn iop_python(_py: Python, m: &PyModule) -> PyResult<()> {
287
321
288
322
289
323
Ok ( ( ) )
290
- }
324
+ }
0 commit comments