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

Implement custom data contract names #153

Merged
merged 5 commits into from
Jun 17, 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
13 changes: 11 additions & 2 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,15 @@ Response codes:
---
### Data Contract by Identifier
Return data contract by given identifier

* `name` field is nullable

```
GET /dataContract/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec

{
identifier: "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec",
name: "DPNS",
owner: "4EfA9Jrvv3nnCFdSf7fad59851iiTRZ6Wcu6YVJ4iSeF",
schema: "{}",
version: 0,
Expand All @@ -235,7 +239,8 @@ Response codes:
### Data Contracts
Return dataContracts set paged and order by block height or documents count.

Valid `order_by` values are `block_height` or `documents_count`
* Valid `order_by` values are `block_height` or `documents_count`
* `name` field is nullable

```
GET /dataContracts?page=1&limit=10&order=asc&order_by=block_height
Expand All @@ -249,6 +254,7 @@ GET /dataContracts?page=1&limit=10&order=asc&order_by=block_height
resultSet: [
{
identifier: "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec",
name: "DPNS",
owner: "4EfA9Jrvv3nnCFdSf7fad59851iiTRZ6Wcu6YVJ4iSeF",
schema: "{}",
version: 0,
Expand Down Expand Up @@ -351,7 +357,7 @@ Response codes:
### Identities
Return all identities paged and order by block height, tx count or balance.

Valid `order_by` values are `block_height`, `tx_count` or `balance`
* Valid `order_by` values are `block_height`, `tx_count` or `balance`
```
GET /identities?page=1&limit=10&order=asc&order_by=block_height

Expand Down Expand Up @@ -386,6 +392,8 @@ Response codes:

### Data contracts by Identity
Return all data contracts by the given identity

* `name` field is nullable
```
GET /identities/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec/dataContracts?page=1&limit=10&order=asc

Expand All @@ -398,6 +406,7 @@ GET /identities/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec/dataContracts?page=
resultSet: [
{
identifier: "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec",
name: "DPNS",
owner: "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec",
version: 0,
schema: null,
Expand Down
9 changes: 5 additions & 4 deletions packages/api/src/dao/DataContractsDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = class DataContractsDAO {
.as('sum_documents')

const subquery = this.knex('data_contracts')
.select('data_contracts.id as id', 'data_contracts.identifier as identifier', 'data_contracts.identifier as my_identifier', 'data_contracts.owner as owner',
.select('data_contracts.id as id', 'data_contracts.identifier as identifier', 'data_contracts.name as name',
'data_contracts.identifier as my_identifier', 'data_contracts.owner as owner',
'data_contracts.is_system as is_system', 'data_contracts.version as version',
'data_contracts.state_transition_hash as tx_hash')
.select(this.knex(sumDocuments)
Expand All @@ -38,15 +39,15 @@ module.exports = class DataContractsDAO {

const filteredContracts = this.knex.with('filtered_data_contracts', subquery)
.select(this.knex.raw('COALESCE(documents_count, 0) as documents_count'))
.select('id', 'owner', 'identifier', 'version', 'tx_hash', 'rank', 'is_system',
.select('id', 'name', 'owner', 'identifier', 'version', 'tx_hash', 'rank', 'is_system',
this.knex('filtered_data_contracts').count('*').as('total_count').where('rank', '1'))
.select(this.knex.raw(`rank() over (${getRankString()}) row_number`))
.from('filtered_data_contracts')
.where('rank', 1)
.as('filtered_data_contracts')

const rows = await this.knex(filteredContracts)
.select('filtered_data_contracts.documents_count', 'filtered_data_contracts.id', 'total_count', 'identifier', 'filtered_data_contracts.owner', 'version', 'row_number',
.select('filtered_data_contracts.documents_count', 'filtered_data_contracts.id', 'name', 'total_count', 'identifier', 'filtered_data_contracts.owner', 'version', 'row_number',
'filtered_data_contracts.tx_hash', 'is_system', 'blocks.timestamp as timestamp', 'blocks.hash as block_hash')
.leftJoin('state_transitions', 'state_transitions.hash', 'filtered_data_contracts.tx_hash')
.leftJoin('blocks', 'blocks.hash', 'state_transitions.block_hash')
Expand All @@ -62,7 +63,7 @@ module.exports = class DataContractsDAO {

getDataContractByIdentifier = async (identifier) => {
const rows = await this.knex('data_contracts')
.select('data_contracts.identifier as identifier', 'data_contracts.owner as owner',
.select('data_contracts.identifier as identifier', 'data_contracts.name as name', 'data_contracts.owner as owner',
'data_contracts.schema as schema', 'data_contracts.is_system as is_system',
'data_contracts.version as version', 'state_transitions.hash as tx_hash', 'blocks.timestamp as timestamp')
.select(this.knex('documents').count('*')
Expand Down
10 changes: 5 additions & 5 deletions packages/api/src/dao/IdentitiesDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,22 @@ module.exports = class IdentitiesDAO {
.as('sum_documents')

const subquery = this.knex('data_contracts')
.select('data_contracts.id', 'data_contracts.identifier as identifier', 'data_contracts.owner as data_contract_owner',
'data_contracts.version as version', 'data_contracts.state_transition_hash as tx_hash',
'data_contracts.is_system as is_system')
.select('data_contracts.id', 'data_contracts.identifier as identifier', 'data_contracts.name as name',
'data_contracts.owner as data_contract_owner', 'data_contracts.version as version',
'data_contracts.state_transition_hash as tx_hash', 'data_contracts.is_system as is_system')
.select(this.knex.raw('rank() over (partition by data_contracts.identifier order by data_contracts.id desc) rank'))
.where('owner', '=', identifier)

const filteredDataContracts = this.knex.with('with_alias', subquery)
.select('id', 'identifier', 'data_contract_owner', 'version', 'tx_hash', 'rank', 'is_system')
.select('id', 'identifier', 'name', 'data_contract_owner', 'version', 'tx_hash', 'rank', 'is_system')
.select(this.knex('with_alias').count('*').where('rank', 1).as('total_count'))
.select(this.knex.raw(`rank() over (order by id ${order}) row_number`))
.from('with_alias')
.where('rank', 1)
.as('data_contractz')

const rows = await this.knex(filteredDataContracts)
.select('data_contractz.id as id', 'identifier', 'data_contract_owner', 'version', 'tx_hash', 'rank', 'total_count', 'row_number', 'is_system', 'blocks.timestamp as timestamp')
.select('data_contractz.id as id', 'name', 'identifier', 'data_contract_owner', 'version', 'tx_hash', 'rank', 'total_count', 'row_number', 'is_system', 'blocks.timestamp as timestamp')
.select(this.knex(sumDocuments)
.count('*')
.whereRaw('sum_documents.dc_identifier = data_contractz.identifier')
Expand Down
8 changes: 5 additions & 3 deletions packages/api/src/models/DataContract.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = class DataContract {
identifier
name
owner
schema
version
Expand All @@ -8,8 +9,9 @@ module.exports = class DataContract {
isSystem
documentsCount

constructor (identifier, owner, schema, version, txHash, timestamp, isSystem, documentsCount) {
constructor (identifier, name, owner, schema, version, txHash, timestamp, isSystem, documentsCount) {
this.identifier = identifier ? identifier.trim() : null
this.name = name ? name.trim() : null
this.owner = owner ? owner.trim() : null
this.schema = schema ?? null
this.version = version ?? null
Expand All @@ -20,7 +22,7 @@ module.exports = class DataContract {
}

// eslint-disable-next-line camelcase
static fromRow ({ identifier, owner, schema, version, tx_hash, timestamp, is_system, documents_count }) {
return new DataContract(identifier, owner, schema ? JSON.stringify(schema) : null, version, tx_hash, timestamp, is_system, Number(documents_count))
static fromRow ({ identifier, name, owner, schema, version, tx_hash, timestamp, is_system, documents_count }) {
return new DataContract(identifier, name, owner, schema ? JSON.stringify(schema) : null, version, tx_hash, timestamp, is_system, Number(documents_count))
}
}
8 changes: 8 additions & 0 deletions packages/api/test/integration/data.contracts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('DataContracts routes', () => {
identifier: dataContracts[dataContracts.length - 1].dataContract.identifier,
version: dataContracts[dataContracts.length - 1].dataContract.version + 1,
schema: '{}',
name: 'L33T D4T4C087R4CT',
documents: dataContracts[dataContracts.length - 1].dataContract.documents
})
const document = await fixtures.document(knex, {
Expand Down Expand Up @@ -112,6 +113,7 @@ describe('DataContracts routes', () => {
.sort((a, b) => a.dataContract.id - b.dataContract.id)
.map(({ transaction, dataContract, block }) => ({
identifier: dataContract.identifier,
name: dataContract.name,
owner: identity.identifier,
schema: null,
version: 0,
Expand Down Expand Up @@ -139,6 +141,7 @@ describe('DataContracts routes', () => {
.slice(0, 10)
.map(({ transaction, dataContract, block }) => ({
identifier: dataContract.identifier,
name: dataContract.name,
owner: identity.identifier,
schema: null,
version: dataContract.version,
Expand Down Expand Up @@ -166,6 +169,7 @@ describe('DataContracts routes', () => {
.slice(6, 12)
.map(({ transaction, dataContract, block }) => ({
identifier: dataContract.identifier,
name: dataContract.name,
owner: identity.identifier,
schema: null,
version: 0,
Expand Down Expand Up @@ -193,6 +197,7 @@ describe('DataContracts routes', () => {
.slice(12, 18)
.map(({ transaction, dataContract, block }) => ({
identifier: dataContract.identifier,
name: dataContract.name,
owner: identity.identifier,
schema: null,
version: 0,
Expand Down Expand Up @@ -221,6 +226,7 @@ describe('DataContracts routes', () => {
.slice(0, 10)
.map(({ transaction, dataContract, block }) => ({
identifier: dataContract.identifier,
name: dataContract.name,
owner: identity.identifier,
schema: null,
version: dataContract.version,
Expand Down Expand Up @@ -249,6 +255,7 @@ describe('DataContracts routes', () => {

const expectedDataContract = {
identifier: dataContract.dataContract.identifier,
name: dataContract.dataContract.name,
owner: identity.identifier,
schema: '{}',
version: 0,
Expand All @@ -270,6 +277,7 @@ describe('DataContracts routes', () => {

const expectedDataContract = {
identifier: dataContract.dataContract.identifier,
name: dataContract.dataContract.name,
owner: identity.identifier,
schema: '{}',
version: dataContract.dataContract.version,
Expand Down
4 changes: 4 additions & 0 deletions packages/api/test/integration/identities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ describe('Identities routes', () => {

const expectedDataContracts = dataContracts.slice(0, 10).map((_dataContract) => ({
identifier: _dataContract.dataContract.identifier,
name: _dataContract.dataContract.name,
owner: identity.identifier,
version: 0,
schema: null,
Expand Down Expand Up @@ -397,6 +398,7 @@ describe('Identities routes', () => {
.slice(0, 10)
.map((_dataContract) => ({
identifier: _dataContract.dataContract.identifier,
name: _dataContract.dataContract.name,
owner: identity.identifier,
version: 0,
schema: null,
Expand Down Expand Up @@ -441,6 +443,7 @@ describe('Identities routes', () => {
.slice(5, 10)
.map((_dataContract) => ({
identifier: _dataContract.dataContract.identifier,
name: _dataContract.dataContract.name,
owner: identity.identifier,
version: 0,
schema: null,
Expand Down Expand Up @@ -486,6 +489,7 @@ describe('Identities routes', () => {
.map((_dataContract) => ({
identifier: _dataContract.dataContract.identifier,
owner: identity.identifier,
name: _dataContract.dataContract.name,
version: 0,
schema: null,
txHash: _dataContract.transaction.hash,
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/integration/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('Other routes', () => {

const expectedDataContract = {
identifier: dataContract.identifier,
name: dataContract.name,
owner: identity.identifier.trim(),
schema: JSON.stringify(dataContract.schema),
version: 0,
Expand Down
3 changes: 2 additions & 1 deletion packages/api/test/utils/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const fixtures = {

return { ...row, txHash: state_transition_hash ?? transaction.hash, id: result[0].id, transaction }
},
dataContract: async (knex, { identifier, schema, version, state_transition_hash, owner, is_system, documents = [] } = {}) => {
dataContract: async (knex, { identifier, name, schema, version, state_transition_hash, owner, is_system, documents = [] } = {}) => {
if (!identifier) {
identifier = generateIdentifier()
}
Expand All @@ -96,6 +96,7 @@ const fixtures = {
const row = {
owner,
identifier,
name: name ?? null,
state_transition_hash,
schema: schema ?? {},
version: version ?? 0,
Expand Down
29 changes: 3 additions & 26 deletions packages/data-contract/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config()

const Dash = require('dash')
const schema = require('./schema.json')

function logInfo (...messages) {
console.log('\x1b[32m [INFO]', ...messages, '\x1b[0m ')
Expand All @@ -9,31 +10,6 @@ function logInfo (...messages) {
async function main () {
logInfo('Client Initialization')

const schema = {
dataContracts: {
type: 'object',
properties: {
identifier: {
type: 'string',
minLength: 43,
maxLength: 44,
position: 0
},
name: {
type: 'string',
maxLength: 32,
minLength: 3,
position: 1.0
}
},
required: [
'identifier',
'name'
],
additionalProperties: false
}
}

const options = {
network: 'testnet',
wallet: {
Expand All @@ -42,8 +18,9 @@ async function main () {
}

if (process.env.SKIP_SYNCHRONIZATION_BEFORE_HEIGHT) {
options.unsafeOptions = { skipSynchronizationBeforeHeight: Number(process.env.SKIP_SYNCHRONIZATION_BEFORE_HEIGHT) }
options.wallet.unsafeOptions = { skipSynchronizationBeforeHeight: Number(process.env.SKIP_SYNCHRONIZATION_BEFORE_HEIGHT) }
}

const client = new Dash.Client(options)

logInfo('Contract Deployment')
Expand Down
24 changes: 24 additions & 0 deletions packages/data-contract/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"dataContracts": {
"type": "object",
"properties": {
"identifier": {
"type": "string",
"minLength": 43,
"maxLength": 44,
"position": 0
},
"name": {
"type": "string",
"maxLength": 32,
"minLength": 3,
"position": 1
}
},
"required": [
"identifier",
"name"
],
"additionalProperties": false
}
}
Loading
Loading