Skip to content

Commit

Permalink
Merge pull request #193 from tronprotocol/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
unicornonea authored Aug 9, 2021
2 parents 7233ee4 + de770ac commit 247a1ea
Show file tree
Hide file tree
Showing 15 changed files with 22,339 additions and 3,468 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.next
node_modules
.DS_Store
package-lock.json
test/setup/TronWeb.js
coverage
yarn-error.log
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ In order to contribute you can

## Recent History

__4.0.0__
- support `broadcastHex` method
- ignore fullnode version check when calling `createToken` method
- update dependencies version
- add strict mode for `pkToAddress` method

__3.2.7__
- Add options `rawParameter` that format of the parameters method and args when creating or triggering a contract
- Update `elliptic` to the latest version 6.5.4
Expand Down
19,536 changes: 19,536 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 18 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "tronweb",
"version": "3.2.7",
"version": "4.0.0",
"description": "JavaScript SDK that encapsulates the TRON HTTP API",
"main": "dist/TronWeb.node.js",
"scripts": {
"prepare": "npm run build",
"build": "npm run clean && webpack --config webpack.config.js --progress --colors",
"build": "npm run clean && webpack --config webpack.config.js --progress --color",
"build:dev": "NODE_ENV=development npm run build",
"clean": "rimraf dist",
"newaccount": "node scripts/test-node.js && node test/helpers/newAccounts 50",
Expand All @@ -23,14 +23,12 @@
"dependencies": {
"@babel/runtime": "^7.0.0",
"axios": "^0.21.1",
"babel-runtime": "^6.26.0",
"bignumber.js": "^7.2.1",
"bignumber.js": "^9.0.1",
"elliptic": "^6.5.4",
"ethers": "^5.0.8",
"ethers": "^5.4.4",
"eventemitter3": "^3.1.0",
"injectpromise": "^1.0.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.14",
"lodash": "^4.17.21",
"semver": "^5.6.0",
"validator": "^13.6.0"
},
Expand All @@ -42,37 +40,32 @@
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-loader": "^8.0.2",
"babel-minify-webpack-plugin": "^0.3.1",
"babel-plugin-istanbul": "^5.0.1",
"babel-plugin-source-map-support": "^2.0.1",
"babel-plugin-source-map-support": "^2.1.3",
"chai": "^4.1.2",
"chalk": "^2.4.1",
"eth-sig-util": "^2.1.0",
"globby": "^8.0.1",
"grunt": "^1.0.4",
"grunt-cli": "^1.3.1",
"grunt-karma": "^3.0.0",
"husky": "^1.0.0-rc.14",
"husky": "^7.0.0",
"istanbul": "^0.4.5",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^4.0.1",
"jsonwebtoken": "^8.5.1",
"karma": "^6.3.3",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.2",
"karma-coverage-istanbul-reporter": "^2.0.4",
"karma-coverage": "^2.0.3",
"karma-coverage-istanbul-reporter": "^3.0.3",
"karma-edge-launcher": "^0.4.2",
"karma-firefox-launcher": "^1.1.0",
"karma-mocha": "^1.3.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "^0.0.32",
"karma-webpack": "4.0.0-rc.2",
"karma-webpack": "4.0.0-rc.6",
"matchdep": "^2.0.0",
"mocha": "^5.2.0",
"puppeteer": "^1.20.0",
"rimraf": "^2.6.2",
"source-map-support": "^0.5.9",
"webpack": "^4.17.2",
"webpack-cli": "^3.1.0",
"webpack-node-externals": "^1.7.2"
"puppeteer": "^10.0.0",
"rimraf": "^3.0.2",
"source-map-support": "^0.5.19",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12",
"webpack-node-externals": "^3.0.0"
},
"author": {
"name": "Kondax",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ export default class TronWeb extends EventEmitter {
utils.crypto.decodeBase58Address(address)
).toLowerCase();
},
fromPrivateKey(privateKey) {
fromPrivateKey(privateKey, strict = false) {
try {
return utils.crypto.pkToAddress(privateKey);
return utils.crypto.pkToAddress(privateKey, strict);
} catch {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/transactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ export default class TransactionBuilder {
frozen_days: parseInt(frozenDuration)
}
}
if (this.tronWeb.fullnodeSatisfies('>=3.5.0') && !(parseInt(frozenAmount) > 0)) {
if (!(parseInt(frozenAmount) > 0)) {
delete data.frozen_supply
}
if (precision && !isNaN(parseInt(precision))) {
Expand Down
36 changes: 36 additions & 0 deletions src/lib/trx.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,38 @@ export default class Trx {
}).catch(err => callback(err));
}

sendHexTransaction(signedHexTransaction = false, options = {}, callback = false) {
if (utils.isFunction(options)) {
callback = options;
options = {};
}

if (!callback)
return this.injectPromise(this.sendHexTransaction, signedHexTransaction, options);

if (!utils.isHex(signedHexTransaction))
return callback('Invalid hex transaction provided');

if (!utils.isObject(options))
return callback('Invalid options provided');

const params = {
transaction: signedHexTransaction
}

this.tronWeb.fullNode.request(
'wallet/broadcasthex',
params,
'post'
).then(result => {
if (result.result) {
result.transaction = JSON.parse(result.transaction)
result.hexTransaction = signedHexTransaction;
}
callback(null, result);
}).catch(err => callback(err));
}

async sendTransaction(to = false, amount = false, options = {}, callback = false) {
if (utils.isFunction(options)) {
callback = options;
Expand Down Expand Up @@ -1147,6 +1179,10 @@ export default class Trx {
return this.sendRawTransaction(...args);
}

broadcastHex(...args) {
return this.sendHexTransaction(...args);
}

signTransaction(...args) {
return this.sign(...args);
}
Expand Down
13 changes: 11 additions & 2 deletions src/utils/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,25 @@ export function isHexChar(c) {
return 0;
}

export function hexStr2byteArray(str) {
// set strict as true: if the length of str is odd, add 0 before the str to make its length as even
export function hexStr2byteArray(str, strict = false) {
if (typeof str !== 'string')
throw new Error('The passed string is not a string')

let len = str.length;

if (strict) {
if (len % 2) {
str = `0${str}`;
len++;
}
}
const byteArray = Array();
let d = 0;
let j = 0;
let k = 0;

for (let i = 0; i < str.length; i++) {
for (let i = 0; i < len; i++) {
const c = str.charAt(i);

if (isHexChar(c)) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ export function passwordToAddress(password) {
return getBase58CheckAddress(com_addressBytes);
}

export function pkToAddress(privateKey) {
const com_priKeyBytes = hexStr2byteArray(privateKey);
export function pkToAddress(privateKey, strict = false) {
const com_priKeyBytes = hexStr2byteArray(privateKey, strict);
const com_addressBytes = getAddressFromPriKey(com_priKeyBytes);

return getBase58CheckAddress(com_addressBytes);
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/BlockLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BlockLib {

pluginInterface() {
return {
requires: '^3.0.0',
requires: '^4.0.0',
fullClass: true
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/GetNowBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GetNowBlock {
someParameter = options.someParameter
}
return {
requires: '^3.0.0',
requires: '^4.0.0',
components: {
trx: {
// will be overridden
Expand Down
1 change: 1 addition & 0 deletions test/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
freeBandwidth: 10,
freeBandwidthLimit: 100
},
SIGNED_HEX_TRANSACTION: '0a85010a02380a220830202d4c1473d46640d8edabfea72f5a67080112630a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412320a1541fbbb1a37f9fbc18a3e2b8fd8c0823251cd3b8ee8121541af4852489314e72ffae825238c52d0e1ffff1ead18c0843d70f182cffca72f1241fa301a789dc51714be33b0f298e3f990650bec285ab628ab8ea5f18aeb3466643ca89b604d9df74fa19a11235e0d7ab974322f0097eefe0b7efb38ea9ce4cf6601',
TEST_TRON_GRID_API: 'http://47.252.84.138:29086',
TEST_TRON_HEADER_API_KEY: 'be815f09-e30f-4b98-b84d-0bb41f0708fd',
TEST_TRON_HEADER_API_JWT_KEY: '95057b57-a2bd-4bd5-9337-5b52cb2b3bc2',
Expand Down
3 changes: 2 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ describe('TronWeb Instance', function () {
assert.equal(newAccount.publicKey.length, 130);
let address = tronWeb.address.fromPrivateKey(newAccount.privateKey);
assert.equal(address, newAccount.address.base58);

address = tronWeb.address.fromPrivateKey(newAccount.privateKey, true);
assert.equal(address, newAccount.address.base58);
// TODO The new accounts returns an uppercase address, while everywhere else we handle lowercase addresses. Maybe we should make it consistent and let createAccount returning a lowercase address
assert.equal(tronWeb.address.toHex(address), newAccount.address.hex.toLowerCase());
});
Expand Down
22 changes: 22 additions & 0 deletions test/lib/trx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
ADDRESS_BASE58,
PRIVATE_KEY,
getTokenOptions,
SIGNED_HEX_TRANSACTION
} = require('../helpers/config');
const testRevertContract = require('../fixtures/contracts').testRevert;

Expand Down Expand Up @@ -993,6 +994,27 @@ describe('TronWeb.trx', function () {
});
});

describe("#broadcastHex", async function () {

it('should broadcast a hex transaction', async function () {
const result = await tronWeb.trx.broadcastHex(SIGNED_HEX_TRANSACTION);
assert.isTrue(result.result);
});

it('should throw invalid hex transaction provided error', async function () {
await assertThrow(
tronWeb.trx.broadcastHex(false),
'Invalid hex transaction provided'
);
});

it('should throw invalid options provided error', async function () {
await assertThrow(
tronWeb.trx.broadcastHex(SIGNED_HEX_TRANSACTION, false),
'Invalid options provided'
);
});
});

describe("#getTransaction", async function () {

Expand Down
Loading

0 comments on commit 247a1ea

Please sign in to comment.