Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
64daf81
refactor: updated to the latest sdk
Dudleyneedham Feb 24, 2021
4965e7b
feat: update to the next sdk version
Dudleyneedham Feb 26, 2021
52d0408
feat: test imports updated for new sdk version
Dudleyneedham Feb 26, 2021
b3af450
feat: updated ctypes controller
Dudleyneedham Feb 26, 2021
ea4e736
fix: fixing issue with ctype controller
Dudleyneedham Feb 26, 2021
337d437
feat: removing sdk depen and adding all packages indiv
Dudleyneedham Mar 2, 2021
b5acdef
feat: updated all the test imports
Dudleyneedham Mar 2, 2021
31dcb32
feat: updated all the kilt packages
Dudleyneedham Mar 2, 2021
4138471
refactor: updating the mock pathing
Dudleyneedham Mar 2, 2021
5633b54
feat: adding new method to check and return owner
Dudleyneedham Mar 3, 2021
1eba3e1
Revert "feat: adding new method to check and return owner"
Dudleyneedham Mar 3, 2021
8ae6a15
fix: error case notonchain, outdated mock
Mar 3, 2021
d27591b
test: fixed ci import and mock paths
LeonFLK Mar 3, 2021
10dc3a3
fix: json build error
LeonFLK Mar 3, 2021
f531885
feat: updating 4
Dudleyneedham Mar 3, 2021
b34c60b
Revert "feat: updating 4"
Dudleyneedham Mar 3, 2021
ad6d789
feat: updating to the latest sdk version 34
Dudleyneedham Mar 3, 2021
48dfa8c
test: fixed ci, mongoose still problematic
LeonFLK Mar 3, 2021
0c6bd5d
lint: newline
LeonFLK Mar 3, 2021
fa562aa
test: fixed unit tests
LeonFLK Mar 3, 2021
3132854
fix: accidently downgraded packages
Dudleyneedham Mar 4, 2021
24e308e
feat: release candidate added
Dudleyneedham Mar 5, 2021
e8e87d8
feat: removed await for identity and add wait ready due to wsam issue
Dudleyneedham Mar 5, 2021
c7f4442
feat: added wait ready due to issue wsam
Dudleyneedham Mar 5, 2021
c85f59f
feat: removed the await from identity
Dudleyneedham Mar 5, 2021
f9765a2
fix: faucet tests
tjwelde Mar 5, 2021
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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
},
"dependencies": {
"@godaddy/terminus": "^4.6.0",
"@kiltprotocol/sdk-js": "^0.19.1-8",
"@kiltprotocol/core": "^0.20.0-rc.2",
"@kiltprotocol/utils": "^0.20.0-rc.2",
"@kiltprotocol/config": "^0.20.0-rc.2",
"@kiltprotocol/chain-helpers": "^0.20.0-rc.2",
"@kiltprotocol/messaging": "^0.20.0-rc.2",
"@kiltprotocol/types": "^0.20.0-rc.2",
"@nestjs/common": "^7.6.6",
"@nestjs/core": "^7.6.6",
"@nestjs/mongoose": "^7.2.2",
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain/interfaces/blockchain.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Blockchain } from '@kiltprotocol/sdk-js'
import { Blockchain } from '@kiltprotocol/chain-helpers'

