tap-wasm 0.6.0

WebAssembly bindings for the Transaction Authorization Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#![allow(dead_code)] // wasm_bindgen_test functions are not detected as tests by clippy

use js_sys::{Array, Object, Reflect};
use tap_wasm::WasmTapAgent;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

/// Test that pack_message properly delegates to TapAgent
#[wasm_bindgen_test]
async fn test_pack_message_delegation() {
    // Create an agent
    let config = Object::new();
    let agent = WasmTapAgent::new(config.into()).expect("Failed to create agent");

    // Create a simple Transfer message
    let message = create_transfer_message(&agent.get_did());

    // Pack the message
    let pack_promise = agent.pack_message(message.into());
    let packed_result = JsFuture::from(pack_promise).await;

    assert!(packed_result.is_ok(), "Message packing should succeed");

    // Verify the result has the expected structure
    let packed = packed_result.unwrap();
    assert!(Reflect::has(&packed, &JsValue::from_str("message")).unwrap());
    assert!(Reflect::has(&packed, &JsValue::from_str("metadata")).unwrap());

    // Get the packed message string
    let message_js = Reflect::get(&packed, &JsValue::from_str("message")).unwrap();
    let message_str = message_js.as_string().expect("Message should be a string");

    // Verify it's not empty
    assert!(
        !message_str.is_empty(),
        "Packed message should not be empty"
    );

    // The message should be in General JWS JSON format (DIDComm v2)
    // Parse it as JSON to verify structure
    let parsed: serde_json::Value =
        serde_json::from_str(&message_str).expect("Packed message should be valid JSON");

    // For signed messages, it should have payload and signatures fields
    assert!(
        parsed.get("payload").is_some(),
        "JWS should have payload field"
    );
    assert!(
        parsed.get("signature").is_some() || parsed.get("signatures").is_some(),
        "JWS should have signature or signatures field"
    );
}

/// Test that unpack_message properly delegates to TapAgent
#[wasm_bindgen_test]
async fn test_unpack_message_delegation() {
    // Create a single agent that will both pack and unpack
    // This tests the basic pack/unpack delegation
    let config = Object::new();
    let agent = WasmTapAgent::new(config.into()).expect("Failed to create agent");

    // Create a message
    let message = create_transfer_message(&agent.get_did());

    // Pack the message
    let pack_promise = agent.pack_message(message.into());
    let packed_result = JsFuture::from(pack_promise)
        .await
        .expect("Packing should succeed");

    // Extract the packed message string
    let packed_message = Reflect::get(&packed_result, &JsValue::from_str("message"))
        .unwrap()
        .as_string()
        .expect("Should have message string");

    // Unpack the message with the same agent
    // For signed messages, any agent can unpack/verify if they have access to the sender's public key
    // Since we're using the same agent, it has access to its own public key
    let unpack_promise = agent.unpack_message(&packed_message, None);
    let unpacked_result = JsFuture::from(unpack_promise).await;

    // Log the error if unpacking failed
    if unpacked_result.is_err() {
        web_sys::console::error_1(&JsValue::from_str(&format!(
            "Failed to unpack message: {:?}",
            unpacked_result.as_ref().err()
        )));
    }

    assert!(unpacked_result.is_ok(), "Message unpacking should succeed");

    // Verify the unpacked message structure
    let unpacked = unpacked_result.unwrap();
    assert!(Reflect::has(&unpacked, &JsValue::from_str("id")).unwrap());
    assert!(Reflect::has(&unpacked, &JsValue::from_str("type")).unwrap());
    assert!(Reflect::has(&unpacked, &JsValue::from_str("from")).unwrap());
    assert!(Reflect::has(&unpacked, &JsValue::from_str("to")).unwrap());
    assert!(Reflect::has(&unpacked, &JsValue::from_str("body")).unwrap());
}

/// Test packing and unpacking Transfer messages
#[wasm_bindgen_test]
async fn test_transfer_message() {
    test_message_type("Transfer", create_transfer_message).await;
}

/// Test packing and unpacking Payment messages
#[wasm_bindgen_test]
async fn test_payment_message() {
    test_message_type("Payment", create_payment_message).await;
}

/// Test packing and unpacking Authorize messages
#[wasm_bindgen_test]
async fn test_authorize_message() {
    test_message_type("Authorize", create_authorize_message).await;
}

/// Test packing and unpacking Reject messages
#[wasm_bindgen_test]
async fn test_reject_message() {
    test_message_type("Reject", create_reject_message).await;
}

/// Test packing and unpacking Cancel messages
#[wasm_bindgen_test]
async fn test_cancel_message() {
    test_message_type("Cancel", create_cancel_message).await;
}

