Skip to content

Commit 462a842

Browse files
authored
playground: Fix sign message bug (#1372)
fix sign message bug
1 parent 0ac34cd commit 462a842

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed

playground/multichain-react-native/src/components/ScopeCard.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ export function ScopeCard({ scope, details }: { scope: Scope; details: SessionDa
156156
const params = extractRequestParams(finalRequestObject);
157157
console.log(`📤 Calling invokeMethod with params:`, params);
158158

159-
const paramsArray = normalizeMethodParams(method, params);
160-
console.log(`📤 Normalized params array:`, paramsArray);
159+
const normalizedParams = normalizeMethodParams(method, params, scope);
160+
console.log(`📤 Normalized params:`, normalizedParams);
161161

162162
const result = await invokeMethod({
163163
scope,
164164
request: {
165165
method,
166-
params: paramsArray,
166+
params: normalizedParams,
167167
},
168168
});
169169

playground/multichain-react-native/src/helpers/MethodInvocationHelpers.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,27 @@ import type { MethodObject } from '@open-rpc/meta-schema';
44
import type { Dispatch, SetStateAction } from 'react';
55

66
/**
7-
* Normalizes parameters for method invocation, ensuring they are always in array format
8-
* and applying special handling for specific methods.
7+
* Normalizes parameters for method invocation.
8+
* - For Solana methods: keeps params as an object
9+
* - For EVM methods: ensures params are in array format
10+
* - Applies special handling for specific methods.
911
*
1012
* @param method - The method name being invoked.
1113
* @param params - The raw parameters.
12-
* @returns Normalized parameters array.
14+
* @param scope - The scope/chain ID to determine the chain type.
15+
* @returns Normalized parameters (array for EVM, object for Solana).
1316
*/
14-
export const normalizeMethodParams = (method: string, params: Json): Json[] => {
15-
// Ensure params is always an array for the SDK
17+
export const normalizeMethodParams = (method: string, params: Json, scope?: string): Json[] | Json => {
18+
// Solana methods should keep params as objects, not arrays
19+
const solanaMethodsThatUseObjects = ['signMessage', 'signTransaction', 'signAllTransactions', 'signAndSendTransaction', 'signIn'];
20+
21+
// Check if this is a Solana method that needs object params
22+
if (solanaMethodsThatUseObjects.includes(method) || scope?.startsWith('solana:')) {
23+
// For Solana, return params as-is (should be an object)
24+
return params;
25+
}
26+
27+
// For EVM methods, ensure params is always an array
1628
let paramsArray = Array.isArray(params) ? params : [params];
1729

1830
// Special handling for eth_signTypedData_v3/v4: second parameter must be JSON string

playground/multichain-react/src/components/ScopeCard.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/** biome-ignore-all lint/suspicious/noExplicitAny: ok */
22
import MetaMaskOpenRPCDocument from '@metamask/api-specs';
33
import type { Scope, SessionData } from '@metamask/multichain-sdk';
4-
import { type CaipAccountId, type CaipChainId, type CaipAccountAddress, parseCaipAccountId, type Json } from '@metamask/utils';
5-
import type { OpenrpcDocument, MethodObject } from '@open-rpc/meta-schema';
6-
import { useState, useCallback, useEffect } from 'react';
7-
import { METHODS_REQUIRING_PARAM_INJECTION, injectParams } from '../constants/methods';
4+
import { type CaipAccountAddress, type CaipAccountId, type CaipChainId, type Json, parseCaipAccountId } from '@metamask/utils';
5+
import type { MethodObject, OpenrpcDocument } from '@open-rpc/meta-schema';
6+
import { useCallback, useEffect, useState } from 'react';
7+
import { injectParams, METHODS_REQUIRING_PARAM_INJECTION } from '../constants/methods';
88
import { getNetworkName } from '../constants/networks';
99
import { escapeHtmlId } from '../helpers/IdHelpers';
1010
import { openRPCExampleToJSON, truncateJSON } from '../helpers/JsonHelpers';
11-
import { generateSolanaMethodExamples } from '../helpers/solana-method-signatures';
1211
import { extractRequestForStorage, extractRequestParams, normalizeMethodParams, updateInvokeMethodResults } from '../helpers/MethodInvocationHelpers';
12+
import { generateSolanaMethodExamples } from '../helpers/solana-method-signatures';
1313
import { useSDK } from '../sdk';
1414

1515
const metamaskOpenrpcDocument: OpenrpcDocument = MetaMaskOpenRPCDocument;
@@ -154,14 +154,14 @@ export function ScopeCard({ scope, details }: { scope: Scope; details: SessionDa
154154
const params = extractRequestParams(finalRequestObject);
155155
console.log(`📤 Calling invokeMethod with params:`, params);
156156

157-
const paramsArray = normalizeMethodParams(method, params);
158-
console.log(`📤 Normalized params array:`, paramsArray);
157+
const normalizedParams = normalizeMethodParams(method, params, scope);
158+
console.log(`📤 Normalized params:`, normalizedParams);
159159

160160
const result = await invokeMethod({
161161
scope,
162162
request: {
163163
method,
164-
params: paramsArray,
164+
params: normalizedParams,
165165
},
166166
});
167167

@@ -308,11 +308,10 @@ export function ScopeCard({ scope, details }: { scope: Scope; details: SessionDa
308308
className={`
309309
w-full mt-4 px-6 py-3 rounded-lg font-medium text-white transition-all duration-200
310310
flex items-center justify-center gap-2
311-
${
312-
!selectedMethods[scope] || !invokeMethodRequests[scope]
313-
? 'bg-gray-300 cursor-not-allowed text-gray-500'
314-
: 'bg-gradient-to-r from-purple-600 to-blue-600 hover:from-purple-700 hover:to-blue-700 hover:shadow-lg hover:scale-105 active:scale-95 cursor-pointer'
315-
}
311+
${!selectedMethods[scope] || !invokeMethodRequests[scope]
312+
? 'bg-gray-300 cursor-not-allowed text-gray-500'
313+
: 'bg-gradient-to-r from-purple-600 to-blue-600 hover:from-purple-700 hover:to-blue-700 hover:shadow-lg hover:scale-105 active:scale-95 cursor-pointer'
314+
}
316315
`}
317316
>
318317
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">

playground/multichain-react/src/helpers/MethodInvocationHelpers.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
import type { SessionData } from '@metamask/multichain-sdk';
2-
import type { CaipChainId, Json, CaipAccountId } from '@metamask/utils';
2+
import type { CaipAccountId, CaipChainId, Json } from '@metamask/utils';
33
import type { MethodObject } from '@open-rpc/meta-schema';
44
import type { Dispatch, SetStateAction } from 'react';
55

66
/**
7-
* Normalizes parameters for method invocation, ensuring they are always in array format
8-
* and applying special handling for specific methods.
7+
* Normalizes parameters for method invocation.
8+
* - For Solana methods: keeps params as an object
9+
* - For EVM methods: ensures params are in array format
10+
* - Applies special handling for specific methods.
911
*
1012
* @param method - The method name being invoked.
1113
* @param params - The raw parameters.
12-
* @returns Normalized parameters array.
14+
* @param scope - The scope/chain ID to determine the chain type.
15+
* @returns Normalized parameters (array for EVM, object for Solana).
1316
*/
14-
export const normalizeMethodParams = (method: string, params: Json): Json[] => {
15-
// Ensure params is always an array for the SDK
17+
export const normalizeMethodParams = (method: string, params: Json, scope?: string): Json[] | Json => {
18+
// Solana methods should keep params as objects, not arrays
19+
const solanaMethodsThatUseObjects = ['signMessage', 'signTransaction', 'signAllTransactions', 'signAndSendTransaction', 'signIn'];
20+
21+
// Check if this is a Solana method that needs object params
22+
if (solanaMethodsThatUseObjects.includes(method) || scope?.startsWith('solana:')) {
23+
// For Solana, return params as-is (should be an object)
24+
return params;
25+
}
26+
27+
// For EVM methods, ensure params is always an array
1628
let paramsArray = Array.isArray(params) ? params : [params];
1729

1830
// Special handling for eth_signTypedData_v3/v4: second parameter must be JSON string

playground/multichain-react/src/helpers/solana-method-signatures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Transaction, PublicKey, SystemProgram, Connection } from '@solana/web3.js';
21
import type { Commitment } from '@solana/web3.js';
2+
import { Connection, PublicKey, SystemProgram, Transaction } from '@solana/web3.js';
33

44
import { FEATURED_NETWORKS } from '../constants/networks';
55

0 commit comments

Comments
 (0)