Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 3006a57

Browse files
author
Alex
authored
6075 - update return format (#6083)
* update return format * format signed message * add rpc tests
1 parent 27cc3c3 commit 3006a57

File tree

5 files changed

+68
-12
lines changed

5 files changed

+68
-12
lines changed

packages/web3-eth-accounts/src/account.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ import { keyStoreSchema } from './schemas';
6161
import { TransactionFactory } from './tx/transactionFactory';
6262
import type {
6363
SignatureObject,
64-
SignResult,
6564
SignTransactionResult,
6665
TypedTransaction,
6766
Web3Account,
67+
SignResult,
6868
} from './types';
6969

7070
/**

packages/web3-eth/src/rpc_method_wrappers.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import {
7171
transactionReceiptSchema,
7272
transactionInfoSchema,
7373
accessListResultSchema,
74+
SignatureObjectSchema,
7475
} from './schemas';
7576
import {
7677
SendSignedTransactionEvents,
@@ -205,7 +206,7 @@ export async function getStorageAt<ReturnFormat extends DataFormat>(
205206
storageSlotFormatted,
206207
blockNumberFormatted,
207208
);
208-
return format({ format: 'bytes' }, response, returnFormat);
209+
return format({ format: 'bytes' }, response as Bytes, returnFormat);
209210
}
210211

211212
/**
@@ -226,7 +227,7 @@ export async function getCode<ReturnFormat extends DataFormat>(
226227
address,
227228
blockNumberFormatted,
228229
);
229-
return format({ format: 'bytes' }, response, returnFormat);
230+
return format({ format: 'bytes' }, response as Bytes, returnFormat);
230231
}
231232

232233
/**
@@ -877,11 +878,10 @@ export async function sign<ReturnFormat extends DataFormat>(
877878
returnFormat: ReturnFormat,
878879
) {
879880
const messageFormatted = format({ format: 'bytes' }, message, DEFAULT_RETURN_FORMAT);
880-
881881
if (web3Context.wallet?.get(addressOrIndex)) {
882882
const wallet = web3Context.wallet.get(addressOrIndex) as Web3BaseWalletAccount;
883-
884-
return wallet.sign(messageFormatted);
883+
const signed = wallet.sign(messageFormatted);
884+
return format(SignatureObjectSchema, signed, returnFormat);
885885
}
886886

887887
if (typeof addressOrIndex === 'number') {
@@ -896,7 +896,8 @@ export async function sign<ReturnFormat extends DataFormat>(
896896
addressOrIndex,
897897
messageFormatted,
898898
);
899-
return format({ format: 'bytes' }, response, returnFormat);
899+
900+
return format({ format: 'bytes' }, response as Bytes, returnFormat);
900901
}
901902

902903
/**
@@ -948,7 +949,7 @@ export async function call<ReturnFormat extends DataFormat>(
948949
blockNumberFormatted,
949950
);
950951

951-
return format({ format: 'bytes' }, response, returnFormat);
952+
return format({ format: 'bytes' }, response as Bytes, returnFormat);
952953
}
953954

954955
// TODO - Investigate whether response is padded as 1.x docs suggest

packages/web3-eth/src/schemas.ts

+23
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,29 @@ export const transactionReceiptSchema = {
479479
},
480480
};
481481

482+
export const SignatureObjectSchema = {
483+
type: 'object',
484+
properties: {
485+
messageHash: {
486+
format: 'bytes',
487+
},
488+
r: {
489+
format: 'bytes32',
490+
},
491+
s: {
492+
format: 'bytes32',
493+
},
494+
v: {
495+
format: 'bytes',
496+
},
497+
message: {
498+
format: 'bytes',
499+
},
500+
signature: {
501+
format: 'bytes',
502+
},
503+
},
504+
};
482505
export const feeHistorySchema = {
483506
type: 'object',
484507
properties: {

packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/sign.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GNU Lesser General Public License for more details.
1414
You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17-
import { Address, Bytes } from 'web3-types';
17+
import { Address, Bytes, FMT_BYTES, FMT_NUMBER } from 'web3-types';
1818
import { hexToBytes } from 'web3-utils';
1919

2020
export const mockRpcResponse = '0x736f796c656e7420677265656e2069732070656f706c65';
@@ -49,3 +49,15 @@ export const testData: TestData[] = [
4949
],
5050
],
5151
];
52+
export const walletTestData: [string, [Bytes, Address | number], any][] = [
53+
[
54+
'message = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"',
55+
['0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8', 0],
56+
{ number: FMT_NUMBER.STR, bytes: FMT_BYTES.UINT8ARRAY },
57+
],
58+
[
59+
'message = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"',
60+
['0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8', 0],
61+
{ number: FMT_NUMBER.STR, bytes: FMT_BYTES.HEX },
62+
],
63+
];

packages/web3-eth/test/unit/rpc_method_wrappers/sign.test.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import { Web3Context } from 'web3-core';
1818
import { format } from 'web3-utils';
1919
import { DEFAULT_RETURN_FORMAT, FMT_BYTES, FMT_NUMBER, Web3EthExecutionAPI } from 'web3-types';
2020
import { ethRpcMethods } from 'web3-rpc-methods';
21-
21+
import { Wallet } from 'web3-eth-accounts';
2222
import { sign } from '../../../src/rpc_method_wrappers';
23-
import { mockRpcResponse, testData } from './fixtures/sign';
23+
import { mockRpcResponse, testData, walletTestData } from './fixtures/sign';
24+
import { createAccountProvider } from '../../fixtures/system_test_utils';
25+
import { SignatureObjectSchema } from '../../../src/schemas';
2426

2527
jest.mock('web3-rpc-methods');
2628

@@ -40,7 +42,6 @@ describe('sign', () => {
4042
inputMessage,
4143
DEFAULT_RETURN_FORMAT,
4244
);
43-
4445
await sign(web3Context, ...inputParameters, DEFAULT_RETURN_FORMAT);
4546
expect(ethRpcMethods.sign).toHaveBeenCalledWith(
4647
web3Context.requestManager,
@@ -49,6 +50,25 @@ describe('sign', () => {
4950
);
5051
},
5152
);
53+
it.each(walletTestData)(
54+
`should call rpcMethods.sign using the context wallet with expected parameters\nTitle: %s\nInput parameters: %s\n and return with expected format`,
55+
async (_, inputParameters, expectedReturnFormat) => {
56+
// set up wallet for signing
57+
const localContext = new Web3Context('http://127.0.0.1:8545');
58+
const accountProvider = createAccountProvider(localContext);
59+
const wallet = new Wallet(accountProvider);
60+
wallet.create(1);
61+
localContext['_wallet'] = wallet;
62+
63+
const result = await sign(localContext, ...inputParameters, expectedReturnFormat);
64+
const expectedFormattedResult = format(
65+
SignatureObjectSchema,
66+
result,
67+
expectedReturnFormat,
68+
);
69+
expect(result).toStrictEqual(expectedFormattedResult);
70+
},
71+
);
5272

5373
it.each(testData)(
5474
`should format mockRpcResponse using provided return format\nTitle: %s\nInput parameters: %s\n`,

0 commit comments

Comments
 (0)