Skip to content

Commit

Permalink
v13.1.0: add connectedWallets
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Dec 28, 2022
1 parent 52cd49a commit c108f86
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 19 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ if(foundWallets.length == 1) {
}
```

### getConnectedWallets

`getConnectedWallets`: Returns an array of currently connected wallets.

```javascript
let wallets = await getConnectedWallets();
// [<Wallet name='MetaMask'>, <Wallet name='Phantom'>]
```

### Name

`name:string`: Returns the name of the wallet.
Expand Down
37 changes: 31 additions & 6 deletions dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,18 @@ <h5 class="px-4">wallet.connect()</h5>
<script>
document.addEventListener("DOMContentLoaded", function() {
let select = document.getElementById('connectWallet')
Web3Wallets.getWallets().forEach((wallet, index)=>{
let option = document.createElement("option")
option.setAttribute('value', index)
option.innerHTML = wallet.name
select.append(option)
})
setTimeout(()=>{
Web3Wallets.getWallets().forEach((wallet, index)=>{
let option = document.createElement("option")
option.setAttribute('value', index)
option.innerHTML = wallet.name
select.append(option)
})
}, 500)
})
window.connectWalletViaSelect = async ()=> {
let wallet = Web3Wallets.getWallets()[parseInt(document.getElementById("connectWallet").value, 10)]
console.log(wallet)
let accounts = await wallet.connect()
window.wallet = wallet
console.log(accounts)
Expand All @@ -92,6 +95,28 @@ <h5 class="px-4">wallet.connect()</h5>
</div>
</div>

<div class="d-block d-md-inline-block p-2 col-12 col-md-4">
<div class="px-0 py-4 rounded-lg bg-white border">
<div class="col px-0">
<div class="overflow-auto" style="height: 120px;">
<h5 class="px-4">getConnectedWallets()</h5>
<div class="px-4">
<div class="text-secondary pb-2">Get all connected wallets.</div>
<script>
window.getConnectedWallets = async ()=> {
let connectedWallets = await Web3Wallets.getConnectedWallets()
console.log('connectedWallets', connectedWallets)
}
</script>
</div>
</div>
<div class="px-4 pt-3">
<button class='btn btn-primary' onClick='getConnectedWallets()'>Try it</button>
</div>
</div>
</div>
</div>

<div class="d-block d-md-inline-block p-2 col-12 col-md-4">
<div class="px-0 py-4 rounded-lg bg-white border">
<div class="col px-0">
Expand Down
15 changes: 14 additions & 1 deletion dist/esm/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,11 +864,24 @@ const getWallets = ()=>{
return availableWallets
};

const getConnectedWallets = async()=>{

let connectedWallets = (await Promise.all(
getWallets().map(async(wallet)=>{
if(await wallet.account()) {
return wallet
}
})
)).filter((value)=>!!value);

return connectedWallets
};

const supported = [
wallets.MetaMask,
wallets.Coinbase,
wallets.WalletConnect,
wallets.WalletLink
];

export { getWallets, supported, wallets };
export { getConnectedWallets, getWallets, supported, wallets };
22 changes: 18 additions & 4 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,10 @@ class WindowSolana {
}

async account() {
if(_optionalChain$3([window, 'optionalAccess', _7 => _7.solana, 'optionalAccess', _8 => _8.publicKey])) {return window.solana.publicKey.toString() }
if(_optionalChain$3([window, 'optionalAccess', _9 => _9.solana]) == undefined){ return }
let { publicKey } = await window.solana.connect({ onlyIfTrusted: true });
if(_optionalChain$3([window, 'optionalAccess', _7 => _7.solana]) == undefined){ return }
if(_optionalChain$3([window, 'optionalAccess', _8 => _8.solana, 'optionalAccess', _9 => _9.publicKey])) { return window.solana.publicKey.toString() }
let publicKey;
try { ({ publicKey } = await window.solana.connect({ onlyIfTrusted: true })); } catch (e) {}
if(publicKey){ return publicKey.toString() }
}

Expand Down Expand Up @@ -1054,6 +1055,19 @@ const getWallets = ()=>{
return availableWallets
};

const getConnectedWallets = async()=>{

let connectedWallets = (await Promise.all(
getWallets().map(async(wallet)=>{
if(await wallet.account()) {
return wallet
}
})
)).filter((value)=>!!value);

return connectedWallets
};

const supported = [
wallets.MetaMask,
wallets.Phantom,
Expand All @@ -1062,4 +1076,4 @@ const supported = [
wallets.WalletLink
];

export { getWallets, supported, wallets };
export { getConnectedWallets, getWallets, supported, wallets };
14 changes: 14 additions & 0 deletions dist/umd/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,27 @@
return availableWallets
};

const getConnectedWallets = async()=>{

let connectedWallets = (await Promise.all(
getWallets().map(async(wallet)=>{
if(await wallet.account()) {
return wallet
}
})
)).filter((value)=>!!value);

return connectedWallets
};

const supported = [
wallets.MetaMask,
wallets.Coinbase,
wallets.WalletConnect,
wallets.WalletLink
];

exports.getConnectedWallets = getConnectedWallets;
exports.getWallets = getWallets;
exports.supported = supported;
exports.wallets = wallets;
Expand Down
21 changes: 18 additions & 3 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,10 @@
}

async account() {
if(_optionalChain$3([window, 'optionalAccess', _7 => _7.solana, 'optionalAccess', _8 => _8.publicKey])) {return window.solana.publicKey.toString() }
if(_optionalChain$3([window, 'optionalAccess', _9 => _9.solana]) == undefined){ return }
let { publicKey } = await window.solana.connect({ onlyIfTrusted: true });
if(_optionalChain$3([window, 'optionalAccess', _7 => _7.solana]) == undefined){ return }
if(_optionalChain$3([window, 'optionalAccess', _8 => _8.solana, 'optionalAccess', _9 => _9.publicKey])) { return window.solana.publicKey.toString() }
let publicKey;
try { ({ publicKey } = await window.solana.connect({ onlyIfTrusted: true })); } catch (e) {}
if(publicKey){ return publicKey.toString() }
}

Expand Down Expand Up @@ -1052,6 +1053,19 @@
return availableWallets
};

const getConnectedWallets = async()=>{

let connectedWallets = (await Promise.all(
getWallets().map(async(wallet)=>{
if(await wallet.account()) {
return wallet
}
})
)).filter((value)=>!!value);

return connectedWallets
};

const supported = [
wallets.MetaMask,
wallets.Phantom,
Expand All @@ -1060,6 +1074,7 @@
wallets.WalletLink
];

exports.getConnectedWallets = getConnectedWallets;
exports.getWallets = getWallets;
exports.supported = supported;
exports.wallets = wallets;
Expand Down
2 changes: 1 addition & 1 deletion package.evm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-wallets-evm",
"moduleName": "Web3Wallets",
"version": "13.0.0",
"version": "13.1.0",
"description": "One-Stop-Shop JavaScript library to integrate various web3 crypto wallets and multiple blockchains at once with a single interface.",
"main": "dist/umd/index.evm.js",
"module": "dist/esm/index.evm.js",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-wallets",
"moduleName": "Web3Wallets",
"version": "13.0.0",
"version": "13.1.0",
"description": "One-Stop-Shop JavaScript library to integrate various web3 crypto wallets and multiple blockchains at once with a single interface.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down
16 changes: 16 additions & 0 deletions src/getConnectedWallets.evm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import getWallets from './getWallets.evm'

const getConnectedWallets = async()=>{

let connectedWallets = (await Promise.all(
getWallets().map(async(wallet)=>{
if(await wallet.account()) {
return wallet
}
})
)).filter((value)=>!!value)

return connectedWallets
}

export default getConnectedWallets
16 changes: 16 additions & 0 deletions src/getConnectedWallets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import getWallets from './getWallets'

const getConnectedWallets = async()=>{

let connectedWallets = (await Promise.all(
getWallets().map(async(wallet)=>{
if(await wallet.account()) {
return wallet
}
})
)).filter((value)=>!!value)

return connectedWallets
}

export default getConnectedWallets
2 changes: 2 additions & 0 deletions src/index.evm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getConnectedWallets from './getConnectedWallets.evm'
import getWallets from './getWallets.evm'
import wallets from './wallets.evm'

Expand All @@ -10,6 +11,7 @@ const supported = [

export {
getWallets,
getConnectedWallets,
supported,
wallets
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getConnectedWallets from './getConnectedWallets'
import getWallets from './getWallets'
import wallets from './wallets'

Expand All @@ -11,6 +12,7 @@ const supported = [

export {
getWallets,
getConnectedWallets,
supported,
wallets
wallets,
}
5 changes: 3 additions & 2 deletions src/wallets/WindowSolana.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export default class WindowSolana {
}

async account() {
if(window?.solana?.publicKey) {return window.solana.publicKey.toString() }
if(window?.solana == undefined){ return }
let { publicKey } = await window.solana.connect({ onlyIfTrusted: true })
if(window?.solana?.publicKey) { return window.solana.publicKey.toString() }
let publicKey
try { ({ publicKey } = await window.solana.connect({ onlyIfTrusted: true })) } catch {}
if(publicKey){ return publicKey.toString() }
}

Expand Down
46 changes: 46 additions & 0 deletions tests/units/wallets/getConnectedWallets.evm.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getConnectedWallets } from 'src/index.evm'
import { mock, resetMocks, trigger } from '@depay/web3-mock'
import { supported as supportedBlockchains } from 'src/blockchains.evm'

describe('getConnectedWallets', () => {

let wallet

describe('with no supported wallet connected', ()=>{

beforeEach(resetMocks)

it('provides no connected wallets', async () => {
expect(await getConnectedWallets()).toStrictEqual([])
})
})

describe('with supported wallet connected', ()=>{

const account = '0xd8da6bf26964af9d7eed9e03e53415d37aa96045'
beforeEach(resetMocks)
beforeEach(()=>{
mock({ blockchain: 'ethereum', accounts: { return: [account] } })
})

it('provides list of connected wallets', async () => {
expect((await getConnectedWallets()).map((wallet)=>wallet.name)).toStrictEqual(["Wallet (Ethereum)"])
})
})

describe('with multiple supported wallets connected', ()=>{

const account = '0xd8da6bf26964af9d7eed9e03e53415d37aa96045'
beforeEach(resetMocks)
beforeEach(()=>{
mock({ blockchain: 'ethereum', accounts: { return: [account] } })
mock({ blockchain: 'bsc', accounts: { return: [account] }, wallet: 'metamask' })
mock({ blockchain: 'solana', accounts: { return: [account] }, wallet: 'phantom' })
})

it('provides list of connected wallets', async () => {
expect((await getConnectedWallets()).map((wallet)=>wallet.name)).toStrictEqual(["MetaMask"])
})
})
})

46 changes: 46 additions & 0 deletions tests/units/wallets/getConnectedWallets.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getConnectedWallets } from 'src'
import { mock, resetMocks, trigger } from '@depay/web3-mock'
import { supported as supportedBlockchains } from 'src/blockchains'

describe('getConnectedWallets', () => {

let wallet

describe('with no supported wallet connected', ()=>{

beforeEach(resetMocks)

it('provides no connected wallets', async () => {
expect(await getConnectedWallets()).toStrictEqual([])
})
})

describe('with supported wallet connected', ()=>{

const account = '0xd8da6bf26964af9d7eed9e03e53415d37aa96045'
beforeEach(resetMocks)
beforeEach(()=>{
mock({ blockchain: 'ethereum', accounts: { return: [account] } })
})

it('provides list of connected wallets', async () => {
expect((await getConnectedWallets()).map((wallet)=>wallet.name)).toStrictEqual(["Wallet (Ethereum)"])
})
})

describe('with multiple supported wallets connected', ()=>{

const account = '0xd8da6bf26964af9d7eed9e03e53415d37aa96045'
beforeEach(resetMocks)
beforeEach(()=>{
mock({ blockchain: 'ethereum', accounts: { return: [account] } })
mock({ blockchain: 'bsc', accounts: { return: [account] }, wallet: 'metamask' })
mock({ blockchain: 'solana', accounts: { return: [account] }, wallet: 'phantom' })
})

it('provides list of connected wallets', async () => {
expect((await getConnectedWallets()).map((wallet)=>wallet.name)).toStrictEqual(["MetaMask"])
})
})
})

0 comments on commit c108f86

Please sign in to comment.