Skip to content

Commit

Permalink
feat: add sdk support (#331)
Browse files Browse the repository at this point in the history
* Add sdk support and move connect logic to its own file

* sdk as dev dependency

* add remove logic

* renderproviderdetails logic

* adding dependencies config for lavamoat

* reorder imports
  • Loading branch information
seaona authored May 3, 2024
1 parent 314b221 commit 2252b6e
Show file tree
Hide file tree
Showing 6 changed files with 890 additions and 188 deletions.
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
"@metamask/eslint-config-nodejs": "^6.0.0",
"@metamask/eth-sig-util": "^7.0.1",
"@metamask/onboarding": "^1.0.0",
"@metamask/sdk": "^0.18.6",
"@openzeppelin/contracts": "4.9.6",
"@walletconnect/modal": "^2.6.2",
"@web3modal/ethers5": "^3.2.0",
"assert": "^2.1.0",
"base64-sol": "1.1.0",
"clean-webpack-plugin": "^4.0.0",
Expand All @@ -54,8 +57,6 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"ethereumjs-util": "^5.1.1",
"@web3modal/ethers5": "^3.2.0",
"@walletconnect/modal": "^2.6.2",
"ethers": "5.7.2",
"gh-pages": "^5.0.0",
"prettier": "^2.3.1",
Expand All @@ -74,7 +75,11 @@
"@web3modal/ethers5>@coinbase/wallet-sdk>@solana/web3.js>bigint-buffer": false,
"@web3modal/ethers5>@coinbase/wallet-sdk>keccak": false,
"webpack-dev-server>ws>bufferutil": false,
"webpack-dev-server>ws>utf-8-validate": false
"webpack-dev-server>ws>utf-8-validate": false,
"@metamask/sdk>@metamask/sdk-communication-layer>bufferutil": false,
"@metamask/sdk>@metamask/sdk-communication-layer>utf-8-validate": false,
"@metamask/sdk>eciesjs>secp256k1": false,
"@web3modal/ethers5>@coinbase/wallet-sdk>@solana/web3.js>rpc-websockets>utf-8-validate": false
}
},
"packageManager": "yarn@1.22.19"
Expand Down
105 changes: 105 additions & 0 deletions src/connections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { MetaMaskSDK } from '@metamask/sdk';
import { createWeb3Modal, defaultConfig } from '@web3modal/ethers5';
import {
handleNewAccounts,
handleNewProviderDetail,
removeProviderDetail,
setActiveProviderDetail,
updateFormElements,
updateSdkConnectionState,
updateWalletConnectState,
} from '.';

const dappMetadata = {
name: 'E2e Test Dapp',
description: 'This is the E2e Test Dapp',
url: 'https://metamask.github.io/test-dapp/',
};

const sdk = new MetaMaskSDK({ dappMetadata });

export const walletConnect = createWeb3Modal({
ethersConfig: defaultConfig({ metadata: dappMetadata }),
projectId: 'e6360eaee594162688065f1c70c863b7', // test id
});

function _setProviderDetail(provider, name, uuid) {
const providerDetail = {
info: {
uuid,
name,
icon: `./${name}.svg`,
rdns: 'io.metamask',
},
provider,
};
return providerDetail;
}

export async function handleSdkConnect(name, button, isConnected) {
if (isConnected) {
handleNewAccounts([]);
updateFormElements();
updateSdkConnectionState(false);
removeProviderDetail(name);
sdk.terminate();
button.innerText = 'Sdk Connect';
button.classList.add('btn-primary');
button.classList.remove('btn-danger');
} else {
await sdk.connect();
const provider = sdk.getProvider();
const uuid = sdk.getChannelId();
const providerDetail = _setProviderDetail(provider, name, uuid);
setActiveProviderDetail(providerDetail);
handleNewProviderDetail(providerDetail);
updateSdkConnectionState(true);
button.innerText = 'Sdk Connect - Disconnect';
button.classList.remove('btn-primary');
button.classList.add('btn-danger');

updateFormElements();

try {
const newAccounts = await provider.request({
method: 'eth_accounts',
});
handleNewAccounts(newAccounts);
} catch (err) {
console.error('Error on init when getting accounts', err);
}
}
}

export async function handleWalletConnect(name, button, isConnected) {
if (isConnected) {
handleNewAccounts([]);
updateFormElements();
updateWalletConnectState(false);
removeProviderDetail(name);
button.innerText = 'Wallet Connect';
button.classList.add('btn-primary');
button.classList.remove('btn-danger');
} else {
const { provider } = walletConnect.getWalletProvider();
const uuid = provider.signer.uri;
const providerDetail = _setProviderDetail(provider, name, uuid);
setActiveProviderDetail(providerDetail);
handleNewProviderDetail(providerDetail);
updateWalletConnectState(true);
button.innerText = 'Wallet Connect - Disconnect';
button.classList.remove('btn-primary');
button.classList.add('btn-danger');

updateFormElements();

try {
const newAccounts = await provider.request({
method: 'eth_accounts',
});
handleNewAccounts(newAccounts);
} catch (err) {
console.error('Error on init when getting accounts', err);
}
}
}
16 changes: 9 additions & 7 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ <h3 class="card-title">
<div class="card">
<div class="card-body">
<h4 class="card-title">
Basic Actions
Connect Actions
</h4>

<button
Expand All @@ -135,10 +135,17 @@ <h4 class="card-title">

<button
class="btn btn-primary btn-lg btn-block mb-3"
id="open-connect-modal"
id="walletConnect"
>
Wallet Connect
</button>
<button
class="btn btn-primary btn-lg btn-block mb-3"
id="sdkConnect"
>
SDK Connect
</button>
<hr />
<button
class="btn btn-primary btn-lg btn-block mb-3"
id="getAccounts"
Expand All @@ -152,11 +159,6 @@ <h4 class="card-title">
</div>
</div>
</div>
</div>
</section>

<section>
<div class="row d-flex justify-content-center">
<div class="col-xl-4 col-lg-6 col-md-12 col-sm-12 col-12">
<div class="card">
<div class="card-body">
Expand Down
Loading

0 comments on commit 2252b6e

Please sign in to comment.