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

test vector changes #152

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
33 changes: 4 additions & 29 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,18 +189,18 @@ export class DevTools {
* creates an example RfqData. Useful for testing purposes
*/
static async createRfqData(opts?: RfqOptions): Promise<RfqData> {
let credential: any = ''
let vcJwt: string = ''

if (opts?.sender) {
const signedCredential = VerifiableCredential.create({
const vc = await VerifiableCredential.create({
type : 'YoloCredential',
issuer : opts.sender.did,
subject : opts.sender.did,
data : {
'beep': 'boop'
}
})
credential = signedCredential
vcJwt = await vc.sign({ did: opts.sender })
}

return {
Expand All @@ -246,7 +221,7 @@ export class DevTools {
}
},
payinAmount : '200.00',
claims : [credential]
claims : [vcJwt]
}
}
}
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.

Loading