-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rfq-private-salt
* main: Refresh offerings test vector (#218) updating parse close vector with success field (#220) Add balance resource and todos (#212)
- Loading branch information
Showing
17 changed files
with
664 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Balance } from '@tbdex/protocol' | ||
import { BalancesApi } from './types.js' | ||
|
||
/** | ||
* An in-memory implementation of {@link BalancesApi} for example and default purposes. | ||
* InMemoryBalancesApi has additional methods {@link InMemoryBalancesApi.addBalance} | ||
* {@link InMemoryBalancesApi.clearRequesterBalances} | ||
* and {@link InMemoryBalancesApi.clearBalances} | ||
*/ | ||
export class InMemoryBalancesApi implements BalancesApi { | ||
/** Map from requester DID to list of Balances */ | ||
balancesMap: Map<string, Balance[]> | ||
|
||
constructor() { | ||
this.balancesMap = new Map<string, Balance[]>() | ||
} | ||
|
||
/** | ||
* Add a single balance resource | ||
* @param balance - Balance to be added to the {@link balancesMap} | ||
*/ | ||
addBalance(opts: {requesterDid: string, balance: Balance}): void { | ||
let requesterBalances = this.balancesMap.get(opts.requesterDid) ?? [] | ||
requesterBalances.push(opts.balance) | ||
this.balancesMap.set(opts.requesterDid, requesterBalances) | ||
} | ||
|
||
/** | ||
* Clear existing list of balances for a single requester | ||
*/ | ||
clearRequesterBalances(opts: {requesterDid: string}): void { | ||
this.balancesMap.delete(opts.requesterDid) | ||
} | ||
|
||
/** | ||
* Clear existing list of balances | ||
*/ | ||
clearAllBalances(): void { | ||
this.balancesMap.clear() | ||
} | ||
|
||
/** | ||
* | ||
* @returns A list of balances | ||
*/ | ||
async getBalances(opts?: { requesterDid: string }): Promise<Balance[]> { | ||
if (opts === undefined || opts.requesterDid === undefined) { | ||
// In production, this should probably return an empty list. | ||
// For example and testing purposes, we return all balances. | ||
|
||
return Array.from(this.balancesMap.values()).flatMap(balances => balances) | ||
} | ||
|
||
return this.balancesMap.get(opts.requesterDid) ?? [] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Request, Response } from 'express' | ||
import type { GetBalancesCallback, BalancesApi } from '../types.js' | ||
import { TbdexHttpClient } from '@tbdex/http-client' | ||
|
||
type GetBalancesOpts = { | ||
callback?: GetBalancesCallback | ||
balancesApi: BalancesApi, | ||
pfiDid: string | ||
} | ||
|
||
export async function getBalances(request: Request, response: Response, opts: GetBalancesOpts): Promise<void> { | ||
const { callback, balancesApi, pfiDid } = opts | ||
|
||
const authzHeader = request.headers['authorization'] | ||
if (!authzHeader) { | ||
response.status(401).json({ errors: [{ detail: 'Authorization header required' }] }) | ||
return | ||
} | ||
|
||
const [_, requestToken] = authzHeader.split('Bearer ') | ||
|
||
if (!requestToken) { | ||
response.status(401).json({ errors: [{ detail: 'Malformed Authorization header. Expected: Bearer TOKEN_HERE' }] }) | ||
return | ||
} | ||
|
||
let requesterDid: string | ||
try { | ||
requesterDid = await TbdexHttpClient.verifyRequestToken({ requestToken: requestToken, pfiDid }) | ||
} catch(e) { | ||
response.status(401).json({ errors: [{ detail: `Malformed Authorization header: ${e}` }] }) | ||
return | ||
} | ||
|
||
const balances = await balancesApi.getBalances({ requesterDid }) | ||
|
||
if (callback) { | ||
// TODO: figure out what to do with callback result. should we pass through the offerings we've fetched | ||
// and allow the callback to modify what's returned? (issue #11) | ||
await callback({ request, response }) | ||
} | ||
|
||
response.status(200).json({ data: balances }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.