Skip to content

Commit 76d2f94

Browse files
committed
refactor: stop accepting an string array as data
1 parent 8822505 commit 76d2f94

File tree

3 files changed

+15
-68
lines changed

3 files changed

+15
-68
lines changed

packages/hathor-rpc-handler/__tests__/rpcMethods/sendTransaction.test.ts

+6-59
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('sendTransaction', () => {
132132
rpcRequest.params.outputs = [{
133133
type: 'data',
134134
value: '100',
135-
data: ['test data'],
135+
data: 'test data',
136136
}];
137137

138138
promptHandler
@@ -163,53 +163,6 @@ describe('sendTransaction', () => {
163163
);
164164
});
165165

166-
it('should split multiple data items into separate outputs', async () => {
167-
rpcRequest.params.outputs = [{
168-
type: 'data',
169-
value: '100',
170-
data: ['data item 1', 'data item 2', 'data item 3'],
171-
}];
172-
173-
promptHandler
174-
.mockResolvedValueOnce({
175-
type: TriggerResponseTypes.SendTransactionConfirmationResponse,
176-
data: { accepted: true },
177-
})
178-
.mockResolvedValueOnce({
179-
type: TriggerResponseTypes.PinRequestResponse,
180-
data: { accepted: true, pinCode: '1234' },
181-
});
182-
183-
sendTransactionMock.mockResolvedValue({ hash: 'txHash123' });
184-
185-
await sendTransaction(rpcRequest, wallet, {}, promptHandler);
186-
187-
// Verify each data item was transformed into a separate output
188-
expect(wallet.sendManyOutputsSendTransaction).toHaveBeenCalledWith(
189-
expect.arrayContaining([
190-
expect.objectContaining({
191-
type: 'data',
192-
value: BigInt(1),
193-
token: '00',
194-
data: 'data item 1',
195-
}),
196-
expect.objectContaining({
197-
type: 'data',
198-
value: BigInt(1),
199-
token: '00',
200-
data: 'data item 2',
201-
}),
202-
expect.objectContaining({
203-
type: 'data',
204-
value: BigInt(1),
205-
token: '00',
206-
data: 'data item 3',
207-
}),
208-
]),
209-
expect.any(Object),
210-
);
211-
});
212-
213166
it('should handle mix of data and regular outputs correctly', async () => {
214167
rpcRequest.params.outputs = [
215168
{
@@ -219,7 +172,7 @@ describe('sendTransaction', () => {
219172
},
220173
{
221174
type: 'data',
222-
data: ['data item 1', 'data item 2'],
175+
data: 'data item',
223176
value: '1',
224177
},
225178
{
@@ -243,7 +196,7 @@ describe('sendTransaction', () => {
243196

244197
await sendTransaction(rpcRequest, wallet, {}, promptHandler);
245198

246-
// Verify the transformation preserves regular outputs and splits data outputs
199+
// Verify the transformation preserves regular outputs and handles data output
247200
expect(wallet.sendManyOutputsSendTransaction).toHaveBeenCalledWith(
248201
expect.arrayContaining([
249202
expect.objectContaining({
@@ -255,13 +208,7 @@ describe('sendTransaction', () => {
255208
type: 'data',
256209
value: BigInt(1),
257210
token: '00',
258-
data: 'data item 1',
259-
}),
260-
expect.objectContaining({
261-
type: 'data',
262-
value: BigInt(1),
263-
token: '00',
264-
data: 'data item 2',
211+
data: 'data item',
265212
}),
266213
expect.objectContaining({
267214
address: 'testAddress2',
@@ -272,8 +219,8 @@ describe('sendTransaction', () => {
272219
expect.any(Object),
273220
);
274221

275-
// Verify the array length matches the expected number of outputs (2 regular + 2 data outputs)
276-
expect(wallet.sendManyOutputsSendTransaction.mock.calls[0][0]).toHaveLength(4);
222+
// Verify the array length matches the expected number of outputs (2 regular + 1 data output)
223+
expect(wallet.sendManyOutputsSendTransaction.mock.calls[0][0]).toHaveLength(3);
277224
});
278225

279226
it('should throw InvalidParamsError for invalid request parameters', async () => {

packages/hathor-rpc-handler/src/rpcMethods/sendTransaction.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const sendTransactionSchema = z.object({
4242
.optional(),
4343
token: z.string().optional(),
4444
type: z.string().optional(),
45-
data: z.array(z.string()).optional(),
45+
data: z.string().optional(),
4646
})
4747
.transform(output => {
4848
// If data is present, automatically set type to 'data'
@@ -58,7 +58,7 @@ const sendTransactionSchema = z.object({
5858
}
5959
return true;
6060
}, {
61-
message: "Value is required when data is not provided"
61+
message: 'Value is required when data is not provided'
6262
})).min(1),
6363
inputs: z.array(z.object({
6464
txId: z.string(),
@@ -105,18 +105,18 @@ export async function sendTransaction(
105105
value?: bigint;
106106
token?: string;
107107
type?: string;
108-
data?: string | string[];
108+
data?: string;
109109
}>>((acc, output) => {
110110
const result = { ...output };
111111

112-
// Each data should be a data output spending 0.01 HTR
113-
if (result.type === 'data' || (result.data && result.data.length > 0)) {
114-
return [...acc, ...(result.data ? result.data.map((data) => ({
112+
// We should manually set the 0.01 HTR to data output
113+
// so it's displayed to the user during confirmation.
114+
if (result.type === 'data') {
115+
return [...acc, {
115116
...output,
116117
value: 1n,
117118
token: constants.NATIVE_TOKEN_UID,
118-
data,
119-
})) : [])];
119+
}];
120120
}
121121

122122
return [...acc, result];

packages/hathor-rpc-handler/src/types/rpcRequest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export interface SendTransactionRpcRequest {
114114
value: string | number | bigint;
115115
token?: string;
116116
type?: string;
117-
data?: string[];
117+
data?: string;
118118
}>;
119119
inputs?: Array<{
120120
txId: string;

0 commit comments

Comments
 (0)