/// Test error handling for invalid message
#[wasm_bindgen_test]
async fn test_invalid_message_error() {
    let config = Object::new();
    let agent = WasmTapAgent::new(config.into()).expect("Failed to create agent");

    // Create an invalid message (missing required fields)
    let invalid_message = Object::new();
    Reflect::set(
        &invalid_message,
        &JsValue::from_str("type"),
        &JsValue::from_str("Invalid"),
    )
    .unwrap();

    // Try to pack the invalid message
    let pack_promise = agent.pack_message(invalid_message.into());
    let result = JsFuture::from(pack_promise).await;

    // Should fail
    assert!(result.is_err(), "Packing invalid message should fail");
}

/// Test error handling for corrupted packed message
#[wasm_bindgen_test]
async fn test_corrupted_message_error() {
    let config = Object::new();
    let agent = WasmTapAgent::new(config.into()).expect("Failed to create agent");

    // Try to unpack corrupted message
    let corrupted = "not.a.valid.jws.message";
    let unpack_promise = agent.unpack_message(corrupted, None);
    let result = JsFuture::from(unpack_promise).await;

    // Should fail
    assert!(result.is_err(), "Unpacking corrupted message should fail");
}

/// Test unpacking with expected type validation
#[wasm_bindgen_test]
async fn test_unpack_with_type_validation() {
    let config = Object::new();
    let agent = WasmTapAgent::new(config.into()).expect("Failed to create agent");

    // Create and pack a Transfer message
    let message = create_transfer_message(&agent.get_did());
    let pack_promise = agent.pack_message(message.into());
    let packed_result = JsFuture::from(pack_promise)
        .await
        .expect("Packing should succeed");

    let packed_message = Reflect::get(&packed_result, &JsValue::from_str("message"))
        .unwrap()
        .as_string()
        .unwrap();

    // Unpack expecting correct type - should succeed
    let unpack_promise = agent.unpack_message(
        &packed_message,
        Some("https://tap.rsvp/schema/1.0#Transfer".to_string()),
    );
    let result = JsFuture::from(unpack_promise).await;
    assert!(result.is_ok(), "Should unpack with correct expected type");

    // Unpack expecting wrong type - should fail
    let unpack_promise2 = agent.unpack_message(
        &packed_message,
        Some("https://tap.rsvp/schema/1.0#Payment".to_string()),
    );
    let result2 = JsFuture::from(unpack_promise2).await;
    assert!(result2.is_err(), "Should fail with wrong expected type");
}

// Helper function to test a specific message type
async fn test_message_type<F>(message_type: &str, create_fn: F)
where
    F: Fn(&str) -> Object,
{
    let config = Object::new();
    let agent = WasmTapAgent::new(config.into()).expect("Failed to create agent");

    // Create message
    let message = create_fn(&agent.get_did());

    // Pack the message
    let pack_promise = agent.pack_message(message.clone().into());
    let packed_result = JsFuture::from(pack_promise).await;
    assert!(
        packed_result.is_ok(),
        "Should pack {} message",
        message_type
    );

    // Get packed message
    let packed = packed_result.unwrap();
    let packed_str = Reflect::get(&packed, &JsValue::from_str("message"))
        .unwrap()
        .as_string()
        .unwrap();

    // Unpack the message
    let unpack_promise = agent.unpack_message(&packed_str, None);
    let unpacked_result = JsFuture::from(unpack_promise).await;
    assert!(
        unpacked_result.is_ok(),
        "Should unpack {} message",
        message_type
    );

    // Verify the type is preserved
    let unpacked = unpacked_result.unwrap();
    let unpacked_type = Reflect::get(&unpacked, &JsValue::from_str("type"))
        .unwrap()
        .as_string()
        .unwrap();

    let expected_type = format!("https://tap.rsvp/schema/1.0#{}", message_type);
    assert_eq!(
        unpacked_type, expected_type,
        "Message type should be preserved"
    );
}

