From 6a7a896da37c744a183c1ab718911ab05f490dd4 Mon Sep 17 00:00:00 2001 From: Dan Turner Date: Mon, 29 Feb 2016 15:59:54 +1100 Subject: [PATCH] Implement the "personal" API's of go-ethereum As per: https://github.com/ethereum/go-ethereum/wiki/Go-ethereum-management-API's --- lib/web3.js | 4 +- lib/web3/methods/personal.js | 73 +++++++++++++++++++++++++++++ test/web3.personal.listAccounts.js | 38 +++++++++++++++ test/web3.personal.methods.js | 13 +++++ test/web3.personal.newAccount.js | 15 ++++++ test/web3.personal.unlockAccount.js | 15 ++++++ 6 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 lib/web3/methods/personal.js create mode 100644 test/web3.personal.listAccounts.js create mode 100644 test/web3.personal.methods.js create mode 100644 test/web3.personal.newAccount.js create mode 100644 test/web3.personal.unlockAccount.js diff --git a/lib/web3.js b/lib/web3.js index 1f6de8b..4a969a2 100644 --- a/lib/web3.js +++ b/lib/web3.js @@ -14,7 +14,7 @@ You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -/** +/** * @file web3.js * @authors: * Jeffrey Wilcke @@ -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 diff --git a/lib/web3/methods/personal.js b/lib/web3/methods/personal.js new file mode 100644 index 0000000..96c3c25 --- /dev/null +++ b/lib/web3/methods/personal.js @@ -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 . +*/ +/** + * @file eth.js + * @author Marek Kotewicz + * @author Fabian Vogelsteller + * @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; diff --git a/test/web3.personal.listAccounts.js b/test/web3.personal.listAccounts.js new file mode 100644 index 0000000..02c085d --- /dev/null +++ b/test/web3.personal.listAccounts.js @@ -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); + }); + }); + }); +}); diff --git a/test/web3.personal.methods.js b/test/web3.personal.methods.js new file mode 100644 index 0000000..22acdb6 --- /dev/null +++ b/test/web3.personal.methods.js @@ -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'); + }); +}); diff --git a/test/web3.personal.newAccount.js b/test/web3.personal.newAccount.js new file mode 100644 index 0000000..4e7fdac --- /dev/null +++ b/test/web3.personal.newAccount.js @@ -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); diff --git a/test/web3.personal.unlockAccount.js b/test/web3.personal.unlockAccount.js new file mode 100644 index 0000000..562fd77 --- /dev/null +++ b/test/web3.personal.unlockAccount.js @@ -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);