Skip to content
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
merged 40 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4c8b1c8
added web3source
joelamouche Dec 8, 2020
a98b814
tried fixing dependencies
joelamouche Dec 8, 2020
a956347
removed load event listener and added name
joelamouche Dec 8, 2020
2d44686
add signer to interface
joelamouche Dec 10, 2020
f8c01a2
Merge branch 'master' into jlm-add-metamask-extension-web3
joelamouche Dec 10, 2020
f056a53
added signPayload draft
joelamouche Dec 10, 2020
42bf1fd
lint
joelamouche Dec 10, 2020
966c6db
modified signpayload
joelamouche Dec 10, 2020
b3ff173
switch back to only signRaw
joelamouche Dec 10, 2020
6303215
sync with master and update to new metamask norm
joelamouche May 5, 2021
a1f12b9
add types to injecetd accounts
joelamouche May 6, 2021
e275ad3
update deps
joelamouche May 7, 2021
6ff55de
hash tx and finalize code
joelamouche May 10, 2021
e8dc31c
lint and type
joelamouche May 10, 2021
83b02de
sync with master
joelamouche May 11, 2021
e40bcce
sync with master
joelamouche May 31, 2021
02c1543
iterate on feedback
joelamouche Jun 1, 2021
034ce08
lint index.ts
joelamouche Jun 1, 2021
0ca3c18
prettier lint index.ts
joelamouche Jun 1, 2021
40cc504
Merge branch 'jlm-lint-extension' into jlm-add-metamask-extension-web3
joelamouche Jun 1, 2021
2f590e6
type optional again
joelamouche Jun 1, 2021
786cdb9
Merge branch 'jlm-add-metamask-extension-web3' of github.com:PureStak…
joelamouche Jun 1, 2021
ba312be
removed sigenr from injected account type
joelamouche Jun 21, 2021
6081cef
sync with master
joelamouche Jun 21, 2021
1314d64
lint
joelamouche Jun 21, 2021
26f14eb
sync with master
joelamouche Jul 5, 2021
7bbb18b
wip remove web3
joelamouche Jul 7, 2021
74da34f
remove laod event
joelamouche Jul 8, 2021
3ba0757
sync
joelamouche Jul 12, 2021
e91483f
update packages
joelamouche Jul 23, 2021
d9822ed
Update packages/extension-dapp/src/compat/metaMaskSource.ts
joelamouche Aug 23, 2021
58188be
Update packages/extension-dapp/src/compat/metaMaskSource.ts
joelamouche Aug 23, 2021
a68bc59
filter accounts by type
joelamouche Aug 23, 2021
a312bf4
sync with master
joelamouche Aug 23, 2021
b8ee32c
lint
joelamouche Aug 23, 2021
5d315b3
fix type typing
joelamouche Aug 24, 2021
88b707c
Update packages/extension-dapp/src/index.ts
joelamouche Aug 25, 2021
bbf0627
filter by array of type, instead of single type
joelamouche Aug 25, 2021
3e046a9
sync with master
joelamouche Aug 26, 2021
846d274
sync with master
joelamouche Sep 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/extension-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"@babel/runtime": "^7.12.5",
"@polkadot/extension-inject": "^0.35.2-7",
"@polkadot/util": "^4.2.1",
"@polkadot/util-crypto": "^4.2.1"
"@polkadot/util-crypto": "^4.2.1",
"web3": "^1.3.0"
},
"peerDependencies": {
"@polkadot/api": "*"
Expand Down
6 changes: 5 additions & 1 deletion packages/extension-dapp/src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

import singleSource from './singleSource';
import web3Source from './Web3Source';

// initialize all the compatibility engines
export default function initCompat (): Promise<boolean> {
console.log('init compat');

return Promise.all([
singleSource()
singleSource(),
Copy link
Member

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.

web3Source()
]).then((): boolean => true);
}
19 changes: 10 additions & 9 deletions packages/extension-dapp/src/compat/singleSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
// });
});
}
137 changes: 137 additions & 0 deletions packages/extension-dapp/src/compat/web3Source.ts
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);
}
// });
});
}
Loading