From 2d5697f40cfa6d4fb150e9feb705869df681e3d4 Mon Sep 17 00:00:00 2001
From: adcpm <fabien@adcpm.com>
Date: Mon, 22 Aug 2016 00:25:38 +0700
Subject: [PATCH] - SteemAuth dependency added - steem.broadcast added with new
 method "vote" - Readme updated

---
 .gitignore                            |  1 +
 .npmignore                            |  3 ++-
 README.md                             | 14 +++++------
 browser.js                            |  4 ----
 dev.js                                | 21 ++---------------
 doc/README.md                         |  8 +++++++
 examples/{index.html => browser.html} |  0
 examples/server.js                    | 29 +++++++++++++++++++++++
 index.js                              |  2 ++
 lib/broadcast.js                      | 34 +++++++++++++++++++++++++++
 lib/browser.js                        |  4 ++++
 package.json                          |  8 ++++---
 test.js                               | 27 +++------------------
 13 files changed, 97 insertions(+), 58 deletions(-)
 delete mode 100644 browser.js
 rename examples/{index.html => browser.html} (100%)
 create mode 100644 examples/server.js
 create mode 100644 lib/broadcast.js
 create mode 100644 lib/browser.js

diff --git a/.gitignore b/.gitignore
index 2621e29..19ce7c3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@ scr
 *.css.map
 *.css
 dev.js
+test.js
 
 # Logs
 logs
diff --git a/.npmignore b/.npmignore
index c0fde0d..8b0cbe1 100644
--- a/.npmignore
+++ b/.npmignore
@@ -2,4 +2,5 @@
 .idea
 src
 npm-debug.log
-dev.js
\ No newline at end of file
+dev.js
+test.js
\ No newline at end of file
diff --git a/README.md b/README.md
index e5789f0..5fa4d78 100644
--- a/README.md
+++ b/README.md
@@ -33,25 +33,25 @@ wss://node.steem.ws<br/>
 wss://this.piston.rocks<br/>
 
 ## Examples
-### Get Accounts
+### Broadcast Vote
 ```js
 var steem = require('steem');
 
-steem.api.getAccounts(['ned', 'dan'], function(err, result) {
+steem.broadcast.vote(username, password, author, permlink, weight, function(err, result) {
 	console.log(err, result);
 });
 ```
 
-### Get State
-```js 
-steem.api.getState('/trends/funny', function(err, result) {
+### Get Accounts
+```js
+steem.api.getAccounts(['ned', 'dan'], function(err, result) {
 	console.log(err, result);
 });
 ```
 
-### Get Config
+### Get State
 ```js 
