Skip to content

Commit

Permalink
Merge branch 'main' of github.com:TBD54566975/tbdex-js into jwt-creat…
Browse files Browse the repository at this point in the history
…e-verify
  • Loading branch information
jiyoontbd committed Jan 25, 2024
2 parents 7210a71 + 72fa522 commit 492d83f
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/integrity-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ jobs:
app-id: ${{ secrets.CICD_ROBOT_GITHUB_APP_ID }}
private-key: ${{ secrets.CICD_ROBOT_GITHUB_APP_PRIVATE_KEY }}
owner: TBD54566975
repositories: sdk-development
repositories: sdk-report-runner

- name: Trigger sdk-development report build
- name: Trigger sdk-report-runner report build
if: github.ref == 'refs/heads/main'
run: |
curl -L \
Expand All @@ -96,7 +96,7 @@ jobs:
-H "Content-Type: application/json" \
--fail \
--data '{"ref": "main"}' \
https://api.github.com/repos/TBD54566975/sdk-development/actions/workflows/build-report.yaml/dispatches
https://api.github.com/repos/TBD54566975/sdk-report-runner/actions/workflows/build-report.yaml/dispatches
env:
APP_TOKEN: ${{ steps.generate_token.outputs.token }}

Expand Down
5 changes: 3 additions & 2 deletions packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@web5/credentials": "0.4.1",
"@web5/crypto": "0.2.4",
"@web5/dids": "0.2.4",
"@types/node": "^7.0.5",
"ajv": "8.12.0",
"bignumber.js": "^9.1.2",
"canonicalize": "2.0.0",
Expand Down Expand Up @@ -87,9 +88,9 @@
"build:esm": "rimraf dist/esm dist/types && tsc",
"build:cjs": "rimraf dist/cjs && tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
"build:browser": "rimraf dist/browser.mjs dist/browser.js && node build/bundles.js",
"test:node": "rimraf tests/compiled && pnpm compile-validators && tsc -p tests/tsconfig.json && c8 mocha",
"test:node": "rimraf tests/compiled && pnpm compile-validators && tsc -p tests/tsconfig.json && mocha",
"test:browser": "pnpm compile-validators && karma start karma.conf.cjs",
"generate-test-vectors": "tsc -p tests/tsconfig.json && node tests/compiled/tests/generate-test-vectors.js",
"generate-test-vectors": "tsc -p tests/tsconfig.json && node tests/compiled/packages/protocol/tests/generate-test-vectors.js",
"build": "pnpm clean && pnpm compile-validators && pnpm build:esm && pnpm build:cjs && pnpm build:browser",
"lint": "eslint . --ext .ts --max-warnings 0",
"lint:fix": "eslint . --ext .ts --fix",
Expand Down
28 changes: 1 addition & 27 deletions packages/protocol/src/dev-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,6 @@ export type RfqOptions = {
receiver?: PortableDid
}


/**
* Options passed to {@link DevTools.createCredential}
* @beta
*/
export type CreateCredentialOptions = Omit<CreateJwtOptions, 'payload'> & {
/** the credential type (e.g. UniversityDegreeCredential) */
type: string
/** data to include in the credential */
data: Record<string, any>
}

/**
* Options passed to {@link DevTools.createJwt}
* @beta
*/
export type CreateJwtOptions = {
/** the thing to sign */
payload: any,
/** the JWT's subject (e.g. Alice's DID) */
subject: string
/** the JWT's issuer */
issuer: PortableDid
}

