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

enable on cennznet extension #25

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
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 .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = {
'.yarn/**',
'**/build/*',
'**/coverage/*',
'**/node_modules/*'
'**/node_modules/*',
'**/config.ts'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checking which file were you planning to exclude?
packages/extension-chains/src/config.ts still included

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excluded it... had few listing err in it... mostly related to sort-key in types..
but in case of enum we don't really want to sort...hv added at the top of the file /* eslint sort-keys: 0 */

],
parserOptions: {
...base.parserOptions,
Expand Down
53 changes: 53 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Release Pipeline (develop/2.0)
KarishmaBothara marked this conversation as resolved.
Show resolved Hide resolved

on:
pull_request:
branches:
- master
types:
- labeled
check_suite:
types:
- completed

jobs:
release-to-github:
needs:
- merge-release-branch
# release to github only when its a prerelease branch and auto merge label is triggered
if: ${{ startsWith(github.head_ref, 'useOnlyCENNZnetExtension') }} && ${{ github.event.label.name == 'automerge' }}
runs-on: ubuntu-latest
steps:
# checkout merged branch
- uses: actions/checkout@v2
with:
ref: useOnlyCENNZnetExtension
- uses: actions/setup-node@v1
with:
node-version: '15.3'
- name: find release version
id: find_version
run: |
echo ::set-output name=tag_name::$(cat packages/extension/package.json \
| grep version \
| head -1 \
| grep -E -o '[0-9|.]+\-*[a-z]*\.[0-9]+|]+')
- name: make release branch
uses: peterjgrainger/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
branch: "release/${{steps.find_version.outputs.tag_name}}"
- name: package
run: |
yarn && yarn build
npx lerna exec -- 'cp package.json build/'
npx lerna exec -- 'cp README.md build/'
npx lerna exec -- 'cp ../../LICENSE build/'
KarishmaBothara marked this conversation as resolved.
Show resolved Hide resolved
- name: publish to npm
env:
NPM_KEY: ${{ secrets.NPM_KEY }}
run: |
echo "//registry.npmjs.org/:_authToken=${NPM_KEY}" > ~/.npmrc
echo 'Publishing to npmjs.org.'
npx lerna exec -- 'cd build && npm publish --access public'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can add --dry-run if you want to test the CI before publishing

Suggested change
npx lerna exec -- 'cd build && npm publish --access public'
npx lerna exec -- 'cd build && npm publish --access public'

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { SignerPayloadJSON } from '@polkadot/types/types';

import { ResponseSigning } from '@cennznet/extension-base/background/types';
import { MetadataDef } from '@cennznet/extension-inject/types';

import { KeyringPair } from '@polkadot/keyring/types';
import { TypeRegistry } from '@polkadot/types';
import keyring from '@polkadot/ui-keyring';
Expand Down
7 changes: 3 additions & 4 deletions packages/extension-base/src/background/handlers/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type { MetadataDef } from '@cennznet/extension-inject/types';
import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '@polkadot/keyring/types';
import type { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
import type { SubjectInfo } from '@polkadot/ui-keyring/observable/types';
import type {
AccountJson,
import type { AccountJson,
AllowedPath,
AuthorizeRequest,
MessageTypes,
Expand Down Expand Up @@ -45,11 +44,11 @@ import type {
ResponseSeedValidate,
ResponseSigningIsLocked,
ResponseType,
SigningRequest
} from '../types';
SigningRequest } from '../types';

import { ALLOWED_PATH, PASSWORD_EXPIRY_MS } from '@cennznet/extension-base/defaults';
import chrome from '@cennznet/extension-inject/chrome';

import { TypeRegistry } from '@polkadot/types';
import keyring from '@polkadot/ui-keyring';
import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/accounts';
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-base/src/background/handlers/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { MetadataDef, ProviderMeta } from '@cennznet/extension-inject/types
import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback } from '@polkadot/rpc-provider/types';
import type { AccountJson, AuthorizeRequest, MetadataRequest, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestRpcUnsubscribe, RequestSign, ResponseRpcListProviders, ResponseSigning, SigningRequest } from '../types';

import { BehaviorSubject } from 'rxjs';

import { addMetadata, knownMetadata } from '@cennznet/extension-chains';
import chrome from '@cennznet/extension-inject/chrome';
import { BehaviorSubject } from 'rxjs';

import { assert } from '@polkadot/util';

import { MetadataStore } from '../../stores';
Expand Down
1 change: 1 addition & 0 deletions packages/extension-base/src/background/handlers/Tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { MessageTypes, RequestAccountList, RequestAuthorizeTab, RequestRpcS

import { PHISHING_PAGE_REDIRECT } from '@cennznet/extension-base/defaults';
import { canDerive } from '@cennznet/extension-base/utils';

import { checkIfDenied } from '@polkadot/phishing';
import keyring from '@polkadot/ui-keyring';
import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/accounts';
Expand Down
446 changes: 224 additions & 222 deletions packages/extension-chains/src/config.ts

Large diffs are not rendered by default.

48 changes: 35 additions & 13 deletions packages/extension-chains/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

import type { MetadataDef } from '@cennznet/extension-inject/types';
import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
import type { Chain, MetadataFetched, RuntimeTypes } from './types';

import { Metadata } from '@polkadot/metadata';
import { TypeRegistry } from '@polkadot/types';
import { base64Decode } from '@polkadot/util-crypto';

import config from './config';
// imports chain details, generally metadata. For the generation of these,
// inside the api, run `yarn chain:info --ws <url>`
Expand All @@ -20,50 +22,70 @@ const definitions = new Map<string, MetadataDef>(
/** when metadata definition stored in extension is outdated
* get the definition for @cennznet/api/extension-releases
* for cennznet specific chains **/
export function getLatestMetaFromServer(genesisHashExpected: string): MetadataFetched | null {
export function getLatestMetaFromServer (genesisHashExpected: string): MetadataFetched | null {
try {
const xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "https://raw.githubusercontent.com/cennznet/api.js/master/extension-releases/metaCalls.json", false);

xmlHttp.open('GET', 'https://raw.githubusercontent.com/cennznet/api.js/master/extension-releases/metaCalls.json', false);
xmlHttp.send(null);
let response = xmlHttp.responseText;
const response: string = xmlHttp.responseText;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const metadataDetails = JSON.parse(response);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
const metaCallsList = metadataDetails?.metaCalls;

if (metaCallsList) {
// metaCalls is { genesisHash-specVersion: metaCalls }
const key = Object.keys(metaCallsList).filter(v => v.includes(genesisHashExpected));
const key = Object.keys(metaCallsList).filter((v) => v.includes(genesisHashExpected));

if (!key[0]) {
return null;
}

const [, specVersion] = key[0].split('-');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
const metaCalls = metaCallsList[key[0]];
return {metaCalls, specVersion: parseInt(specVersion)};

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
return { metaCalls, specVersion: parseInt(specVersion) } as MetadataFetched;
}

return null;
} catch (e) {
console.log('Err:',e);
console.log('Err:', e);

return null;
}
}

/** when types stored in extension is outdated
* get the types for @cennznet/api/extension-releases
* for cennznet specific chains **/
export function getLatestTypesFromServer(genesisHashExpected: string): RuntimeTypes | null {
export function getLatestTypesFromServer (genesisHashExpected: string): RuntimeTypes | null {
try {
const xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "https://raw.githubusercontent.com/cennznet/api.js/master/extension-releases/runtimeModuleTypes.json", false);

xmlHttp.open('GET', 'https://raw.githubusercontent.com/cennznet/api.js/master/extension-releases/runtimeModuleTypes.json', false);
xmlHttp.send(null);
let response = xmlHttp.responseText;
const response = xmlHttp.responseText;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const additionalTypes = JSON.parse(response);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
const typesForCurrentChain = additionalTypes[genesisHashExpected];

if (typesForCurrentChain) {
const types = typesForCurrentChain.types;
const userExtensions = typesForCurrentChain.userExtensions;
return {types, userExtensions};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
const types: Record<string, Record<string, string> | string> = typesForCurrentChain.types;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
const userExtensions: ExtDef = typesForCurrentChain.userExtensions;

return { types, userExtensions } as RuntimeTypes;
}

return null;
} catch (e) {
console.log('Err:',e);
console.log('Err:', e);

return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/extension-chains/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2019-2021 @polkadot/extension-chains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';

import type { MetadataDef } from '@cennznet/extension-inject/types';
import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
import type { Registry } from '@polkadot/types/types';

export interface Chain {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-dapp/src/compat/singleSource.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2019-2021 @polkadot/extension-dapp authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Signer } from '@polkadot/api/types';
import type { Injected, InjectedAccount, InjectedWindow } from '@cennznet/extension-inject/types';
import type { Signer } from '@polkadot/api/types';

// RxJs interface, only the bare-bones of what we need here
interface Subscriber<T> {
Expand Down
18 changes: 17 additions & 1 deletion packages/extension-dapp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import type { Injected, InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedExtensionInfo, InjectedProviderWithMeta, InjectedWindow, ProviderList, Unsubcall, Web3AccountsOptions } from '@cennznet/extension-inject/types';

import { InjectedWindowProvider } from '@cennznet/extension-inject/types';

import { u8aEq } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';

Expand Down Expand Up @@ -44,9 +46,20 @@ let web3EnablePromise: Promise<InjectedExtension[]> | null = null;

export { isWeb3Injected, web3EnablePromise };

const CENNZNET_EXT = 'cennznet-extension';

function getWindowExtensions (originName: string): Promise<[InjectedExtensionInfo, Injected | void][]> {
let enableList: Record<string, InjectedWindowProvider>;

// Use only cennznet extension if its installed
if (win.injectedWeb3[CENNZNET_EXT]) {
enableList = { [CENNZNET_EXT]: win.injectedWeb3[CENNZNET_EXT] };
} else { // Use any other extension (fall back)
enableList = win.injectedWeb3;
}

return Promise.all(
Object.entries(win.injectedWeb3).map(([name, { enable, version }]): Promise<[InjectedExtensionInfo, Injected | void]> =>
Object.entries(enableList).map(([name, { enable, version }]): Promise<[InjectedExtensionInfo, Injected | void]> =>
Promise.all([
Promise.resolve({ name, version }),
enable(originName).catch((error: Error): void => {
Expand All @@ -55,6 +68,8 @@ function getWindowExtensions (originName: string): Promise<[InjectedExtensionInf
])
)
);

return Promise.resolve([]);
}

// enables all the providers found on the injected window interface
Expand Down Expand Up @@ -113,6 +128,7 @@ export async function web3Accounts ({ ss58Format }: Web3AccountsOptions = {}): P
return mapAccounts(source, list, ss58Format);
} catch (error) {
console.error('web3accounts failed:', error);

// cannot handle this one
return [];
}
Expand Down
24 changes: 14 additions & 10 deletions packages/extension-ui/src/Popup/Accounts/Account.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ describe('Account component', () => {
wrapper.find('.settings').first().simulate('click');
await act(flushAllPromises);

expect(wrapper.find('a.menuItem').length).toBe(4);
expect(wrapper.find('a.menuItem').length).toBe(5);
expect(wrapper.find('a.menuItem').at(0).text()).toBe('Rename');
expect(wrapper.find('a.menuItem').at(1).text()).toBe('Derive New Account');
expect(wrapper.find('a.menuItem').at(2).text()).toBe('Export Account');
expect(wrapper.find('a.menuItem').at(3).text()).toBe('Forget Account');
expect(wrapper.find('a.menuItem').at(2).text()).toBe('View on UNcover');
expect(wrapper.find('a.menuItem').at(3).text()).toBe('Export Account');
expect(wrapper.find('a.menuItem').at(4).text()).toBe('Forget Account');
expect(wrapper.find('.genesisSelection').exists()).toBe(true);
});

Expand All @@ -52,9 +53,10 @@ describe('Account component', () => {
wrapper.find('.settings').first().simulate('click');
await act(flushAllPromises);

expect(wrapper.find('a.menuItem').length).toBe(2);
expect(wrapper.find('a.menuItem').length).toBe(3);
expect(wrapper.find('a.menuItem').at(0).text()).toBe('Rename');
expect(wrapper.find('a.menuItem').at(1).text()).toBe('Forget Account');
expect(wrapper.find('a.menuItem').at(1).text()).toBe('View on UNcover');
expect(wrapper.find('a.menuItem').at(2).text()).toBe('Forget Account');
expect(wrapper.find('.genesisSelection').exists()).toBe(true);
});

Expand All @@ -63,10 +65,11 @@ describe('Account component', () => {
wrapper.find('.settings').first().simulate('click');
await act(flushAllPromises);

expect(wrapper.find('a.menuItem').length).toBe(3);
expect(wrapper.find('a.menuItem').length).toBe(4);
expect(wrapper.find('a.menuItem').at(0).text()).toBe('Rename');
expect(wrapper.find('a.menuItem').at(1).text()).toBe('Export Account');
expect(wrapper.find('a.menuItem').at(2).text()).toBe('Forget Account');
expect(wrapper.find('a.menuItem').at(1).text()).toBe('View on UNcover');
expect(wrapper.find('a.menuItem').at(2).text()).toBe('Export Account');
expect(wrapper.find('a.menuItem').at(3).text()).toBe('Forget Account');
expect(wrapper.find('.genesisSelection').exists()).toBe(true);
});

Expand All @@ -75,9 +78,10 @@ describe('Account component', () => {
wrapper.find('.settings').first().simulate('click');
await act(flushAllPromises);

expect(wrapper.find('a.menuItem').length).toBe(2);
expect(wrapper.find('a.menuItem').length).toBe(3);
expect(wrapper.find('a.menuItem').at(0).text()).toBe('Rename');
expect(wrapper.find('a.menuItem').at(1).text()).toBe('Forget Account');
expect(wrapper.find('a.menuItem').at(1).text()).toBe('View on UNcover');
expect(wrapper.find('a.menuItem').at(2).text()).toBe('Forget Account');
expect(wrapper.find('.genesisSelection').exists()).toBe(false);
});
});
16 changes: 8 additions & 8 deletions packages/extension-ui/src/Popup/Accounts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

import type { AccountJson } from '@cennznet/extension-base/background/types';

import React, { useCallback, useMemo, useState } from 'react';
import styled from 'styled-components';

import { canDerive } from '@cennznet/extension-base/utils';
import defaultConfig from '@cennznet/extension-chains/config';
import { ThemeProps } from '@cennznet/extension-ui/types';
import React, { useCallback, useMemo, useState } from 'react';
import styled from 'styled-components';

import { Address, Dropdown, Link, MenuDivider } from '../../components';
import useGenesisHashOptions from '../../hooks/useGenesisHashOptions';
import useTranslation from '../../hooks/useTranslation';
import { editAccount, tieAccount } from '../../messaging';
import { Name } from '../../partials';
import defaultConfig from "@cennznet/extension-chains/config";

interface Props extends AccountJson {
className?: string;
Expand All @@ -31,7 +30,7 @@ function Account ({ address, className, genesisHash, isExternal, isHardware, isH
const [{ isEditing, toggleActions }, setEditing] = useState<EditState>({ isEditing: false, toggleActions: 0 });
const [editedName, setName] = useState<string | undefined | null>(name);
const genesisOptions = useGenesisHashOptions();
const nikauChain = defaultConfig.CENNZNetChain.find(blkChain => blkChain.chain === 'CENNZnet Nikau');
const nikauChain = defaultConfig.CENNZNetChain.find((blkChain) => blkChain.chain === 'CENNZnet Nikau');

const _onChangeGenesis = useCallback(
(genesisHash?: string | null): void => {
Expand Down Expand Up @@ -74,8 +73,9 @@ function Account ({ address, className, genesisHash, isExternal, isHardware, isH
</Link>
)}
<a className='menuItem'
href={genesisHash === nikauChain?.genesisHash ? `https://uncoverexplorer.com/account/${address}?network=Nikau` : `https://uncoverexplorer.com/account/${address}`}
target="_blank"
href={genesisHash === nikauChain?.genesisHash ? `https://uncoverexplorer.com/account/${address}?network=Nikau` : `https://uncoverexplorer.com/account/${address}`}
rel='noreferrer'
target='_blank'
>
{t<string>('View on UNcover')}
</a>
Expand Down Expand Up @@ -111,7 +111,7 @@ function Account ({ address, className, genesisHash, isExternal, isHardware, isH
</>
)}
</>
), [_onChangeGenesis, _toggleEdit, address, genesisHash, genesisOptions, isExternal, isHardware, t, type]);
), [_onChangeGenesis, _toggleEdit, address, genesisHash, genesisOptions, isExternal, isHardware, t, type, nikauChain]);

return (
<div className={className}>
Expand Down
Loading