Skip to content

Commit

Permalink
chore: [#1] refactor hashMessageContent to use struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mdnorman committed Sep 14, 2022
1 parent ff421fd commit 0d7eb46
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 27 deletions.
32 changes: 28 additions & 4 deletions spec/contracts/facets/message/BasicMessageCostFacet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ describe('post', () => {
await message.connect(ACCOUNT1).post({ text: 'the message', uri: 'the uri', uriType: URIType.Link, messageRef: 0 });
const id1 = await erc721Enumerable.tokenByIndex(0);

expect<BigNumber>(id1).toEqual(hashMessageContent(ACCOUNT1.address, 'the message', 'the uri', URIType.Link, 0));
expect<BigNumber>(id1).toEqual(
hashMessageContent(ACCOUNT1.address, {
text: 'the message',
uri: 'the uri',
uriType: URIType.Link,
messageRef: 0,
}),
);
expect<string>(await message.text(id1)).toEqual('the message');
expect<[string, URIType]>(await message.uri(id1)).toEqual(['the uri', URIType.Link]);
expect<BigNumber>(await message.messageRef(id1)).toEqBN(0);
Expand All @@ -61,7 +68,12 @@ describe('post', () => {
const id2 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id2).toEqual(
hashMessageContent(ACCOUNT1.address, 'the message 2', 'the uri 2', URIType.Image, id1),
hashMessageContent(ACCOUNT1.address, {
text: 'the message 2',
uri: 'the uri 2',
uriType: URIType.Image,
messageRef: id1,
}),
);
expect<string>(await message.text(id2)).toEqual('the message 2');
expect<[string, URIType]>(await message.uri(id2)).toEqual(['the uri 2', URIType.Image]);
Expand All @@ -77,7 +89,14 @@ describe('post', () => {
.post({ text: 'the message', uri: 'the uri', uriType: URIType.Link, messageRef: 0 }, { value: 100 });
const id1 = await erc721Enumerable.tokenByIndex(0);

expect<BigNumber>(id1).toEqual(hashMessageContent(ACCOUNT1.address, 'the message', 'the uri', URIType.Link, 0));
expect<BigNumber>(id1).toEqual(
hashMessageContent(ACCOUNT1.address, {
text: 'the message',
uri: 'the uri',
uriType: URIType.Link,
messageRef: 0,
}),
);
expect<string>(await message.text(id1)).toEqual('the message');
expect<[string, URIType]>(await message.uri(id1)).toEqual(['the uri', URIType.Link]);
expect<BigNumber>(await message.messageRef(id1)).toEqBN(0);
Expand All @@ -88,7 +107,12 @@ describe('post', () => {
const id2 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id2).toEqual(
hashMessageContent(ACCOUNT1.address, 'the message 2', 'the uri 2', URIType.Image, id1),
hashMessageContent(ACCOUNT1.address, {
text: 'the message 2',
uri: 'the uri 2',
uriType: URIType.Image,
messageRef: id1,
}),
);
expect<string>(await message.text(id2)).toEqual('the message 2');
expect<[string, URIType]>(await message.uri(id2)).toEqual(['the uri 2', URIType.Image]);
Expand Down
32 changes: 28 additions & 4 deletions spec/contracts/facets/message/MessageContentLimitsFacet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ describe('post', () => {
await message.connect(ACCOUNT1).post({ text: 'the message', uri: 'the uri', uriType: URIType.Link, messageRef: 0 });
const id1 = await erc721Enumerable.tokenByIndex(0);

expect<BigNumber>(id1).toEqual(hashMessageContent(ACCOUNT1.address, 'the message', 'the uri', URIType.Link, 0));
expect<BigNumber>(id1).toEqual(
hashMessageContent(ACCOUNT1.address, {
text: 'the message',
uri: 'the uri',
uriType: URIType.Link,
messageRef: 0,
}),
);
expect<string>(await message.text(id1)).toEqual('the message');
expect<[string, URIType]>(await message.uri(id1)).toEqual(['the uri', URIType.Link]);
expect<BigNumber>(await message.messageRef(id1)).toEqBN(0);
Expand All @@ -61,7 +68,12 @@ describe('post', () => {
const id2 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id2).toEqual(
hashMessageContent(ACCOUNT1.address, 'the message 2', 'the uri 2', URIType.Image, id1),
hashMessageContent(ACCOUNT1.address, {
text: 'the message 2',
uri: 'the uri 2',
uriType: URIType.Image,
messageRef: id1,
}),
);
expect<string>(await message.text(id2)).toEqual('the message 2');
expect<[string, URIType]>(await message.uri(id2)).toEqual(['the uri 2', URIType.Image]);
Expand All @@ -75,7 +87,14 @@ describe('post', () => {
await message.connect(ACCOUNT1).post({ text: 'the message', uri: 'the uri', uriType: URIType.Link, messageRef: 0 });
const id1 = await erc721Enumerable.tokenByIndex(0);

expect<BigNumber>(id1).toEqual(hashMessageContent(ACCOUNT1.address, 'the message', 'the uri', URIType.Link, 0));
expect<BigNumber>(id1).toEqual(
hashMessageContent(ACCOUNT1.address, {
text: 'the message',
uri: 'the uri',
uriType: URIType.Link,
messageRef: 0,
}),
);
expect<string>(await message.text(id1)).toEqual('the message');
expect<[string, URIType]>(await message.uri(id1)).toEqual(['the uri', URIType.Link]);
expect<BigNumber>(await message.messageRef(id1)).toEqBN(0);
Expand All @@ -86,7 +105,12 @@ describe('post', () => {
const id2 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id2).toEqual(
hashMessageContent(ACCOUNT1.address, 'the message 2', 'the uri 2 two', URIType.Image, id1),
hashMessageContent(ACCOUNT1.address, {
text: 'the message 2',
uri: 'the uri 2 two',
uriType: URIType.Image,
messageRef: id1,
}),
);
expect<string>(await message.text(id2)).toEqual('the message 2');
expect<[string, URIType]>(await message.uri(id2)).toEqual(['the uri 2 two', URIType.Image]);
Expand Down
47 changes: 38 additions & 9 deletions spec/contracts/facets/message/MessageFacet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ describe('post', () => {
await message.connect(ACCOUNT1).post({ text: 'the message', uri: 'the uri', uriType: URIType.Link, messageRef: 0 });
const id1 = await erc721Enumerable.tokenByIndex(0);

expect<BigNumber>(id1).toEqual(hashMessageContent(ACCOUNT1.address, 'the message', 'the uri', URIType.Link, 0));
expect<BigNumber>(id1).toEqual(
hashMessageContent(ACCOUNT1.address, {
text: 'the message',
uri: 'the uri',
uriType: URIType.Link,
messageRef: 0,
}),
);
expect<string>(await message.text(id1)).toEqual('the message');
expect<[string, URIType]>(await message.uri(id1)).toEqual(['the uri', URIType.Link]);
expect<BigNumber>(await message.messageRef(id1)).toEqBN(0);
Expand All @@ -210,7 +217,12 @@ describe('post', () => {
const id2 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id2).toEqual(
hashMessageContent(ACCOUNT1.address, 'the message 2', 'the uri 2', URIType.Image, id1),
hashMessageContent(ACCOUNT1.address, {
text: 'the message 2',
uri: 'the uri 2',
uriType: URIType.Image,
messageRef: id1,
}),
);
expect<string>(await message.text(id2)).toEqual('the message 2');
expect<[string, URIType]>(await message.uri(id2)).toEqual(['the uri 2', URIType.Image]);
Expand All @@ -219,15 +231,19 @@ describe('post', () => {
await message.connect(ACCOUNT1).post({ text: '', uri: '', uriType: URIType.None, messageRef: 0 });
const id3 = await erc721Enumerable.tokenByIndex(2);

expect<BigNumber>(id3).toEqual(hashMessageContent(ACCOUNT1.address, '', '', URIType.None, 0));
expect<BigNumber>(id3).toEqual(
hashMessageContent(ACCOUNT1.address, { text: '', uri: '', uriType: URIType.None, messageRef: 0 }),
);
expect<string>(await message.text(id3)).toEqual('');
expect<[string, URIType]>(await message.uri(id3)).toEqual(['', URIType.None]);
expect<BigNumber>(await message.messageRef(id3)).toEqBN(0);

await message.connect(ACCOUNT1).post({ text: '', uri: '', uriType: URIType.None, messageRef: id2 });
const id4 = await erc721Enumerable.tokenByIndex(3);

expect<BigNumber>(id4).toEqual(hashMessageContent(ACCOUNT1.address, '', '', URIType.None, id2));
expect<BigNumber>(id4).toEqual(
hashMessageContent(ACCOUNT1.address, { text: '', uri: '', uriType: URIType.None, messageRef: id2 }),
);
expect<string>(await message.text(id4)).toEqual('');
expect<[string, URIType]>(await message.uri(id4)).toEqual(['', URIType.None]);
expect<BigNumber>(await message.messageRef(id4)).toEqBN(id2);
Expand All @@ -243,15 +259,19 @@ describe('post', () => {
await message.connect(ACCOUNT2).post({ text: '', uri: '', uriType: URIType.None, messageRef: id1 });
const id2 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id2).toEqual(hashMessageContent(ACCOUNT2.address, '', '', URIType.None, id1));
expect<BigNumber>(id2).toEqual(
hashMessageContent(ACCOUNT2.address, { text: '', uri: '', uriType: URIType.None, messageRef: id1 }),
);
expect<string>(await message.text(id2)).toEqual('');
expect<[string, URIType]>(await message.uri(id2)).toEqual(['', URIType.None]);
expect<BigNumber>(await message.messageRef(id2)).toEqBN(id1);

await message.connect(ACCOUNT3).post({ text: '', uri: '', uriType: URIType.None, messageRef: id1 });
const id3 = await erc721Enumerable.tokenByIndex(2);

expect<BigNumber>(id3).toEqual(hashMessageContent(ACCOUNT3.address, '', '', URIType.None, id1));
expect<BigNumber>(id3).toEqual(
hashMessageContent(ACCOUNT3.address, { text: '', uri: '', uriType: URIType.None, messageRef: id1 }),
);
expect<string>(await message.text(id3)).toEqual('');
expect<[string, URIType]>(await message.uri(id3)).toEqual(['', URIType.None]);
expect<BigNumber>(await message.messageRef(id3)).toEqBN(id1);
Expand All @@ -267,7 +287,9 @@ describe('post', () => {
await message.connect(ACCOUNT1).post({ text: 'the message', uri: 'the uri', uriType: URIType.Link, messageRef: 0 });
const id2 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id2).toEqual(hashMessageContent(ACCOUNT1.address, '', '', URIType.None, id1));
expect<BigNumber>(id2).toEqual(
hashMessageContent(ACCOUNT1.address, { text: '', uri: '', uriType: URIType.None, messageRef: id1 }),
);
expect<string>(await message.text(id2)).toEqual('');
expect<[string, URIType]>(await message.uri(id2)).toEqual(['', URIType.None]);
expect<BigNumber>(await message.messageRef(id2)).toEqBN(id1);
Expand All @@ -288,7 +310,9 @@ describe('post', () => {
.post({ text: 'the message 2', uri: 'the uri 2', uriType: URIType.Link, messageRef: id2 });
const id3 = await erc721Enumerable.tokenByIndex(1);

expect<BigNumber>(id3).toEqual(hashMessageContent(ACCOUNT1.address, '', '', URIType.None, id1));
expect<BigNumber>(id3).toEqual(
hashMessageContent(ACCOUNT1.address, { text: '', uri: '', uriType: URIType.None, messageRef: id1 }),
);
expect<string>(await message.text(id3)).toEqual('');
expect<[string, URIType]>(await message.uri(id3)).toEqual(['', URIType.None]);
expect<BigNumber>(await message.messageRef(id3)).toEqBN(id1);
Expand Down Expand Up @@ -385,7 +409,12 @@ describe('post', () => {
).toHaveEmittedWith(erc721, 'Transfer', [
ZERO_ADDRESS,
ACCOUNT1.address,
hashMessageContent(ACCOUNT1.address, 'the message', 'the uri', URIType.Link),
hashMessageContent(ACCOUNT1.address, {
text: 'the message',
uri: 'the uri',
uriType: URIType.Link,
messageRef: 0,
}),
]);
});
});
20 changes: 10 additions & 10 deletions src/contracts/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with Paypr Contracts. If not, see <https://www.gnu.org/licenses/>.
*/

import { BigNumber, BigNumberish } from 'ethers';
import { BigNumber } from 'ethers';
import { defaultAbiCoder, keccak256, ParamType } from 'ethers/lib/utils';
// noinspection ES6PreferShortImport
import { IMessage } from '../../types/contracts/contracts/facets/message/IMessage';
Expand All @@ -29,26 +29,26 @@ export enum URIType {
Image,
}

export type PostMessageContent = Partial<MessageContentStruct>;

export const withMessageContentDefaults = ({
text,
uri,
uriType,
messageRef,
}: Partial<MessageContentStruct>): MessageContentStruct => ({
}: PostMessageContent): MessageContentStruct => ({
text: text || '',
uri: uri || '',
uriType: uriType || URIType.None,
messageRef: messageRef || 0,
});

export const hashMessageContent = (
sender: string,
text: string = '',
uri: string = '',
uriType: URIType = URIType.None,
messageRef: BigNumberish = 0,
) => {
if (!BigNumber.from(messageRef).eq(BigNumber.from(0)) && text === '' && uri === '') {
export const isMessageRepost = ({ text, uri, messageRef }: MessageContentStruct) =>
!BigNumber.from(messageRef).eq(BigNumber.from(0)) && text === '' && uri === '';

export const hashMessageContent = (sender: string, content: PostMessageContent) => {
const { text, uri, uriType, messageRef } = withMessageContentDefaults(content);
if (isMessageRepost({ text, uri, uriType, messageRef })) {
return BigNumber.from(
keccak256(defaultAbiCoder.encode(['address', messageContentType], [sender, { text, uri, uriType, messageRef }])),
);
Expand Down

0 comments on commit 0d7eb46

Please sign in to comment.