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

Commit

Permalink
feat: add validation (#4)
Browse files Browse the repository at this point in the history
* add `tbdex` submodule

* add `json_schema` and `path` deps

* remove unused pattern

* generate id using `TypeId`

* use `JsonSchema`

* add `Validator`

* progress on `verifyOfferingRequirements()`

* fix validation errors

* remove print

* fix serialization issue with `requiredPaymentDetails` json schema

* add `decimal` dep

* add `orElse` to `firstWhere()`

* add more exception handling to `verifyOfferingRequirements()`

* change description in `having()`

* add todo about explicit parsing

* update `getOffering()` and `getRfq()` to test `verifyOfferingRequirements()`

* add tests for `verifyOfferingRequirements()`

* refactor `initialize()`

* use `json` as variable name for `jsonEncode()`

* update `tbdex` submodule

* use private instance to initialize validator

* add just command to copy json-schemas from tbdex submodule

* remove initialize validator

* check in json schemas from tbdex submodule

* update path to new json-schemas folder

* add get, test, and analyze

* delete issue template

* add ci workflow

* update exception message

* use `@` for all commands

* update ci steps

* add dart to hermit

* fix property access on `dynamic`

* add publish to none

* move private constructor

* fix metadata casts

* remove unused constructor
  • Loading branch information
ethanwlee authored Apr 1, 2024
1 parent bf615e9 commit 3130a86
Show file tree
Hide file tree
Showing 40 changed files with 849 additions and 179 deletions.
31 changes: 0 additions & 31 deletions .github/ISSUE_TEMPLATE/bug-report.md

This file was deleted.

4 changes: 0 additions & 4 deletions .github/ISSUE_TEMPLATE/config.yml

This file was deleted.

30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Init Hermit
uses: cashapp/activate-hermit@v1
with:
cache: true

- name: Install Dependencies
run: just get

- name: Run Static Analysis
run: just analyze

- name: Run Tests
run: just test
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tbdex"]
path = tbdex
url = [email protected]:TBD54566975/tbdex.git
12 changes: 12 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
schemas:
@git submodule update --init --recursive
@cp -r tbdex/hosted/json-schemas lib/src/protocol

get:
@dart pub get

test:
@dart test

analyze:
@dart analyze
1 change: 1 addition & 0 deletions bin/.dart-3.3.3.pkg
1 change: 1 addition & 0 deletions bin/.just-1.25.2.pkg
1 change: 1 addition & 0 deletions bin/dart
1 change: 1 addition & 0 deletions bin/dart.sym
1 change: 1 addition & 0 deletions bin/dartaotruntime
1 change: 1 addition & 0 deletions bin/just
20 changes: 20 additions & 0 deletions lib/src/protocol/json-schemas/balance.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://tbdex.dev/balance.schema.json",
"type": "object",
"properties": {
"additionalProperties": false,
"currencyCode": {
"type": "string",
"description": "ISO 3166 currency code string"
},
"available": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "The amount available to be transacted with"
}
},
"required": [
"currencyCode",
"available"
]
}
14 changes: 14 additions & 0 deletions lib/src/protocol/json-schemas/close.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://tbdex.dev/close.schema.json",
"type": "object",
"additionalProperties": false,
"properties": {
"reason": {
"type": "string"
},
"success": {
"type": "boolean"
}
}
}
15 changes: 15 additions & 0 deletions lib/src/protocol/json-schemas/definitions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://tbdex.dev/definitions.json",
"type": "object",
"definitions": {
"did": {
"type": "string",
"pattern": "^did:([a-z0-9]+):((?:(?:[a-zA-Z0-9._-]|(?:%[0-9a-fA-F]{2}))*:)*((?:[a-zA-Z0-9._-]|(?:%[0-9a-fA-F]{2}))+))((;[a-zA-Z0-9_.:%-]+=[a-zA-Z0-9_.:%-]*)*)(\/[^#?]*)?([?][^#]*)?(#.*)?$"
},
"decimalString": {
"type": "string",
"pattern": "^([0-9]+(?:[.][0-9]+)?)$"
}
}
}
69 changes: 69 additions & 0 deletions lib/src/protocol/json-schemas/message.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://tbdex.dev/message.schema.json",
"definitions": {
"MessageMetadata": {
"type": "object",
"additionalProperties": false,
"properties": {
"from": {
"$ref": "definitions.json#/definitions/did",
"description": "The sender's DID"
},
"to": {
"$ref": "definitions.json#/definitions/did",
"description": "The recipient's DID"
},
"kind": {
"type": "string",
"enum": ["rfq", "quote", "order", "orderstatus", "close"],
"description": "The message kind (e.g. rfq, quote)"
},
"id": {
"type": "string",
"description": "The message ID"
},
"exchangeId": {
"type": "string",
"description": "ID for a 'thread' of messages between Alice <-> PFI. Set by the first message in a thread"
},
"externalId": {
"type": "string",
"description": "Arbitrary ID for the caller to associate with the message."
},
"createdAt": {
"type": "string",
"description": "ISO8601 formatted string representing the timestamp"
},
"protocol": {
"type": "string",
"description": "Version of the protocol in use (x.x format)"
}
},
"required": ["from", "to", "kind", "id", "exchangeId", "createdAt", "protocol"]
},
"Private": {
"type": "object",
"description": "An ephemeral JSON object used to transmit sensitive data (e.g. PII)"
}
},
"type": "object",
"properties": {
"metadata": {
"$ref": "#/definitions/MessageMetadata"
},
"data": {
"type": "object",
"description": "The actual message content"
},
"signature": {
"type": "string",
"description": "Signature that verifies the authenticity and integrity of a message"
},
"private": {
"$ref": "#/definitions/Private"
}
},
"additionalProperties": false,
"required": ["metadata", "data", "signature"]
}
153 changes: 153 additions & 0 deletions lib/src/protocol/json-schemas/offering.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://tbdex.dev/offering.schema.json",
"type": "object",
"properties": {
"additionalProperties": false,
"description": {
"type": "string",
"description": "Brief description of what is being offered."
},
"payin": {
"type": "object",
"additionalProperties": false,
"properties": {
"currencyCode": {
"type": "string",
"description": "ISO 3166 currency code string"
},
"min": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Minimum amount of currency that can be requested"
},
"max": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Maximum amount of currency that can be requested"
},
"methods": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"description": "The type of payment method. e.g. BITCOIN_ADDRESS, DEBIT_CARD, etc."
},
"name": {
"type": "string",
"description": "Payment Method name. Expected to be rendered on screen."
},
"description": {
"type": "string",
"description": "Blurb containing helpful information about the payment method. Expected to be rendered on screen. e.g. \"segwit addresses only\""
},
"group": {
"type": "string",
"description": "Value that can be used to group specific payment methods together (e.g. Mobile Money vs. Direct Bank Deposit)."
},
"requiredPaymentDetails": {
"$ref": "http://json-schema.org/draft-07/schema",
"description": "A JSON Schema containing the fields that need to be collected in order to use this payment method"
},
"min": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Minimum amount required to use this payment method."
},
"max": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Maximum amount allowed when using this payment method."
},
"fee": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Fee charged to use this payment method. Absence of this field implies that there is no _additional_ fee associated to the respective payment method."
}
},
"required": ["kind"]
}
}
},
"required": ["currencyCode", "methods"]
},
"payout": {
"type": "object",
"additionalProperties": false,
"properties": {
"currencyCode": {
"type": "string",
"description": "ISO 3166 currency code string"
},
"min": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Minimum amount of currency that can be requested"
},
"max": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Maximum amount of currency that can be requested"
},
"methods": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"description": "The type of payment method. e.g. BITCOIN_ADDRESS, DEBIT_CARD, etc."
},
"name": {
"type": "string",
"description": "Payment Method name. Expected to be rendered on screen."
},
"description": {
"type": "string",
"description": "Blurb containing helpful information about the payment method. Expected to be rendered on screen. e.g. \"segwit addresses only\""
},
"group": {
"type": "string",
"description": "Value that can be used to group specific payment methods together (e.g. Mobile Money vs. Direct Bank Deposit)."
},
"requiredPaymentDetails": {
"$ref": "http://json-schema.org/draft-07/schema",
"description": "A JSON Schema containing the fields that need to be collected in order to use this payment method"
},
"min": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Minimum amount required to use this payment method."
},
"max": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Maximum amount allowed when using this payment method."
},
"fee": {
"$ref": "definitions.json#/definitions/decimalString",
"description": "Fee charged to use this payment method. absence of this field implies that there is no _additional_ fee associated to the respective payment method"
},
"estimatedSettlementTime": {
"type": "number",
"description": "Estimated time in seconds for the payout to be settled. e.g. 3600 for 1 hour. 0 for instant settlement.",
"minimum": 0
}
},
"required": ["kind", "estimatedSettlementTime"]
}
}
},
"required": ["currencyCode", "methods"]
},
"payoutUnitsPerPayinUnit": {
"type": "string",
"description": "Number of payout currency units for one payin currency unit (i.e 290000 USD for 1 BTC)"
},
"requiredClaims": {
"type": "object",
"description": "PresentationDefinition that describes the credential(s) the PFI requires in order to provide a quote."
}
},
"required": [
"description",
"payin",
"payout",
"payoutUnitsPerPayinUnit"
]
}
7 changes: 7 additions & 0 deletions lib/src/protocol/json-schemas/order.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://tbdex.dev/order.schema.json",
"type": "object",
"additionalProperties": false,
"properties": {}
}
Loading

0 comments on commit 3130a86

Please sign in to comment.