Skip to content

Commit

Permalink
Merge pull request #148 from AElfProject/feature/get-transactionId
Browse files Browse the repository at this point in the history
feat: 🎸 get transaction id
  • Loading branch information
hzz780 committed Apr 8, 2024
2 parents f8f9b7a + ae0c787 commit e9297f5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 38 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
61 changes: 31 additions & 30 deletions src/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
Expand Down
Binary file added test/.DS_Store
Binary file not shown.
23 changes: 18 additions & 5 deletions test/unit/util/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
isJson,
toBigNumber,
uint8ArrayToHex,
setPath
setPath,
getTransactionId
} from '../../../src/util/utils';

describe('test utils', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -124,4 +131,10 @@ describe('test utils', () => {
}
});
});
test('test getTransactionId', () => {
const txId = getTransactionId(
'0a220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd512220a2089ac786c8ad3b56f63a6f2767369a5273f801de2415b613c783cad3d148ce3ab18d5d3bb35220491cf6ba12a18537761704578616374546f6b656e73466f72546f6b656e73325008c0f7f27110bbe5947c1a09534752544553542d311a03454c4622220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd52a08088996ceb0061000320631323334353682f10441ec6ad50c4b210976ba0ba5c287ab6fabd0c444839e2505ecb1b5f52838095b290cb245ec1c97dade3bde6ac14c6892e526569e9b71240d3c120b1a6c8e41afba00'
);
expect(txId).toEqual('cf564f3169012cb173efcf5543b2a71b030b16fad3ddefe3e04a5c1e1bc0047d');
});
});

0 comments on commit e9297f5

Please sign in to comment.