-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add metamask extension web3 #566
Merged
jacogr
merged 40 commits into
polkadot-js:master
from
PureStake:jlm-add-metamask-extension-web3
Sep 3, 2021
Merged
Changes from 7 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
4c8b1c8
added web3source
joelamouche a98b814
tried fixing dependencies
joelamouche a956347
removed load event listener and added name
joelamouche 2d44686
add signer to interface
joelamouche f8c01a2
Merge branch 'master' into jlm-add-metamask-extension-web3
joelamouche f056a53
added signPayload draft
joelamouche 42bf1fd
lint
joelamouche 966c6db
modified signpayload
joelamouche b3ff173
switch back to only signRaw
joelamouche 6303215
sync with master and update to new metamask norm
joelamouche a1f12b9
add types to injecetd accounts
joelamouche e275ad3
update deps
joelamouche 6ff55de
hash tx and finalize code
joelamouche e8dc31c
lint and type
joelamouche 83b02de
sync with master
joelamouche e40bcce
sync with master
joelamouche 02c1543
iterate on feedback
joelamouche 034ce08
lint index.ts
joelamouche 0ca3c18
prettier lint index.ts
joelamouche 40cc504
Merge branch 'jlm-lint-extension' into jlm-add-metamask-extension-web3
joelamouche 2f590e6
type optional again
joelamouche 786cdb9
Merge branch 'jlm-add-metamask-extension-web3' of github.com:PureStak…
joelamouche ba312be
removed sigenr from injected account type
joelamouche 6081cef
sync with master
joelamouche 1314d64
lint
joelamouche 26f14eb
sync with master
joelamouche 7bbb18b
wip remove web3
joelamouche 74da34f
remove laod event
joelamouche 3ba0757
sync
joelamouche e91483f
update packages
joelamouche d9822ed
Update packages/extension-dapp/src/compat/metaMaskSource.ts
joelamouche 58188be
Update packages/extension-dapp/src/compat/metaMaskSource.ts
joelamouche a68bc59
filter accounts by type
joelamouche a312bf4
sync with master
joelamouche b8ee32c
lint
joelamouche 5d315b3
fix type typing
joelamouche 88b707c
Update packages/extension-dapp/src/index.ts
joelamouche bbf0627
filter by array of type, instead of single type
joelamouche 3e046a9
sync with master
joelamouche 846d274
sync with master
joelamouche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,15 +73,16 @@ function injectSingleSource (win: SingleWindow): void { | |
// https://github.com/cennznet/singlesource-extension/blob/f7cb35b54e820bf46339f6b88ffede1b8e140de0/react-example/src/App.js#L19 | ||
export default function initSingleSource (): Promise<boolean> { | ||
return new Promise((resolve): void => { | ||
window.addEventListener('load', (): void => { | ||
const win = window as Window & SingleWindow; | ||
// window.addEventListener('load', (): void => { | ||
console.log('loading singlesource'); | ||
joelamouche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const win = window as Window & SingleWindow; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Believe I commented elsewhere - the original is correct. (All ok, since this will be dropped anyway) |
||
|
||
if (win.SingleSource) { | ||
injectSingleSource(win); | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
}); | ||
if (win.SingleSource) { | ||
injectSingleSource(win); | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
// }); | ||
}); | ||
} |
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,137 @@ | ||
// Copyright 2019-2020 @polkadot/extension-dapp authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// import type { Signer } from "@polkadot/api/types"; | ||
import type { Injected, InjectedAccount, InjectedWindow } from '@polkadot/extension-inject/types'; | ||
|
||
import Web3 from 'web3'; | ||
|
||
import { SignerPayloadJSON, SignerPayloadRaw, SignerResult } from '@polkadot/types/types'; | ||
|
||
// RxJs interface, only the bare-bones of what we need here | ||
// interface Subscriber<T> { | ||
// subscribe: ( | ||
// cb: (value: T) => void | ||
// ) => { | ||
// unsubscribe(): void; | ||
// }; | ||
// } | ||
|
||
// interface SingleSourceAccount { | ||
// address: string; | ||
// assets: { assetId: number }[]; | ||
// name: string; | ||
// } | ||
|
||
// interface SingleSource { | ||
// accounts$: Subscriber<SingleSourceAccount[]>; | ||
// environment$: string[]; | ||
// signer: Signer; | ||
// } | ||
|
||
// interface SingleWindow extends InjectedWindow { | ||
// SingleSource: SingleSource; | ||
// } | ||
// interface SingleSourceAccount { | ||
// address: string; | ||
// assets: { assetId: number }[]; | ||
// name: string; | ||
// } | ||
|
||
// interface SingleSource { | ||
// accounts$: Subscriber<SingleSourceAccount[]>; | ||
// environment$: string[]; | ||
// signer: Signer; | ||
// } | ||
|
||
interface Web3Window extends InjectedWindow { | ||
web3: Web3; | ||
// this is injected by metaMask | ||
ethereum: any; | ||
} | ||
|
||
// transfor the Web3 accounts into a simple address/name array | ||
function transformAccounts (accounts: string[]): InjectedAccount[] { | ||
return accounts.map((acc, i) => { | ||
return { address: acc, name: 'MetaMask Address #' + i.toString() }; | ||
}); | ||
} | ||
|
||
// add a compat interface of SingleSource to window.injectedWeb3 | ||
function injectWeb3 (win: Web3Window): void { | ||
// let accounts: InjectedAccount[] = []; | ||
|
||
// we don't yet have an accounts subscribe on the interface, simply get the | ||
// accounts and store them, any get will resolve the last found values | ||
// win.web3.accounts$.subscribe((_accounts): void => { | ||
// accounts = transformAccounts(_accounts); | ||
// }); | ||
|
||
// decorate the compat interface | ||
win.injectedWeb3.Web3Source = { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/require-await | ||
enable: async (_: string): Promise<Injected> => { | ||
win.web3 = new Web3(win.ethereum); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
await win.ethereum.enable(); | ||
|
||
// let mainAccount=(await win.web3.eth.getAccounts())[0] | ||
return { | ||
accounts: { | ||
get: async (): Promise<InjectedAccount[]> => { | ||
console.log('fetching accounts'); | ||
console.log(await win.web3.eth.getAccounts()); | ||
|
||
return transformAccounts(await win.web3.eth.getAccounts()); | ||
}, | ||
subscribe: (cb: (accounts: InjectedAccount[]) => void): (() => void) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call | ||
const sub = win.ethereum.on('accountsChanged', function (accounts:string[]) { | ||
cb(transformAccounts(accounts)); | ||
}); | ||
|
||
return (): void => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
sub.unsubscribe(); | ||
}; | ||
} | ||
}, | ||
signer: { | ||
signPayload: async (payload: SignerPayloadJSON) : Promise<SignerResult> => { | ||
jacogr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log('signPayload'); | ||
|
||
return { id: 0, signature: await win.web3.eth.sign(JSON.stringify(payload), payload.address) }; | ||
}, | ||
signRaw: async (raw: SignerPayloadRaw): Promise<SignerResult> => { | ||
console.log('signature', await win.web3.eth.sign(raw.data, raw.address)); | ||
|
||
return { id: 0, signature: await win.web3.eth.sign(raw.data, raw.address) }; | ||
} | ||
} | ||
}; | ||
}, | ||
version: win.web3.version | ||
}; | ||
} | ||
|
||
// TODO udpate descr | ||
// returns the SingleSource instance, as per | ||
// https://github.com/cennznet/singlesource-extension/blob/f7cb35b54e820bf46339f6b88ffede1b8e140de0/react-example/src/App.js#L19 | ||
export default function initWeb3Source (): Promise<boolean> { | ||
console.log('initWeb3Source'); | ||
|
||
return new Promise((resolve): void => { | ||
// console.log('listening') | ||
// window.addEventListener("load", (): void => { | ||
console.log('loading web3'); | ||
const win = window as Window & Web3Window; | ||
|
||
if (win.ethereum) { | ||
injectWeb3(win); | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
// }); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note-to-self: After this can drop the singleSource. It was always great as a reference, but now that we have a proper reference available here, not needed anymore.