Skip to content

Commit

Permalink
fix: add messageId to input message decoder (FuelLabs#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight authored Jun 15, 2023
1 parent 8d6442c commit 8d21cd8
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/smooth-cameras-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/providers": patch
"@fuel-ts/transactions": patch
---

fix: add messageId to input message decoder
3 changes: 3 additions & 0 deletions packages/fuel-gauge/src/coverage-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ describe('Coverage Contract', () => {

const EXPECTED_MESSAGES_A: Message[] = [
{
messageId: '0x98fcab1a57eaf34c0a2c743f6537f125d3ae46d59cd118a41016321f631584b2',
sender: WALLET_B.address,
recipient: WALLET_A.address,
nonce: bn(1),
Expand All @@ -398,6 +399,7 @@ describe('Coverage Contract', () => {
];
const EXPECTED_MESSAGES_B: Message[] = [
{
messageId: '0x5ef913b1853aa1ae27afd51b509dbcb9961fb3b588b2c694a0c526c6034a2157',
sender: WALLET_A.address,
recipient: WALLET_B.address,
nonce: bn('1017517292834129547'),
Expand Down Expand Up @@ -430,6 +432,7 @@ describe('Coverage Contract', () => {
);

const message: Message = {
messageId: '0x98fcab1a57eaf34c0a2c743f6537f125d3ae46d59cd118a41016321f631584b2',
sender: sender.address,
recipient: recipient.address,
nonce: bn(1),
Expand Down
1 change: 1 addition & 0 deletions packages/providers/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GqlMessageStatus as MessageStatus } from './__generated__/operations';
* A Fuel message
*/
export type Message = {
messageId: BytesLike;
sender: AbstractAddress;
recipient: AbstractAddress;
nonce: BN;
Expand Down
1 change: 1 addition & 0 deletions packages/providers/src/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fragment messageFragment on Message {
nonce
messageStatus: status
daHeight
messageId
}

fragment messageProofFragment on MessageProof {
Expand Down
5 changes: 5 additions & 0 deletions packages/providers/src/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ describe('Provider', () => {
});
const MessageInput: MessageTransactionRequestInput = {
type: InputType.Message,
messageId: NativeAssetId,
amount: 100,
sender: NativeAssetId,
recipient: NativeAssetId,
Expand Down Expand Up @@ -342,6 +343,7 @@ describe('Provider', () => {
];
const MessageInput: MessageTransactionRequestInput = {
type: InputType.Message,
messageId: NativeAssetId,
amount: 100,
sender: NativeAssetId,
recipient: NativeAssetId,
Expand Down Expand Up @@ -402,6 +404,7 @@ describe('Provider', () => {
];
const MessageInput: MessageTransactionRequestInput = {
type: InputType.Message,
messageId: NativeAssetId,
amount: 100,
sender: NativeAssetId,
recipient: NativeAssetId,
Expand Down Expand Up @@ -477,6 +480,7 @@ describe('Provider', () => {
];
const MessageInput: MessageTransactionRequestInput = {
type: InputType.Message,
messageId: NativeAssetId,
amount: 100,
sender: NativeAssetId,
recipient: NativeAssetId,
Expand Down Expand Up @@ -537,6 +541,7 @@ describe('Provider', () => {
];
const MessageInput: MessageTransactionRequestInput = {
type: InputType.Message,
messageId: NativeAssetId,
amount: 100,
sender: NativeAssetId,
recipient: NativeAssetId,
Expand Down
2 changes: 2 additions & 0 deletions packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ export default class Provider {
}

return {
messageId: resource.messageId,
sender: Address.fromAddressOrString(resource.sender),
recipient: Address.fromAddressOrString(resource.recipient),
nonce: bn(resource.nonce),
Expand Down Expand Up @@ -741,6 +742,7 @@ export default class Provider {
const messages = result.messages.edges!.map((edge) => edge!.node!);

return messages.map((message) => ({
messageId: message.messageId,
sender: Address.fromAddressOrString(message.sender),
recipient: Address.fromAddressOrString(message.recipient),
nonce: bn(message.nonce),
Expand Down
11 changes: 8 additions & 3 deletions packages/providers/src/transaction-request/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ export type CoinTransactionRequestInput = {
export type MessageTransactionRequestInput = {
type: InputType.Message;

/** Amount of coins */
amount: BigNumberish;
/** Address of recipient */
messageId: BytesLike;

/** Address of sender */
sender: BytesLike;

/** Address of sender */
/** Address of recipient */
recipient: BytesLike;

/** Amount of coins */
amount: BigNumberish;

/** Index of witness that authorizes the message */
witnessIndex: number;

Expand Down Expand Up @@ -123,8 +126,10 @@ export const inputify = (value: TransactionRequestInput): Input => {
const predicate = arrayify(value.predicate ?? '0x');
const predicateData = arrayify(value.predicateData ?? '0x');
const data = arrayify(value.data ?? '0x');

return {
type: InputType.Message,
messageId: hexlify(value.messageId),
sender: hexlify(value.sender),
recipient: hexlify(value.recipient),
amount: bn(value.amount),
Expand Down
2 changes: 2 additions & 0 deletions packages/transactions/src/coders/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ describe('InputCoder', () => {
it('Can encode Message Id', () => {
const input: InputMessage = {
type: InputType.Message,
messageId: '0xa2be1e294136b22c8168159892eb26b2503232f1e711e2e6fbb7f8a8b50633b7',
amount: bn(1000),
sender: '0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e',
recipient: '0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a',
Expand All @@ -139,6 +140,7 @@ describe('InputCoder', () => {
it('Can encode Message', () => {
const input: Input = {
type: InputType.Message,
messageId: '0xa2be1e294136b22c8168159892eb26b2503232f1e711e2e6fbb7f8a8b50633b7',
amount: bn(1000),
sender: '0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e',
recipient: '0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a',
Expand Down
16 changes: 11 additions & 5 deletions packages/transactions/src/coders/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,18 @@ export class InputContractCoder extends Coder<InputContract, InputContract> {
export type InputMessage = {
type: InputType.Message;

/** Amount of coins */
amount: BN;
/** ID of Message */
messageId: string;

/** Address of sender */
sender: string;

/** Address of sender */
/** Address of recipient */
recipient: string;

/** Amount of coins */
amount: BN;

/** data of message */
data: string;

Expand Down Expand Up @@ -249,8 +252,8 @@ export class InputMessageCoder extends Coder<InputMessage, InputMessage> {
encode(value: InputMessage): Uint8Array {
const parts: Uint8Array[] = [];
const encodedData = new ByteArrayCoder(value.dataLength).encode(value.data);
const mId = InputMessageCoder.getMessageId(value);
parts.push(new ByteArrayCoder(32).encode(mId));
const messageId = InputMessageCoder.getMessageId(value);
parts.push(new ByteArrayCoder(32).encode(messageId));
parts.push(new ByteArrayCoder(32).encode(value.sender));
parts.push(new ByteArrayCoder(32).encode(value.recipient));
parts.push(new U64Coder().encode(value.amount));
Expand Down Expand Up @@ -278,6 +281,8 @@ export class InputMessageCoder extends Coder<InputMessage, InputMessage> {
let decoded;
let o = offset;

[decoded, o] = new B256Coder().decode(data, o);
const messageId = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const sender = decoded;
[decoded, o] = new B256Coder().decode(data, o);
Expand All @@ -304,6 +309,7 @@ export class InputMessageCoder extends Coder<InputMessage, InputMessage> {
return [
{
type: InputType.Message,
messageId,
sender,
recipient,
amount,
Expand Down

0 comments on commit 8d21cd8

Please sign in to comment.