change logic, transaction.community is now community on which blockchain it is, not longer signerCommunity, transaction.otherCommunity is now the same like TransactionBody.otherGroup

This commit is contained in:
einhornimmond 2024-02-03 16:30:07 +01:00
parent cc8174a29b
commit 95b4810631
10 changed files with 79 additions and 30 deletions

View File

@ -59,6 +59,10 @@ export class TransactionBuilder {
return this.transaction.community
}
public getOtherCommunity(): Community | undefined {
return this.transaction.otherCommunity
}
public setSigningAccount(signingAccount: Account): TransactionBuilder {
this.transaction.signingAccount = signingAccount
return this
@ -103,22 +107,18 @@ export class TransactionBuilder {
return this
}
public async setSenderCommunityFromSenderUser(
senderUser: UserIdentifier,
): Promise<TransactionBuilder> {
public async setCommunityFromUser(user: UserIdentifier): Promise<TransactionBuilder> {
// get sender community
const community = await CommunityRepository.getCommunityForUserIdentifier(senderUser)
const community = await CommunityRepository.getCommunityForUserIdentifier(user)
if (!community) {
throw new LogError("couldn't find community for transaction")
}
return this.setCommunity(community)
}
public async setOtherCommunityFromRecipientUser(
recipientUser: UserIdentifier,
): Promise<TransactionBuilder> {
public async setOtherCommunityFromUser(user: UserIdentifier): Promise<TransactionBuilder> {
// get recipient community
const otherCommunity = await CommunityRepository.getCommunityForUserIdentifier(recipientUser)
const otherCommunity = await CommunityRepository.getCommunityForUserIdentifier(user)
return this.setOtherCommunity(otherCommunity)
}

View File

@ -44,8 +44,8 @@ describe('data/transaction.logic', () => {
b = new Transaction()
b.community = new Community()
b.communityId = 1
b.otherCommunityId = 2
b.communityId = 2
b.otherCommunityId = 1
b.id = 2
b.signingAccountId = 1
b.recipientAccountId = 2

View File

@ -81,8 +81,8 @@ export class TransactionLogic {
if (
this.self.signingAccountId !== otherTransaction.signingAccountId ||
this.self.recipientAccountId !== otherTransaction.recipientAccountId ||
this.self.communityId !== otherTransaction.communityId ||
this.self.otherCommunityId !== otherTransaction.otherCommunityId ||
this.self.communityId !== otherTransaction.otherCommunityId ||
this.self.otherCommunityId !== otherTransaction.communityId ||
this.self.createdAt.getTime() !== otherTransaction.createdAt.getTime()
) {
logger.info('transaction a and b are not pairs', {

View File

@ -1,11 +1,10 @@
// https://www.npmjs.com/package/@apollo/protobufjs
import { IsEnum, IsObject, IsPositive, ValidateNested } from 'class-validator'
import { Decimal } from 'decimal.js-light'
import { InputType, Field, Int } from 'type-graphql'
import { InputTransactionType } from '@enum/InputTransactionType'
import { isValidDateString } from '@validator/DateString'
import { IsPositiveDecimal } from '@validator/Decimal'
import { IsEnum, IsObject, IsPositive, ValidateNested } from 'class-validator'
import { Decimal } from 'decimal.js-light'
import { InputType, Field, Int } from 'type-graphql'
import { UserIdentifier } from './UserIdentifier'

View File

@ -9,9 +9,9 @@ export class UserIdentifier {
@IsUUID('4')
uuid: string
@Field(() => String, { nullable: true })
@Field(() => String)
@IsUUID('4')
communityUuid?: string
communityUuid: string
@Field(() => Int, { defaultValue: 1, nullable: true })
@IsPositive()

View File

@ -1,8 +1,11 @@
import { Community } from '@entity/Community'
import { CrossGroupType } from '@/data/proto/3_3/enum/CrossGroupType'
import { TransactionErrorType } from '@/graphql/enum/TransactionErrorType'
import { TransactionDraft } from '@/graphql/input/TransactionDraft'
import { UserIdentifier } from '@/graphql/input/UserIdentifier'
import { TransactionError } from '@/graphql/model/TransactionError'
import { iotaTopicFromCommunityUUID } from '@/utils/typeConverter'
export abstract class AbstractTransactionRole {
// eslint-disable-next-line no-useless-constructor
@ -26,7 +29,7 @@ export abstract class AbstractTransactionRole {
* OUTBOUND: stored on 'gdd1', otherGroup: 'gdd2'
* INBOUND: goes to receiver, stored on receiver community blockchain
* INBOUND: stored on 'gdd2', otherGroup: 'gdd1'
* @returns
* @returns iota topic
*/
public getOtherGroup(): string {
let user: UserIdentifier
@ -42,7 +45,7 @@ export abstract class AbstractTransactionRole {
'missing sender/signing user community id for cross group transaction',
)
}
return user.communityUuid
return iotaTopicFromCommunityUUID(user.communityUuid)
case CrossGroupType.OUTBOUND:
user = this.getRecipientUser()
if (!user.communityUuid) {
@ -51,7 +54,7 @@ export abstract class AbstractTransactionRole {
'missing recipient user community id for cross group transaction',
)
}
return user.communityUuid
return iotaTopicFromCommunityUUID(user.communityUuid)
default:
throw new TransactionError(
TransactionErrorType.NOT_IMPLEMENTED_YET,

View File

@ -272,6 +272,7 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context
)
expect(body).toMatchObject({
type: CrossGroupType.OUTBOUND,
otherGroup: foreignTopic,
transfer: {
sender: {
amount: '100',
@ -296,15 +297,15 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context
type: TransactionType.GRADIDO_TRANSFER,
protocolVersion: '3.3',
community: {
rootPubkey: keyPair.publicKey,
foreign: 0,
iotaTopic: topic,
},
otherCommunity: {
rootPubkey: foreignKeyPair.publicKey,
foreign: 1,
iotaTopic: foreignTopic,
},
otherCommunity: {
rootPubkey: keyPair.publicKey,
foreign: 0,
iotaTopic: topic,
},
signingAccount: {
derive2Pubkey: firstUser.account.derive2Pubkey,
},
@ -328,6 +329,7 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context
)
expect(body).toMatchObject({
type: CrossGroupType.INBOUND,
otherGroup: topic,
transfer: {
sender: {
amount: '100',

View File

@ -1,5 +1,12 @@
import { Community } from '@entity/Community'
import { CommunityRepository } from '@/data/Community.repository'
import { CrossGroupType } from '@/data/proto/3_3/enum/CrossGroupType'
import { TransactionErrorType } from '@/graphql/enum/TransactionErrorType'
import { UserIdentifier } from '@/graphql/input/UserIdentifier'
import { TransactionError } from '@/graphql/model/TransactionError'
import { logger } from '@/logging/logger'
import { UserIdentifierLoggingView } from '@/logging/UserIdentifierLogging.view'
import { AbstractTransactionRole } from './AbstractTransaction.role'
@ -15,4 +22,26 @@ export class CreationTransactionRole extends AbstractTransactionRole {
public getCrossGroupType(): CrossGroupType {
return CrossGroupType.LOCAL
}
public async getCommunity(): Promise<Community> {
if (this.self.user.communityUuid !== this.self.linkedUser.communityUuid) {
throw new TransactionError(
TransactionErrorType.LOGIC_ERROR,
'mismatch community uuids on creation transaction',
)
}
const community = await CommunityRepository.getCommunityForUserIdentifier(this.self.user)
if (!community) {
logger.error(
'missing community for user identifier',
new UserIdentifierLoggingView(this.self.user),
)
throw new TransactionError(TransactionErrorType.NOT_FOUND, "couldn't find community for user")
}
return community
}
public async getOtherCommunity(): Promise<Community | null> {
return null
}
}

View File

@ -1,7 +1,9 @@
import { Community } from '@entity/Community'
import { Transaction } from '@entity/Transaction'
import { AccountLogic } from '@/data/Account.logic'
import { KeyPair } from '@/data/KeyPair'
import { CrossGroupType } from '@/data/proto/3_3/enum/CrossGroupType'
import { TransactionBodyBuilder } from '@/data/proto/TransactionBody.builder'
import { TransactionBuilder } from '@/data/Transaction.builder'
import { UserRepository } from '@/data/User.repository'
@ -53,12 +55,15 @@ export class TransactionRecipeRole {
this.transactionBuilder
.fromTransactionBodyBuilder(transactionBodyBuilder)
.addBackendTransaction(transactionDraft)
await this.transactionBuilder.setSenderCommunityFromSenderUser(signingUser)
await this.transactionBuilder.setCommunityFromUser(transactionDraft.user)
if (recipientUser.communityUuid !== signingUser.communityUuid) {
await this.transactionBuilder.setOtherCommunityFromRecipientUser(recipientUser)
await this.transactionBuilder.setOtherCommunityFromUser(transactionDraft.linkedUser)
}
const transaction = this.transactionBuilder.getTransaction()
const communityKeyPair = new KeyPair(this.transactionBuilder.getCommunity())
const communityKeyPair = new KeyPair(
this.getSigningCommunity(transactionTypeRole.getCrossGroupType()),
)
const accountLogic = new AccountLogic(signingAccount)
// sign
this.transactionBuilder.setSignature(
@ -67,6 +72,17 @@ export class TransactionRecipeRole {
return this
}
public getSigningCommunity(crossGroupType: CrossGroupType): Community {
if (crossGroupType === CrossGroupType.INBOUND) {
const otherCommunity = this.transactionBuilder.getOtherCommunity()
if (!otherCommunity) {
throw new TransactionError(TransactionErrorType.NOT_FOUND, 'missing other community')
}
return otherCommunity
}
return this.transactionBuilder.getCommunity()
}
public getTransaction(): Transaction {
return this.transactionBuilder.getTransaction()
}

View File

@ -63,7 +63,7 @@
"@entity/*": ["../dlt-database/entity/*", "../../dlt-database/build/entity/*"]
},
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": ["node_modules/@types"], /* List of folders to include type definitions from. */
"typeRoots": ["node_modules/@types", "@types"], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */