diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index 91e3dc8f7c4..253ee3575e3 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -61,10 +61,10 @@ import { keyStoreSchema } from './schemas'; import { TransactionFactory } from './tx/transactionFactory'; import type { SignatureObject, - SignResult, SignTransactionResult, TypedTransaction, Web3Account, + SignResult, } from './types'; /** diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 3a85ad72342..0e0f8deca11 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -71,6 +71,7 @@ import { transactionReceiptSchema, transactionInfoSchema, accessListResultSchema, + SignatureObjectSchema, } from './schemas'; import { SendSignedTransactionEvents, @@ -205,7 +206,7 @@ export async function getStorageAt( storageSlotFormatted, blockNumberFormatted, ); - return format({ format: 'bytes' }, response, returnFormat); + return format({ format: 'bytes' }, response as Bytes, returnFormat); } /** @@ -226,7 +227,7 @@ export async function getCode( address, blockNumberFormatted, ); - return format({ format: 'bytes' }, response, returnFormat); + return format({ format: 'bytes' }, response as Bytes, returnFormat); } /** @@ -877,11 +878,10 @@ export async function sign( returnFormat: ReturnFormat, ) { const messageFormatted = format({ format: 'bytes' }, message, DEFAULT_RETURN_FORMAT); - if (web3Context.wallet?.get(addressOrIndex)) { const wallet = web3Context.wallet.get(addressOrIndex) as Web3BaseWalletAccount; - - return wallet.sign(messageFormatted); + const signed = wallet.sign(messageFormatted); + return format(SignatureObjectSchema, signed, returnFormat); } if (typeof addressOrIndex === 'number') { @@ -896,7 +896,8 @@ export async function sign( addressOrIndex, messageFormatted, ); - return format({ format: 'bytes' }, response, returnFormat); + + return format({ format: 'bytes' }, response as Bytes, returnFormat); } /** @@ -948,7 +949,7 @@ export async function call( blockNumberFormatted, ); - return format({ format: 'bytes' }, response, returnFormat); + return format({ format: 'bytes' }, response as Bytes, returnFormat); } // TODO - Investigate whether response is padded as 1.x docs suggest diff --git a/packages/web3-eth/src/schemas.ts b/packages/web3-eth/src/schemas.ts index aa3c8a0d18a..f5b200204db 100644 --- a/packages/web3-eth/src/schemas.ts +++ b/packages/web3-eth/src/schemas.ts @@ -479,6 +479,29 @@ export const transactionReceiptSchema = { }, }; +export const SignatureObjectSchema = { + type: 'object', + properties: { + messageHash: { + format: 'bytes', + }, + r: { + format: 'bytes32', + }, + s: { + format: 'bytes32', + }, + v: { + format: 'bytes', + }, + message: { + format: 'bytes', + }, + signature: { + format: 'bytes', + }, + }, +}; export const feeHistorySchema = { type: 'object', properties: { diff --git a/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/sign.ts b/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/sign.ts index 641e59cf1a4..fa3f475d802 100644 --- a/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/sign.ts +++ b/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/sign.ts @@ -14,7 +14,7 @@ GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -import { Address, Bytes } from 'web3-types'; +import { Address, Bytes, FMT_BYTES, FMT_NUMBER } from 'web3-types'; import { hexToBytes } from 'web3-utils'; export const mockRpcResponse = '0x736f796c656e7420677265656e2069732070656f706c65'; @@ -49,3 +49,15 @@ export const testData: TestData[] = [ ], ], ]; +export const walletTestData: [string, [Bytes, Address | number], any][] = [ + [ + 'message = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"', + ['0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8', 0], + { number: FMT_NUMBER.STR, bytes: FMT_BYTES.UINT8ARRAY }, + ], + [ + 'message = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"', + ['0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8', 0], + { number: FMT_NUMBER.STR, bytes: FMT_BYTES.HEX }, + ], +]; diff --git a/packages/web3-eth/test/unit/rpc_method_wrappers/sign.test.ts b/packages/web3-eth/test/unit/rpc_method_wrappers/sign.test.ts index 8f521e51973..ebbbc16955c 100644 --- a/packages/web3-eth/test/unit/rpc_method_wrappers/sign.test.ts +++ b/packages/web3-eth/test/unit/rpc_method_wrappers/sign.test.ts @@ -18,9 +18,11 @@ import { Web3Context } from 'web3-core'; import { format } from 'web3-utils'; import { DEFAULT_RETURN_FORMAT, FMT_BYTES, FMT_NUMBER, Web3EthExecutionAPI } from 'web3-types'; import { ethRpcMethods } from 'web3-rpc-methods'; - +import { Wallet } from 'web3-eth-accounts'; import { sign } from '../../../src/rpc_method_wrappers'; -import { mockRpcResponse, testData } from './fixtures/sign'; +import { mockRpcResponse, testData, walletTestData } from './fixtures/sign'; +import { createAccountProvider } from '../../fixtures/system_test_utils'; +import { SignatureObjectSchema } from '../../../src/schemas'; jest.mock('web3-rpc-methods'); @@ -40,7 +42,6 @@ describe('sign', () => { inputMessage, DEFAULT_RETURN_FORMAT, ); - await sign(web3Context, ...inputParameters, DEFAULT_RETURN_FORMAT); expect(ethRpcMethods.sign).toHaveBeenCalledWith( web3Context.requestManager, @@ -49,6 +50,25 @@ describe('sign', () => { ); }, ); + it.each(walletTestData)( + `should call rpcMethods.sign using the context wallet with expected parameters\nTitle: %s\nInput parameters: %s\n and return with expected format`, + async (_, inputParameters, expectedReturnFormat) => { + // set up wallet for signing + const localContext = new Web3Context('http://127.0.0.1:8545'); + const accountProvider = createAccountProvider(localContext); + const wallet = new Wallet(accountProvider); + wallet.create(1); + localContext['_wallet'] = wallet; + + const result = await sign(localContext, ...inputParameters, expectedReturnFormat); + const expectedFormattedResult = format( + SignatureObjectSchema, + result, + expectedReturnFormat, + ); + expect(result).toStrictEqual(expectedFormattedResult); + }, + ); it.each(testData)( `should format mockRpcResponse using provided return format\nTitle: %s\nInput parameters: %s\n`,