Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up unused and extraneous code #181

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions packages/http-server/tests/create-exchange.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ErrorDetail, Offering, Rfq } from '@tbdex/http-client'
import { ErrorDetail, Offering, Order, Rfq } from '@tbdex/http-client'
import type { Server } from 'http'

import { DevTools, RequestContext, TbdexHttpServer } from '../src/main.js'
import { expect } from 'chai'
import { InMemoryExchangesApi } from '../src/in-memory-exchanges-api.js'
import { InMemoryOfferingsApi } from '../src/in-memory-offerings-api.js'
import { BearerDid } from '@web5/dids'
import { BearerDid, DidDht, DidJwk } from '@web5/dids'
import Sinon from 'sinon'
import { Message } from '@tbdex/protocol'

describe('POST /exchanges/:exchangeId/rfq', () => {
let api: TbdexHttpServer
Expand Down Expand Up @@ -54,8 +55,8 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
})

it('returns a 400 if create exchange request contains a replyTo which is not a valid URL', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid('dht')
const aliceDid = await DidJwk.create()
const pfiDid = await DidDht.create()
const rfq = await DevTools.createRfq({ sender: aliceDid, receiver: pfiDid })
await rfq.sign(aliceDid)

Expand All @@ -75,10 +76,16 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
})

it('returns a 400 if request body is not a valid RFQ', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

const order = await DevTools.createOrder({ sender: aliceDid, receiver: pfiDid })
const order = Order.create({
metadata: {
from : aliceDid.uri,
to : pfiDid.uri,
exchangeId : Message.generateId('rfq')
}
})
await order.sign(aliceDid)

const resp = await fetch('http://localhost:8000/exchanges/123/rfq', {
Expand All @@ -97,8 +104,8 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
})

it('returns a 400 if request body if integrity check fails', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

const rfq = await DevTools.createRfq({ sender: aliceDid, receiver: pfiDid })
// deliberately omit rfq.sign(aliceDid)
Expand All @@ -119,8 +126,8 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
})

it('returns a 409 if request body if RFQ already exists', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

const rfq = await DevTools.createRfq({ sender: aliceDid, receiver: pfiDid })
await rfq.sign(aliceDid);
Expand All @@ -143,8 +150,8 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
})

it('returns a 400 if request body if offering doesnt exist', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

const offering = DevTools.createOffering()
// deliberately omit (api.offeringsApi as InMemoryOfferingsApi).addOffering(offering)
Expand Down Expand Up @@ -176,8 +183,8 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
})

