Skip to content

Commit

Permalink
fix: sign stuff with web3-eth-accounts (#10394)
Browse files Browse the repository at this point in the history
* fix: sign stuff with web3-eth-accounts

* fix: markdownlint

---------

Co-authored-by: guanbinrui <guanbinrui@users.noreply.github.com>
  • Loading branch information
guanbinrui and guanbinrui committed Aug 14, 2023
1 parent 7401967 commit 2455d9c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { useCallback, useMemo } from 'react'
import { Card, Typography, Box } from '@mui/material'
import { Stack } from '@mui/system'
import { LoadingBase, makeStyles, parseColor } from '@masknet/theme'
import { ChainId } from '@masknet/web3-shared-evm'
import { ChainId, signMessage } from '@masknet/web3-shared-evm'
import { RedPacketStatus, type RedPacketJSONPayload } from '@masknet/web3-providers/types'
import { formatBalance, isZero, TokenType } from '@masknet/web3-shared-base'
import { NetworkPluginID, isFacebook, isTwitter } from '@masknet/shared-base'
import { ChainResolver, NetworkResolver, Web3 } from '@masknet/web3-providers'
import { ChainResolver, NetworkResolver } from '@masknet/web3-providers'
import { useChainContext, useNetworkContext } from '@masknet/web3-hooks-base'
import { TransactionConfirmModal } from '@masknet/shared'
import { usePostLink, useSiteAdaptorContext } from '@masknet/plugin-infra/content-script'
Expand Down Expand Up @@ -158,9 +158,7 @@ export function RedPacket(props: RedPacketProps) {
payload.contract_version,
account,
payload.rpid,
payload.contract_version > 3
? Web3.getWeb3().eth.accounts.sign(account, payload.password).signature
: payload.password,
payload.contract_version > 3 ? signMessage(account, payload.password).signature : payload.password,
payloadChainId,
)

Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/RedPacket/src/SiteAdaptor/RedPacketNft.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo } from 'react'
import { makeStyles, ActionButton, LoadingBase, parseColor, ShadowRootTooltip, useDetectOverflow } from '@masknet/theme'
import type { ChainId } from '@masknet/web3-shared-evm'
import { signMessage, type ChainId } from '@masknet/web3-shared-evm'
import { type RedPacketNftJSONPayload } from '@masknet/web3-providers/types'
import { Card, Typography, Button, Box } from '@mui/material'
import {
Expand All @@ -11,7 +11,7 @@ import {
TransactionConfirmModal,
} from '@masknet/shared'
import { useChainContext, useNetworkContext, useNonFungibleAsset } from '@masknet/web3-hooks-base'
import { NetworkResolver, Web3 } from '@masknet/web3-providers'
import { NetworkResolver } from '@masknet/web3-providers'
import { TokenType } from '@masknet/web3-shared-base'
import { usePostLink, useSiteAdaptorContext } from '@masknet/plugin-infra/content-script'
import { NetworkPluginID, CrossIsolationMessages, isTwitter, isFacebook } from '@masknet/shared-base'
Expand Down Expand Up @@ -203,7 +203,7 @@ export function RedPacketNft({ payload }: RedPacketNftProps) {
const [{ loading: isClaiming }, claimCallback] = useClaimNftRedpacketCallback(
payload.id,
availability?.totalAmount,
Web3.getWeb3().eth.accounts.sign(account, payload.privateKey).signature ?? '',
signMessage(account, payload.privateKey).signature ?? '',
)
const [showTooltip, textRef] = useDetectOverflow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
isCryptoPunksContractAddress,
getEthereumConstant,
getTokenConstant,
createAccount,
} from '@masknet/web3-shared-evm'
import {
type FungibleToken,
Expand Down Expand Up @@ -445,7 +446,7 @@ export class ConnectionReadonlyAPI

createAccount(initial?: ConnectionOptions_Base<ChainId, ProviderType, Transaction> | undefined): Account<ChainId> {
const options = this.ConnectionOptions.fill(initial)
const account = this.getWeb3().eth.accounts.create()
const account = createAccount()
return {
account: account.address,
chainId: options.chainId,
Expand Down
11 changes: 3 additions & 8 deletions packages/web3-providers/src/Web3/EVM/apis/SignerAPI.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import * as _metamask_eth_sig_util from /* webpackDefer: true */ '@metamask/eth-sig-util'
import type { Transaction } from '@masknet/web3-shared-evm'
import { signTransaction, type Transaction } from '@masknet/web3-shared-evm'
import { SignType, toHex } from '@masknet/shared-base'
import { ConnectionReadonlyAPI } from './ConnectionReadonlyAPI.js'
import type { SignerAPI_Base } from '../../../entry-types.js'

export class SignerAPI implements SignerAPI_Base.Provider {
private Web3 = new ConnectionReadonlyAPI()

async sign<T>(type: SignType, key: Buffer, message: T): Promise<string> {
switch (type) {
case SignType.Message:
Expand All @@ -26,12 +23,10 @@ export class SignerAPI implements SignerAPI_Base.Provider {
const chainId = transaction.chainId
if (!chainId) throw new Error('Invalid chain id.')

const { rawTransaction } = await this.Web3.getWeb3({
chainId,
}).eth.accounts.signTransaction(transaction, toHex(key))
const { rawTransaction } = await signTransaction(transaction, toHex(key))
if (!rawTransaction) throw new Error('Failed to sign transaction.')

return rawTransaction

default:
throw new Error(`Unknown sign type: ${type}.`)
}
Expand Down
1 change: 1 addition & 0 deletions packages/web3-shared/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"web3-core": "1.8.0",
"web3-core-helpers": "1.8.0",
"web3-eth-abi": "1.8.0",
"web3-eth-accounts": "1.8.0",
"web3-eth-contract": "1.8.0",
"web3-utils": "1.8.0"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/web3-shared/evm/src/helpers/createAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Accounts from 'web3-eth-accounts'

export function createAccount() {
const accounts = new (Accounts as unknown as new () => Accounts.Accounts)()
return accounts.create()
}
6 changes: 6 additions & 0 deletions packages/web3-shared/evm/src/helpers/signMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Accounts from 'web3-eth-accounts'

export function signMessage(message: string, privateKey: string) {
const accounts = new (Accounts as unknown as new () => Accounts.Accounts)()
return accounts.sign(message, privateKey)
}
8 changes: 8 additions & 0 deletions packages/web3-shared/evm/src/helpers/signTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { toHex } from 'web3-utils'
import Accounts from 'web3-eth-accounts'
import type { Transaction } from '../types/index.js'

export function signTransaction(transaction: Transaction, privateKey: string) {
const accounts = new (Accounts as unknown as new () => Accounts.Accounts)()
return accounts.signTransaction(transaction, toHex(privateKey))
}
3 changes: 3 additions & 0 deletions packages/web3-shared/evm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export * from './helpers/token.js'
export * from './helpers/isRiskyMethodType.js'
export * from './helpers/isReadonlyMethodType.js'
export * from './helpers/isMaskOnlyMethodType.js'
export * from './helpers/createAccount.js'
export * from './helpers/signTransaction.js'
export * from './helpers/signMessage.js'

export * from './libs/index.js'
export * from './types/index.js'
16 changes: 15 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2455d9c

Please sign in to comment.