Skip to content

Commit

Permalink
Fix black hole (#142)
Browse files Browse the repository at this point in the history
* exemplar wip

* gitignore

* updated

* add dotenv

* updates

* fix black hole

* adding updates
  • Loading branch information
nitro-neal authored Oct 18, 2024
1 parent 83d762b commit 6bac761
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 64 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lazy_static = "1.5.0"
serde = { version = "1.0.193", features = ["derive", "rc"] }
serde_json = "1.0.108"
thiserror = "1.0.50"
http-std = { git = "https://github.com/TBD54566975/web5-rs", rev = "52bf9ca268a0fd17c68a8fc0c75d8c68d1cdb41f" }
web5 = { git = "https://github.com/TBD54566975/web5-rs", rev = "52bf9ca268a0fd17c68a8fc0c75d8c68d1cdb41f" }
web5_uniffi_wrapper = { git = "https://github.com/TBD54566975/web5-rs", rev = "52bf9ca268a0fd17c68a8fc0c75d8c68d1cdb41f" }
http-std = { git = "https://github.com/TBD54566975/web5-rs", rev = "6f6c336744ce0a35c5dc73e517be14f91c49b3b2" }
web5 = { git = "https://github.com/TBD54566975/web5-rs", rev = "6f6c336744ce0a35c5dc73e517be14f91c49b3b2" }
web5_uniffi_wrapper = { git = "https://github.com/TBD54566975/web5-rs", rev = "6f6c336744ce0a35c5dc73e517be14f91c49b3b2" }

14 changes: 9 additions & 5 deletions crates/tbdex/src/http_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub mod offerings;
use crate::errors::{Result, TbdexError};
use http_std::FetchOptions;
use serde::{de::DeserializeOwned, Serialize};
use std::time::{Duration, SystemTime};
use chrono::{Duration, Utc};
use std::time::SystemTime;
use uuid::Uuid;
use web5::{
dids::{
Expand All @@ -19,14 +20,17 @@ use web5::{
};

fn generate_access_token(pfi_did_uri: &str, bearer_did: &BearerDid) -> Result<String> {
let now = SystemTime::now();
let exp = now + Duration::from_secs(60);
let now = Utc::now();
let exp = now + Duration::seconds(60);

let now_system_time: SystemTime = now.into();
let exp_system_time: SystemTime = exp.into();

let claims = &JwtClaims {
aud: Some(vec![pfi_did_uri.to_string()]),
iss: Some(bearer_did.did.uri.clone()),
iat: Some(now),
exp: Some(exp),
iat: Some(now_system_time),
exp: Some(exp_system_time),
jti: Some(Uuid::new_v4().to_string()),
..Default::default()
};
Expand Down
3 changes: 1 addition & 2 deletions examples/hosted-wallet-ts/src/pfi/api/exchanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ export default function exchangesRouter(
throw new Error('Offering not found');
}

// TODO: This causes a WASM black hole exception, so we're commenting it out for now
// await rfq.verifyOfferingRequirements(offering);
await rfq.verifyOfferingRequirements(offering);

if (createExchangeRequestBody.replyTo) {
exchangesToReplyTo.set(rfq.metadata.exchangeId, createExchangeRequestBody.replyTo);
Expand Down
74 changes: 20 additions & 54 deletions examples/hosted-wallet-ts/src/wallet/happypathpollingflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import {
Quote,
Order,
OrderInstructions,
OrderStatus,
Close,
BearerDid,
getOfferings,
createExchange,
submitOrder,
GetExchangeResponseBody,
Message,
getExchange,
Exchange,
} from 'tbdex';
import axios from 'axios';

async function runHappyPathFlow(
pfiDidUri: string,
Expand Down Expand Up @@ -65,14 +62,8 @@ async function runHappyPathFlow(
let quote: Quote | undefined;
while (!quote) {
await delay(500);
const exchangeData = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
for (const element of exchangeData) {
if (element.metadata.kind === 'quote') {
console.log('Found quote!');
quote = Quote.fromJSONString(JSON.stringify(element));
break;
}
}
const exchange = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
quote = exchange.quote;
}
console.log(`Received quote with ID: ${quote.metadata.id}\n`);

Expand All @@ -89,35 +80,24 @@ async function runHappyPathFlow(
let orderInstructions: OrderInstructions | undefined;
while (!orderInstructions) {
await delay(500);
const exchangeData = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
for (const element of exchangeData) {
if (element.metadata.kind === 'orderinstructions') {
console.log('Found order instructions!');
orderInstructions = OrderInstructions.fromJSONString(
JSON.stringify(element)
);
break;
}
}
const exchange = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
orderInstructions = exchange.orderInstructions;
}
console.log(
`Received order instructions with ID: ${orderInstructions.metadata.id}\n`
);
console.log(`Received order instructions with ID: ${orderInstructions.metadata.id}\n`);

// Step 6: Wait for Order Status: PAYOUT_SETTLED
console.log('6. Waiting for Order Status: PAYOUT_SETTLED...');
let payoutSettled = false;
while (!payoutSettled) {
await delay(500);
const exchangeData = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
for (const element of exchangeData) {
if (element.metadata.kind === 'orderstatus') {
const orderStatus = OrderStatus.fromJSONString(JSON.stringify(element));
if (orderStatus.data.status === 'PAYOUT_SETTLED') {
console.log('Order status PAYOUT_SETTLED received.');
payoutSettled = true;
break;
}
const exchange = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
const orderStatuses = exchange.orderStatuses;

for(const orderStatus of orderStatuses) {
if (orderStatus.data.status === 'PAYOUT_SETTLED') {
console.log('Order status PAYOUT_SETTLED received.');
payoutSettled = true;
break;
}
}
}
Expand All @@ -128,14 +108,8 @@ async function runHappyPathFlow(
let close: Close | undefined;
while (!close) {
await delay(500);
const exchangeData = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
for (const element of exchangeData) {
if (element.metadata.kind === 'close') {
console.log('Found close message!');
close = Close.fromJSONString(JSON.stringify(element));
break;
}
}
const exchange = await fetchExchangeData(pfiDidUri, bearerDid, exchangeId);
close = exchange.close;
}

console.log(
Expand All @@ -158,21 +132,13 @@ async function fetchExchangeData(
pfiDidUri: string,
bearerDid: BearerDid,
exchangeId: string
): Promise<Message[]> {
): Promise<Exchange> {
try {
// TODO: This is not working and returning an unknwon WASM exception, to get around this I'm using axios instead of our library for getExchange
// const exchange = await getExchange(pfiDidUri, bearerDid, exchangeId);
// console.log(JSON.stringify(exchange));

const response = await axios.get('http://localhost:8082/exchanges/' + exchangeId);

const getExchangeResponseBody = GetExchangeResponseBody.fromJSONString(
JSON.stringify(response.data)
);
return getExchangeResponseBody.data;
const exchange = await getExchange(pfiDidUri, bearerDid, exchangeId);
return exchange;
} catch (error) {
console.error('Error fetching exchange data:', error);
return [];
return;
}
}

Expand Down

0 comments on commit 6bac761

Please sign in to comment.