// Helper functions to create different message types
fn create_transfer_message(from_did: &str) -> Object {
    let message = Object::new();
    Reflect::set(
        &message,
        &JsValue::from_str("id"),
        &JsValue::from_str("test-transfer-123"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("type"),
        &JsValue::from_str("https://tap.rsvp/schema/1.0#Transfer"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("from"),
        &JsValue::from_str(from_did),
    )
    .unwrap();

    let to_array = Array::new();
    to_array.push(&JsValue::from_str("did:key:z6MkRecipient"));
    Reflect::set(&message, &JsValue::from_str("to"), &to_array).unwrap();

    // Create Transfer body
    let body = Object::new();
    Reflect::set(
        &body,
        &JsValue::from_str("amount"),
        &JsValue::from_str("100.0"),
    )
    .unwrap();
    Reflect::set(
        &body,
        &JsValue::from_str("asset"),
        &JsValue::from_str("eip155:1/erc20:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
    )
    .unwrap();

    let originator = Object::new();
    Reflect::set(
        &originator,
        &JsValue::from_str("@id"),
        &JsValue::from_str(from_did),
    )
    .unwrap();
    Reflect::set(&body, &JsValue::from_str("originator"), &originator).unwrap();

    let beneficiary = Object::new();
    Reflect::set(
        &beneficiary,
        &JsValue::from_str("@id"),
        &JsValue::from_str("did:key:z6MkBeneficiary"),
    )
    .unwrap();
    Reflect::set(&body, &JsValue::from_str("beneficiary"), &beneficiary).unwrap();

    Reflect::set(&message, &JsValue::from_str("body"), &body).unwrap();
    message
}

fn create_payment_message(from_did: &str) -> Object {
    let message = Object::new();
    Reflect::set(
        &message,
        &JsValue::from_str("id"),
        &JsValue::from_str("test-payment-123"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("type"),
        &JsValue::from_str("https://tap.rsvp/schema/1.0#Payment"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("from"),
        &JsValue::from_str(from_did),
    )
    .unwrap();

    let to_array = Array::new();
    to_array.push(&JsValue::from_str("did:key:z6MkMerchant"));
    Reflect::set(&message, &JsValue::from_str("to"), &to_array).unwrap();

    // Create Payment body
    let body = Object::new();
    Reflect::set(
        &body,
        &JsValue::from_str("amount"),
        &JsValue::from_str("50.0"),
    )
    .unwrap();
    Reflect::set(
        &body,
        &JsValue::from_str("currency"),
        &JsValue::from_str("USD"),
    )
    .unwrap();

    let merchant = Object::new();
    Reflect::set(
        &merchant,
        &JsValue::from_str("@id"),
        &JsValue::from_str("did:key:z6MkMerchant"),
    )
    .unwrap();
    Reflect::set(&body, &JsValue::from_str("merchant"), &merchant).unwrap();

    Reflect::set(&message, &JsValue::from_str("body"), &body).unwrap();
    message
}

fn create_authorize_message(from_did: &str) -> Object {
    let message = Object::new();
    Reflect::set(
        &message,
        &JsValue::from_str("id"),
        &JsValue::from_str("test-authorize-123"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("type"),
        &JsValue::from_str("https://tap.rsvp/schema/1.0#Authorize"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("from"),
        &JsValue::from_str(from_did),
    )
    .unwrap();

    let to_array = Array::new();
    to_array.push(&JsValue::from_str("did:key:z6MkOriginator"));
    Reflect::set(&message, &JsValue::from_str("to"), &to_array).unwrap();

    // Create Authorize body
    let body = Object::new();
    Reflect::set(
        &body,
        &JsValue::from_str("transaction_id"),
        &JsValue::from_str("tx-123"),
    )
    .unwrap();

    Reflect::set(&message, &JsValue::from_str("body"), &body).unwrap();
    message
}

fn create_reject_message(from_did: &str) -> Object {
    let message = Object::new();
    Reflect::set(
        &message,
        &JsValue::from_str("id"),
        &JsValue::from_str("test-reject-123"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("type"),
        &JsValue::from_str("https://tap.rsvp/schema/1.0#Reject"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("from"),
        &JsValue::from_str(from_did),
    )
    .unwrap();

    let to_array = Array::new();
    to_array.push(&JsValue::from_str("did:key:z6MkOriginator"));
    Reflect::set(&message, &JsValue::from_str("to"), &to_array).unwrap();

    // Create Reject body
    let body = Object::new();
    Reflect::set(
        &body,
        &JsValue::from_str("transaction_id"),
        &JsValue::from_str("tx-123"),
    )
    .unwrap();
    Reflect::set(
        &body,
        &JsValue::from_str("reason"),
        &JsValue::from_str("Insufficient funds"),
    )
    .unwrap();

    Reflect::set(&message, &JsValue::from_str("body"), &body).unwrap();
    message
}

fn create_cancel_message(from_did: &str) -> Object {
    let message = Object::new();
    Reflect::set(
        &message,
        &JsValue::from_str("id"),
        &JsValue::from_str("test-cancel-123"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("type"),
        &JsValue::from_str("https://tap.rsvp/schema/1.0#Cancel"),
    )
    .unwrap();
    Reflect::set(
        &message,
        &JsValue::from_str("from"),
        &JsValue::from_str(from_did),
    )
    .unwrap();

    let to_array = Array::new();
    to_array.push(&JsValue::from_str("did:key:z6MkCounterparty"));
    Reflect::set(&message, &JsValue::from_str("to"), &to_array).unwrap();

    // Create Cancel body
    let body = Object::new();
    Reflect::set(
        &body,
        &JsValue::from_str("transaction_id"),
        &JsValue::from_str("tx-123"),
    )
    .unwrap();
    Reflect::set(
        &body,
        &JsValue::from_str("by"),
        &JsValue::from_str(from_did),
    )
    .unwrap();

    Reflect::set(&message, &JsValue::from_str("body"), &body).unwrap();
    message
}