Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

feat: MultiTx & SDK Compatibility, non re-try #296

Merged
merged 4 commits into from
Dec 8, 2020
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@babel/core": "^7.10.4",
"@kiltprotocol/sdk-js": "^0.19.1-2",
"@kiltprotocol/sdk-js": "^0.19.1-8",
"@polkadot/ui-identicon": "^0.33.1",
"@types/react-select": "^2.0.11",
"@types/reselect": "^2.2.0",
Expand Down
54 changes: 34 additions & 20 deletions src/components/DevTools/DevTools.anticov.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import * as sdk from '@kiltprotocol/sdk-js'
import Kilt, {
DelegationRootNode,
Identity,
BlockchainUtils,
DelegationNode,
MessageBody,
MessageBodyType,
Permission,
UUID,
} from '@kiltprotocol/sdk-js'
import { IMetadata } from '@kiltprotocol/sdk-js/build/types/CTypeMetadata'
import { ICTypeSchema } from '@kiltprotocol/sdk-js/build/types/CType'
import BN from 'bn.js'
Expand All @@ -18,21 +27,23 @@ import FeedbackService, {
notifyFailure,
} from '../../services/FeedbackService'

const ctype = sdk.CType.fromSchema(CTYPE.schema as ICTypeSchema)
const { IS_IN_BLOCK } = BlockchainUtils

const ctype = Kilt.CType.fromSchema(CTYPE.schema as ICTypeSchema)
const metadata: IMetadata = CTYPE_METADATA

interface ISetup {
root: sdk.Identity
delegationRoot: sdk.DelegationRootNode
root: Identity
delegationRoot: DelegationRootNode
}

let cachedSetup: ISetup

