Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Add external_id field (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebe-lew authored Feb 20, 2024
1 parent ebefc54 commit 5e9a7a2
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-needles-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tbdex/protocol": patch
---

Add external id to message metadata
2 changes: 1 addition & 1 deletion packages/http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@web5/common": "0.2.3",
"@web5/credentials": "0.4.2",
"@web5/crypto": "0.4.0",
"@web5/dids": "0.4.0",
"@web5/dids": "0.4.1",
"ms": "2.1.3",
"query-string": "8.1.0",
"typeid-js": "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@tbdex/http-client": "workspace:*",
"@tbdex/protocol": "workspace:*",
"@web5/dids": "0.4.0",
"@web5/dids": "0.4.1",
"cors": "2.8.5",
"express": "4.18.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@web5/common": "0.2.3",
"@web5/credentials": "0.4.2",
"@web5/crypto": "0.4.0",
"@web5/dids": "0.4.0",
"@web5/dids": "0.4.1",
"ajv": "8.12.0",
"bignumber.js": "^9.1.2",
"canonicalize": "2.0.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/protocol/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ export abstract class Message {
return this.metadata.createdAt
}

/** the external ID */
get externalId() {
return this.metadata.externalId
}

/** Rfq type guard */
isRfq(): this is Rfq {
return this.metadata.kind === 'rfq'
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export type MessageMetadata = {
exchangeId: string
/** Message creation time. Expressed as ISO8601 */
createdAt: string
/** Arbitrary ID for the caller to associate with the message. Optional */
externalId?: string
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/protocol/tests/close.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { expect } from 'chai'

describe('Close', () => {
describe('create', () => {
it('sets exchangeId to whatever is passed in', () => {
it('sets exchangeId and externalId to whatever is passed in', () => {
const exchangeId = Message.generateId('rfq')
const closeMessage = Close.create({
metadata : { from: 'did:ex:alice', to: 'did:ex:pfi', exchangeId },
metadata : { from: 'did:ex:alice', to: 'did:ex:pfi', exchangeId, externalId: 'ext_1234' },
data : { reason: 'life is hard' }
})

expect(closeMessage.exchangeId).to.equal(exchangeId)
expect(closeMessage.externalId).to.equal('ext_1234')
})
})
})
10 changes: 5 additions & 5 deletions packages/protocol/tests/generate-test-vectors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VerifiableCredential } from '@web5/credentials'
import { Close, DevTools, Message, Order, OrderStatus, Quote, Rfq } from '../src/main.js'
import { Close, DevTools, Message, Order, OrderStatus, Quote, Resource, Rfq } from '../src/main.js'
import fs from 'fs'

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ const generateParseRfqVector = async () => {
const rfq = Rfq.create({
metadata : { from: aliceDid.uri, to: 'did:ex:pfi' },
data : {
offeringId : 'abcd123',
offeringId : Resource.generateId('offering'),
payinMethod : {
kind : 'DEBIT_CARD',
paymentDetails : {
Expand Down Expand Up @@ -96,7 +96,7 @@ const generateParseRfqVector = async () => {
const generateParseOrderVector = async () => {
const aliceDid = await DevTools.createDid()
const order = Order.create({
metadata: { from: aliceDid.uri, to: 'did:ex:pfi', exchangeId: 'abcd123' }
metadata: { from: aliceDid.uri, to: 'did:ex:pfi', exchangeId: Message.generateId('rfq'), externalId: 'ext_1234' }
})

await order.sign(aliceDid)
Expand All @@ -112,7 +112,7 @@ const generateParseOrderVector = async () => {
const generateParseCloseVector = async () => {
const pfiDid = await DevTools.createDid('dht')
const close = Close.create({
metadata : { from: pfiDid.uri, to: 'did:ex:alice', exchangeId: 'abcd123' },
metadata : { from: pfiDid.uri, to: 'did:ex:alice', exchangeId: Message.generateId('rfq') },
data : {
reason: 'The reason for closing the exchange'
}
Expand All @@ -131,7 +131,7 @@ const generateParseCloseVector = async () => {
const generateParseOrderStatusVector = async () => {
const pfiDid = await DevTools.createDid()
const orderStatus = OrderStatus.create({
metadata : { from: pfiDid.uri, to: 'did:ex:alice', exchangeId: 'abcd123' },
metadata : { from: pfiDid.uri, to: 'did:ex:alice', exchangeId: Message.generateId('rfq') },
data : {
orderStatus: 'wee'
}
Expand Down
25 changes: 19 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e9a7a2

Please sign in to comment.