it(`returns a 400 if request body if RFQ does not fulfill offering's requirements`, async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

// Add offering to api.offeringsApi
const offering = DevTools.createOffering()
Expand Down Expand Up @@ -220,8 +227,8 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
let rfq: Rfq

beforeEach(async () => {
aliceDid = await DevTools.createDid()
pfiDid = await DevTools.createDid()
aliceDid = await DidJwk.create()
pfiDid = await DidJwk.create()

// Add offering with no required claims to api.offeringsApi
offering = Offering.create({
Expand Down
4 changes: 2 additions & 2 deletions packages/http-server/tests/get-exchanges.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ describe('GET /exchanges', () => {
})

it('calls the callback if it is provided', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()
const rfq = await DevTools.createRfq({ sender: aliceDid, receiver: pfiDid })
await rfq.sign(aliceDid);
(api.exchangesApi as InMemoryExchangesApi).addMessage(rfq)
Expand Down
7 changes: 4 additions & 3 deletions packages/http-server/tests/get-offerings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GetOfferingsFilter, RequestContext, TbdexHttpServer } from '../src/main
import { expect } from 'chai'
import { InMemoryOfferingsApi } from '../src/in-memory-offerings-api.js'
import Sinon from 'sinon'
import { DidJwk } from '@web5/dids'

describe('GET /offerings', () => {
let api: TbdexHttpServer
Expand All @@ -21,7 +22,7 @@ describe('GET /offerings', () => {
})

it('returns an array of offerings', async () => {
const pfiDid = await DevTools.createDid()
const pfiDid = await DidJwk.create()
const offering = DevTools.createOffering()
await offering.sign(pfiDid);
(api.offeringsApi as InMemoryOfferingsApi).addOffering(offering)
Expand All @@ -36,7 +37,7 @@ describe('GET /offerings', () => {
})

it('constructs the filter from query params and passes it to OfferingsApi', async () => {
const pfiDid = await DevTools.createDid()
const pfiDid = await DidJwk.create()

// Add an offering to OfferingsApi
const offering = DevTools.createOffering()
Expand Down Expand Up @@ -69,7 +70,7 @@ describe('GET /offerings', () => {
})

it('calls the callback if it is provided', async () => {
const pfiDid = await DevTools.createDid()
const pfiDid = await DidJwk.create()
const offering = DevTools.createOffering()
await offering.sign(pfiDid);
(api.offeringsApi as InMemoryOfferingsApi).addOffering(offering)
Expand Down
39 changes: 20 additions & 19 deletions packages/http-server/tests/submit-close.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Close, DevTools, TbdexHttpServer } from '../src/main.js'
import { expect } from 'chai'
import { InMemoryExchangesApi } from '../src/in-memory-exchanges-api.js'
import Sinon from 'sinon'
import { DidJwk } from '@web5/dids'


describe('POST /exchanges/:exchangeId/close', () => {
Expand Down Expand Up @@ -53,8 +54,8 @@ describe('POST /exchanges/:exchangeId/close', () => {
})

it('returns a 400 if request body is not a valid close object', async () => {
const alice = await DevTools.createDid()
const rfq = DevTools.createRfq({
const alice = await DidJwk.create()
const rfq = await DevTools.createRfq({
sender: alice
})
const resp = await fetch('http://localhost:8000/exchanges/123/close', {
Expand All @@ -73,8 +74,8 @@ describe('POST /exchanges/:exchangeId/close', () => {
})

it(`returns a 404 if the exchange doesn't exist`, async () => {
const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()
const close = Close.create({
metadata: {
from : alice.uri,
Expand All @@ -100,8 +101,8 @@ describe('POST /exchanges/:exchangeId/close', () => {
})

it(`returns a 409 if close is not allowed based on the exchange's current state`, async () => {
const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()

const rfq = await DevTools.createRfq({
sender : alice,
Expand Down Expand Up @@ -147,8 +148,8 @@ describe('POST /exchanges/:exchangeId/close', () => {
})

it('returns a 400 if request body if integrity check fails', async () => {
const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()

const rfq = await DevTools.createRfq({
sender : alice,
Expand Down Expand Up @@ -188,8 +189,8 @@ describe('POST /exchanges/:exchangeId/close', () => {
it('returns a 202 if close is created by alice', async () => {
// scenario: Alice creates an exchange and submits a Close message

const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()

const rfq = await DevTools.createRfq({
sender : alice,
Expand Down Expand Up @@ -222,8 +223,8 @@ describe('POST /exchanges/:exchangeId/close', () => {
it('returns a 202 if close is created by pfi', async () => {
// scenario: Alice creates an exchange and PFI submits a Close message

const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()

const rfq = await DevTools.createRfq({
sender : alice,
Expand Down Expand Up @@ -255,9 +256,9 @@ describe('POST /exchanges/:exchangeId/close', () => {

it('returns a 400 if the close is created by neither alice nor pfi', async () => {

const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const imposter = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()
const imposter = await DidJwk.create()

const rfq = await DevTools.createRfq({
sender : alice,
Expand Down Expand Up @@ -296,8 +297,8 @@ describe('POST /exchanges/:exchangeId/close', () => {

describe('onSubmitClose callback', () => {
it('does not call the callback if the close is is not valid for the current exchange', async () => {
const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()

// Close message signed by Alice
const close = Close.create({
Expand All @@ -323,8 +324,8 @@ describe('POST /exchanges/:exchangeId/close', () => {
})

it('returns a 202 if the provided callback succeeds and passes correct arguments to callback', async () => {
const alice = await DevTools.createDid()
const pfi = await DevTools.createDid()
const alice = await DidJwk.create()
const pfi = await DidJwk.create()

const rfq = await DevTools.createRfq({
sender : alice,
Expand Down
29 changes: 15 additions & 14 deletions packages/http-server/tests/submit-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DevTools, Order, RequestContext, TbdexHttpServer } from '../src/main.js
import { expect } from 'chai'
import { InMemoryExchangesApi } from '../src/in-memory-exchanges-api.js'
import Sinon from 'sinon'
import { DidJwk } from '@web5/dids'


describe('POST /exchanges/:exchangeId/order', () => {
Expand Down Expand Up @@ -53,8 +54,8 @@ describe('POST /exchanges/:exchangeId/order', () => {
})

it(`returns a 404 if the exchange doesn't exist`, async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()
const order = Order.create({
metadata: {
from : aliceDid.uri,
Expand All @@ -81,8 +82,8 @@ describe('POST /exchanges/:exchangeId/order', () => {
it('returns a 400 if request body is not a valid order object', async () => {
// scenario: Send an Rfq to the submitOrder endpoint

const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

const rfq = await DevTools.createRfq({ sender: aliceDid, receiver: pfiDid })
await rfq.sign(aliceDid)
Expand All @@ -103,8 +104,8 @@ describe('POST /exchanges/:exchangeId/order', () => {
})

it('returns a 400 if request body if integrity check fails', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

const order = Order.create({
metadata: {
Expand All @@ -131,8 +132,8 @@ describe('POST /exchanges/:exchangeId/order', () => {
})

it(`returns a 409 if order is not allowed based on the exchange's current state`, async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()
const rfq = Rfq.create({
metadata: {
from : aliceDid.uri,
Expand Down Expand Up @@ -168,8 +169,8 @@ describe('POST /exchanges/:exchangeId/order', () => {
})

it(`returns a 400 if quote has expired`, async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

// Add an exchange which has a Quote that expired 10 seconds ago
const rfq = Rfq.create({
Expand Down Expand Up @@ -220,8 +221,8 @@ describe('POST /exchanges/:exchangeId/order', () => {
})

it('returns a 202 if order is accepted', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

// Add an exchange of Rfq and Quote to the exchangesApi
const rfq = Rfq.create({
Expand Down Expand Up @@ -268,8 +269,8 @@ describe('POST /exchanges/:exchangeId/order', () => {

describe('onSubmitClose callback', () => {
it('returns a 202 if the provided callback succeeds and passes correct arguments to callback', async () => {
const aliceDid = await DevTools.createDid()
const pfiDid = await DevTools.createDid()
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

// Add an exchange of Rfq and Quote to the exchangesApi
const rfq = Rfq.create({
Expand Down
24 changes: 1 addition & 23 deletions packages/protocol/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type {
JwsHeaderParams,
JwkParamsEcPublic,
JwkParamsOkpPublic,
PrivateKeyJwk,
PublicKeyJwk,
} from '@web5/crypto'

import { sha256 } from '@noble/hashes/sha256'
Expand Down Expand Up @@ -76,9 +74,7 @@ export class Crypto {
verificationMethodId = `${did.uri}${verificationMethodId}`
}



const jwsHeader: JwsHeader = { alg: signer.algorithm, kid: verificationMethodId }
const jwsHeader: JwsHeaderParams = { alg: signer.algorithm, kid: verificationMethodId }
const base64UrlEncodedJwsHeader = Convert.object(jwsHeader).toBase64Url()
const base64urlEncodedJwsPayload = Convert.uint8Array(payload).toBase64Url()

Expand Down Expand Up @@ -157,22 +153,4 @@ export class Crypto {
const [did] = jwsHeader.kid.split('#')
return did
}

/**
* Gets crv property from a PublicKeyJwk or PrivateKeyJwk. Returns empty string if crv is undefined.
*/
static extractNamedCurve(jwk: PrivateKeyJwk | PublicKeyJwk | undefined): string {
if (jwk && 'crv' in jwk) {
return jwk.crv
} else {
return ''
}
}
}

/**
* monkey patch of JwsHeaderParams to include `EdDSA` as a valid alg
* **NOTE**: Remove this once upstream `@web5/crypto` package is fixed
* @internal
*/
type JwsHeader = Omit<JwsHeaderParams, 'alg'> & { alg: JwsHeaderParams['alg'] | 'EdDSA' }
Loading
Loading