Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Add parity_defaultAccount RPC (with subscription) (#4383)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Feb 3, 2017
1 parent 4a18e34 commit 2a5967f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
62 changes: 60 additions & 2 deletions js/src/api/rpc/parity/parity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
Expand All @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { inAddress, inAddresses, inData, inHex, inNumber16, inOptions } from '../../format/input';
import { inAddress, inAddresses, inData, inHex, inNumber16, inOptions, inBlockNumber } from '../../format/input';
import { outAccountInfo, outAddress, outAddresses, outChainStatus, outHistogram, outNumber, outPeers, outTransaction } from '../../format/output';

export default class Parity {
Expand Down Expand Up @@ -76,6 +76,17 @@ export default class Parity {
.execute('parity_dappsInterface');
}

decryptMessage (address, data) {
return this._transport
.execute('parity_decryptMessage', inAddress(address), inHex(data));
}

defaultAccount () {
return this._transport
.execute('parity_defaultAccount')
.then(outAddress);
}

defaultExtraData () {
return this._transport
.execute('parity_defaultExtraData');
Expand All @@ -101,6 +112,11 @@ export default class Parity {
.execute('parity_enode');
}

encryptMessage (pubkey, data) {
return this._transport
.execute('parity_encryptMessage', inHex(pubkey), inHex(data));
}

executeUpgrade () {
return this._transport
.execute('parity_executeUpgrade');
Expand All @@ -111,6 +127,17 @@ export default class Parity {
.execute('parity_extraData');
}

futureTransactions () {
return this._transport
.execute('parity_futureTransactions');
}

gasCeilTarget () {
return this._transport
.execute('parity_gasCeilTarget')
.then(outNumber);
}

gasFloorTarget () {
return this._transport
.execute('parity_gasFloorTarget')
Expand Down Expand Up @@ -156,11 +183,22 @@ export default class Parity {
.execute('parity_killAccount', inAddress(account), password);
}

listAccounts (count, offset = null, blockNumber = 'latest') {
return this._transport
.execute('parity_listAccounts', count, inAddress(offset), inBlockNumber(blockNumber))
.then((accounts) => (accounts || []).map(outAddress));
}

listRecentDapps () {
return this._transport
.execute('parity_listRecentDapps');
}

listStorageKeys (address, count, hash = null, blockNumber = 'latest') {
return this._transport
.execute('parity_listStorageKeys', inAddress(address), count, inHex(hash), inBlockNumber(blockNumber));
}

removeAddress (address) {
return this._transport
.execute('parity_removeAddress', inAddress(address));
Expand Down Expand Up @@ -265,6 +303,11 @@ export default class Parity {
.then(outAddress);
}

postSign (address, hash) {
return this._transport
.execute('parity_postSign', inAddress(address), inHex(hash));
}

postTransaction (options) {
return this._transport
.execute('parity_postTransaction', inOptions(options));
Expand Down Expand Up @@ -311,16 +354,31 @@ export default class Parity {
.execute('parity_setDappsAddresses', dappId, inAddresses(addresses));
}

setEngineSigner (address, password) {
return this._transport
.execute('parity_setEngineSigner', inAddress(address), password);
}

setExtraData (data) {
return this._transport
.execute('parity_setExtraData', inData(data));
}

setGasCeilTarget (quantity) {
return this._transport
.execute('parity_setGasCeilTarget', inNumber16(quantity));
}

setGasFloorTarget (quantity) {
return this._transport
.execute('parity_setGasFloorTarget', inNumber16(quantity));
}

setMaxTransactionGas (quantity) {
return this._transport
.execute('parity_setMaxTransactionGas', inNumber16(quantity));
}

setMinGasPrice (quantity) {
return this._transport
.execute('parity_setMinGasPrice', inNumber16(quantity));
Expand Down
1 change: 1 addition & 0 deletions js/src/api/subscriptions/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const events = {
'logging': { module: 'logging' },
'eth_blockNumber': { module: 'eth' },
'parity_allAccountsInfo': { module: 'personal' },
'parity_defaultAccount': { module: 'personal' },
'eth_accounts': { module: 'personal' },
'signer_requestsToConfirm': { module: 'signer' }
};
Expand Down
14 changes: 14 additions & 0 deletions js/src/api/subscriptions/personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ export default class Personal {
this._started = true;

return Promise.all([
this._defaultAccount(),
this._listAccounts(),
this._accountsInfo(),
this._loggingSubscribe()
]);
}

_defaultAccount = () => {
return this._api.parity
.defaultAccount()
.then((defaultAccount) => {
this._updateSubscriptions('parity_defaultAccount', null, defaultAccount);
});
}

_listAccounts = () => {
return this._api.eth
.accounts()
Expand Down Expand Up @@ -73,6 +82,11 @@ export default class Personal {
case 'parity_setAccountMeta':
this._accountsInfo();
return;

case 'parity_setDappsAddresses':
case 'parity_setNewDappsWhitelist':
this._defaultAccount();
return;
}
});
}
Expand Down
43 changes: 37 additions & 6 deletions js/src/api/subscriptions/personal.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
Expand All @@ -18,31 +18,48 @@ import sinon from 'sinon';

import Personal from './personal';

const TEST_DEFAULT = '0xfa64203C044691aA57251aF95f4b48d85eC00Dd5';
const TEST_INFO = {
'0xfa64203C044691aA57251aF95f4b48d85eC00Dd5': {
[TEST_DEFAULT]: {
name: 'test'
}
};
const TEST_LIST = ['0xfa64203C044691aA57251aF95f4b48d85eC00Dd5'];
const TEST_LIST = [TEST_DEFAULT];

function stubApi (accounts, info) {
const _calls = {
accountsInfo: [],
allAccountsInfo: [],
listAccounts: []
listAccounts: [],
defaultAccount: []
};

return {
_calls,
parity: {
accountsInfo: () => {
const stub = sinon.stub().resolves(info || TEST_INFO)();

_calls.accountsInfo.push(stub);
return stub;
},
allAccountsInfo: () => {
const stub = sinon.stub().resolves(info || TEST_INFO)();

_calls.allAccountsInfo.push(stub);
return stub;
},
defaultAccount: () => {
const stub = sinon.stub().resolves(Object.keys(info || TEST_INFO)[0])();

_calls.defaultAccount.push(stub);
return stub;
}
},
eth: {
accounts: () => {
const stub = sinon.stub().resolves(accounts || TEST_LIST)();

_calls.listAccounts.push(stub);
return stub;
}
Expand Down Expand Up @@ -85,6 +102,10 @@ describe('api/subscriptions/personal', () => {
expect(personal.isStarted).to.be.true;
});

it('calls parity_accountsInfo', () => {
expect(api._calls.accountsInfo.length).to.be.ok;
});

it('calls parity_allAccountsInfo', () => {
expect(api._calls.allAccountsInfo.length).to.be.ok;
});
Expand All @@ -94,8 +115,10 @@ describe('api/subscriptions/personal', () => {
});

it('updates subscribers', () => {
expect(cb.firstCall).to.have.been.calledWith('eth_accounts', null, TEST_LIST);
expect(cb.secondCall).to.have.been.calledWith('parity_allAccountsInfo', null, TEST_INFO);
expect(cb).to.have.been.calledWith('parity_defaultAccount', null, TEST_DEFAULT);
expect(cb).to.have.been.calledWith('eth_accounts', null, TEST_LIST);
expect(cb).to.have.been.calledWith('parity_accountsInfo', null, TEST_INFO);
expect(cb).to.have.been.calledWith('parity_allAccountsInfo', null, TEST_INFO);
});
});

Expand All @@ -110,7 +133,15 @@ describe('api/subscriptions/personal', () => {
expect(personal.isStarted).to.be.true;
});

it('calls parity_defaultAccount', () => {
expect(api._calls.defaultAccount.length).to.be.ok;
});

it('calls personal_accountsInfo', () => {
expect(api._calls.accountsInfo.length).to.be.ok;
});

it('calls personal_allAccountsInfo', () => {
expect(api._calls.allAccountsInfo.length).to.be.ok;
});

Expand Down
11 changes: 11 additions & 0 deletions js/src/jsonrpc/interfaces/parity.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ export default {
}
},

defaultAccount: {
section: SECTION_ACCOUNTS,
desc: 'Returns the defaultAccount that is to be used with transactions',
params: [],
returns: {
type: Address,
desc: 'The account address',
example: '0x63Cf90D3f0410092FC0fca41846f596223979195'
}
},

defaultExtraData: {
desc: 'Returns the default extra data',
params: [],
Expand Down

0 comments on commit 2a5967f

Please sign in to comment.