/**
* Utility functions for testing purposes
* @beta
Expand Down Expand Up @@ -214,7 +189,7 @@ export class DevTools {
* creates an example RfqData. Useful for testing purposes
*/
static async createRfqData(opts?: RfqOptions): Promise<RfqData> {
let vcJwt: any = ''
let vcJwt: string = ''

if (opts?.sender) {
const vc = await VerifiableCredential.create({
Expand All @@ -225,7 +200,6 @@ export class DevTools {
'beep': 'boop'
}
})

vcJwt = await vc.sign({ did: opts.sender })
}

Expand Down
122 changes: 105 additions & 17 deletions packages/protocol/tests/generate-test-vectors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { DevTools, Message, Offering, Quote, Rfq } from '../src/main.js'
import { DidKeyMethod } from '@web5/dids'
import { VerifiableCredential } from '@web5/credentials'
import { Close, DevTools, Message, Order, OrderStatus, Quote, Rfq } from '../src/main.js'
import fs from 'fs'

/**
* Use this util when you are modifying or adding a new test vector to `tbdex`.
*/


type TestVector = {
description: string
input: string
Expand All @@ -13,11 +14,8 @@ type TestVector = {
}

const generateParseOfferingVector = async () => {
const did = await DevTools.createDid()
const offering = Offering.create({
metadata : { from: did.did },
data : DevTools.createOfferingData()
})
const did = await DidKeyMethod.create()
const offering = DevTools.createOffering()

await offering.sign(did)

Expand All @@ -30,7 +28,7 @@ const generateParseOfferingVector = async () => {
}

const generateParseQuoteVector = async () => {
const did = await DevTools.createDid()
const did = await DidKeyMethod.create()
const quote = Quote.create({
metadata: {
exchangeId : Message.generateId('rfq'),
Expand All @@ -50,10 +48,40 @@ const generateParseQuoteVector = async () => {
}

const generateParseRfqVector = async () => {
const did = await DevTools.createDid()
const did = await DidKeyMethod.create()
const vc = await VerifiableCredential.create({
type : 'PuupuuCredential',
issuer : did.did,
subject : did.did,
data : {
'beep': 'boop'
}
})

const vcJwt = await vc.sign({ did })

const rfq = Rfq.create({
metadata : { from: did.did, to: 'did:ex:pfi' },
data : await DevTools.createRfqData()
data : {
offeringId : 'abcd123',
payinMethod : {
kind : 'DEBIT_CARD',
paymentDetails : {
'cardNumber' : '1234567890123456',
'expiryDate' : '12/22',
'cardHolderName' : 'Ephraim Bartholomew Winthrop',
'cvv' : '123'
}
},
payoutMethod: {
kind : 'BTC_ADDRESS',
paymentDetails : {
btcAddress: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
}
},
payinAmount : '20000.00',
claims : [vcJwt]
}
})

await rfq.sign(did)
Expand All @@ -66,23 +94,83 @@ const generateParseRfqVector = async () => {
}
}

const generateParseOrderVector = async () => {
const did = await DidKeyMethod.create()
const order = Order.create({
metadata: { from: did.did, to: 'did:ex:pfi', exchangeId: 'abcd123' }
})

await order.sign(did)

return {
description : 'Order parses from string',
input : JSON.stringify(order),
output : order.toJSON(),
error : false,
}
}

const generateParseCloseVector = async () => {
const did = await DidKeyMethod.create()
const close = Close.create({
metadata : { from: did.did, to: 'did:ex:pfi', exchangeId: 'abcd123' },
data : {
reason: 'The reason for closing the exchange'
}
})

await close.sign(did)

return {
description : 'Close parses from string',
input : JSON.stringify(close),
output : close.toJSON(),
error : false,
}
}

const generateParseOrderStatusVector = async () => {
const did = await DidKeyMethod.create()
const orderStatus = OrderStatus.create({
metadata : { from: did.did, to: 'did:ex:pfi', exchangeId: 'abcd123' },
data : {
orderStatus: 'wee'
}
})

await orderStatus.sign(did)

return {
description : 'Order Status parses from string',
input : JSON.stringify(orderStatus),
output : orderStatus.toJSON(),
error : false,
}
}

/**
* Generates TestVector objects and prints em out.
* From there, you can prettify it and paste it into the corresponding test vector file by hand.
* If you think this is janky, soz bro :/ it's the level of (in)convenience that works for me right now.
* Generates TestVector objects and overwrites the corresponding test vector files in `tbdex`.
*/
const overWriteTestVectors = async () => {

// Add more test vector generators as you need them. This is not a complete list.
const vectorFilePair: { filename: string, vector: TestVector }[] = [
{ filename: 'parse-offering.json', vector: await generateParseOfferingVector() },
{ filename: 'parse-quote.json', vector: await generateParseQuoteVector() },
{ filename: 'parse-rfq.json', vector: await generateParseRfqVector() }
{ filename: 'parse-close.json', vector: await generateParseCloseVector() },
{ filename: 'parse-rfq.json', vector: await generateParseRfqVector() },
{ filename: 'parse-order.json', vector: await generateParseOrderVector() },
{ filename: 'parse-orderstatus.json', vector: await generateParseOrderStatusVector() },
]

for (const { filename, vector } of vectorFilePair) {
console.log(filename)
console.log(JSON.stringify(vector, null, 2))
const fileLocation = `../../tbdex/hosted/test-vectors/protocol/vectors/${filename}`
console.log(`Overwriting ${fileLocation} with new test vector.`)
try {
fs.writeFileSync(fileLocation, JSON.stringify(vector, null, 2))
} catch (err) {
console.error(`Error writing file ${fileLocation}:`, err)
}
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/protocol/tests/test-vectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ describe('TbdexTestVectorsProtocol', () => {
expect(rfq.toJSON()).to.deep.eq(ParseRfq.output)
})
})

7 changes: 7 additions & 0 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 492d83f

Please sign in to comment.