From 9d93d17da1f3f93327621c626c557ca68512ecd1 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 6 Jun 2022 20:57:12 +0800 Subject: [PATCH 1/3] fix: sign message with phantom --- packages/injected-script/sdk/Base.ts | 2 +- packages/injected-script/sdk/Coin98.ts | 6 +- .../src/SNSAdaptor/components/TabContent.tsx | 107 +++++++++--------- .../src/state/Connection/providers/Phantom.ts | 18 +-- 4 files changed, 68 insertions(+), 65 deletions(-) diff --git a/packages/injected-script/sdk/Base.ts b/packages/injected-script/sdk/Base.ts index 253aa966ec3f..a830fb979abc 100644 --- a/packages/injected-script/sdk/Base.ts +++ b/packages/injected-script/sdk/Base.ts @@ -57,7 +57,7 @@ export class InjectedProvider { /** * Send RPC request to the sdk object. */ - request(data: unknown): Promise { + request(data: unknown): Promise { return createPromise((id) => sendEvent('web3BridgeExecute', [this.pathname, 'request'].join('.'), id, data)) } diff --git a/packages/injected-script/sdk/Coin98.ts b/packages/injected-script/sdk/Coin98.ts index 58512549ea7a..27e08bc70d05 100644 --- a/packages/injected-script/sdk/Coin98.ts +++ b/packages/injected-script/sdk/Coin98.ts @@ -18,11 +18,11 @@ export class Coin98Provider extends InjectedProvider { super(pathnameMap[type]) } - override async request(data: RequestArguments): Promise { + override async request(data: RequestArguments): Promise { // coin98 cannot handle it correctly (test with coin98 v6.0.3) if (data.method === 'eth_chainId') { - return this.getProperty('chainId') + return this.getProperty('chainId') as T } - return super.request(data) + return super.request(data) } } diff --git a/packages/plugins/Debugger/src/SNSAdaptor/components/TabContent.tsx b/packages/plugins/Debugger/src/SNSAdaptor/components/TabContent.tsx index 6e11642bea22..5db93092794c 100644 --- a/packages/plugins/Debugger/src/SNSAdaptor/components/TabContent.tsx +++ b/packages/plugins/Debugger/src/SNSAdaptor/components/TabContent.tsx @@ -2,7 +2,7 @@ import { useAccount, useBalance, useBlockNumber, useWeb3Connection, useWeb3State import { makeStyles } from '@masknet/theme' import type { NetworkPluginID, SocialAddress, SocialIdentity } from '@masknet/web3-shared-base' import { useTokenConstants } from '@masknet/web3-shared-evm' -import { Button, List, ListItem, ListItemText, Table, TableCell, TableRow, Typography } from '@mui/material' +import { Button, List, ListItem, ListItemText, Table, TableBody, TableCell, TableRow, Typography } from '@mui/material' import { useCallback } from 'react' export interface TabContentProps { @@ -56,11 +56,7 @@ export function TabContent({ identity, socialAddressList }: TabContentProps) { {socialAddressList?.map((x) => ( - {x.type}: {x.label} - - } + primary={{x.type}} secondary={x.address} /> @@ -86,60 +82,63 @@ export function TabContent({ identity, socialAddressList }: TabContentProps) { const onPersonaSign = useCallback(async () => { const signed = await connection.signMessage('hello world', 'personalSign') + console.log(signed) window.alert(`Signed: ${signed}`) }, [connection]) return (
- - - Balance of {Others?.formatAddress(account, 4)} - - - {balance} - - - - - Block Number - - - {blockNumber} - - - - - Native Token Transfer - - - - - - - - Sign Message - - - - - - - - Identity - - {renderIdentity()} - - - - Social Address List - - {renderAddressNames()} - + + + + Balance of {Others?.formatAddress(account, 4)} + + + {balance} + + + + + Block Number + + + {blockNumber} + + + + + Native Token Transfer + + + + + + + + Sign Message + + + + + + + + Identity + + {renderIdentity()} + + + + Social Address List + + {renderAddressNames()} + +
) diff --git a/packages/plugins/Solana/src/state/Connection/providers/Phantom.ts b/packages/plugins/Solana/src/state/Connection/providers/Phantom.ts index acc34591ae0d..7aecd6571124 100644 --- a/packages/plugins/Solana/src/state/Connection/providers/Phantom.ts +++ b/packages/plugins/Solana/src/state/Connection/providers/Phantom.ts @@ -10,33 +10,37 @@ export class PhantomProvider extends BaseInjectedProvider implements SolanaProvi super(ProviderType.Phantom, injectedPhantomProvider) } - override signMessage(dataToSign: string) { - return this.bridge.request({ + override async signMessage(dataToSign: string) { + const { signature } = await this.bridge.request<{ + publicKey: string + signature: string + }>({ method: PhantomMethodType.SIGN_MESSAGE, params: { message: new TextEncoder().encode(dataToSign), display: 'hex', }, - }) as Promise + }) + return signature } override signTransaction(transaction: Transaction) { - return this.bridge.request({ + return this.bridge.request({ method: PhantomMethodType.SIGN_TRANSACTION, params: { message: bs58.encode(transaction.serializeMessage()), }, - }) as Promise + }) } override async signTransactions(transactions: Transaction[]) { - return this.bridge.request({ + return this.bridge.request({ method: 'signAllTransactions', params: { message: transactions.map((transaction) => { return bs58.encode(transaction.serializeMessage()) }), }, - }) as Promise + }) } } From 7991a9285a0ea2730b862a578c0c446c53402648 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 6 Jun 2022 21:15:35 +0800 Subject: [PATCH 2/3] fix: signMessage with Sollet --- .../src/state/Connection/providers/SolflareProvider.ts | 6 +++--- .../plugins/Solana/src/state/Connection/providers/Sollet.ts | 3 ++- packages/web3-shared/solana/constants/descriptors.ts | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts b/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts index b00091e31321..c4fba25d6642 100644 --- a/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts +++ b/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts @@ -1,6 +1,6 @@ import type { PublicKey, Transaction } from '@solana/web3.js' import { injectedSolflareProvider } from '@masknet/injected-script' -import { Coin98MethodType, ProviderType } from '@masknet/web3-shared-solana' +import { Coin98MethodType, PhantomMethodType, ProviderType } from '@masknet/web3-shared-solana' import type { SolanaProvider } from '../types' import { BaseInjectedProvider } from './BaseInjected' @@ -11,7 +11,7 @@ export class SolflareProvider extends BaseInjectedProvider implements SolanaProv override async signMessage(dataToSign: string): Promise { const { signature } = (await this.bridge.request({ - method: Coin98MethodType.SOL_SIGN, + method: PhantomMethodType.SIGN_MESSAGE, params: [new TextEncoder().encode(dataToSign)], })) as { signature: string } return signature @@ -19,7 +19,7 @@ export class SolflareProvider extends BaseInjectedProvider implements SolanaProv override async signTransaction(transaction: Transaction): Promise { const { signature, publicKey } = (await this.bridge.request({ - method: Coin98MethodType.SOL_SIGN, + method: PhantomMethodType.SIGN_TRANSACTION, params: [transaction], })) as { signature: Buffer; publicKey: PublicKey } transaction.addSignature(publicKey, signature) diff --git a/packages/plugins/Solana/src/state/Connection/providers/Sollet.ts b/packages/plugins/Solana/src/state/Connection/providers/Sollet.ts index 440634d68d40..4769a27dee37 100644 --- a/packages/plugins/Solana/src/state/Connection/providers/Sollet.ts +++ b/packages/plugins/Solana/src/state/Connection/providers/Sollet.ts @@ -1,3 +1,4 @@ +import base58 from 'bs58' import type { Transaction } from '@solana/web3.js' import Wallet from '@project-serum/sol-wallet-adapter' import { ChainId, ProviderType } from '@masknet/web3-shared-solana' @@ -23,7 +24,7 @@ export class SolletProvider extends BaseProvider implements SolanaProvider { override async signMessage(dataToSign: string) { const data = new TextEncoder().encode(dataToSign) const { signature } = await this.solanaProvider.sign(data, 'uft8') - return signature.toString('utf8') + return base58.encode(signature) } override signTransaction(transaction: Transaction) { diff --git a/packages/web3-shared/solana/constants/descriptors.ts b/packages/web3-shared/solana/constants/descriptors.ts index d23c0fa0e048..fe6d6f80fba1 100644 --- a/packages/web3-shared/solana/constants/descriptors.ts +++ b/packages/web3-shared/solana/constants/descriptors.ts @@ -154,7 +154,7 @@ export const PROVIDER_DESCRIPTORS: Array x.value), supportedEnhanceableSites: getEnumAsArray(EnhanceableSite).map((x) => x.value), From 8ebedc467748bc908479d33b31cab30ca99ff905 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 6 Jun 2022 21:29:36 +0800 Subject: [PATCH 3/3] fix: sign message with blocto --- .../Flow/src/state/Connection/connection.ts | 19 +++++++++++++++---- .../Connection/providers/SolflareProvider.ts | 2 +- packages/web3-shared/flow/types.ts | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/plugins/Flow/src/state/Connection/connection.ts b/packages/plugins/Flow/src/state/Connection/connection.ts index 8969f982e119..7f013f5dd842 100644 --- a/packages/plugins/Flow/src/state/Connection/connection.ts +++ b/packages/plugins/Flow/src/state/Connection/connection.ts @@ -1,6 +1,6 @@ import { first } from 'lodash-unified' import { unreachable } from '@dimensiondev/kit' -import type { BlockObject, CompositeSignature, MutateOptions, QueryOptions } from '@blocto/fcl' +import type { BlockObject, MutateOptions, QueryOptions } from '@blocto/fcl' import { ChainId, ProviderType, SchemaType, TransactionStatusCode } from '@masknet/web3-shared-flow' import { Account, @@ -13,6 +13,7 @@ import { import { Providers } from './provider' import type { FlowWeb3Connection as BaseConnection, FlowConnectionOptions } from './types' import { Web3StateSettings } from '../../settings' +import { toHex } from '@masknet/shared-base' class Connection implements BaseConnection { constructor(private chainId: ChainId, private account: string, private providerType: ProviderType) {} @@ -180,16 +181,26 @@ class Connection implements BaseConnection { } async signMessage(dataToSign: string, signType?: string, options?: FlowConnectionOptions) { const web3 = await this.getWeb3(options) - return web3.currentUser.signUserMessage(dataToSign) + const data = new TextEncoder().encode(dataToSign) + const signed = first(await web3.currentUser.signUserMessage(toHex(data))) + if (!signed) throw new Error('Failed to sign message.') + return signed.signature } async verifyMessage( dataToVerify: string, - signature: CompositeSignature[], + signature: string, signType?: string, options?: FlowConnectionOptions, ): Promise { const web3 = await this.getWeb3(options) - return web3.verifyUserSignatures(dataToVerify, signature) + if (!options?.account) throw new Error('No account found.') + return web3.verifyUserSignatures(dataToVerify, [ + { + addr: options?.account, + keyId: 1, + signature, + }, + ]) } async callTransaction(query: QueryOptions, options?: FlowConnectionOptions) { const web3 = await this.getWeb3(options) diff --git a/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts b/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts index c4fba25d6642..bfbaabc6ee00 100644 --- a/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts +++ b/packages/plugins/Solana/src/state/Connection/providers/SolflareProvider.ts @@ -1,6 +1,6 @@ import type { PublicKey, Transaction } from '@solana/web3.js' import { injectedSolflareProvider } from '@masknet/injected-script' -import { Coin98MethodType, PhantomMethodType, ProviderType } from '@masknet/web3-shared-solana' +import { PhantomMethodType, ProviderType } from '@masknet/web3-shared-solana' import type { SolanaProvider } from '../types' import { BaseInjectedProvider } from './BaseInjected' diff --git a/packages/web3-shared/flow/types.ts b/packages/web3-shared/flow/types.ts index cf469de72984..fab229476589 100644 --- a/packages/web3-shared/flow/types.ts +++ b/packages/web3-shared/flow/types.ts @@ -1,6 +1,6 @@ /// -import type { CompositeSignature, MutateOptions, BlockObject, TransactionObject } from '@blocto/fcl' +import type { MutateOptions, BlockObject, TransactionObject } from '@blocto/fcl' export enum ChainId { Mainnet = 1, @@ -42,7 +42,7 @@ export enum TransactionStatusCode { export type Web3 = typeof import('@blocto/fcl') export type Web3Provider = {} -export type Signature = CompositeSignature[] +export type Signature = string export type GasOption = never export type Block = BlockObject export type Transaction = MutateOptions