Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
fix(sdk): expose apis to get account or network even when not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang committed Feb 18, 2020
1 parent 8d69b43 commit 00654f2
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
15 changes: 9 additions & 6 deletions packages/extension/src/client/Aztec/ApiManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ export default class ApiManager {
default:
}
}
if (!this.aztecAccount && !this.sessionPromise) {
this.generateDefaultApis();
}

this.eventListeners.notify('profileChanged', changedType, objValue);
});
}

generateDefaultApis() {
const apis = ApiPermissionService.generateApis();
async generateDefaultApis() {
const apis = await ApiPermissionService.generateApis();
this.setApis(apis);
}

Expand Down Expand Up @@ -154,14 +157,14 @@ export default class ApiManager {
this.aztecAccount = null;
this.error = null;
this.unbindOneTimeProfileChangeListener();
this.generateDefaultApis();
await this.generateDefaultApis();
await ConnectionService.disconnect();
}

async refreshSession(options) {
this.aztecAccount = null;
this.error = null;
this.generateDefaultApis();
await this.generateDefaultApis();
await ConnectionService.disconnect();

let networkSwitchedDuringStart = false;
Expand Down Expand Up @@ -208,8 +211,8 @@ export default class ApiManager {
} = await ApiPermissionService.ensurePermission() || {};
return aztecAccount;
},
(aztecAccount) => {
const apis = ApiPermissionService.generateApis(true);
async (aztecAccount) => {
const apis = await ApiPermissionService.generateApis(true);
this.setApis(apis);

if (this.autoRefreshOnProfileChange) {
Expand Down
28 changes: 28 additions & 0 deletions packages/extension/src/client/Aztec/__tests__/ApiManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ describe('ApiManager listeners', () => {
name: 'Rinkeby',
});
});

it('generate default apis again if not enabled', () => {
const generateDefaultApisSpy = jest.spyOn(manager, 'generateDefaultApis')
.mockImplementation(jest.fn());

expect(generateDefaultApisSpy).toHaveBeenCalledTimes(0);

Web3Service.eventListeners.notify('profile', 'networkChanged', '12345');
expect(generateDefaultApisSpy).toHaveBeenCalledTimes(1);

manager.aztecAccount = {
address: 'aztec_address',
};

Web3Service.eventListeners.notify('profile', 'networkChanged', '12345');
expect(generateDefaultApisSpy).toHaveBeenCalledTimes(1);

manager.aztecAccount = null;
manager.sessionPromise = {};

Web3Service.eventListeners.notify('profile', 'accountChanged', 'address_123');
expect(generateDefaultApisSpy).toHaveBeenCalledTimes(1);

manager.sessionPromise = null;

Web3Service.eventListeners.notify('profile', 'networkChanged', '12345');
expect(generateDefaultApisSpy).toHaveBeenCalledTimes(2);
});
});

describe('ApiManager.handleResolveSession', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/extension/src/client/Aztec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Aztec {
});
};

manager.generateDefaultApis();

Object.keys(aztec).forEach((name) => {
if (this[name]) {
warnLog(`Api '${name}' is already in Aztec.`);
Expand All @@ -27,6 +25,10 @@ class Aztec {
});
}

async generateInitialApis() { // eslint-disable-line class-methods-use-this
await manager.generateDefaultApis();
}

get enabled() { // eslint-disable-line class-methods-use-this
return !!manager.aztecAccount;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Web3 from 'web3';
import {
getNetworkName,
} from '~/utils/network';
Expand All @@ -11,20 +12,42 @@ const availableWeb3Apis = [
'deploy',
];

export default function generateApis(hasPermission = false) {
const web3 = {
getNetwork: () => ({
id: Web3Service.networkId,
name: getNetworkName(Web3Service.networkId),
}),
getAccount: () => ({ ...Web3Service.account }),
};
export default async function generateApis(hasPermission = false) {
const web3 = {};

let networkId;
let account;
if (hasPermission) {
({
networkId,
account,
} = Web3Service);
availableWeb3Apis.forEach((name) => {
web3[name] = (...args) => Web3Service[name](...args);
});
} else {
const web3Instance = new Web3(window.ethereum);
networkId = await web3Instance.eth.net.getId();
const [address] = await web3Instance.eth.getAccounts();
if (address) {
account = {
address,
};
}
}

web3.network = !networkId
? null
: {
id: networkId,
name: getNetworkName(networkId),
};
web3.account = !account
? null
: { ...account };
web3.getNetwork = () => web3.network;
web3.getAccount = () => web3.account;

// TODO - assign mock modules that show warnings when calling apis before enabled

return {
Expand Down
7 changes: 5 additions & 2 deletions packages/extension/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Aztec from '~/client/Aztec';

document.addEventListener('DOMContentLoaded', () => {
window.aztec = new Aztec();
document.addEventListener('DOMContentLoaded', async () => {
const aztec = new Aztec();
await aztec.generateInitialApis();
delete aztec.generateInitialApis;
window.aztec = aztec;

// TODO - callback's name should be configurable through url:
// /sdk/aztec/?key=API_KEY&callback=aztecCallback
Expand Down

0 comments on commit 00654f2

Please sign in to comment.