From 85c72906519a9ab6ed9bb96ac7847a0ff00c528e Mon Sep 17 00:00:00 2001 From: zergweak Date: Sat, 14 Apr 2018 17:56:51 +0800 Subject: [PATCH] modify http.js for address format --- package-lock.json | 9 +++++---- package.json | 2 +- src/client/http.js | 11 ++++++----- src/lib/base58.js | 7 ++++++- src/utils/account.js | 4 ++-- src/utils/crypto.js | 7 ++++--- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index c963e46..cd0a1d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@tronprotocol/wallet-api", - "version": "0.0.17", + "version": "0.0.21", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1795,9 +1795,9 @@ } }, "mocha": { - "version": "5.0.5", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.0.5.tgz", - "integrity": "sha512-3MM3UjZ5p8EJrYpG7s+29HAI9G7sTzKEe4+w37Dg0QP7qL4XGsV+Q2xet2cE37AqdgN1OtYQB6Vl98YiPV3PgA==", + "version": "5.1.0", + "resolved": "http://registry.npm.taobao.org/mocha/download/mocha-5.1.0.tgz", + "integrity": "sha1-X/Ecw5wLtlMwrGxB+QhmNOPj9oY=", "requires": { "browser-stdout": "1.3.1", "commander": "2.11.0", @@ -1807,6 +1807,7 @@ "glob": "7.1.2", "growl": "1.10.3", "he": "1.1.1", + "minimatch": "3.0.4", "mkdirp": "0.5.1", "supports-color": "4.4.0" } diff --git a/package.json b/package.json index 1ceb8f8..3bd486e 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "grpc-caller": "^0.4.0", "grpc-tools": "^1.6.6", "js-sha3": "^0.7.0", - "mocha": "^5.0.5", + "mocha": "^5.1.0", "qs": "^6.5.1" } } diff --git a/src/client/http.js b/src/client/http.js index 77402f7..f103854 100644 --- a/src/client/http.js +++ b/src/client/http.js @@ -6,7 +6,7 @@ const {AccountList, NumberMessage, WitnessList, AssetIssueList} = require("../pr const {TransferContract} = require("../protocol/core/Contract_pb"); const qs = require("qs"); const stringToBytes = require("../lib/code").stringToBytes; -const { signTransaction, passwordToAddress } = require("../utils/crypto"); +const { getBase58CheckAddress, signTransaction, passwordToAddress } = require("../utils/crypto"); class HttpClient { @@ -38,6 +38,7 @@ class HttpClient { * @returns {Promise<*>} */ async getLatestBlock() { + let {data} = await xhr.get(`${this.url}/getBlockToView`); let currentBlock = base64DecodeFromString(data); let block = Block.deserializeBinary(currentBlock); @@ -71,7 +72,7 @@ class HttpClient { size: recentBlock.length, parentHash: byteArray2hexStr(blockData.getBlockHeader().getRawData().getParenthash()), number: blockData.getBlockHeader().getRawData().getNumber(), - witnessAddress: byteArray2hexStr(blockData.getBlockHeader().getRawData().getWitnessAddress()), + witnessAddress: getBase58CheckAddress(Array.from(blockData.getBlockHeader().getRawData().getWitnessAddress())), time: blockData.getBlockHeader().getRawData().getTimestamp(), transactionsCount: blockData.getTransactionsList().length, contraxtType: Transaction.Contract.ContractType, @@ -104,7 +105,7 @@ class HttpClient { return accountList.map(account => { let name = bytesToString(account.getAccountName()); - let address = byteArray2hexStr(account.getAddress()); + let address = getBase58CheckAddress(Array.from(account.getAddress())); let balance = account.getBalance(); let balanceNum = 0; if (balance !== 0) { @@ -134,7 +135,7 @@ class HttpClient { return witnessList.map(witness => { return { - address: byteArray2hexStr(witness.getAddress()), + address: getBase58CheckAddress(Array.from(witness.getAddress())), url: witness.getUrl(), latestBlockNumber: witness.getLatestblocknum(), producedTotal: witness.getTotalproduced(), @@ -156,7 +157,7 @@ class HttpClient { return assetIssueListObj.getAssetissueList().map(asset => { return { name: bytesToString(asset.getName()), - ownerAddress: byteArray2hexStr(asset.getOwnerAddress()), + ownerAddress: getBase58CheckAddress(Array.from(asset.getOwnerAddress())), totalSupply: asset.getTotalSupply(), startTime: asset.getStartTime(), endTime: asset.getEndTime(), diff --git a/src/lib/base58.js b/src/lib/base58.js index 441c86c..3e5abf9 100644 --- a/src/lib/base58.js +++ b/src/lib/base58.js @@ -65,4 +65,9 @@ function decode58(string) { for (i = 0; string[i] === '1' && i < string.length - 1; i++) bytes.push(0) return bytes.reverse() -} \ No newline at end of file +} + +module.exports = { + encode58, + decode58 +}; \ No newline at end of file diff --git a/src/utils/account.js b/src/utils/account.js index 7019e83..1b60e8a 100644 --- a/src/utils/account.js +++ b/src/utils/account.js @@ -1,6 +1,6 @@ const byteArray2hexStr = require("./bytes").byteArray2hexStr; const base64EncodeToString = require("../lib/code").base64EncodeToString; -const {genPriKey, getAddressFromPriKey} = require("./crypto"); +const {getBase58CheckAddress, genPriKey, getAddressFromPriKey} = require("./crypto"); /** * Generate a new account @@ -8,7 +8,7 @@ const {genPriKey, getAddressFromPriKey} = require("./crypto"); function generateAccount() { let priKeyBytes = genPriKey(); let addressBytes = getAddressFromPriKey(priKeyBytes); - let address = byteArray2hexStr(addressBytes); + let address = getBase58CheckAddress(addressBytes); let password = base64EncodeToString(priKeyBytes); let privateKey = byteArray2hexStr(priKeyBytes); diff --git a/src/utils/crypto.js b/src/utils/crypto.js index bba20ab..bcc6004 100644 --- a/src/utils/crypto.js +++ b/src/utils/crypto.js @@ -7,6 +7,7 @@ const jsSHA = require("../lib/sha256"); const { byte2hexStr, byteArray2hexStr } = require("./bytes"); const add_pre_fix = 'a0'; //a0 + address ,a0 is version +const add_pre_fix_byte = 0xa0; //a0 + address ,a0 is version /** @@ -82,7 +83,7 @@ function getBase58CheckAddress(addressBytes) { return base58Check; } -function validAddress(base58Sting) { +function isAddressValid(base58Sting) { if (typeof(base58Sting) != 'string') { return false; } @@ -188,7 +189,7 @@ function SHA256(msgBytes) { function passwordToAddress(password) { let com_priKeyBytes = base64DecodeFromString(password); let com_addressBytes = getAddressFromPriKey(com_priKeyBytes); - return byteArray2hexStr(com_addressBytes); + return getBase58CheckAddress(com_addressBytes); } module.exports = { @@ -198,6 +199,6 @@ module.exports = { getAddressFromPriKey, getPubKeyFromPriKey, getBase58CheckAddress, - validAddress, + isAddressValid, getBase58CheckAddressFromPriKeyBase64String };