Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #115 from KILTprotocol/dn-sdk-update
Browse files Browse the repository at this point in the history
feat: sdk update
  • Loading branch information
Dudleyneedham authored Mar 5, 2021
2 parents 99c6c29 + f9765a2 commit 017c3f7
Show file tree
Hide file tree
Showing 25 changed files with 596 additions and 594 deletions.
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

0 comments on commit 017c3f7

Please sign in to comment.