-steem.api.getConfig(function(err, result) {
+steem.api.getState('/trends/funny', function(err, result) {
 	console.log(err, result);
 });
 ```
diff --git a/browser.js b/browser.js
deleted file mode 100644
index c75f16e..0000000
--- a/browser.js
+++ /dev/null
@@ -1,4 +0,0 @@
-module.exports = {
-	api: require('./lib/api'),
-	formatter: require('./lib/formatter'),
-};
\ No newline at end of file
diff --git a/dev.js b/dev.js
index ac18b16..43ea805 100644
--- a/dev.js
+++ b/dev.js
@@ -1,21 +1,4 @@
 var steem = require('./index');
 
-steem.api.login('******', '****************', function(err, result) {
-	console.log(result);
-	var trx = {
-		expiration: '2016-08-20T02:59:51',
-		extensions: [],
-		operations: [['vote', {
-			voter: 'siol',
-			author: 'rogerkver',
-			permlink: 'roger-ver-the-world-s-first-bitcoin-investor-is-now-on-steemit',
-			weight: 10000
-		}]],
-		ref_block_num: 40607,
-		ref_block_prefix: 2394871259,
-		signatures: ['206239640514a1aac6ef29b0fdf7bf1f1457526a77bb4cb35bc82d7f614d271bb54783850b6da824db5cf787d7a7b9b8a1da5fd3d2c2ebf53437906fc93f45d238']
-	};
-	steem.api.broadcastTransaction(trx, function(err, result) {
-		console.log(err, result);
-	});
-});
\ No newline at end of file
+var username = process.env.STEEM_USERNAME;
+var password = process.env.STEEM_PASSWORD;
diff --git a/doc/README.md b/doc/README.md
index c9a2877..763daf9 100644
--- a/doc/README.md
+++ b/doc/README.md
@@ -476,4 +476,12 @@ console.log(reputation);
 ```js 
 var power = steem.formatter.vestToSteem(user.vesting_shares, props.total_vesting_shares, props.total_vesting_fund_steem)
 console.log(power);
+```
+
+## Broadcast
+### Vote
+```js
+steem.broadcast.vote(username, password, author, permlink, weight, function(err, result) {
+	console.log(err, result);
+});
 ```
\ No newline at end of file
diff --git a/examples/index.html b/examples/browser.html
similarity index 100%
rename from examples/index.html
rename to examples/browser.html
diff --git a/examples/server.js b/examples/server.js
new file mode 100644
index 0000000..a1467a6
--- /dev/null
+++ b/examples/server.js
@@ -0,0 +1,29 @@
+var steem = require('./../index');
+
+steem.api.getAccountCount(function(err, result) {
+	console.log(err, result);
+});
+
+steem.api.getAccounts(['dan'], function(err, result) {
+	console.log(err, result);
+	var reputation = steem.formatter.reputation(result[0].reputation);
+	console.log(reputation);
+});
+
+steem.api.getState('trending/steemit', function(err, result) {
+	console.log(err, result);
+});
+
+steem.api.getFollowing('ned', 0, 'blog', 10, function(err, result) {
+	console.log(err, result);
+});
+
+steem.api.getFollowers('dan', 0, 'blog', 10, function(err, result) {
+	console.log(err, result);
+});
+
+steem.api.streamOperations(function(err, result) {
+	if (!err && result[1].author == 'fabien') {
+		console.log(result);
+	}
+});
\ No newline at end of file
diff --git a/index.js b/index.js
index c75f16e..efe8c46 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,6 @@
 module.exports = {
 	api: require('./lib/api'),
 	formatter: require('./lib/formatter'),
+	auth: require('steemauth'),
+	broadcast: require('./lib/broadcast')
 };
\ No newline at end of file
diff --git a/lib/broadcast.js b/lib/broadcast.js
new file mode 100644
index 0000000..4f086bc
--- /dev/null
+++ b/lib/broadcast.js
@@ -0,0 +1,34 @@
+var moment = require('moment'),
+	steemAuth = require('steemauth'),
+	steemApi = require('./api');
+
+module.exports = {
+	send: function(username, password, tx, privKeys, callback) {
+		steemApi.login(username, password, function() {
+			steemApi.getDynamicGlobalProperties(function(err, result) {
+				tx.expiration = moment.utc(result.timestamp).add(15, 'second').format().replace('Z', '');
+				tx.ref_block_num = result.head_block_number & 0xFFFF;
+				tx.ref_block_prefix =  new Buffer(result.head_block_id, 'hex').readUInt32LE(4);
+				var signedTransaction = steemAuth.signTransaction(tx, privKeys);
+				steemApi.broadcastTransactionWithCallback(function(){}, signedTransaction, function(err, result) {
+					callback(err, result);
+				});
+			});
+		});
+	},
+	vote: function(username, password, author, permlink, weight, callback) {
+		var tx = {
+			extensions: [],
+			operations: [['vote', {
+				voter: username,
+				author: author,
+				permlink: permlink,
+				weight: weight
+			}]]
+		};
+		var privKeys = steemAuth.getPrivateKeys(username, password, ['posting']);
+		this.send(username, password, tx, privKeys, function(err, result) {
+			callback(err, result);
+		})
+	}
+};
\ No newline at end of file
diff --git a/lib/browser.js b/lib/browser.js
new file mode 100644
index 0000000..8ecce55
--- /dev/null
+++ b/lib/browser.js
@@ -0,0 +1,4 @@
+module.exports = {
+	api: require('./api'),
+	formatter: require('./formatter'),
+};
\ No newline at end of file
diff --git a/package.json b/package.json
index f11528b..66af987 100644
--- a/package.json
+++ b/package.json
@@ -1,11 +1,11 @@
 {
   "name": "steem",
-  "version": "0.3.7",
+  "version": "0.3.8",
   "description": "Steem.js the JavaScript API for Steem blockchain",
   "main": "index.js",
   "scripts": {
     "test": "node test.js",
-    "build": "browserify browser.js -o | uglifyjs > lib/steem.min.js && browserify browser.js -o | uglifyjs > examples/steem.min.js"
+    "build": "browserify lib/browser.js -o | uglifyjs > lib/steem.min.js && browserify lib/browser.js -o | uglifyjs > examples/steem.min.js"
   },
   "repository": {
     "type": "git",
@@ -17,13 +17,15 @@
     "blockchain",
     "steemjs"
   ],
-  "author": "AdCpm <fabien@adcpm.com> (https://github.com/adcpm)",
+  "author": "AdCpm <fabien@bonustrack.co> (https://github.com/adcpm)",
   "license": "MIT",
   "bugs": {
     "url": "https://github.com/adcpm/steem/issues"
   },
   "homepage": "https://github.com/adcpm/steem#readme",
   "dependencies": {
+    "moment": "^2.14.1",
+    "steemauth": "0.0.4",
     "ws": "^1.1.1"
   },
   "devDependencies": {
diff --git a/test.js b/test.js
index 5a37f7b..f98d58a 100644
--- a/test.js
+++ b/test.js
@@ -1,29 +1,8 @@
 var steem = require('./index');
 
-steem.api.getAccountCount(function(err, result) {
-	console.log(err, result);
-});
-
-steem.api.getAccounts(['dan'], function(err, result) {
-	console.log(err, result);
-	var reputation = steem.formatter.reputation(result[0].reputation);
-	console.log(reputation);
-});
-
-steem.api.getState('trending/steemit', function(err, result) {
-	console.log(err, result);
-});
-
-steem.api.getFollowing('ned', 0, 'blog', 10, function(err, result) {
-	console.log(err, result);
-});
+var username = process.env.STEEM_USERNAME;
+var password = process.env.STEEM_PASSWORD;
 
-steem.api.getFollowers('dan', 0, 'blog', 10, function(err, result) {
+steem.broadcast.vote(username, password, 'infovore', 'mentorship-channel-for-artists-this-week-on-steemit-steemians-speak-behind-the-username-steemmag-steemit-s-weekend-digest-6-p-2', 10000, function(err, result) {
 	console.log(err, result);
-});
-
-steem.api.streamOperations(function(err, result) {
-	if (!err && result[1].author == 'fabien') {
-		console.log(result);
-	}
 });
\ No newline at end of file
-- 
GitLab