Implement the "personal" API's of go-ethereum

As per: https://github.com/ethereum/go-ethereum/wiki/Go-ethereum-management-API's
This commit is contained in:
Dan Turner 2016-02-29 15:59:54 +11:00
parent c5a6eaabf9
commit 6a7a896da3
6 changed files with 157 additions and 1 deletions

View File

@ -31,6 +31,7 @@ var Eth = require('./web3/methods/eth');
var DB = require('./web3/methods/db');
var Shh = require('./web3/methods/shh');
var Net = require('./web3/methods/net');
var Personal = require('./web3/methods/personal');
var Settings = require('./web3/settings');
var version = require('./version.json');
var utils = require('./utils/utils');
@ -50,6 +51,7 @@ function Web3 (provider) {
this.db = new DB(this);
this.shh = new Shh(this);
this.net = new Net(this);
this.personal = new Personal(this);
this.settings = new Settings();
this.version = {
api: version.version

View File

@ -0,0 +1,73 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file eth.js
* @author Marek Kotewicz <marek@ethdev.com>
* @author Fabian Vogelsteller <fabian@ethdev.com>
* @date 2015
*/
"use strict";
var Method = require('../method');
var Property = require('../property');
function Personal(web3) {
this._requestManager = web3._requestManager;
var self = this;
methods().forEach(function(method) {
method.attachToObject(self);
method.setRequestManager(self._requestManager);
});
properties().forEach(function(p) {
p.attachToObject(self);
p.setRequestManager(self._requestManager);
});
}
var methods = function () {
var newAccount = new Method({
name: 'newAccount',
call: 'personal_newAccount',
params: 1
});
var unlockAccount = new Method({
name: 'unlockAccount',
call: 'personal_unlockAccount',
params: 2
});
return [
newAccount,
unlockAccount
];
};
var properties = function () {
return [
new Property({
name: 'listAccounts',
getter: 'personal_listAccounts'
})
];
};
module.exports = Personal;

View File

@ -0,0 +1,38 @@
var chai = require('chai');
var assert = chai.assert;
var Web3 = require('../index');
var web3 = new Web3();
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var method = 'listAccounts';
var tests = [{
result: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'],
formattedResult: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'],
call: 'personal_'+ method
}];
describe('web3.personal', function () {
describe(method, function () {
tests.forEach(function (test, index) {
it('property test: ' + index, function () {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, []);
});
// when
var result = web3.personal[method];
// then
assert.deepEqual(test.formattedResult, result);
});
});
});
});

View File

@ -0,0 +1,13 @@
var chai = require('chai');
var assert = chai.assert;
var Web3 = require('../index.js');
var web3 = new Web3();
var u = require('./helpers/test.utils.js');
describe('web3.net', function() {
describe('methods', function() {
u.propertyExists(web3.personal, 'listAccounts');
u.methodExists(web3.personal, 'newAccount');
u.methodExists(web3.personal, 'unlockAccount');
});
});

View File

@ -0,0 +1,15 @@
var chai = require('chai');
var web3 = require('../index');
var testMethod = require('./helpers/test.method.js');
var method = 'newAccount';
var tests = [{
args: ['P@ssw0rd!'],
formattedArgs: ['P@ssw0rd!'],
result: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'],
formattedResult: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'],
call: 'personal_'+ method
}];
testMethod.runTests('personal', method, tests);

View File

@ -0,0 +1,15 @@
var chai = require('chai');
var web3 = require('../index');
var testMethod = require('./helpers/test.method.js');
var method = 'unlockAccount';
var tests = [{
args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'P@ssw0rd!'],
formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'P@ssw0rd!'],
result: true,
formattedResult: true,
call: 'personal_'+ method
}];
testMethod.runTests('personal', method, tests);