Skip to content

Commit b13976f

Browse files
committed
Merge branch 'master' into refactor/pushTx-false
2 parents 5cfee6d + 4e81cb8 commit b13976f

File tree

4 files changed

+95
-25
lines changed

4 files changed

+95
-25
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('sendNanoContractTx', () => {
6868
const expectedActions = [
6969
{
7070
...(rpcRequest.params.actions[0] as unknown as NanoContractActionWithStringAmount),
71-
amount: BigInt(100), // Expected conversion to BigInt
71+
amount: 100n, // Expected conversion to BigInt
7272
}
7373
];
7474

@@ -147,11 +147,11 @@ describe('sendNanoContractTx', () => {
147147
const expectedActions = [
148148
{
149149
...stringActions[0],
150-
amount: BigInt(100),
150+
amount: 100n,
151151
},
152152
{
153153
...stringActions[1],
154-
amount: BigInt(200),
154+
amount: 200n,
155155
},
156156
];
157157

@@ -268,7 +268,7 @@ describe('sendNanoContractTx', () => {
268268
args: rpcRequest.params.args,
269269
actions: [{
270270
...originalAction,
271-
amount: BigInt(100), // Convert amount to BigInt
271+
amount: 100n, // Convert amount to BigInt
272272
}],
273273
};
274274

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

+70-5
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ describe('sendTransaction', () => {
5757
inputs: [{
5858
txId: 'testTxId',
5959
index: 0,
60-
value: 100,
60+
value: 100n,
6161
address: 'testAddress',
6262
token: '00',
6363
}],
64+
outputs: [{
65+
address: 'testAddress',
66+
value: BigInt(100),
67+
token: '00',
68+
}],
6469
}),
6570
run: sendTransactionMock,
6671
}),
@@ -104,13 +109,13 @@ describe('sendTransaction', () => {
104109
data: {
105110
outputs: [{
106111
address: 'testAddress',
107-
value: BigInt(100),
112+
value: 100n,
108113
token: '00',
109114
}],
110115
inputs: [{
111116
txId: 'testTxId',
112117
index: 0,
113-
value: 100,
118+
value: 100n,
114119
address: 'testAddress',
115120
token: '00',
116121
}],
@@ -127,7 +132,7 @@ describe('sendTransaction', () => {
127132
rpcRequest.params.outputs = [{
128133
type: 'data',
129134
value: '100',
130-
data: ['test data'],
135+
data: 'test data',
131136
}];
132137

133138
promptHandler
@@ -147,15 +152,75 @@ describe('sendTransaction', () => {
147152
// Verify data output was transformed correctly
148153
expect(wallet.sendManyOutputsSendTransaction).toHaveBeenCalledWith(
149154
expect.arrayContaining([
155+
expect.objectContaining({
156+
type: 'data',
157+
value: 1n,
158+
token: '00',
159+
data: 'test data',
160+
}),
161+
]),
162+
expect.any(Object),
163+
);
164+
});
165+
166+
it('should handle mix of data and regular outputs correctly', async () => {
167+
rpcRequest.params.outputs = [
168+
{
169+
address: 'testAddress1',
170+
value: '100',
171+
token: '00',
172+
},
173+
{
174+
type: 'data',
175+
data: 'data item',
176+
value: '1',
177+
},
178+
{
179+
address: 'testAddress2',
180+
value: '200',
181+
token: '00',
182+
}
183+
];
184+
185+
promptHandler
186+
.mockResolvedValueOnce({
187+
type: TriggerResponseTypes.SendTransactionConfirmationResponse,
188+
data: { accepted: true },
189+
})
190+
.mockResolvedValueOnce({
191+
type: TriggerResponseTypes.PinRequestResponse,
192+
data: { accepted: true, pinCode: '1234' },
193+
});
194+
195+
sendTransactionMock.mockResolvedValue({ hash: 'txHash123' });
196+
197+
await sendTransaction(rpcRequest, wallet, {}, promptHandler);
198+
199+
// Verify the transformation preserves regular outputs and handles data output
200+
expect(wallet.sendManyOutputsSendTransaction).toHaveBeenCalledWith(
201+
expect.arrayContaining([
202+
expect.objectContaining({
203+
address: 'testAddress1',
204+
value: BigInt(100),
205+
token: '00',
206+
}),
150207
expect.objectContaining({
151208
type: 'data',
152209
value: BigInt(1),
153210
token: '00',
154-
data: ['test data'],
211+
data: 'data item',
212+
}),
213+
expect.objectContaining({
214+
address: 'testAddress2',
215+
value: BigInt(200),
216+
token: '00',
155217
}),
156218
]),
157219
expect.any(Object),
158220
);
221+
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);
159224
});
160225

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

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

+20-15
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(),
@@ -100,16 +100,27 @@ export async function sendTransaction(
100100
validateNetwork(wallet, params.network);
101101

102102
// Transform outputs before sending to wallet-lib
103-
const transformedOutputs = params.outputs.map(output => {
103+
const transformedOutputs = params.outputs.reduce<Array<{
104+
address?: string;
105+
value?: bigint;
106+
token?: string;
107+
type?: string;
108+
data?: string;
109+
}>>((acc, output) => {
104110
const result = { ...output };
105111

106-
if (result.type === 'data' || (result.data && result.data.length > 0)) {
107-
result.value = BigInt(1);
108-
result.token = constants.NATIVE_TOKEN_UID;
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, {
116+
...output,
117+
value: 1n,
118+
token: constants.NATIVE_TOKEN_UID,
119+
}];
109120
}
110121

111-
return result;
112-
});
122+
return [...acc, result];
123+
}, []);
113124

114125
// sendManyOutputsSendTransaction throws if it doesn't receive a pin,
115126
// but doesn't use it until prepareTxData is called, so we can just assign
@@ -142,13 +153,7 @@ export async function sendTransaction(
142153
type: TriggerTypes.SendTransactionConfirmationPrompt,
143154
method: rpcRequest.method,
144155
data: {
145-
outputs: params.outputs as unknown as Array<{
146-
address?: string;
147-
value: number;
148-
token?: string;
149-
type?: string;
150-
data?: string[];
151-
}>,
156+
outputs: preparedTx.outputs,
152157
inputs: preparedTx.inputs,
153158
changeAddress: params.changeAddress,
154159
}

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)