export declare interface BlockchainService {
connect(): Promise<Blockchain>
Expand Down
7 changes: 5 additions & 2 deletions src/blockchain/universal-blockchain.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Kilt, { Blockchain } from '@kiltprotocol/sdk-js'
import { config as SDKConfig, connect as SDKConnect } from '@kiltprotocol/core'
import { Blockchain } from '@kiltprotocol/chain-helpers'

import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common'
import { ConfigService } from '../config/config.service'
import { BlockchainService } from './interfaces/blockchain.interfaces'
Expand All @@ -13,9 +15,10 @@ export class UniversalBlockchainService

public async connect(): Promise<Blockchain> {
const bootNodeAddress = this.configService.get('BOOT_NODE_ADDRESS')
SDKConfig({ address: bootNodeAddress })
console.log(`Connecting to ${bootNodeAddress}`)
if (!UniversalBlockchainService.instance) {
UniversalBlockchainService.instance = Kilt.connect(bootNodeAddress)
UniversalBlockchainService.instance = SDKConnect()
}
return Promise.resolve(UniversalBlockchainService.instance)
}
Expand Down
3 changes: 1 addition & 2 deletions src/contacts/contacts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import {
UseGuards,
} from '@nestjs/common'
import { Contact, ContactsService } from './interfaces/contacts.interfaces'
import { Did } from '@kiltprotocol/sdk-js'
import { Did, IDidDocumentSigned } from '@kiltprotocol/core'
import { Optional } from 'typescript-optional'
import { AuthGuard } from '../auth/auth.guard'
import { IDidDocumentSigned } from '@kiltprotocol/sdk-js/build/did/Did'

@Controller('contacts')
export class ContactsController {
Expand Down
10 changes: 4 additions & 6 deletions src/contacts/contacts.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ import {
} from './interfaces/contacts.interfaces'
import { MongoDbMContactsService } from './mongodb-contacts.service'
import { Optional } from 'typescript-optional'
import {
IDidDocumentSigned,
IDENTIFIER_PREFIX,
} from '@kiltprotocol/sdk-js/build/did/Did'
import { IDidDocumentSigned } from '@kiltprotocol/core'
const IDENTIFIER_PREFIX = 'did:kilt:'

jest.mock('@kiltprotocol/sdk-js/build/did/Did', () => {
jest.mock('@kiltprotocol/core/lib/did/Did', () => {
return {
verifyDidDocumentSignature: jest.fn((): boolean => true),
getIdentifierFromAddress: jest.fn(
Expand Down Expand Up @@ -97,7 +95,7 @@ describe('Contact Module', () => {
let contactsController: ContactsController
let contactsService: ContactsService

const mockedVerifyDidDocumentSignature = require('@kiltprotocol/sdk-js/build/did/Did')
const mockedVerifyDidDocumentSignature = require('@kiltprotocol/core/lib/did/Did')
.verifyDidDocumentSignature
const fakeContactService: ContactsService = {
add: jest.fn(
Expand Down
3 changes: 1 addition & 2 deletions src/contacts/interfaces/contacts.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PublicIdentity } from '@kiltprotocol/sdk-js'
import { IDidDocumentSigned } from '@kiltprotocol/sdk-js/build/did/Did'
import { PublicIdentity, IDidDocumentSigned } from '@kiltprotocol/core'
import { Document } from 'mongoose'
import { Optional } from 'typescript-optional'

Expand Down
2 changes: 1 addition & 1 deletion src/contacts/mongodb-contacts.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PublicIdentity } from '@kiltprotocol/sdk-js'
import { PublicIdentity } from '@kiltprotocol/core'
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
Expand Down
61 changes: 28 additions & 33 deletions src/ctypes/ctypes.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CType as SDKCtype } from '@kiltprotocol/sdk-js'
import { getOwner } from '@kiltprotocol/sdk-js/build/ctype/CType.chain'
import { CType as SDKCType } from '@kiltprotocol/core'
import {
Body,
Controller,
Expand Down Expand Up @@ -46,43 +45,39 @@ export class CTypesController {

@Post()
public async register(@Body() cTypeInput: CType) {
return this.verifyCTypeAndReturnChainOwner(cTypeInput).then(
async verified => {
if (verified) {
console.log(
`All valid => registering cType ` +
JSON.stringify({ ...cTypeInput.cType, owner: verified }, null, 4)
const verified = await this.verifyCType(cTypeInput)
if (verified) {
console.log(
`All valid => registering cType ` +
JSON.stringify(
{ ...cTypeInput.cType, owner: cTypeInput.cType.owner },
null,
4
)
)
} else {
throw new CTypeNotOnChainException()
}

const result = await this.cTypesService.register({
...cTypeInput,
cType: { ...cTypeInput.cType, owner: verified },
})
const result = await this.cTypesService.register({
...cTypeInput,
})

if (!result) {
console.log(
`The CType with hash: ${
cTypeInput.cType.hash
} already exists in this DB!`
)
throw new AlreadyRegisteredException()
}
} else {
throw new CTypeNotOnChainException()
}
}
)
if (!result) {
console.log(
`The CType with hash: ${
cTypeInput.cType.hash
} already exists in this DB!`
)
throw new AlreadyRegisteredException()
}
}

private async verifyCTypeAndReturnChainOwner(
cTypeInput: CType
): Promise<string | null> {
private async verifyCType(cTypeInput: CType) {
try {
const cType = new SDKCtype(cTypeInput.cType)

return getOwner(cType.hash)
} catch (e) {
console.log('error: ' + e)
const ctype = new SDKCType(cTypeInput.cType)
return ctype.verifyStored()
} catch {
throw new InvalidCtypeDefinitionException()
}
}
Expand Down
34 changes: 20 additions & 14 deletions src/ctypes/ctypes.module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CType, CTypeService, CTypeDB } from './interfaces/ctype.interfaces'
import { Optional } from 'typescript-optional'
import { Blockchain, CType as SDKCType, CTypeMetadata, Identity} from '@kiltprotocol/sdk-js'
import { CType as SDKCType, CTypeMetadata, Identity } from '@kiltprotocol/core'
import { Blockchain } from '@kiltprotocol/chain-helpers'
import { Test } from '@nestjs/testing'
import { CTypesController } from './ctypes.controller'
import { AuthGuard } from '../auth/auth.guard'
Expand All @@ -10,8 +11,9 @@ import { AlreadyRegisteredException } from './exceptions/already-registered.exce
import { NotFoundException } from '@nestjs/common/exceptions'
import { getModelToken } from '@nestjs/mongoose'
import { MongoDbCTypesService } from './mongodb-ctypes.service'
import { cryptoWaitReady } from '@polkadot/util-crypto'

jest.mock('@kiltprotocol/sdk-js/build/ctype/CType.chain', () => {
jest.mock('@kiltprotocol/core/lib/ctype/CType.chain', () => {
return {
getOwner: jest.fn(async (): Promise<string | null> => null),
}
Expand Down Expand Up @@ -72,9 +74,9 @@ describe('CType Module', () => {
let ctypesService: CTypeService
let aliceAddress: string

const blockchainApi = require('@kiltprotocol/sdk-js/build/blockchainApiConnection/BlockchainApiConnection')
const blockchainApi = require('@kiltprotocol/chain-helpers/lib/blockchainApiConnection/BlockchainApiConnection')

const mockedGetOwner = require('@kiltprotocol/sdk-js/build/ctype/CType.chain')
const mockedGetOwner = require('@kiltprotocol/core/lib/ctype/CType.chain')
.getOwner

const fakeCTypeService: CTypeService = {
Expand All @@ -91,8 +93,10 @@ describe('CType Module', () => {
}

beforeAll(
async () =>
(aliceAddress = (await Identity.buildFromURI('//Alice')).address)
async () => (
await cryptoWaitReady(),
(aliceAddress = Identity.buildFromURI('//Alice').address)
)
)

beforeEach(async () => {
Expand Down Expand Up @@ -120,31 +124,31 @@ describe('CType Module', () => {
})
afterEach(() => jest.clearAllMocks())

describe('verifyCTypeAndReturnChainOwner', () => {
describe('verifyCType', () => {
it('valid CType', async () => {
mockedGetOwner.mockResolvedValue(aliceAddress)
const testCType = { ...SDKCTypeA, owner: aliceAddress }
const serviceCType: CType = { cType: testCType, metaData: metaDataA }
await expect(
ctypesController['verifyCTypeAndReturnChainOwner'](serviceCType)
).resolves.toEqual(aliceAddress)
ctypesController['verifyCType'](serviceCType)
).resolves.toEqual(true)
})
it('invalid CType', async () => {
mockedGetOwner.mockResolvedValue(aliceAddress)
const testCType = { ...SDKCTypeA, owner: aliceAddress, hash: '0x1' }
const serviceCType: CType = { cType: testCType, metaData: metaDataA }

await expect(
ctypesController['verifyCTypeAndReturnChainOwner'](serviceCType)
ctypesController['verifyCType'](serviceCType)
).rejects.toThrow(new InvalidCtypeDefinitionException())
})
it('offChain CType', async () => {
mockedGetOwner.mockResolvedValue(null)
const testCType = { ...SDKCTypeA, owner: aliceAddress }
const serviceCType: CType = { cType: testCType, metaData: metaDataA }
await expect(
ctypesController['verifyCTypeAndReturnChainOwner'](serviceCType)
).resolves.toBeNull()
ctypesController['verifyCType'](serviceCType)
).resolves.toEqual(false)
})
})
describe('register', () => {
Expand Down Expand Up @@ -245,8 +249,10 @@ describe('CType Module', () => {
let aliceAddress: string

beforeAll(
async () =>
(aliceAddress = (await Identity.buildFromURI('//Alice')).address)
async () => (
await cryptoWaitReady(),
(aliceAddress = Identity.buildFromURI('//Alice').address)
)
)

beforeEach(async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/ctypes/interfaces/ctype.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICType, ICTypeMetadata } from '@kiltprotocol/sdk-js'
import { ICType, ICTypeMetadata } from '@kiltprotocol/types'
import { Document } from 'mongoose'
import { Optional } from 'typescript-optional'

Expand Down
2 changes: 1 addition & 1 deletion src/ctypes/mongodb-ctypes.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICType } from '@kiltprotocol/sdk-js'
import { ICType } from '@kiltprotocol/types'
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
Expand Down
7 changes: 4 additions & 3 deletions src/faucet/faucet.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Balance, Identity, BlockchainUtils } from '@kiltprotocol/sdk-js'
import { Balance, Identity } from '@kiltprotocol/core'
import { BlockchainUtils } from '@kiltprotocol/chain-helpers'
import {
Controller,
Inject,
Expand Down Expand Up @@ -36,7 +37,7 @@ export class FaucetController {
throw new BadRequestException('no target address')
}
console.log(`Faucet drop requested for ${address} from ${request.ip}`)
if (!checkAddress(address, 42)[0]) {
if (!checkAddress(address, 38)[0]) {
throw new FaucetDropInvalidAddressException()
}

Expand Down Expand Up @@ -66,7 +67,7 @@ export class FaucetController {
private async transferTokens(address: string): Promise<boolean> {
try {
console.log(`Transfer tokens from faucet to ${address}`)
const faucetAccount: Identity = await Identity.buildFromSeed(
const faucetAccount: Identity = Identity.buildFromSeed(
hexToU8a(process.env.FAUCET_ACCOUNT)
)
const tx = await Balance.makeTransfer(
Expand Down
Loading