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

6075 - update return format #6083

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ import { keyStoreSchema } from './schemas';
import { TransactionFactory } from './tx/transactionFactory';
import type {
SignatureObject,
SignResult,
SignTransactionResult,
TypedTransaction,
Web3Account,
SignResult,
} from './types';

/**
Expand Down
15 changes: 8 additions & 7 deletions packages/web3-eth/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
transactionReceiptSchema,
transactionInfoSchema,
accessListResultSchema,
SignatureObjectSchema,
} from './schemas';
import {
SendSignedTransactionEvents,
Expand Down Expand Up @@ -205,7 +206,7 @@ export async function getStorageAt<ReturnFormat extends DataFormat>(
storageSlotFormatted,
blockNumberFormatted,
);
return format({ format: 'bytes' }, response, returnFormat);
return format({ format: 'bytes' }, response as Bytes, returnFormat);
}

/**
Expand All @@ -226,7 +227,7 @@ export async function getCode<ReturnFormat extends DataFormat>(
address,
blockNumberFormatted,
);
return format({ format: 'bytes' }, response, returnFormat);
return format({ format: 'bytes' }, response as Bytes, returnFormat);
}

/**
Expand Down Expand Up @@ -877,11 +878,10 @@ export async function sign<ReturnFormat extends DataFormat>(
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') {
Expand All @@ -896,7 +896,8 @@ export async function sign<ReturnFormat extends DataFormat>(
addressOrIndex,
messageFormatted,
);
return format({ format: 'bytes' }, response, returnFormat);

return format({ format: 'bytes' }, response as Bytes, returnFormat);
}

/**
Expand Down Expand Up @@ -948,7 +949,7 @@ export async function call<ReturnFormat extends DataFormat>(
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
Expand Down
23 changes: 23 additions & 0 deletions packages/web3-eth/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://www.gnu.org/licenses/>.
*/
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';
Expand Down Expand Up @@ -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 },
],
];
26 changes: 23 additions & 3 deletions packages/web3-eth/test/unit/rpc_method_wrappers/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -40,7 +42,6 @@ describe('sign', () => {
inputMessage,
DEFAULT_RETURN_FORMAT,
);

await sign(web3Context, ...inputParameters, DEFAULT_RETURN_FORMAT);
expect(ethRpcMethods.sign).toHaveBeenCalledWith(
web3Context.requestManager,
Expand All @@ -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`,
Expand Down