@@ -54,6 +54,54 @@ class InMemorySenderPersister {
5454 }
5555}
5656
57+ class InMemoryReceiverPersisterAsync {
58+ id : number ;
59+ events : any [ ] ;
60+ closed : boolean ;
61+
62+ constructor ( id : number ) {
63+ this . id = id ;
64+ this . events = [ ] ;
65+ this . closed = false ;
66+ }
67+
68+ async save ( event : any ) : Promise < void > {
69+ this . events . push ( event ) ;
70+ }
71+
72+ async load ( ) : Promise < any [ ] > {
73+ return this . events ;
74+ }
75+
76+ async close ( ) : Promise < void > {
77+ this . closed = true ;
78+ }
79+ }
80+
81+ class InMemorySenderPersisterAsync {
82+ id : number ;
83+ events : any [ ] ;
84+ closed : boolean ;
85+
86+ constructor ( id : number ) {
87+ this . id = id ;
88+ this . events = [ ] ;
89+ this . closed = false ;
90+ }
91+
92+ async save ( event : any ) : Promise < void > {
93+ this . events . push ( event ) ;
94+ }
95+
96+ async load ( ) : Promise < any [ ] > {
97+ return this . events ;
98+ }
99+
100+ async close ( ) : Promise < void > {
101+ this . closed = true ;
102+ }
103+ }
104+
57105describe ( "URI tests" , ( ) => {
58106 test ( "URL encoded payjoin parameter" , ( ) => {
59107 const uri =
@@ -172,6 +220,76 @@ describe("Persistence tests", () => {
172220 } ) ;
173221} ) ;
174222
223+ describe ( "Async Persistence tests" , ( ) => {
224+ test ( "receiver async persistence" , async ( ) => {
225+ const persister = new InMemoryReceiverPersisterAsync ( 1 ) ;
226+ const address = "tb1q6d3a2w975yny0asuvd9a67ner4nks58ff0q8g4" ;
227+ const ohttpKeys = payjoin . OhttpKeys . decode (
228+ new Uint8Array ( [
229+ 0x01 , 0x00 , 0x16 , 0x04 , 0xba , 0x48 , 0xc4 , 0x9c , 0x3d , 0x4a ,
230+ 0x92 , 0xa3 , 0xad , 0x00 , 0xec , 0xc6 , 0x3a , 0x02 , 0x4d , 0xa1 ,
231+ 0x0c , 0xed , 0x02 , 0x18 , 0x0c , 0x73 , 0xec , 0x12 , 0xd8 , 0xa7 ,
232+ 0xad , 0x2c , 0xc9 , 0x1b , 0xb4 , 0x83 , 0x82 , 0x4f , 0xe2 , 0xbe ,
233+ 0xe8 , 0xd2 , 0x8b , 0xfe , 0x2e , 0xb2 , 0xfc , 0x64 , 0x53 , 0xbc ,
234+ 0x4d , 0x31 , 0xcd , 0x85 , 0x1e , 0x8a , 0x65 , 0x40 , 0xe8 , 0x6c ,
235+ 0x53 , 0x82 , 0xaf , 0x58 , 0x8d , 0x37 , 0x09 , 0x57 , 0x00 , 0x04 ,
236+ 0x00 , 0x01 , 0x00 , 0x03 ,
237+ ] ) . buffer ,
238+ ) ;
239+
240+ const builder = new payjoin . ReceiverBuilder (
241+ address ,
242+ "https://example.com" ,
243+ ohttpKeys ,
244+ ) ;
245+ await builder . build ( ) . saveAsync ( persister ) ;
246+
247+ const result = await payjoin . replayReceiverEventLogAsync ( persister ) ;
248+ const state = result . state ( ) ;
249+
250+ assert . strictEqual (
251+ state . tag ,
252+ "Initialized" ,
253+ "State should be Initialized" ,
254+ ) ;
255+ } ) ;
256+
257+ test ( "sender async persistence" , async ( ) => {
258+ const persister = new InMemoryReceiverPersisterAsync ( 1 ) ;
259+ const address = "2MuyMrZHkbHbfjudmKUy45dU4P17pjG2szK" ;
260+ const ohttpKeys = payjoin . OhttpKeys . decode (
261+ new Uint8Array ( [
262+ 0x01 , 0x00 , 0x16 , 0x04 , 0xba , 0x48 , 0xc4 , 0x9c , 0x3d , 0x4a ,
263+ 0x92 , 0xa3 , 0xad , 0x00 , 0xec , 0xc6 , 0x3a , 0x02 , 0x4d , 0xa1 ,
264+ 0x0c , 0xed , 0x02 , 0x18 , 0x0c , 0x73 , 0xec , 0x12 , 0xd8 , 0xa7 ,
265+ 0xad , 0x2c , 0xc9 , 0x1b , 0xb4 , 0x83 , 0x82 , 0x4f , 0xe2 , 0xbe ,
266+ 0xe8 , 0xd2 , 0x8b , 0xfe , 0x2e , 0xb2 , 0xfc , 0x64 , 0x53 , 0xbc ,
267+ 0x4d , 0x31 , 0xcd , 0x85 , 0x1e , 0x8a , 0x65 , 0x40 , 0xe8 , 0x6c ,
268+ 0x53 , 0x82 , 0xaf , 0x58 , 0x8d , 0x37 , 0x09 , 0x57 , 0x00 , 0x04 ,
269+ 0x00 , 0x01 , 0x00 , 0x03 ,
270+ ] ) . buffer ,
271+ ) ;
272+
273+ const receiver = await new payjoin . ReceiverBuilder (
274+ address ,
275+ "https://example.com" ,
276+ ohttpKeys ,
277+ )
278+ . build ( )
279+ . saveAsync ( persister ) ;
280+ const uri = receiver . pjUri ( ) ;
281+
282+ const senderPersister = new InMemorySenderPersisterAsync ( 1 ) ;
283+ const psbt =
284+ "cHNidP8BAHMCAAAAAY8nutGgJdyYGXWiBEb45Hoe9lWGbkxh/6bNiOJdCDuDAAAAAAD+////AtyVuAUAAAAAF6kUHehJ8GnSdBUOOv6ujXLrWmsJRDCHgIQeAAAAAAAXqRR3QJbbz0hnQ8IvQ0fptGn+votneofTAAAAAAEBIKgb1wUAAAAAF6kU3k4ekGHKWRNbA1rV5tR5kEVDVNCHAQcXFgAUx4pFclNVgo1WWAdN1SYNX8tphTABCGsCRzBEAiB8Q+A6dep+Rz92vhy26lT0AjZn4PRLi8Bf9qoB/CMk0wIgP/Rj2PWZ3gEjUkTlhDRNAQ0gXwTO7t9n+V14pZ6oljUBIQMVmsAaoNWHVMS02LfTSe0e388LNitPa1UQZyOihY+FFgABABYAFEb2Giu6c4KO5YW0pfw3lGp9jMUUAAA=" ;
285+ const withReplyKey = await new payjoin . SenderBuilder ( psbt , uri )
286+ . buildRecommended ( BigInt ( 1000 ) )
287+ . saveAsync ( senderPersister ) ;
288+
289+ assert . ok ( withReplyKey , "Sender should be created successfully" ) ;
290+ } ) ;
291+ } ) ;
292+
175293describe ( "Validation" , ( ) => {
176294 test ( "receiver builder rejects bad address" , ( ) => {
177295 assert . throws ( ( ) => {
0 commit comments