Skip to content

Commit

Permalink
feat: 🎸 call api->auto unlock wallet; add case; refactor;
Browse files Browse the repository at this point in the history
  • Loading branch information
hzz780 committed Aug 18, 2020
1 parent ef108ed commit 91b6cac
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 265 deletions.
367 changes: 119 additions & 248 deletions app/web/background.js

Large diffs are not rendered by default.

46 changes: 43 additions & 3 deletions app/web/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class NightAElf {
this.appName = options.appName;
this.chain = this.chain();
this.chainId;
// Todo: resultOnly: true/false for polyfill
}

callbackWrap(result, callback = () => {}) {
Expand All @@ -61,6 +62,9 @@ class NightAElf {
params: params // Array
}
}).then(result => {
if (result.error === 300000) {
return this.callAElfChain(methodName, params, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -74,6 +78,9 @@ class NightAElf {
payload: params.payload
}
}).then(result => {
if (result.error === 300000) {
return this.login(params, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -88,6 +95,9 @@ class NightAElf {
payload: params.payload
}
}).then(result => {
if (result.error === 300000) {
return this.logout(params, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -111,6 +121,9 @@ class NightAElf {
type: params.type || '',
contractAddress: params.contractAddress || ''
}).then(result => {
if (result.error === 300000) {
return this.checkPermission(params, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -125,6 +138,9 @@ class NightAElf {
payload: params.payload
}
}).then(result => {
if (result.error === 300000) {
return this.setContractPermission(params, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -135,6 +151,9 @@ class NightAElf {
method: 'REMOVE_CONTRACT_PERMISSION',
payload: params.payload
}).then(result => {
if (result.error === 300000) {
return this.removeContractPermission(params, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -146,6 +165,9 @@ class NightAElf {
method: 'REMOVE_METHODS_WHITELIST',
payload: params.payload
}).then(result => {
if (result.error === 300000) {
return this.removeMethodsWhitelist(params, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -155,6 +177,9 @@ class NightAElf {
appName: param.appName || this.appName,
method: 'GET_ADDRESS'
}).then(result => {
if (result.error === 300000) {
return this.getAddress(param, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -166,6 +191,9 @@ class NightAElf {
hexToBeSign: param.hexToBeSign,
method: 'GET_SIGNATURE'
}).then(result => {
if (result.error === 300000) {
return this.getSignature(param, callback);
}
return this.callbackWrap(result, callback);
});
}
Expand All @@ -182,6 +210,11 @@ class NightAElf {
if (!result.error) {
this.chainId = result.result.ChainId;
}

if (result.error === 300000) {
return getChainStatus(callback);
}

return this.callbackWrap(result, callback);
});
};
Expand Down Expand Up @@ -264,16 +297,19 @@ class NightAElf {
params: filterParams
}
}).then(result => {
if (result.error === 300000) {
return _callAelfContract(params, methodName, contractAddress, method);
}

return this.callbackWrap(result, callback);
});
// }
};

const contractAt = (contractAddress, wallet, ...args) => {
const contractAt = (contractAddress, wallet, ...otherArgsArray) => {
const {
callback
// isSync 在插件中被禁止使用
} = extractArgumentsIntoObject(args);
} = extractArgumentsIntoObject(otherArgsArray);

return window.NightElf.api({
appName: this.appName,
Expand All @@ -286,6 +322,10 @@ class NightAElf {
contractAddress: contractAddress
}
}).then(result => {
if (result.error === 300000) {
return contractAt(contractAddress, wallet, ...otherArgsArray);
}

if (result.error) {
callback(result.error, result);
if (callback.length) {
Expand Down
22 changes: 11 additions & 11 deletions app/web/pages/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ ReactDOM.render(
<IntlProvider locale={navigator.language} messages={chooseLocale()} >
<LocaleProvider locale={antdChooseLocale()} >
<Router history={hashHistory}>
<Route path="/" component={Lock}></Route>
<Route path="/home" component={Home}></Route>
<Route path="/createkeypairs" component={CreateKeypairs}></Route>
<Route path="/keypairs" component={Keypairs}></Route>
<Route path="/permissions" component={Permissions}></Route>
<Route path='/permissionsdetail/:data' component={PermissionsDetail}></Route>
<Route path="/import" component={Import}></Route>
<Route path="/backupkeypairs/:address" component={BackupKeypairs}></Route>
<Route path="/extensionmanager" component={ExtensionManager}></Route>
<Route path="/loadfrombackup" component={LoadFromBackup}></Route>
<Route path="/" component={Lock}/>
<Route path="/home" component={Home}/>
<Route path="/createkeypairs" component={CreateKeypairs}/>
<Route path="/keypairs" component={Keypairs}/>
<Route path="/permissions" component={Permissions}/>
<Route path='/permissionsdetail/:data' component={PermissionsDetail}/>
<Route path="/import" component={Import}/>
<Route path="/backupkeypairs/:address" component={BackupKeypairs}/>
<Route path="/extensionmanager" component={ExtensionManager}/>
<Route path="/loadfrombackup" component={LoadFromBackup}/>
{/* TODO: contracts just for test */}
<Route path="/contracts" component={Contracts}></Route>
<Route path="/contracts" component={Contracts}/>
</Router>
</LocaleProvider>
</IntlProvider>,
Expand Down
24 changes: 24 additions & 0 deletions app/web/service/LockService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file LockUnlockService.js
* @author hzz780
*/

import errorHandler from '../utils/errorHandler';

export default class LockService {
async lockGuard(aesSeed) {
return !(aesSeed && aesSeed.length) ? {
...errorHandler(200005)
} : {
...errorHandler(0)
};
}

async callUnLockPagePrompt(Background, sendResponse) {
Background.openPrompt(sendResponse,{
payload: {
method: 'UNLOCK_NIGHT_ELF'
}
});
}
}
1 change: 0 additions & 1 deletion app/web/service/NotificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,4 @@ export default class NotificationService {
window.close();
}
}

}
17 changes: 17 additions & 0 deletions app/web/utils/getPromptRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file utils/getPromptRoute.js
* @author hzz780
* 2020.08.18
*/

export default function getPromptRoute(message) {
let method = message.payload.method ? message.payload.method : message.payload.payload.method;
const routMap = {
SET_PERMISSION: '#/',
SET_CONTRACT_PERMISSION: '#/',
LOGIN: '#/loginkeypairs',
CALL_AELF_CONTRACT: '#/examine-approve',
UNLOCK_NIGHT_ELF: '#/unlock'
};
return message.router || routMap[method] || '#/confirmation';
}
4 changes: 4 additions & 0 deletions devDemos/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ <h1>Hello Test.html</h1>
<br>

<button type="button" id="get-signature">get-signature</button>
<br>

<button type="button" id="get-address-btn">Get Address</button>
<br>

<!-- <div class="sub-title">init & call different contract</div> -->
<!-- <button type="button" id="bind-permission-01">permission</button> -->
Expand Down
15 changes: 13 additions & 2 deletions devDemos/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ document.addEventListener('NightElf', result => {
};

getChainStatus.onclick = function () {
// If you unlock you night ELF
// When you get the unlock prompt page, callback way will be influence.
// You can not get callback in this demo.[Use callback && promise in the same time]
// But if you use callback way only. It's ok.
aelf.chain.getChainStatus((error, result) => {
console.log('>>>>>>>>>>>>> getChainStatus >>>>>>>>>>>>>');
console.log('>>>>>>>>>>>>> getChainStatus callback >>>>>>>>>>>>>');
console.log(error, result);
});

aelf.chain.getChainStatus().then(result => {
console.log('>>>>>>>>>>>>> getChainStatus >>>>>>>>>>>>>');
console.log('>>>>>>>>>>>>> getChainStatus promise >>>>>>>>>>>>>');
console.log('promise then', result);
}).catch(error => {
console.log('promise catch', error);
Expand Down Expand Up @@ -325,6 +329,13 @@ document.addEventListener('NightElf', result => {
});
};

const getAddressBtn = document.getElementById('get-address-btn');
getAddressBtn.onclick = function () {
aelf.getAddress({}, (error, result) => {
console.log('getAddress>>>>>>>>>>>>>>>>>>', result);
});
};

// For test
// const permissionIndex = [0];
// const permissionsTemp = [{
Expand Down

0 comments on commit 91b6cac

Please sign in to comment.