Skip to content

Commit

Permalink
Merge pull request tronprotocol#3 from sullof/javatron3.6.5
Browse files Browse the repository at this point in the history
Javatron3.6.5
  • Loading branch information
shenyongri110 committed Oct 11, 2019
2 parents 8430b21 + 5216cf0 commit 8ee618c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tronweb",
"version": "2.7.2",
"version": "2.7.3",
"description": "JavaScript SDK that encapsulates the TRON HTTP API",
"main": "dist/TronWeb.node.js",
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/transactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let self;
//helpers

function toHex(value) {
return self.tronWeb.address.toHex(value);
return TronWeb.address.toHex(value);
}

function fromUtf8(value) {
Expand Down Expand Up @@ -1965,5 +1965,4 @@ export default class TransactionBuilder {
this.alterTransaction(transaction, {data, dataFormat}, callback);
}


}
114 changes: 110 additions & 4 deletions src/lib/trx.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import {ADDRESS_PREFIX} from 'utils/address';
import Validator from "../paramValidator";

const TRX_MESSAGE_HEADER = '\x19TRON Signed Message:\n32';
// it should be: '\x15TRON Signed Message:\n32';
const ETH_MESSAGE_HEADER = '\x19Ethereum Signed Message:\n32';

function toHex(value) {
return TronWeb.address.toHex(value);
}

export default class Trx {
constructor(tronWeb = false) {
if (!tronWeb || !tronWeb instanceof TronWeb)
Expand Down Expand Up @@ -134,7 +139,7 @@ export default class Trx {
this.getBlock(block).then(({transactions = false}) => {
if (!transactions)
callback('Transaction not found in block');
else if (typeof index == 'number'){
else if (typeof index == 'number') {
if (index >= 0 && index < transactions.length)
callback(null, transactions[index]);
else
Expand Down Expand Up @@ -622,8 +627,8 @@ export default class Trx {
}

static verifySignature(message, address, signature, useTronHeader = true) {
message = message.replace(/^0x/,'');
signature = signature.replace(/^0x/,'');
message = message.replace(/^0x/, '');
signature = signature.replace(/^0x/, '');
const messageBytes = [
...toUtf8Bytes(useTronHeader ? TRX_MESSAGE_HEADER : ETH_MESSAGE_HEADER),
...utils.code.hexStr2byteArray(message)
Expand Down Expand Up @@ -704,7 +709,7 @@ export default class Trx {
}

static signString(message, privateKey, useTronHeader = true) {
message = message.replace(/^0x/,'');
message = message.replace(/^0x/, '');
const signingKey = new SigningKey(privateKey);
const messageBytes = [
...toUtf8Bytes(useTronHeader ? TRX_MESSAGE_HEADER : ETH_MESSAGE_HEADER),
Expand Down Expand Up @@ -1306,4 +1311,105 @@ export default class Trx {
}).catch(err => callback(err));
}

async getReward(address, options = {}, callback = false) {
options.confirmed = true;
return this._getReward(address, options, callback);
}

async getUnconfirmedReward(address, options = {}, callback = false) {
options.confirmed = false;
return this._getReward(address, options, callback);
}

async getBrokerage(address, options = {}, callback = false) {
options.confirmed = true;
return this._getBrokerage(address, options, callback);
}

async getUnconfirmedBrokerage(address, options = {}, callback = false) {
options.confirmed = false;
return this._getBrokerage(address, options, callback);
}

async _getReward(address = this.tronWeb.defaultAddress.hex, options, callback = false) {
if (utils.isFunction(options)) {
callback = options;
options = {};
}

if (utils.isFunction(address)) {
callback = address;
address = this.tronWeb.defaultAddress.hex;
} else if (utils.isObject(address)) {
options = address;
address = this.tronWeb.defaultAddress.hex;
}

if (!callback)
return this.injectPromise(this._getReward, address, options);

if (this.validator.notValid([
{
name: 'origin',
type: 'address',
value: address
}
], callback))
return;

const data = {
owner_address: toHex(address)
};

this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getReward`, data, 'post')
.then((result = {}) => {

if (typeof result.reward === 'undefined')
return callback('Not found.');

callback(null, result.reward);
}).catch(err => callback(err));
}


async _getBrokerage(address = this.tronWeb.defaultAddress.hex, options, callback = false) {
if (utils.isFunction(options)) {
callback = options;
options = {};
}

if (utils.isFunction(address)) {
callback = address;
address = this.tronWeb.defaultAddress.hex;
} else if (utils.isObject(address)) {
options = address;
address = this.tronWeb.defaultAddress.hex;
}

if (!callback)
return this.injectPromise(this._getBrokerage, address, options);

if (this.validator.notValid([
{
name: 'origin',
type: 'address',
value: address
}
], callback))
return;

const data = {
owner_address: toHex(address)
};

this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getBrokerage`, data, 'post')
.then((result = {}) => {

if (typeof result.brokerage === 'undefined')
return callback('Not found.');

callback(null, result.brokerage);
}).catch(err => callback(err));
}

};
3 changes: 1 addition & 2 deletions test/lib/transactionBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ describe('TronWeb.transactionBuilder', function () {
});

// TODO fix this, Jackie
describe.skip("#triggerSmartContract", async function () {
describe("#triggerSmartContract", async function () {

let transaction;
before(async function () {
Expand Down Expand Up @@ -1489,5 +1489,4 @@ describe('TronWeb.transactionBuilder', function () {
});
});


});
37 changes: 37 additions & 0 deletions test/lib/trx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1760,5 +1760,42 @@ describe('TronWeb.trx', function () {

});

describe("#getReward", async function () {
it('should get the reward', async function () {

let reward = await tronWeb.trx.getReward(accounts[0]);
assert.equal(reward, 0)

});
});

describe("#getUnconfirmedReward", async function () {
it('should get the reward', async function () {

let reward = await tronWeb.trx.getUnconfirmedReward(accounts[0]);
assert.equal(reward, 0)

});
});

describe("#getBrokerage", async function () {
it('should get the brokerage', async function () {

let brokerage = await tronWeb.trx.getBrokerage(accounts[0]);
assert.equal(brokerage, 0)

});
});

describe("#getUnconfirmedBrokerage", async function () {
it('should get the brokerage', async function () {

let brokerage = await tronWeb.trx.getUnconfirmedBrokerage(accounts[0]);
assert.equal(brokerage, 0)

});
});



});

0 comments on commit 8ee618c

Please sign in to comment.