From ae0c7878f2ac8f2b6e0691a0f1286577b0d02604 Mon Sep 17 00:00:00 2001 From: mason-hz Date: Mon, 8 Apr 2024 16:31:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20get=20transaction=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc | 5 +-- package.json | 2 +- src/util/utils.js | 61 ++++++++++++++++++----------------- test/.DS_Store | Bin 0 -> 6148 bytes test/unit/util/utils.test.js | 23 ++++++++++--- 5 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 test/.DS_Store diff --git a/.eslintrc b/.eslintrc index edb87536..db60ecdc 100644 --- a/.eslintrc +++ b/.eslintrc @@ -29,9 +29,10 @@ "no-underscore-dangle": "off", "no-bitwise": "off", "no-mixed-operators": "off", - "max-len": ["error", { "code": 120 }], + "max-len": ["error", { "code": 120 , "ignoreComments": true}], "class-methods-use-this": "off", "no-plusplus": "off", - "implicit-arrow-linebreak": "off" + "implicit-arrow-linebreak": "off", + "object-curly-newline": "off" } } diff --git a/package.json b/package.json index cc40ab13..8ef2c8a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-sdk", - "version": "3.4.7", + "version": "3.4.8", "description": "aelf-sdk js library", "main": "dist/aelf.cjs.js", "browser": "dist/aelf.umd.js", diff --git a/src/util/utils.js b/src/util/utils.js index a6c1e2bd..1cff808b 100644 --- a/src/util/utils.js +++ b/src/util/utils.js @@ -7,15 +7,9 @@ import BigNumber from 'bignumber.js'; import bs58 from 'bs58'; import { UNIT_MAP, UNSIGNED_256_INT } from '../common/constants'; import { Transaction } from './proto'; -import { - OUTPUT_TRANSFORMERS, - encodeAddress, - transform, - transformArrayToMap -} from './transform'; +import { OUTPUT_TRANSFORMERS, encodeAddress, transform, transformArrayToMap } from './transform'; import sha256 from './sha256'; - export const base58 = { encode(data, encoding = 'hex') { let result = data; @@ -62,10 +56,8 @@ export const chainIdConvertor = { } }; -const arrayBufferToHex = arrayBuffer => Array.prototype.map.call( - new Uint8Array(arrayBuffer), - n => (`0${n.toString(16)}`).slice(-2) -).join(''); +const arrayBufferToHex = arrayBuffer => + Array.prototype.map.call(new Uint8Array(arrayBuffer), n => `0${n.toString(16)}`.slice(-2)).join(''); export const arrayToHex = value => { let hex = ''; @@ -103,10 +95,9 @@ export const padLeft = (string, charLen, sign) => { */ export const padRight = (string, charLen, sign) => { const length = charLen - string.length + 1; - return string + (new Array(length < 0 ? 0 : length).join(sign || '0')); + return string + new Array(length < 0 ? 0 : length).join(sign || '0'); }; - /** * Returns a hex rep from the encoded address * @@ -142,8 +133,8 @@ export const encodeAddressRep = hex => { * @param {Object} object * @return {Boolean} */ -export const isBigNumber = object => object instanceof BigNumber - || (object && object.constructor && object.constructor.name === 'BigNumber'); +export const isBigNumber = object => + object instanceof BigNumber || (object && object.constructor && object.constructor.name === 'BigNumber'); /** * Returns true if object is string, otherwise false @@ -152,8 +143,8 @@ export const isBigNumber = object => object instanceof BigNumber * @param {Object} object * @return {Boolean} */ -export const isString = object => typeof object === 'string' - || (object && object.constructor && object.constructor.name === 'String'); +export const isString = object => + typeof object === 'string' || (object && object.constructor && object.constructor.name === 'String'); /** * Returns true if object is function, otherwise false @@ -171,7 +162,7 @@ export const isFunction = object => typeof object === 'function'; * @param {Object} object * @return {Boolean} */ -export const isObject = object => object !== null && !(Array.isArray(object)) && typeof object === 'object'; +export const isObject = object => object !== null && !Array.isArray(object) && typeof object === 'object'; /** * Returns true if object is boolean, otherwise false @@ -238,7 +229,9 @@ export const getValueOfUnit = unit => { const unitValue = UNIT_MAP[unit ? unit.toLowerCase() : 'ether']; if (unitValue === undefined) { // eslint-disable-next-line max-len - throw new Error(`This unit doesn\'t exists, please use the one of the following units ${JSON.stringify(UNIT_MAP, null, 2)}`); + throw new Error( + `This unit doesn\'t exists, please use the one of the following units ${JSON.stringify(UNIT_MAP, null, 2)}` + ); } return new BigNumber(unitValue, 10); }; @@ -307,13 +300,11 @@ export const toWei = (number, unit) => { export const toTwosComplement = number => { const bigNumber = toBigNumber(number).round(); if (bigNumber.lessThan(0)) { - return new BigNumber(UNSIGNED_256_INT, 16) - .plus(bigNumber).plus(1); + return new BigNumber(UNSIGNED_256_INT, 16).plus(bigNumber).plus(1); } return bigNumber; }; - /** * Returns hex * @@ -379,14 +370,7 @@ export const unpackSpecifiedTypeData = ({ data, dataType, encoding = 'hex' }) => }; export function deserializeTransaction(rawTx, paramsDataType) { - const { - from, - to, - params, - refBlockPrefix, - signature, - ...rest - } = unpackSpecifiedTypeData({ + const { from, to, params, refBlockPrefix, signature, ...rest } = unpackSpecifiedTypeData({ data: rawTx, dataType: Transaction }); @@ -421,6 +405,23 @@ export function getAuthorization(userName, password) { const base = Buffer.from(`${userName}:${password}`).toString('base64'); return `Basic ${base}`; } +/** + * + * Use rawTransaction to get transaction id + * @param {String} rawTx rawTransaction + * @return {String} string + * + * const txId = getTransactionId('0a220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd512220a2089ac786c8ad3b56f63a6f2767369a5273f801de2415b613c783cad3d148ce3ab18d5d3bb35220491cf6ba12a18537761704578616374546f6b656e73466f72546f6b656e73325008c0f7f27110bbe5947c1a09534752544553542d311a03454c4622220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd52a08088996ceb0061000320631323334353682f10441ec6ad50c4b210976ba0ba5c287ab6fabd0c444839e2505ecb1b5f52838095b290cb245ec1c97dade3bde6ac14c6892e526569e9b71240d3c120b1a6c8e41afba00'); + * console.log(txId); + * // => cf564f3169012cb173efcf5543b2a71b030b16fad3ddefe3e04a5c1e1bc0047d + */ +export function getTransactionId(rawTx) { + const hash = Buffer.from(rawTx.replace('0x', ''), 'hex'); + const decode = Transaction.decode(hash); + decode.signature = null; + const encode = Transaction.encode(decode).finish(); + return sha256(encode); +} // /** // * Converts value to it's hex representation diff --git a/test/.DS_Store b/test/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..21e950f81759e76f25c249e0e42702cf6de6fda2 GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8O({YS3VK`cTClC!DqcdYFJMFuDm5WNgE3p0)*MP9cYPsW#OHBl zcLNr47O^w1`_1oe_JiyXV~o4=u*aCq7_*=ua#Sh=-Ibx5Nk-&2Mm7l&8G!W>Oib*r z1Acp*MXb-FApidTNu15P-A`VtH#Rn#R?})*_uiAty_r9oXRbfJMe9n+Bq()1xQ?TI zY;T{-bmqrtG*tz07(vS2O`L`@cjY_{Gga&9fYq{EV|#D07!Hp+qBl4>U3SFcwBPNB zqr<^+*|K)_56&*f&&f+F-!z{b=vK02u!MI|%yM46X`0CN5jvx8r#bjBTx)Dr{5z$ycEZQ6MLU%)R@`N&@_ zA&VFw2L2fXyfN}dE-cENtv{BBXRUzt01XB63RFNqU%3Q;f&0j=3hKB(9r7H5g+?3& S{i+<0E&_@W>WG0~VBiZ3l1ViH literal 0 HcmV?d00001 diff --git a/test/unit/util/utils.test.js b/test/unit/util/utils.test.js index 066e03cd..d35e63de 100644 --- a/test/unit/util/utils.test.js +++ b/test/unit/util/utils.test.js @@ -13,7 +13,8 @@ import { isJson, toBigNumber, uint8ArrayToHex, - setPath + setPath, + getTransactionId } from '../../../src/util/utils'; describe('test utils', () => { @@ -32,9 +33,15 @@ describe('test utils', () => { test('decode and encode address hex represent', () => { /* eslint-disable max-len */ - expect(decodeAddressRep('rkws1GibTwWQnLyLvpRtnDQiZYf51tEqQDwpGaou5s4ZQvi1v')).toBe('70fb1d6779d84f718966eb0558619bd70a2b56fe8f74d60737d1efabb701c119'); - expect(encodeAddressRep('70fb1d6779d84f718966eb0558619bd70a2b56fe8f74d60737d1efabb701c119')).toBe('rkws1GibTwWQnLyLvpRtnDQiZYf51tEqQDwpGaou5s4ZQvi1v'); - expect(encodeAddressRep('0x70fb1d6779d84f718966eb0558619bd70a2b56fe8f74d60737d1efabb701c119')).toBe('rkws1GibTwWQnLyLvpRtnDQiZYf51tEqQDwpGaou5s4ZQvi1v'); + expect(decodeAddressRep('rkws1GibTwWQnLyLvpRtnDQiZYf51tEqQDwpGaou5s4ZQvi1v')).toBe( + '70fb1d6779d84f718966eb0558619bd70a2b56fe8f74d60737d1efabb701c119' + ); + expect(encodeAddressRep('70fb1d6779d84f718966eb0558619bd70a2b56fe8f74d60737d1efabb701c119')).toBe( + 'rkws1GibTwWQnLyLvpRtnDQiZYf51tEqQDwpGaou5s4ZQvi1v' + ); + expect(encodeAddressRep('0x70fb1d6779d84f718966eb0558619bd70a2b56fe8f74d60737d1efabb701c119')).toBe( + 'rkws1GibTwWQnLyLvpRtnDQiZYf51tEqQDwpGaou5s4ZQvi1v' + ); }); test('is bigNumBer', () => { @@ -87,7 +94,7 @@ describe('test utils', () => { test('transform into bigNumber', () => { expect(toBigNumber(1)).toStrictEqual(new BigNumber(1)); - expect(toBigNumber(('0x1'))).toStrictEqual(new BigNumber(1)); + expect(toBigNumber('0x1')).toStrictEqual(new BigNumber(1)); expect(toBigNumber(new BigNumber(1213))).toStrictEqual(new BigNumber(1213)); expect(toBigNumber(undefined)).toStrictEqual(new BigNumber(0)); }); @@ -124,4 +131,10 @@ describe('test utils', () => { } }); }); + test('test getTransactionId', () => { + const txId = getTransactionId( + '0a220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd512220a2089ac786c8ad3b56f63a6f2767369a5273f801de2415b613c783cad3d148ce3ab18d5d3bb35220491cf6ba12a18537761704578616374546f6b656e73466f72546f6b656e73325008c0f7f27110bbe5947c1a09534752544553542d311a03454c4622220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd52a08088996ceb0061000320631323334353682f10441ec6ad50c4b210976ba0ba5c287ab6fabd0c444839e2505ecb1b5f52838095b290cb245ec1c97dade3bde6ac14c6892e526569e9b71240d3c120b1a6c8e41afba00' + ); + expect(txId).toEqual('cf564f3169012cb173efcf5543b2a71b030b16fad3ddefe3e04a5c1e1bc0047d'); + }); });