async function setup(): Promise<ISetup> {
if (!cachedSetup) {
const root = await sdk.Identity.buildFromMnemonic(ROOT_SEED)
const root = await Identity.buildFromMnemonic(ROOT_SEED)

const delegationRoot = new sdk.DelegationRootNode(
const delegationRoot = new DelegationRootNode(
DELEGATION_ROOT_ID,
ctype.hash,
root.address
Expand All @@ -50,24 +61,24 @@ async function setup(): Promise<ISetup> {
async function newDelegation(delegate: IMyIdentity): Promise<void> {
const { root, delegationRoot } = await setup()

const delegationNode = new sdk.DelegationNode(
sdk.UUID.generate(),
const delegationNode = new DelegationNode(
UUID.generate(),
delegationRoot.id,
delegate.identity.address,
[sdk.Permission.ATTEST]
[Permission.ATTEST]
)
const signature = delegate.identity.signStr(delegationNode.generateHash())
const tx = await delegationNode.store(root, signature)
await sdk.Blockchain.submitSignedTx(tx)
await BlockchainUtils.submitSignedTx(tx, { resolveOn: IS_IN_BLOCK })
notifySuccess(`Delegation successfully created for ${delegate.metaData.name}`)
await DelegationsService.importDelegation(
delegationNode.id,
'AntiCov Attester',
false
)
notifySuccess(`Delegation imported. Switch to Delegation Tab to see it.`)
const messageBody: sdk.MessageBody = {
type: sdk.MessageBodyType.INFORM_CREATE_DELEGATION,
const messageBody: MessageBody = {
type: MessageBodyType.INFORM_CREATE_DELEGATION,
content: { delegationId: delegationNode.id, isPCR: false },
}
await MessageRepository.sendToAddresses(
Expand All @@ -80,26 +91,29 @@ async function verifyOrAddCtypeAndRoot(): Promise<void> {
const { root, delegationRoot } = await setup()
if (!(await ctype.verifyStored())) {
const tx = await ctype.store(root)
await sdk.Blockchain.submitSignedTx(tx)
await BlockchainUtils.submitSignedTx(tx, { resolveOn: IS_IN_BLOCK })
CTypeRepository.register({
cType: ctype,
metaData: { metadata, ctypeHash: ctype.hash },
})
notifySuccess(`CTYPE ${metadata.title.default} successfully created.`)
}
// delegationRoot.verify() is unreliable when using the currently released mashnet-node & sdk
// workaround is checking the ctype hash of the query result; it is 0x000... if it doesn't exist on chain
const queriedRoot = await sdk.DelegationRootNode.query(delegationRoot.id)
// delegationRoot.verify() is unreliable when using the currently released mashnet-node & // workaround is checking the ctype hash of the query result; it is 0x000... if it doesn't exist on chain
const queriedRoot = await DelegationRootNode.query(delegationRoot.id)
if (queriedRoot?.cTypeHash !== ctype.hash) {
const tx = await delegationRoot.store(root)
await sdk.Blockchain.submitSignedTx(tx)
const messageBody: sdk.MessageBody = {
type: sdk.MessageBodyType.INFORM_CREATE_DELEGATION,
await BlockchainUtils.submitSignedTx(tx, { resolveOn: IS_IN_BLOCK })
const messageBody: MessageBody = {
type: MessageBodyType.INFORM_CREATE_DELEGATION,
content: { delegationId: delegationRoot.id, isPCR: false },
}
notifySuccess(`AntiCov Delegation Root successfully created.`)
// sending root owner message for importing the root
const message = new sdk.Message(messageBody, root, root.getPublicIdentity())
const message = new Kilt.Message(
messageBody,
root,
root.getPublicIdentity()
)
await MessageRepository.dispatchMessage(message)
notifySuccess(`Sent Delegation Root to AntiCov root authority.`)
}
Expand Down
25 changes: 14 additions & 11 deletions src/components/DevTools/DevTools.ctypes.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as sdk from '@kiltprotocol/sdk-js'

import { ERROR_CTYPE_ALREADY_EXISTS } from '@kiltprotocol/sdk-js'
import Kilt, {
CType,
ICType,
ICTypeMetadata,
ERROR_CTYPE_ALREADY_EXISTS,
BlockchainUtils,
} from '@kiltprotocol/sdk-js'
import CTypeRepository from '../../services/CtypeRepository'
import errorService from '../../services/ErrorService'
import { notifySuccess, notifyError } from '../../services/FeedbackService'
Expand All @@ -11,9 +15,9 @@ import cTypesPool from './data/cTypes.json'

type UpdateCallback = (bsCTypeKey: keyof BsCTypesPool) => void

interface IBsCTypesPoolElement extends sdk.ICType {
interface IBsCTypesPoolElement extends ICType {
owner: string
metadata: sdk.ICTypeMetadata['metadata']
metadata: ICTypeMetadata['metadata']
}

export type BsCTypesPool = {
Expand All @@ -27,12 +31,11 @@ class BsCType {
// replace owner key with his address
const ownerIdentity = (await BsIdentity.getByKey(bsCTypeData.owner))
.identity
const cType = sdk.CType.fromSchema(
bsCTypeData.schema,
ownerIdentity.address
)
const cType = CType.fromSchema(bsCTypeData.schema, ownerIdentity.address)
const tx = cType.store(ownerIdentity)
await sdk.Blockchain.submitSignedTx(await tx)
await BlockchainUtils.submitSignedTx(await tx, {
resolveOn: BlockchainUtils.IS_IN_BLOCK,
})
return tx
.catch(error => {
if (error === ERROR_CTYPE_ALREADY_EXISTS) {
Expand Down Expand Up @@ -78,7 +81,7 @@ class BsCType {
}

public static async getByHash(
hash: sdk.ICType['hash']
hash: ICType['hash']
): Promise<ICTypeWithMetadata> {
const cType = await CTypeRepository.findByHash(hash)
if (cType) {
Expand Down
56 changes: 34 additions & 22 deletions src/components/DevTools/DevTools.delegations.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import * as sdk from '@kiltprotocol/sdk-js'
import {
BlockchainUtils,
DelegationNode,
DelegationRootNode,
IInformCreateDelegation,
IRequestAcceptDelegation,
ISubmitAcceptDelegation,
MessageBodyType,
Permission,
UUID,
} from '@kiltprotocol/sdk-js'
import ContactRepository from '../../services/ContactRepository'

import DelegationsService from '../../services/DelegationsService'
Expand All @@ -16,21 +26,21 @@ import pcrPool from './data/pcr.json'

type UpdateCallback = (bsDelegationKey: keyof BsDelegationsPool) => void

type Permission = 'ATTEST' | 'DELEGATE'
type Permissions = 'ATTEST' | 'DELEGATE'

type RootData = {
ownerIdentity: IMyIdentity
rootDelegation: sdk.DelegationRootNode
rootDelegation: DelegationRootNode
}

type ParentData = {
ownerIdentity: IMyIdentity
delegation: sdk.DelegationNode | sdk.DelegationRootNode
delegation: DelegationNode | DelegationRootNode
metaData?: IMyDelegation['metaData']
}

type DelegationDataForMessages = {
delegation: sdk.DelegationNode
delegation: DelegationNode
isPCR: boolean
ownerIdentity: IMyIdentity
signature: string
Expand All @@ -42,7 +52,7 @@ type BsDelegationsPoolElement = {

children?: BsDelegationsPool
cTypeKey?: keyof BsCTypesPool
permissions?: Permission[]
permissions?: Permissions[]
}

export type BsDelegationsPool = {
Expand Down Expand Up @@ -76,16 +86,16 @@ class BsDelegation {
}

// creation
let newPermissions: sdk.Permission[]
let newPermissions: Permission[]
if (isPCR) {
newPermissions = [sdk.Permission.ATTEST]
newPermissions = [Permission.ATTEST]
} else {
newPermissions = (permissions || []).map(
permission => sdk.Permission[permission]
permission => Permission[permission]
)
}
const delegation = new sdk.DelegationNode(
sdk.UUID.generate(),
const delegation = new DelegationNode(
UUID.generate(),
rootData.rootDelegation.id,
ownerIdentity.identity.address,
newPermissions,
Expand All @@ -95,7 +105,9 @@ class BsDelegation {
const signature = ownerIdentity.identity.signStr(delegation.generateHash())
const metaData = { alias }
const tx = await DelegationsService.storeOnChain(delegation, signature)
await sdk.Blockchain.submitSignedTx(tx)
await BlockchainUtils.submitSignedTx(tx, {
resolveOn: BlockchainUtils.IS_IN_BLOCK,
})
DelegationsService.store({
cTypeHash: rootData.rootDelegation.cTypeHash,
...delegation,
Expand Down Expand Up @@ -180,8 +192,8 @@ class BsDelegation {
}

// await creation
const rootDelegation = new sdk.DelegationRootNode(
sdk.UUID.generate(),
const rootDelegation = new DelegationRootNode(
UUID.generate(),
cType.cType.hash,
ownerIdentity.identity.address
)
Expand Down Expand Up @@ -210,7 +222,7 @@ class BsDelegation {
isPCR: boolean,
withMessages: boolean,
updateCallback?: UpdateCallback
): Promise<void | sdk.Claim> {
): Promise<void> {
const pool = isPCR ? BsDelegation.pcrPool : BsDelegation.delegationsPool
const bsDelegationKeys = Object.keys(pool)
const requests = bsDelegationKeys.reduce(
Expand Down Expand Up @@ -314,7 +326,7 @@ class BsDelegation {
signature,
} = delegationDataForMessages

const delegationData: sdk.IRequestAcceptDelegation['content']['delegationData'] = {
const delegationData: IRequestAcceptDelegation['content']['delegationData'] = {
account: parentData.ownerIdentity.identity.address,
id: delegation.id,
isPCR,
Expand All @@ -323,7 +335,7 @@ class BsDelegation {
}

// send invitation from inviter(parentIdentity) to invitee (ownerIdentity)
const requestAcceptDelegation: sdk.IRequestAcceptDelegation = {
const requestAcceptDelegation: IRequestAcceptDelegation = {
content: {
delegationData,
metaData: parentData.metaData,
Expand All @@ -333,7 +345,7 @@ class BsDelegation {
),
},
},
type: sdk.MessageBodyType.REQUEST_ACCEPT_DELEGATION,
type: MessageBodyType.REQUEST_ACCEPT_DELEGATION,
}
await MessageRepository.singleSend(
requestAcceptDelegation,
Expand All @@ -342,15 +354,15 @@ class BsDelegation {
)

// send invitation acceptance back
const submitAcceptDelegation: sdk.ISubmitAcceptDelegation = {
const submitAcceptDelegation: ISubmitAcceptDelegation = {
content: {
delegationData,
signatures: {
invitee: signature,
inviter: requestAcceptDelegation.content.signatures.inviter,
},
},
type: sdk.MessageBodyType.SUBMIT_ACCEPT_DELEGATION,
type: MessageBodyType.SUBMIT_ACCEPT_DELEGATION,
}
await MessageRepository.singleSend(
submitAcceptDelegation,
Expand All @@ -359,12 +371,12 @@ class BsDelegation {
)

// inform about delegation creation
const informCreateDelegation: sdk.IInformCreateDelegation = {
const informCreateDelegation: IInformCreateDelegation = {
content: {
delegationId: delegation.id,
isPCR,
},
type: sdk.MessageBodyType.INFORM_CREATE_DELEGATION,
type: MessageBodyType.INFORM_CREATE_DELEGATION,
}
await MessageRepository.singleSend(
informCreateDelegation,
Expand Down
10 changes: 6 additions & 4 deletions src/containers/CtypeCreate/CtypeCreate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as sdk from '@kiltprotocol/sdk-js'
import { BlockchainUtils, CType, ICTypeMetadata } from '@kiltprotocol/sdk-js'
import React from 'react'
import { connect, MapStateToProps } from 'react-redux'
import { RouteComponentProps, withRouter } from 'react-router'
Expand Down Expand Up @@ -73,8 +73,8 @@ class CTypeCreate extends React.Component<Props, State> {
const { connected, isValid, cType: stateCtype } = this.state
stateCtype.owner = selectedIdentity?.identity.address
if (selectedIdentity && connected && isValid) {
let cType: sdk.CType
let metaData: sdk.ICTypeMetadata
let cType: CType
let metaData: ICTypeMetadata
try {
const inputICTypeWithMetadata = fromInputModel(stateCtype)
;({ cType, metaData } = inputICTypeWithMetadata)
Expand All @@ -99,7 +99,9 @@ class CTypeCreate extends React.Component<Props, State> {
}

const tx = cType.store(selectedIdentity.identity)
await sdk.Blockchain.submitSignedTx(await tx)
await BlockchainUtils.submitSignedTx(await tx, {
resolveOn: BlockchainUtils.IS_IN_BLOCK,
})
tx.then(() => {
blockUi.updateMessage(
`CTYPE stored on blockchain,\nnow registering CTYPE`
Expand Down
Loading