1
- use std:: str:: FromStr ;
2
- use std:: time:: Duration ;
1
+ use crate :: ohttp:: ClientResponse ;
3
2
4
- // use payjoin::bitcoin::psbt::Psbt;
5
- // use payjoin::bitcoin::FeeRate;
6
- // use payjoin::receive as pdk;
7
-
8
- use crate :: bitcoin_ffi:: Network ;
9
- // use crate::error::PayjoinError;
10
- use crate :: ohttp:: OhttpKeys ;
11
3
use crate :: Url ;
12
- use crate :: error:: PayjoinError ;
13
4
14
5
use {
15
6
crate :: utils:: result:: JsResult ,
@@ -74,11 +65,93 @@ impl From<super::Sender> for Sender {
74
65
#[ wasm_bindgen]
75
66
impl Sender {
76
67
/// Extract serialized Request and Context from a Payjoin Proposal.
77
- pub fn extract_v2 ( & self , ohttp_relay : String ) -> JsResult < Request > { //(Request, V2PostContext) > {
78
- let url = Url :: parse ( ohttp_relay) ?;
68
+ pub fn extract_v2 ( & self , ohttp_relay : String ) -> JsResult < RequestV2PostContext > {
69
+ let url = Url :: parse ( ohttp_relay) ?;
79
70
match self . 0 . extract_v2 ( url. into ( ) ) {
80
- Ok ( ( req, _ctx ) ) => Ok ( req . into ( ) ) , // (req.into(), ctx.into())),
71
+ Ok ( ( req, ctx ) ) => Ok ( RequestV2PostContext :: new ( req. into ( ) , ctx. into ( ) ) ) ,
81
72
Err ( e) => Err ( e. into ( ) ) ,
82
73
}
83
74
}
84
- }
75
+ }
76
+
77
+ #[ wasm_bindgen]
78
+ pub struct V2PostContext ( payjoin:: send:: v2:: V2PostContext ) ;
79
+
80
+ impl From < payjoin:: send:: v2:: V2PostContext > for V2PostContext {
81
+ fn from ( ctx : payjoin:: send:: v2:: V2PostContext ) -> Self {
82
+ Self ( ctx)
83
+ }
84
+ }
85
+
86
+ #[ wasm_bindgen]
87
+ impl V2PostContext {
88
+ // consumes self, so V2PostContext won't be available in js after calling this
89
+ pub fn process_response ( self , response : & [ u8 ] ) -> JsResult < V2GetContext > {
90
+ self . 0 . process_response ( response)
91
+ . map ( Into :: into)
92
+ . map_err ( Into :: into)
93
+ }
94
+ }
95
+
96
+ #[ wasm_bindgen]
97
+ pub struct V2GetContext ( payjoin:: send:: v2:: V2GetContext ) ;
98
+
99
+ impl From < payjoin:: send:: v2:: V2GetContext > for V2GetContext {
100
+ fn from ( ctx : payjoin:: send:: v2:: V2GetContext ) -> Self {
101
+ Self ( ctx)
102
+ }
103
+ }
104
+
105
+ #[ wasm_bindgen]
106
+ impl V2GetContext {
107
+ pub fn extract_req ( & self , ohttp_relay : String ) -> JsResult < RequestOhttpContext > {
108
+ self . 0 . extract_req ( payjoin:: Url :: parse ( & ohttp_relay) ?)
109
+ . map ( |( request, ctx) | RequestOhttpContext :: new ( request. into ( ) , ctx. into ( ) ) )
110
+ . map_err ( Into :: into)
111
+ }
112
+
113
+ pub fn process_response ( & self , response : & [ u8 ] , ohttp_ctx : & ClientResponse ) -> JsResult < Option < String > > {
114
+ self . 0 . process_response ( response, ohttp_ctx. into ( ) )
115
+ . map ( |opt_psbt| opt_psbt. map ( |psbt| psbt. to_string ( ) ) )
116
+ . map_err ( Into :: into)
117
+ }
118
+ }
119
+
120
+ #[ wasm_bindgen]
121
+ pub struct RequestOhttpContext ( Request , ClientResponse ) ;
122
+
123
+ impl RequestOhttpContext {
124
+ pub fn new ( request : Request , ohttp_ctx : ClientResponse ) -> Self {
125
+ Self ( request, ohttp_ctx)
126
+ }
127
+
128
+ pub fn request ( & self ) -> Request {
129
+ self . 0 . clone ( )
130
+ }
131
+
132
+ pub fn ohttp_ctx ( self ) -> ClientResponse {
133
+ self . 1
134
+ }
135
+ }
136
+
137
+ #[ wasm_bindgen]
138
+ pub struct RequestV2PostContext ( Request , V2PostContext ) ;
139
+
140
+ #[ wasm_bindgen]
141
+ impl RequestV2PostContext {
142
+ #[ wasm_bindgen( constructor) ]
143
+ pub fn new ( request : Request , context : V2PostContext ) -> Self {
144
+ Self ( request, context)
145
+ }
146
+
147
+ #[ wasm_bindgen( getter) ]
148
+ pub fn request ( & self ) -> Request {
149
+ self . 0 . clone ( ) // Assuming Request implements Clone
150
+ }
151
+
152
+ // consumes self, so RequestV2PostContext won't be available in js after getting context, however, using destructuring on the js end makes this seemless.
153
+ #[ wasm_bindgen( getter) ]
154
+ pub fn context ( self ) -> V2PostContext {
155
+ self . 1
156
+ }
157
+ }
0 commit comments