Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 217887d

Browse files
authored
Upgrade MetaMask support (#217)
Fetch the account address from MetaMask using request({ method: 'eth_requestAccounts' }). Previously used the deprecated selectedAddress field. Configure chainId to StreamrClient options. The parameter is required when using MetaMask sidechain signing. BREAKING CHANGE: client.getAddress() is now async We could be more explicit about network chainIds. Sidechain chainId could be a required parameter in StreamrClient options and we could also add chainId parameter to options.mainnet definition. The chainId validation can be enhanced in another PR later, if needed. Note that there are currently no automated tests to check the MetaMask integration.
1 parent 4bdc631 commit 217887d

File tree

11 files changed

+31
-24
lines changed

11 files changed

+31
-24
lines changed

examples/node/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/webpack/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "streamr-client",
3-
"version": "5.0.0-beta.9",
3+
"version": "5.0.0-beta.10",
44
"description": "JavaScript client library for Streamr",
55
"repository": {
66
"type": "git",

src/Config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type StrictStreamrClientOptions = {
3333
groupKeys: Todo
3434
keyExchange: Todo
3535
mainnet?: ConnectionInfo|string
36-
sidechain?: ConnectionInfo|string
36+
sidechain: ConnectionInfo & { chainId?: number }
3737
tokenAddress: EthereumAddress,
3838
dataUnion: {
3939
minimumWithdrawTokenWei: BigNumber|number|string
@@ -86,7 +86,10 @@ export default function ClientConfig(opts: StreamrClientOptions = {}) {
8686
// Ethereum and Data Union related options
8787
// For ethers.js provider params, see https://docs.ethers.io/ethers.js/v5-beta/api-providers.html#provider
8888
mainnet: undefined, // Default to ethers.js default provider settings
89-
sidechain: 'https://rpc.xdaichain.com/',
89+
sidechain: {
90+
url: 'https://rpc.xdaichain.com/',
91+
chainId: 100
92+
},
9093
tokenAddress: '0x0Cf0Ee63788A0849fE5297F3407f701E122cC023',
9194
dataUnion: {
9295
minimumWithdrawTokenWei: '1000000', // Threshold value set in AMB configs, smallest token amount to pass over the bridge
@@ -106,7 +109,7 @@ export default function ClientConfig(opts: StreamrClientOptions = {}) {
106109

107110
const options: StrictStreamrClientOptions = {
108111
...defaults,
109-
...opts,
112+
...opts, // sidechain is not merged with the defaults
110113
dataUnion: {
111114
...defaults.dataUnion,
112115
...opts.dataUnion

src/Ethereum.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,35 @@ export default class StreamrEthereum {
1919
if (auth.privateKey) {
2020
const key = auth.privateKey
2121
const address = getAddress(computeAddress(key))
22-
this._getAddress = () => address
22+
this._getAddress = async () => address
2323
this.getSigner = () => new Wallet(key, this.getMainnetProvider())
2424
this.getSidechainSigner = async () => new Wallet(key, this.getSidechainProvider())
2525
} else if (auth.ethereum) {
26-
this._getAddress = () => {
27-
if (auth.ethereum.selectedAddress) {
26+
this._getAddress = async () => {
27+
try {
28+
const accounts = await auth.ethereum.request({ method: 'eth_requestAccounts' })
29+
const account = getAddress(accounts[0]) // convert to checksum case
30+
return account
31+
} catch {
2832
throw new Error('no addresses connected+selected in Metamask')
2933
}
30-
return getAddress(auth.ethereum.selectedAddress)
3134
}
3235
this._getSigner = () => {
3336
const metamaskProvider = new Web3Provider(auth.ethereum)
3437
const metamaskSigner = metamaskProvider.getSigner()
3538
return metamaskSigner
3639
}
3740
this._getSidechainSigner = async () => {
38-
// chainId is required for checking when using Metamask
3941
if (!options.sidechain || !options.sidechain.chainId) {
4042
throw new Error('Streamr sidechain not configured (with chainId) in the StreamrClient options!')
4143
}
4244

4345
const metamaskProvider = new Web3Provider(auth.ethereum)
4446
const { chainId } = await metamaskProvider.getNetwork()
4547
if (chainId !== options.sidechain.chainId) {
46-
throw new Error(`Please connect Metamask to Ethereum blockchain with chainId ${options.sidechain.chainId}`)
48+
throw new Error(
49+
`Please connect Metamask to Ethereum blockchain with chainId ${options.sidechain.chainId}: current chainId is ${chainId}`
50+
)
4751
}
4852
const metamaskSigner = metamaskProvider.getSigner()
4953
return metamaskSigner
@@ -57,7 +61,7 @@ export default class StreamrEthereum {
5761
}
5862
}
5963

60-
getAddress() {
64+
async getAddress() {
6165
if (!this._getAddress) {
6266
// _getAddress is assigned in constructor
6367
throw new Error('StreamrClient is not authenticated with private key')

src/StreamrClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ export class StreamrClient extends EventEmitter {
381381
return this.connection.enableAutoDisconnect(...args)
382382
}
383383

384-
getAddress(): EthereumAddress {
384+
async getAddress(): Promise<EthereumAddress> {
385385
return this.ethereum.getAddress()
386386
}
387387

src/dataunion/DataUnion.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class DataUnion {
9494
* Send a joinRequest, or get into data union instantly with a data union secret
9595
*/
9696
async join(secret?: string): Promise<JoinResponse> {
97-
const memberAddress = this.client.getAddress() as string
97+
const memberAddress = await this.client.getAddress()
9898
const body: any = {
9999
memberAddress
100100
}
@@ -132,7 +132,7 @@ export class DataUnion {
132132
* @returns receipt once withdraw is complete (tokens are seen in mainnet)
133133
*/
134134
async withdrawAll(options?: DataUnionWithdrawOptions): Promise<TransactionReceipt> {
135-
const recipientAddress = this.client.getAddress()
135+
const recipientAddress = await this.client.getAddress()
136136
return this._executeWithdraw(
137137
() => this.getWithdrawAllTx(),
138138
recipientAddress,
@@ -464,7 +464,7 @@ export class DataUnion {
464464
* @internal
465465
*/
466466
static async _deploy(options: DataUnionDeployOptions = {}, client: StreamrClient): Promise<DataUnion> {
467-
const deployerAddress = client.getAddress()
467+
const deployerAddress = await client.getAddress()
468468
const {
469469
owner,
470470
joinPartAgents,

test/integration/dataunion/adminFee.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('DataUnion admin fee', () => {
4141
const dataUnion = await adminClient.deployDataUnion()
4242
const oldFee = await dataUnion.getAdminFee()
4343
log(`DU owner: ${await dataUnion.getAdminAddress()}`)
44-
log(`Sending tx from ${adminClient.getAddress()}`)
44+
log(`Sending tx from ${await adminClient.getAddress()}`)
4545
const tr = await dataUnion.setAdminFee(0.1)
4646
log(`Transaction receipt: ${JSON.stringify(tr)}`)
4747
const newFee = await dataUnion.getAdminFee()

test/integration/dataunion/deploy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('DataUnion deploy', () => {
3434

3535
it('not specified: defaults to deployer', async () => {
3636
const dataUnion = await adminClient.deployDataUnion()
37-
expect(await dataUnion.getAdminAddress()).toBe(adminClient.getAddress())
37+
expect(await dataUnion.getAdminAddress()).toBe(await adminClient.getAddress())
3838
}, 60000)
3939

4040
it('specified', async () => {

0 commit comments

Comments
 (0)