From 4b347a98366028d81fa74caf64e319116d814e76 Mon Sep 17 00:00:00 2001 From: Alex Shorsher Date: Fri, 27 May 2022 11:58:23 -0400 Subject: [PATCH] provide message type in *MessageBody utils Signed-off-by: Alex Shorsher --- server/src/controllers/tokens.ts | 25 +++++++++++++------------ server/src/utils.ts | 7 ++++--- server/test/messages.test.ts | 4 ++-- server/test/tokens.test.ts | 12 +++++++++--- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/server/src/controllers/tokens.ts b/server/src/controllers/tokens.ts index b49ddd5..33d315e 100644 --- a/server/src/controllers/tokens.ts +++ b/server/src/controllers/tokens.ts @@ -14,6 +14,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { plainToClassFromExist } from 'class-transformer'; import { firefly } from '../clients/firefly'; import { + FF_MESSAGES, formatTemplate, FormDataSchema, getBroadcastMessageBody, @@ -85,8 +86,8 @@ export class TokensController { if (body.messagingMethod) { mintBody.message = body.messagingMethod === 'broadcast' - ? getBroadcastMessageBody(body) - : getPrivateMessageBody(body); + ? getBroadcastMessageBody(body, undefined, FF_MESSAGES.TRANSFER_BROADCAST) + : getPrivateMessageBody(body, undefined, FF_MESSAGES.TRANSFER_PRIVATE); } const transfer = await firefly.mintTokens(mintBody); return { type: 'token_transfer', id: transfer.localId }; @@ -112,8 +113,8 @@ export class TokensController { if (body.messagingMethod) { mintBody.message = body.messagingMethod === 'broadcast' - ? getBroadcastMessageBody(body, data.id) - : getPrivateMessageBody(body, data.id); + ? getBroadcastMessageBody(body, data.id, FF_MESSAGES.TRANSFER_BROADCAST) + : getPrivateMessageBody(body, data.id, FF_MESSAGES.TRANSFER_PRIVATE); } const transfer = await firefly.mintTokens(mintBody); return { type: 'token_transfer', id: transfer.localId }; @@ -133,8 +134,8 @@ export class TokensController { if (body.messagingMethod) { burnBody.message = body.messagingMethod === 'broadcast' - ? getBroadcastMessageBody(body) - : getPrivateMessageBody(body); + ? getBroadcastMessageBody(body, undefined, FF_MESSAGES.TRANSFER_BROADCAST) + : getPrivateMessageBody(body, undefined, FF_MESSAGES.TRANSFER_PRIVATE); } const transfer = await firefly.burnTokens(burnBody); return { type: 'token_transfer', id: transfer.localId }; @@ -160,8 +161,8 @@ export class TokensController { if (body.messagingMethod) { burnBody.message = body.messagingMethod === 'broadcast' - ? getBroadcastMessageBody(body, data.id) - : getPrivateMessageBody(body, data.id); + ? getBroadcastMessageBody(body, data.id, FF_MESSAGES.TRANSFER_BROADCAST) + : getPrivateMessageBody(body, data.id, FF_MESSAGES.TRANSFER_PRIVATE); } const transfer = await firefly.burnTokens(burnBody); return { type: 'token_transfer', id: transfer.localId }; @@ -182,8 +183,8 @@ export class TokensController { if (body.messagingMethod) { transferBody.message = body.messagingMethod === 'broadcast' - ? getBroadcastMessageBody(body) - : getPrivateMessageBody(body); + ? getBroadcastMessageBody(body, undefined, FF_MESSAGES.TRANSFER_BROADCAST) + : getPrivateMessageBody(body, undefined, FF_MESSAGES.TRANSFER_PRIVATE); } const transfer = await firefly.transferTokens(transferBody); return { type: 'token_transfer', id: transfer.localId }; @@ -210,8 +211,8 @@ export class TokensController { if (body.messagingMethod) { transferBody.message = body.messagingMethod === 'broadcast' - ? getBroadcastMessageBody(body, data.id) - : getPrivateMessageBody(body, data.id); + ? getBroadcastMessageBody(body, data.id, FF_MESSAGES.TRANSFER_BROADCAST) + : getPrivateMessageBody(body, data.id, FF_MESSAGES.TRANSFER_PRIVATE); } const transfer = await firefly.transferTokens(transferBody); return { type: 'token_transfer', id: transfer.localId }; diff --git a/server/src/utils.ts b/server/src/utils.ts index 5090954..603a31b 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -111,24 +111,25 @@ export function quoteAndEscape(varName: string, options?: QuoteOptions) { return result; } -export function getBroadcastMessageBody(body: BroadcastValue, blobId?: string) { +export function getBroadcastMessageBody(body: BroadcastValue, blobId?: string, messageType?: FF_MESSAGES) { const dataBody = blobId ? { id: blobId } : getMessageBody(body); return { header: { tag: body.tag || undefined, topics: body.topic ? [body.topic] : undefined, + type: messageType || undefined }, data: [dataBody], }; } -export function getPrivateMessageBody(body: PrivateValue, blobId?: string) { +export function getPrivateMessageBody(body: PrivateValue, blobId?: string, messageType?: FF_MESSAGES) { const dataBody = blobId ? { id: blobId } : getMessageBody(body); return { header: { tag: body.tag || undefined, topics: body.topic ? [body.topic] : undefined, - type: FF_MESSAGES.TRANSFER_PRIVATE, + type: messageType || undefined }, group: { members: body.recipients.map((r) => ({ identity: r })), diff --git a/server/test/messages.test.ts b/server/test/messages.test.ts index 89c7021..4206472 100644 --- a/server/test/messages.test.ts +++ b/server/test/messages.test.ts @@ -79,7 +79,7 @@ describe('Messages', () => { expect(mockFireFly.uploadDataBlob).not.toHaveBeenCalled(); expect(mockFireFly.sendPrivateMessage).toHaveBeenCalledWith({ - header: { tag: 'test-tag', topics: ['test-topic'], type: 'transfer_private' }, + header: { tag: 'test-tag', topics: ['test-topic']}, group: { members: [{ identity: 'alpha' }, { identity: 'beta' }], }, @@ -109,7 +109,7 @@ describe('Messages', () => { expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt'); expect(mockFireFly.sendPrivateMessage).toHaveBeenCalledWith({ - header: { tag: 'test-tag', topics: undefined, type: 'transfer_private' }, + header: { tag: 'test-tag', topics: undefined }, group: { members: [{ identity: 'alpha' }, { identity: 'beta' }], }, diff --git a/server/test/tokens.test.ts b/server/test/tokens.test.ts index 6b0c47a..5ebc43c 100644 --- a/server/test/tokens.test.ts +++ b/server/test/tokens.test.ts @@ -119,6 +119,7 @@ describe('Tokens', () => { header: { tag: 'test', topics: ['one'], + type: "transfer_broadcast" }, }, pool: 'my-pool', @@ -160,6 +161,7 @@ describe('Tokens', () => { header: { tag: 'test', topics: ['one'], + type: "transfer_broadcast" }, }, pool: 'my-pool', @@ -281,7 +283,7 @@ describe('Tokens', () => { .expect({ type: 'token_transfer', id: 'transfer1' }); expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt'); - expect(mockFireFly.mintTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined}}, "pool": "test-pool", "tokenIndex": ""}); + expect(mockFireFly.mintTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined, "type": "transfer_broadcast"}}, "pool": "test-pool", "tokenIndex": ""}); }); test('Mint tokens with private blob', async () => { @@ -372,6 +374,7 @@ describe('Tokens', () => { header: { tag: 'test', topics: ['one'], + type: "transfer_broadcast" }, }, pool: 'my-pool', @@ -413,6 +416,7 @@ describe('Tokens', () => { header: { tag: 'test', topics: ['one'], + type: "transfer_broadcast" }, }, pool: 'my-pool', @@ -442,7 +446,7 @@ describe('Tokens', () => { .expect({ type: 'token_transfer', id: 'transfer1' }); expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt'); - expect(mockFireFly.burnTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined}}, "pool": "test-pool"}); + expect(mockFireFly.burnTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined, "type": "transfer_broadcast"}}, "pool": "test-pool"}); }); test('Burn tokens with private blob', async () => { @@ -624,6 +628,7 @@ describe('Tokens', () => { header: { tag: 'test', topics: ['one'], + type: "transfer_broadcast", }, }, pool: 'my-pool', @@ -667,6 +672,7 @@ describe('Tokens', () => { header: { tag: 'test', topics: ['one'], + type: "transfer_broadcast", }, }, pool: 'my-pool', @@ -792,7 +798,7 @@ describe('Tokens', () => { .expect({ type: 'token_transfer', id: 'transfer1' }); expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt'); - expect(mockFireFly.transferTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined}}, "pool": "test-pool", "to": "0x111", "tokenIndex": undefined}); + expect(mockFireFly.transferTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined, "type": "transfer_broadcast"}}, "pool": "test-pool", "to": "0x111", "tokenIndex": undefined}); }); test('Transfer tokens with private blob', async () => {