@@ -5,37 +5,52 @@ use crate::error::WasmCredentialClientError;
5
5
use crate :: opts:: CredentialClientOpts ;
6
6
use js_sys:: Promise ;
7
7
use nym_credential_storage:: ephemeral_storage:: EphemeralCredentialStorage ;
8
- use nym_credential_storage:: models:: StoredIssuedCredential ;
8
+ use nym_credential_storage:: storage:: Storage ;
9
+ use nym_credentials_interface:: TicketType ;
9
10
use nym_network_defaults:: NymNetworkDetails ;
10
- use nym_validator_client :: nyxd :: { Config , CosmWasmCoin } ;
11
- use nym_validator_client:: DirectSigningReqwestRpcNyxdClient ;
11
+ use crate :: bandwidth :: BandwidthAcquireClient ;
12
+ use nym_validator_client:: nyxd :: CosmWasmCoin ;
12
13
use serde:: { Deserialize , Serialize } ;
13
14
use tsify:: Tsify ;
14
15
use wasm_bindgen:: prelude:: * ;
15
16
use wasm_bindgen_futures:: future_to_promise;
16
- use wasm_utils:: console_log;
17
17
use wasm_utils:: error:: PromisableResult ;
18
18
use zeroize:: { Zeroize , ZeroizeOnDrop } ;
19
+ use nym_credentials:: ecash:: bandwidth:: serialiser:: VersionedSerialise ;
19
20
20
21
#[ wasm_bindgen( js_name = acquireCredential) ]
21
- pub fn acquire_credential ( mnemonic : String , amount : String , opts : CredentialClientOpts ) -> Promise {
22
+ pub fn acquire_credential (
23
+ mnemonic : String ,
24
+ amount : String ,
25
+ client_id_private_key_base58 : String ,
26
+ ticketbook_type : TicketType ,
27
+ opts : CredentialClientOpts ,
28
+ ) -> Promise {
22
29
future_to_promise ( async move {
23
- acquire_credential_async ( mnemonic, amount, opts)
24
- . await
25
- . map ( |credential| {
26
- serde_wasm_bindgen:: to_value ( & credential) . expect ( "this serialization can't fail" )
27
- } )
28
- . into_promise_result ( )
30
+ acquire_credential_async (
31
+ mnemonic,
32
+ amount,
33
+ client_id_private_key_base58,
34
+ ticketbook_type,
35
+ opts,
36
+ )
37
+ . await
38
+ . map ( |credential| {
39
+ serde_wasm_bindgen:: to_value ( & credential) . expect ( "this serialization can't fail" )
40
+ } )
41
+ . into_promise_result ( )
29
42
} )
30
43
}
31
44
32
45
async fn acquire_credential_async (
33
46
mnemonic : String ,
34
47
amount : String ,
48
+ client_id_private_key_base58 : String ,
49
+ ticketbook_type : TicketType ,
35
50
opts : CredentialClientOpts ,
36
51
) -> Result < WasmIssuedCredential , WasmCredentialClientError > {
37
- // start by parsing mnemonic so that we could immediately move it into a Zeroizing wrapper
38
- let mnemonic = crate :: helpers:: parse_mnemonic ( mnemonic) ?;
52
+ // // start by parsing mnemonic so that we could immediately move it into a Zeroizing wrapper
53
+ // let mnemonic = crate::helpers::parse_mnemonic(mnemonic)?;
39
54
40
55
// why are we parsing into CosmWasmCoin and not "our" Coin?
41
56
// simple. because it has the nicest 'FromStr' impl
@@ -61,44 +76,64 @@ async fn acquire_credential_async(
61
76
}
62
77
} ;
63
78
64
- let config = Config :: try_from_nym_network_details ( & network) ?;
65
-
66
- // just get the first nyxd endpoint
67
- let nyxd_endpoint = network
68
- . endpoints
69
- . get ( 0 )
70
- . ok_or ( WasmCredentialClientError :: NoNyxdEndpoints ) ?
71
- . try_nyxd_url ( ) ?;
72
-
73
- let client = DirectSigningReqwestRpcNyxdClient :: connect_reqwest_with_mnemonic (
74
- config,
75
- nyxd_endpoint,
76
- mnemonic,
77
- ) ;
78
-
79
- console_log ! ( "starting the deposit..." ) ;
80
- let deposit_state = nym_bandwidth_controller:: acquire:: deposit ( & client, amount) . await ?;
81
- let blinded_serial = deposit_state. voucher . blinded_serial_number_bs58 ( ) ;
82
- console_log ! (
83
- "obtained bandwidth voucher with the following blinded serial number: {blinded_serial}"
84
- ) ;
85
-
86
- // TODO: use proper persistent storage here. probably indexeddb like we have for our 'normal' wasm client
87
79
let ephemeral_storage = EphemeralCredentialStorage :: default ( ) ;
88
80
89
- // store credential in the ephemeral storage...
90
- nym_bandwidth_controller:: acquire:: get_bandwidth_voucher (
91
- & deposit_state,
92
- & client,
81
+ let client = BandwidthAcquireClient :: new (
82
+ network,
83
+ mnemonic,
93
84
& ephemeral_storage,
94
- )
95
- . await ?;
96
-
97
- // and immediately get it out!
98
- let mut credentials = ephemeral_storage. take_credentials ( ) . await ;
99
- let cred = credentials. pop ( ) . expect ( "we just got a credential issued" ) ;
100
-
101
- Ok ( cred. into ( ) )
85
+ client_id_private_key_base58,
86
+ ticketbook_type,
87
+ ) ?;
88
+
89
+ client. acquire ( ) . await ?;
90
+
91
+ // let config = Config::try_from_nym_network_details(&network)?;
92
+ //
93
+ // // just get the first nyxd endpoint
94
+ // let nyxd_endpoint = network
95
+ // .endpoints
96
+ // .get(0)
97
+ // .ok_or(WasmCredentialClientError::NoNyxdEndpoints)?
98
+ // .try_nyxd_url()?;
99
+ //
100
+ // let client = DirectSigningReqwestRpcNyxdClient::connect_reqwest_with_mnemonic(
101
+ // config,
102
+ // nyxd_endpoint,
103
+ // mnemonic,
104
+ // );
105
+ //
106
+ // console_log!("starting the deposit...");
107
+ // let deposit_state = nym_bandwidth_controller::acquire::deposit(&client, amount).await?;
108
+ // let blinded_serial = deposit_state.voucher.blinded_serial_number_bs58();
109
+ // console_log!(
110
+ // "obtained bandwidth voucher with the following blinded serial number: {blinded_serial}"
111
+ // );
112
+ //
113
+ // // TODO: use proper persistent storage here. probably indexeddb like we have for our 'normal' wasm client
114
+ // let ephemeral_storage = EphemeralCredentialStorage::default();
115
+ //
116
+ // // store credential in the ephemeral storage...
117
+ // nym_bandwidth_controller::acquire::get_bandwidth_voucher(
118
+ // &deposit_state,
119
+ // &client,
120
+ // &ephemeral_storage,
121
+ // )
122
+ // .await?;
123
+ //
124
+
125
+ match ephemeral_storage. get_next_unspent_usable_ticketbook ( 1u32 ) . await ? {
126
+ Some ( ticket_book) => {
127
+ let serialized = ticket_book. ticketbook . pack ( ) ;
128
+
129
+ Ok ( WasmIssuedCredential {
130
+ serialization_revision : serialized. revision ,
131
+ credential_data : serialized. data ,
132
+ ticketbook_type : format ! ( "{}" , ticketbook_type) ,
133
+ } )
134
+ } ,
135
+ None => Err ( WasmCredentialClientError :: TicketbookCredentialStoreIsNone ) ,
136
+ }
102
137
}
103
138
104
139
#[ derive( Tsify , Debug , Clone , Serialize , Deserialize , Zeroize , ZeroizeOnDrop ) ]
@@ -107,17 +142,17 @@ async fn acquire_credential_async(
107
142
pub struct WasmIssuedCredential {
108
143
pub serialization_revision : u8 ,
109
144
pub credential_data : Vec < u8 > ,
110
- pub credential_type : String ,
111
- pub epoch_id : u32 ,
145
+ pub ticketbook_type : String ,
146
+ // pub epoch_id: u32,
112
147
}
113
148
114
- impl From < StoredIssuedCredential > for WasmIssuedCredential {
115
- fn from ( value : StoredIssuedCredential ) -> Self {
116
- WasmIssuedCredential {
117
- serialization_revision : value. serialization_revision ,
118
- credential_data : value. credential_data . clone ( ) ,
119
- credential_type : value. credential_type . clone ( ) ,
120
- epoch_id : value. epoch_id ,
121
- }
122
- }
123
- }
149
+ // impl From<StoredIssuedCredential> for WasmIssuedCredential {
150
+ // fn from(value: StoredIssuedCredential) -> Self {
151
+ // WasmIssuedCredential {
152
+ // serialization_revision: value.serialization_revision,
153
+ // credential_data: value.credential_data.clone(),
154
+ // ticketbook_type : value.ticketbook_type .clone(),
155
+ // // epoch_id: value.epoch_id,
156
+ // }
157
+ // }
158
+ // }
0 commit comments