Skip to content

Commit

Permalink
Merge branch 'main' into plew.external-id
Browse files Browse the repository at this point in the history
* main:
  Stricten, test, and bugfix http-server (#170)
  Version Packages (#167)
  Update web5/dids, web5/credentials, web5/crypto, web5/common to latest  (#177)
  Add exchange state machine (#168)
  Stricten http-client (#169)
  Make `pfiDid` required property in `TbdexHttpServer` (#166)
  • Loading branch information
phoebe-lew committed Feb 20, 2024
2 parents 4581339 + ebefc54 commit af022bf
Show file tree
Hide file tree
Showing 45 changed files with 2,274 additions and 1,079 deletions.
18 changes: 18 additions & 0 deletions packages/http-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# @tbdex/http-client

## 0.26.0

### Minor Changes

- eba04b8: Upgrade packages web5/[email protected], web5/[email protected], web5/[email protected], web5/[email protected]

- Deprecate did:ion and did:key in favour of did:jwk and did:dht
- Migrate from `PortableDid` to `BearerDid` with the latest @web5/dids upgrade
- Replaces dependency on `Web5Crypto` with `BearerDid` signer abstraction for signing operations

- 629f0c7: Stricten, tested, and remove untested code

### Patch Changes

- Updated dependencies [eba04b8]
- Updated dependencies [589edc3]
- @tbdex/protocol@0.26.0

## 0.25.0

### Minor Changes
Expand Down
10 changes: 5 additions & 5 deletions packages/http-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbdex/http-client",
"version": "0.25.0",
"version": "0.26.0",
"type": "module",
"description": "Http client that can be used to send tbdex messages",
"license": "Apache-2.0",
Expand Down Expand Up @@ -50,10 +50,10 @@
},
"dependencies": {
"@tbdex/protocol": "workspace:*",
"@web5/common": "0.2.2",
"@web5/credentials": "0.4.1",
"@web5/crypto": "0.2.4",
"@web5/dids": "0.2.4",
"@web5/common": "0.2.3",
"@web5/credentials": "0.4.2",
"@web5/crypto": "0.4.0",
"@web5/dids": "0.4.0",
"ms": "2.1.3",
"query-string": "8.1.0",
"typeid-js": "0.3.0"
Expand Down
90 changes: 21 additions & 69 deletions packages/http-client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JwtPayload } from '@web5/crypto'
import type { ErrorDetail } from './types.js'
import type { PortableDid } from '@web5/dids'
import type { DidDocument, BearerDid } from '@web5/dids'
import {
MessageModel,
Parser,
Expand Down Expand Up @@ -30,7 +30,7 @@ import ms from 'ms'
* @beta
*/
export type GenerateRequestTokenParams = {
requesterDid: PortableDid
requesterDid: BearerDid
pfiDid: string
}

Expand Down Expand Up @@ -93,56 +93,6 @@ export class TbdexHttpClient {
}
}

/**
* Discover PFIs that are anchored via did:ion. These have a type of "PFI" and an id of PFI.
* You can then query the endpoints for offerings.
*/
static async discoverPFIs() {
const BASE_URL = 'https://ion.tbd.engineering'
const DID_TYPE_ENDPOINT = '/didtype/1669'
const IDENTIFIER_PREFIX = '/identifiers/'

async function fetchDIDList() {
const response = await fetch(BASE_URL + DID_TYPE_ENDPOINT)
if (!response.ok) {
throw new Error('Failed to fetch DID list')
}
return await response.json()
}

async function fetchDIDData(did) {
console.log(BASE_URL + IDENTIFIER_PREFIX + did)
const response = await fetch(BASE_URL + IDENTIFIER_PREFIX + did)
if (!response.ok) {
throw new Error('Failed to fetch DID data for ' + did)
}
return await response.json()
}

const ids = await fetchDIDList()
const promises = ids.map(id => {
const ionDid = 'did:ion:' + id
return fetchDIDData(ionDid)
})
const didDataList = await Promise.all(promises)

const pfiServiceEndpoints = didDataList.reduce((results, didData) => {
const services = didData.didDocument.service
const pfiServices = services.filter(service => service.type === 'PFI')

if (pfiServices.length > 0) {
results.push({
did : didData.didDocument.id,
serviceEndpoint : pfiServices[0].serviceEndpoint
})
}

return results
}, [])

return pfiServiceEndpoints
}

/**
* gets offerings from the pfi provided
* @param opts - options
Expand Down Expand Up @@ -218,6 +168,7 @@ export class TbdexHttpClient {

}

// TODO: Wrap Message[] in Exchange object and verify each message
/**
* returns all exchanges created by requester
* @param _opts - options
Expand Down Expand Up @@ -268,21 +219,20 @@ export class TbdexHttpClient {
* @param did - the pfi's DID
*/
static async getPfiServiceEndpoint(did: string) {
let didDocument: DidDocument
try {
const didDocument = await resolveDid(did)
const [didService] = didUtils.getServices({ didDocument, type: 'PFI' })
didDocument = await resolveDid(did)
} catch (e) {
throw new InvalidDidError(e.message)
}

if (!didService?.serviceEndpoint) {
throw new MissingServiceEndpointError(`${did} has no PFI service entry`)
}
const [didService] = didUtils.getServices({ didDocument, type: 'PFI' })

return didService.serviceEndpoint
} catch (e) {
if (e instanceof MissingServiceEndpointError) {
throw e
}
throw new InvalidDidError(e)
if (!didService?.serviceEndpoint) {
throw new MissingServiceEndpointError(`${did} has no PFI service entry`)
}

return didService.serviceEndpoint
}

/**
Expand All @@ -301,19 +251,20 @@ export class TbdexHttpClient {
* @throws {@link RequestTokenSigningError} If an error occurs during the token generation.
*/
static async generateRequestToken(params: GenerateRequestTokenParams): Promise<string> {
const { pfiDid, requesterDid } = params
const now = Date.now()
const exp = (now + ms('1m'))

const jwtPayload: JwtPayload = {
aud : params.pfiDid,
iss : params.requesterDid.did,
aud : pfiDid,
iss : requesterDid.uri,
exp : Math.floor(exp / 1000),
iat : Math.floor(now / 1000),
jti : typeid().getSuffix()
}

try {
return await Jwt.sign({ signerDid: params.requesterDid, payload: jwtPayload })
return await Jwt.sign({ signerDid: requesterDid, payload: jwtPayload })
} catch(e) {
throw new RequestTokenSigningError({ message: e.message, cause: e })
}
Expand Down Expand Up @@ -356,7 +307,8 @@ export class TbdexHttpClient {
throw new RequestTokenAudienceMismatchError({ message: 'Request token contains invalid audience. Expected aud property to be PFI DID.' })
}

return requestTokenPayload.iss
// TODO: check iss against signer DID
return requestTokenPayload.iss!
}
}

Expand Down Expand Up @@ -400,7 +352,7 @@ export type GetExchangeOptions = {
/** the exchange you want to fetch */
exchangeId: string
/** the message author's DID */
did: PortableDid
did: BearerDid
}

/**
Expand All @@ -410,7 +362,7 @@ export type GetExchangeOptions = {
export type GetExchangesOptions = {
/** the DID of the PFI from whom you want to get offerings */
pfiDid: string
did: PortableDid,
did: BearerDid,
filter?: {
id: string | string[]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/http-client/src/errors/request-error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type RequestErrorParams = {

Check warning on line 1 in packages/http-client/src/errors/request-error.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: ae-missing-release-tag

"RequestErrorParams" is part of the package's API, but it is missing a release tag (`@alpha`, `@beta`, `@public`, or `@internal`)

Check warning on line 1 in packages/http-client/src/errors/request-error.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: ae-undocumented

Missing documentation for "RequestErrorParams".
message: string
recipientDid: string
url?: string
url: string
cause?: unknown
}

Expand Down
Loading

0 comments on commit af022bf

Please sign in to comment.