diff --git a/dlt-connector/src/interactions/backendToDb/transaction/CreateTransactionRecipe.context.test.ts b/dlt-connector/src/interactions/backendToDb/transaction/CreateTransactionRecipe.context.test.ts index cfd022991..03474ff28 100644 --- a/dlt-connector/src/interactions/backendToDb/transaction/CreateTransactionRecipe.context.test.ts +++ b/dlt-connector/src/interactions/backendToDb/transaction/CreateTransactionRecipe.context.test.ts @@ -1,6 +1,7 @@ import 'reflect-metadata' import { Account } from '@entity/Account' import { Community } from '@entity/Community' +import { User } from '@entity/User' import { TestDB } from '@test/TestDB' import { Decimal } from 'decimal.js-light' import { v4 } from 'uuid' @@ -26,6 +27,7 @@ import { TransactionLoggingView } from '@/logging/TransactionLogging.view' import { AddCommunityContext } from '../community/AddCommunity.context' import { CreateTransactionRecipeContext } from './CreateTransationRecipe.context' +import { iotaTopicFromCommunityUUID } from '@/utils/typeConverter' jest.mock('@typeorm/DataSource', () => ({ getDataSource: jest.fn(() => TestDB.instance.dbConnect), @@ -35,6 +37,12 @@ CONFIG.IOTA_HOME_COMMUNITY_SEED = '034b0229a2ba4e98e1cc5e8767dca886279b484303ffa const homeCommunityUuid = v4() const foreignCommunityUuid = v4() +type UserSet = { + identifier: UserIdentifier + user: User + account: Account +} + function createUserIdentifier(userUuid: string, communityUuid: string): UserIdentifier { const user = new UserIdentifier() user.uuid = userUuid @@ -46,13 +54,6 @@ const keyPair = new KeyPair(new Mnemonic(CONFIG.IOTA_HOME_COMMUNITY_SEED)) const foreignKeyPair = new KeyPair( new Mnemonic('5d4e163c078cc6b51f5c88f8422bc8f21d1d59a284515ab1ea79e1c176ebec50'), ) -const moderator = createUserIdentifier('ff8bbdcb-fc8b-4b5d-98e3-8bd7e1afcdbb', homeCommunityUuid) -const firstUser = createUserIdentifier('8e47e32e-0182-4099-b94d-0cac567d1392', homeCommunityUuid) -const secondUser = createUserIdentifier('9c8611dd-ee93-4cdb-a600-396c2ca91cc7', homeCommunityUuid) -const foreignUser = createUserIdentifier( - 'b0155716-5219-4c50-b3d3-0757721ae0d2', - foreignCommunityUuid, -) function createUserAndAccount(userIdentifier: UserIdentifier): Account { const accountDraft = new UserAccountDraft() @@ -75,6 +76,27 @@ function createUserAndAccount(userIdentifier: UserIdentifier): Account { return account } +function createUserSet(userUuid: string, communityUuid: string): UserSet { + const identifier = createUserIdentifier(userUuid, communityUuid) + const account = createUserAndAccount(identifier) + if (!account.user) { + throw Error('user missing') + } + return { + identifier, + account, + user: account.user, + } +} + +let moderator: UserSet +let firstUser: UserSet +let secondUser: UserSet +let foreignUser: UserSet + +const topic = iotaTopicFromCommunityUUID(homeCommunityUuid) +const foreignTopic = iotaTopicFromCommunityUUID(foreignCommunityUuid) + describe('interactions/backendToDb/transaction/Create Transaction Recipe Context Test', () => { beforeAll(async () => { await TestDB.instance.setupTestDB() @@ -97,13 +119,17 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context foreignKeyPair.fillInCommunityKeys(foreignCommunity) foreignCommunity.save() - const accounts = [ - createUserAndAccount(moderator), - createUserAndAccount(firstUser), - createUserAndAccount(secondUser), - createUserAndAccount(foreignUser), - ] - await Account.save(accounts) + moderator = createUserSet('ff8bbdcb-fc8b-4b5d-98e3-8bd7e1afcdbb', homeCommunityUuid) + firstUser = createUserSet('8e47e32e-0182-4099-b94d-0cac567d1392', homeCommunityUuid) + secondUser = createUserSet('9c8611dd-ee93-4cdb-a600-396c2ca91cc7', homeCommunityUuid) + foreignUser = createUserSet('b0155716-5219-4c50-b3d3-0757721ae0d2', foreignCommunityUuid) + + await Account.save([ + moderator.account, + firstUser.account, + secondUser.account, + foreignUser.account, + ]) }) afterAll(async () => { @@ -115,8 +141,8 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context creationTransactionDraft.amount = new Decimal('2000') creationTransactionDraft.backendTransactionId = 1 creationTransactionDraft.createdAt = new Date().toISOString() - creationTransactionDraft.linkedUser = moderator - creationTransactionDraft.user = firstUser + creationTransactionDraft.linkedUser = moderator.identifier + creationTransactionDraft.user = firstUser.identifier creationTransactionDraft.type = InputTransactionType.CREATION creationTransactionDraft.targetDate = new Date().toISOString() const context = new CreateTransactionRecipeContext(creationTransactionDraft) @@ -124,26 +150,19 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context const transaction = context.getTransactionRecipe() // console.log(new TransactionLoggingView(transaction)) - expect( - transaction.signingAccount?.derive2Pubkey.compare( - Buffer.from('19ea7313abc54f120ee0041e5b3b63e34562b0a19b96fa3e6e23cc9bff827a36', 'hex'), - ), - ).toBe(0) - expect( - transaction.recipientAccount?.derive2Pubkey.compare( - Buffer.from('5875e1a5e101301cc774b7462566ec2d1a0b04a091dab2e32cecd713b3346224', 'hex'), - ), - ).toBe(0) - expect(transaction).toMatchObject({ type: TransactionType.GRADIDO_CREATION, protocolVersion: '3.3', community: { - rootPubkey: Buffer.from( - '07cbf56d4b6b7b188c5f6250c0f4a01d0e44e1d422db1935eb375319ad9f9af0', - 'hex', - ), + rootPubkey: keyPair.publicKey, foreign: 0, + iotaTopic: topic, + }, + signingAccount: { + derive2Pubkey: moderator.account.derive2Pubkey, + }, + recipientAccount: { + derive2Pubkey: firstUser.account.derive2Pubkey, }, amount: new Decimal(2000), backendTransactions: [ @@ -158,11 +177,8 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context expect(body.creation).toBeDefined() if (!body.creation) throw new Error() const bodyReceiverPubkey = Buffer.from(body.creation.recipient.pubkey) - expect( - bodyReceiverPubkey.compare( - Buffer.from('5875e1a5e101301cc774b7462566ec2d1a0b04a091dab2e32cecd713b3346224', 'hex'), - ), - ).toBe(0) + expect(bodyReceiverPubkey.compare(firstUser.account.derive2Pubkey)).toBe(0) + expect(body).toMatchObject({ type: CrossGroupType.LOCAL, creation: { @@ -176,68 +192,221 @@ describe('interactions/backendToDb/transaction/Create Transaction Recipe Context it('local send transaction', async () => { const sendTransactionDraft = new TransactionDraft() sendTransactionDraft.amount = new Decimal('100') - sendTransactionDraft.backendTransactionId = 1 + sendTransactionDraft.backendTransactionId = 2 sendTransactionDraft.createdAt = new Date().toISOString() - sendTransactionDraft.linkedUser = secondUser - sendTransactionDraft.user = firstUser + sendTransactionDraft.linkedUser = secondUser.identifier + sendTransactionDraft.user = firstUser.identifier sendTransactionDraft.type = InputTransactionType.SEND sendTransactionDraft.targetDate = new Date().toISOString() const context = new CreateTransactionRecipeContext(sendTransactionDraft) await context.run() const transaction = context.getTransactionRecipe() + + // console.log(new TransactionLoggingView(transaction)) + expect(transaction).toMatchObject({ + type: TransactionType.GRADIDO_TRANSFER, + protocolVersion: '3.3', + community: { + rootPubkey: keyPair.publicKey, + foreign: 0, + iotaTopic: topic, + }, + signingAccount: { + derive2Pubkey: firstUser.account.derive2Pubkey, + }, + recipientAccount: { + derive2Pubkey: secondUser.account.derive2Pubkey, + }, + amount: new Decimal(100), + backendTransactions: [ + { + typeId: InputTransactionType.SEND, + }, + ], + }) + const body = TransactionBody.fromBodyBytes(transaction.bodyBytes) - console.log(new TransactionBodyLoggingView(body)) - console.log(new TransactionLoggingView(transaction)) + // console.log(new TransactionBodyLoggingView(body)) + expect(body.transfer).toBeDefined() + if (!body.transfer) throw new Error() + expect(Buffer.from(body.transfer.recipient).compare(secondUser.account.derive2Pubkey)).toBe(0) + expect(Buffer.from(body.transfer.sender.pubkey).compare(firstUser.account.derive2Pubkey)).toBe( + 0, + ) + expect(body).toMatchObject({ + type: CrossGroupType.LOCAL, + transfer: { + sender: { + amount: '100', + }, + }, + }) }) it('local recv transaction', async () => { const recvTransactionDraft = new TransactionDraft() recvTransactionDraft.amount = new Decimal('100') - recvTransactionDraft.backendTransactionId = 1 + recvTransactionDraft.backendTransactionId = 3 recvTransactionDraft.createdAt = new Date().toISOString() - recvTransactionDraft.linkedUser = secondUser - recvTransactionDraft.user = firstUser + recvTransactionDraft.linkedUser = firstUser.identifier + recvTransactionDraft.user = secondUser.identifier recvTransactionDraft.type = InputTransactionType.RECEIVE recvTransactionDraft.targetDate = new Date().toISOString() const context = new CreateTransactionRecipeContext(recvTransactionDraft) await context.run() const transaction = context.getTransactionRecipe() + // console.log(new TransactionLoggingView(transaction)) + expect(transaction).toMatchObject({ + type: TransactionType.GRADIDO_TRANSFER, + protocolVersion: '3.3', + community: { + rootPubkey: keyPair.publicKey, + foreign: 0, + iotaTopic: topic, + }, + signingAccount: { + derive2Pubkey: firstUser.account.derive2Pubkey, + }, + recipientAccount: { + derive2Pubkey: secondUser.account.derive2Pubkey, + }, + amount: new Decimal(100), + backendTransactions: [ + { + typeId: InputTransactionType.RECEIVE, + }, + ], + }) + const body = TransactionBody.fromBodyBytes(transaction.bodyBytes) - console.log(new TransactionBodyLoggingView(body)) - console.log(new TransactionLoggingView(transaction)) + // console.log(new TransactionBodyLoggingView(body)) + expect(body.transfer).toBeDefined() + if (!body.transfer) throw new Error() + expect(Buffer.from(body.transfer.recipient).compare(secondUser.account.derive2Pubkey)).toBe(0) + expect(Buffer.from(body.transfer.sender.pubkey).compare(firstUser.account.derive2Pubkey)).toBe( + 0, + ) + expect(body).toMatchObject({ + type: CrossGroupType.LOCAL, + transfer: { + sender: { + amount: '100', + }, + }, + }) }) it('cross group send transaction', async () => { const crossGroupSendTransactionDraft = new TransactionDraft() crossGroupSendTransactionDraft.amount = new Decimal('100') - crossGroupSendTransactionDraft.backendTransactionId = 1 + crossGroupSendTransactionDraft.backendTransactionId = 4 crossGroupSendTransactionDraft.createdAt = new Date().toISOString() - crossGroupSendTransactionDraft.linkedUser = foreignUser - crossGroupSendTransactionDraft.user = firstUser + crossGroupSendTransactionDraft.linkedUser = foreignUser.identifier + crossGroupSendTransactionDraft.user = firstUser.identifier crossGroupSendTransactionDraft.type = InputTransactionType.SEND crossGroupSendTransactionDraft.targetDate = new Date().toISOString() const context = new CreateTransactionRecipeContext(crossGroupSendTransactionDraft) await context.run() const transaction = context.getTransactionRecipe() + // console.log(new TransactionLoggingView(transaction)) + expect(transaction).toMatchObject({ + type: TransactionType.GRADIDO_TRANSFER, + protocolVersion: '3.3', + community: { + rootPubkey: keyPair.publicKey, + foreign: 0, + iotaTopic: topic, + }, + otherCommunity: { + rootPubkey: foreignKeyPair.publicKey, + foreign: 1, + iotaTopic: foreignTopic, + }, + signingAccount: { + derive2Pubkey: firstUser.account.derive2Pubkey, + }, + recipientAccount: { + derive2Pubkey: foreignUser.account.derive2Pubkey, + }, + amount: new Decimal(100), + backendTransactions: [ + { + typeId: InputTransactionType.SEND, + }, + ], + }) const body = TransactionBody.fromBodyBytes(transaction.bodyBytes) - console.log(new TransactionBodyLoggingView(body)) - console.log(new TransactionLoggingView(transaction)) + // console.log(new TransactionBodyLoggingView(body)) + expect(body.transfer).toBeDefined() + if (!body.transfer) throw new Error() + expect(Buffer.from(body.transfer.recipient).compare(foreignUser.account.derive2Pubkey)).toBe(0) + expect(Buffer.from(body.transfer.sender.pubkey).compare(firstUser.account.derive2Pubkey)).toBe( + 0, + ) + expect(body).toMatchObject({ + type: CrossGroupType.OUTBOUND, + transfer: { + sender: { + amount: '100', + }, + }, + }) }) it('cross group recv transaction', async () => { const crossGroupRecvTransactionDraft = new TransactionDraft() crossGroupRecvTransactionDraft.amount = new Decimal('100') - crossGroupRecvTransactionDraft.backendTransactionId = 1 + crossGroupRecvTransactionDraft.backendTransactionId = 5 crossGroupRecvTransactionDraft.createdAt = new Date().toISOString() - crossGroupRecvTransactionDraft.linkedUser = foreignUser - crossGroupRecvTransactionDraft.user = firstUser + crossGroupRecvTransactionDraft.linkedUser = firstUser.identifier + crossGroupRecvTransactionDraft.user = foreignUser.identifier crossGroupRecvTransactionDraft.type = InputTransactionType.RECEIVE crossGroupRecvTransactionDraft.targetDate = new Date().toISOString() const context = new CreateTransactionRecipeContext(crossGroupRecvTransactionDraft) await context.run() const transaction = context.getTransactionRecipe() + // console.log(new TransactionLoggingView(transaction)) + expect(transaction).toMatchObject({ + type: TransactionType.GRADIDO_TRANSFER, + protocolVersion: '3.3', + community: { + rootPubkey: keyPair.publicKey, + foreign: 0, + iotaTopic: topic, + }, + otherCommunity: { + rootPubkey: foreignKeyPair.publicKey, + foreign: 1, + iotaTopic: foreignTopic, + }, + signingAccount: { + derive2Pubkey: firstUser.account.derive2Pubkey, + }, + recipientAccount: { + derive2Pubkey: foreignUser.account.derive2Pubkey, + }, + amount: new Decimal(100), + backendTransactions: [ + { + typeId: InputTransactionType.RECEIVE, + }, + ], + }) const body = TransactionBody.fromBodyBytes(transaction.bodyBytes) - console.log(new TransactionBodyLoggingView(body)) - console.log(new TransactionLoggingView(transaction)) + // console.log(new TransactionBodyLoggingView(body)) + expect(body.transfer).toBeDefined() + if (!body.transfer) throw new Error() + expect(Buffer.from(body.transfer.recipient).compare(foreignUser.account.derive2Pubkey)).toBe(0) + expect(Buffer.from(body.transfer.sender.pubkey).compare(firstUser.account.derive2Pubkey)).toBe( + 0, + ) + expect(body).toMatchObject({ + type: CrossGroupType.INBOUND, + transfer: { + sender: { + amount: '100', + }, + }, + }) }) }) diff --git a/dlt-connector/src/logging/TransferAmountLogging.view.ts b/dlt-connector/src/logging/TransferAmountLogging.view.ts index 8d320b99f..2384bfdb4 100644 --- a/dlt-connector/src/logging/TransferAmountLogging.view.ts +++ b/dlt-connector/src/logging/TransferAmountLogging.view.ts @@ -10,7 +10,7 @@ export class TransferAmountLoggingView extends AbstractLoggingView { // eslint-disable-next-line @typescript-eslint/no-explicit-any public toJSON(): any { return { - publicKey: Buffer.from(this.self.pubkey).toString(this.bufferStringFormat), + pubkey: Buffer.from(this.self.pubkey).toString(this.bufferStringFormat), amount: this.self.amount, communityId: this.self.communityId, }