-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab022b4
commit 90d41d7
Showing
5 changed files
with
80 additions
and
11 deletions.
There are no files selected for viewing
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
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
66 changes: 66 additions & 0 deletions
66
packages/examples/reactNativeSdkDemo/src/screens/DemoSign.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,66 @@ | ||
import { useSDK } from '@metamask/sdk-react-native'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { | ||
Button, | ||
Text, | ||
View, | ||
} from 'react-native'; | ||
import { ethers } from 'ethers'; | ||
|
||
|
||
export function DemoSignView(): React.JSX.Element { | ||
const { provider: MMProvider, account, connected } = useSDK(); | ||
const [provider, setProvider] = useState<ethers.providers.Web3Provider>(); | ||
|
||
|
||
useEffect(() => { | ||
if (connected && MMProvider && !provider) { | ||
// The following line will crash the app | ||
// if the option `infuraAPIKey` is provided to the MetaMaskProvider | ||
// We have this error on the ios sdk side: metamask-ios-sdk/Network.swift:46 | ||
// *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_NSInlineData)' | ||
const provider = new ethers.providers.Web3Provider( | ||
MMProvider | ||
); | ||
|
||
setProvider(provider); | ||
} | ||
}, [connected, MMProvider, setProvider]); | ||
|
||
return ( | ||
<> | ||
<Text> | ||
Sign Demo | ||
</Text> | ||
{ | ||
(!provider || !account) | ||
? <Text> User is not connected</Text> | ||
: <DemoSignComponent account={account} provider={provider} /> | ||
} | ||
</> | ||
); | ||
} | ||
|
||
|
||
function DemoSignComponent({ account, provider }: { account: string; provider: ethers.providers.Web3Provider }): React.JSX.Element { | ||
const [signature, setSignature] = useState<string>(); | ||
|
||
async function sign() { | ||
const signer = provider.getSigner(account); | ||
|
||
const signature = await signer.signMessage('Hello'); | ||
setSignature(signature); | ||
} | ||
|
||
return ( | ||
<View> | ||
<Text> | ||
Sign message with address: {account} | ||
</Text> | ||
<Button title="Sign" onPress={sign} /> | ||
<Text> | ||
Signature: {signature} | ||
</Text> | ||
</View> | ||
); | ||
} |
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