-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add legacy send method for MetaMask provider (#48)
* feat: add legacy send method for MetaMask provider and web3-react exple * feat: remove unused imports * feat: add signature to example
- Loading branch information
Showing
6 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
examples/react-apps/src/pages/web3-react/__test__/web3-react-page.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { generateTestingUtils } from "eth-testing"; | ||
import { ethers } from "ethers"; | ||
import Web3ReactPage from ".."; | ||
|
||
describe("Web3 React app", () => { | ||
let originalEthereum: any; | ||
const testingUtils = generateTestingUtils({ | ||
providerType: "MetaMask", | ||
}); | ||
|
||
beforeAll(() => { | ||
originalEthereum = global.window.ethereum; | ||
global.window.ethereum = testingUtils.getProvider(); | ||
}); | ||
|
||
afterEach(() => { | ||
testingUtils.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
global.window.ethereum = originalEthereum; | ||
}); | ||
test("User should be able to see its address after connection and sign a message", async () => { | ||
const randomWallet = ethers.Wallet.createRandom(); | ||
|
||
testingUtils.mockNotConnectedWallet(); | ||
|
||
testingUtils.mockRequestAccounts( | ||
[randomWallet.address], | ||
{ | ||
balance: "0x1bc16d674ec80000", | ||
} | ||
); | ||
|
||
render(<Web3ReactPage />); | ||
|
||
expect(screen.queryByText(/account/i)).not.toBeInTheDocument(); | ||
|
||
userEvent.click(screen.getByRole('button', { name: /connect/i})); | ||
|
||
await screen.findByText(`Account: ${randomWallet.address}`); | ||
|
||
const signature = await randomWallet.signMessage("hello"); | ||
testingUtils.lowLevel.mockRequest("personal_sign", signature); | ||
userEvent.click(screen.getByRole("button", { name: /sign message/i })) | ||
await screen.findByText(`Signature: ${signature}`); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as React from 'react'; | ||
import { ethers } from "ethers"; | ||
import { useWeb3React, Web3ReactProvider } from "@web3-react/core"; | ||
import { InjectedConnector } from "@web3-react/injected-connector"; | ||
|
||
const connector = new InjectedConnector({}); | ||
|
||
function Web3ReactPage() { | ||
return ( | ||
<Web3ReactProvider getLibrary={(p) => new ethers.providers.Web3Provider(p)}> | ||
<Example /> | ||
</Web3ReactProvider> | ||
) | ||
} | ||
|
||
function Example() { | ||
const { activate, account, library } = useWeb3React(); | ||
|
||
const [signature, setSignature] = React.useState(""); | ||
|
||
const sign = async () => { | ||
if (!library) { | ||
throw new Error("Oh no, it's broken :(") | ||
} | ||
const signer = library.getSigner(); | ||
const signature = await signer.signMessage("hello"); | ||
setSignature(signature); | ||
} | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={() => activate(connector)} | ||
> | ||
Connect | ||
</button> | ||
{account ? <div>Account: {account}</div> : null} | ||
{account | ||
? ( | ||
<div> | ||
<button onClick={sign}>Sign message</button> | ||
{signature ? <div>Signature: {signature}</div> : null} | ||
</div> | ||
) | ||
: null | ||
} | ||
</div> | ||
) | ||
} | ||
|
||
export default Web3ReactPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters