From 487e08829d46bdef0a32fe49c1329143300dd06d Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Tue, 14 Jul 2020 14:42:21 +0900 Subject: [PATCH] Add a BigInt package for browsers that do not support BigInt --- package-lock.json | 5 +++++ package.json | 3 ++- src/index.ts | 4 ++++ src/modules/data/Hash.ts | 28 ++++++++++++++++++++++------ tests/Hash.test.ts | 4 +++- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 29a4bb511..ee2181369 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2750,6 +2750,11 @@ "esprima": "^4.0.0" } }, + "jsbi": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/jsbi/-/jsbi-3.1.3.tgz", + "integrity": "sha512-nBJqA0C6Qns+ZxurbEoIR56wyjiUszpNy70FHvxO5ervMoCbZVE3z3kxr5nKGhlxr/9MhKTSUBs7cAwwuf3g9w==" + }, "json-parse-better-errors": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz", diff --git a/package.json b/package.json index 018bb3649..df176512e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "boa-sdk-ts", - "version": "0.0.1", + "version": "0.0.2", "description": "The TypeScript BOA SDK libary", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -29,6 +29,7 @@ "@ctrl/ts-base32": "^1.2.1", "axios": "^0.19.2", "crc": "^3.8.0", + "jsbi": "^3.1.3", "lodash": "^4.17.19", "randombytes": "^2.1.0", "sodium-javascript": "^0.6.1", diff --git a/src/index.ts b/src/index.ts index 4ef303d53..4e7ba7d88 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,3 +21,7 @@ export { Signature } from './modules/data/Signature'; export { Validator } from './modules/data/Validator'; export { BOAClient } from './modules/net/BOAClient'; + +// For BinInt +import { default as JSBInt } from 'jsbi'; +export { JSBInt }; diff --git a/src/modules/data/Hash.ts b/src/modules/data/Hash.ts index 167f27e76..3960a9839 100644 --- a/src/modules/data/Hash.ts +++ b/src/modules/data/Hash.ts @@ -13,6 +13,7 @@ import * as sodium from 'sodium-javascript' import {readFromString, writeToString} from "../utils/buffer" +import JSBI from 'jsbi'; /** * The Class for creating hash @@ -91,18 +92,24 @@ export function hashMulti (source1: Buffer, source2: Buffer): Hash /** * Makes a UTXOKey - * @param h Hash of transaction - * @param index Index of the output + * @param h {Hash} Hash of transaction + * @param index {number | string} Index of the output * @returns Instance of Hash * See_Also https://github.com/bpfkorea/agora/blob/v0.x.x/source/agora/consensus/data/UTXOSetValue.d#L50-L53 */ -export function makeUTXOKey (h: Hash, index: bigint): Hash +export function makeUTXOKey (h: Hash, index: number | string | object): Hash { let buf = Buffer.alloc(8); // See https://github.com/nodejs/node/blob/ // 88fb5a5c7933022de750745e51e5dc0996a1e2c4/lib/internal/buffer.js#L573-L592 - let lo = Number(index & BigInt(0xffffffff)); + let lo = + JSBI.toNumber( + JSBI.bitwiseAnd( + JSBI.BigInt(index), + JSBI.BigInt(0xffffffff) + ) + ); buf[0] = lo; lo = lo >> 8; buf[1] = lo; @@ -110,8 +117,17 @@ export function makeUTXOKey (h: Hash, index: bigint): Hash buf[2] = lo; lo = lo >> 8; buf[3] = lo; - - let hi = Number(index >> BigInt(32) & BigInt(0xffffffff)); + + let hi = + JSBI.toNumber( + JSBI.bitwiseAnd( + JSBI.signedRightShift( + JSBI.BigInt(index), + JSBI.BigInt(32) + ), + JSBI.BigInt(0xffffffff) + ) + ); buf[4] = hi; hi = hi >> 8; buf[5] = hi; diff --git a/tests/Hash.test.ts b/tests/Hash.test.ts index 9c6412ab9..fa100e77c 100644 --- a/tests/Hash.test.ts +++ b/tests/Hash.test.ts @@ -13,6 +13,7 @@ import * as assert from 'assert'; import * as boasdk from '../lib'; +import { default as JSBI } from 'jsbi'; describe('Hash', () => { // Buffer has the same content. However, when printed with hex strings, @@ -61,7 +62,8 @@ describe('Hash', () => { let tx_hash = new boasdk.Hash(); tx_hash.fromString('0x5d7f6a7a30f7ff591c8649f61eb8a35d034824ed5cd252c2c6f10cdbd223671' + '3dc369ef2a44b62ba113814a9d819a276ff61582874c9aee9c98efa2aa1f10d73'); - let hash = boasdk.makeUTXOKey(tx_hash, BigInt(1)); + //let hash = boasdk.makeUTXOKey(tx_hash, JSBI.BigInt(1)); + let hash = boasdk.makeUTXOKey(tx_hash, boasdk.JSBInt.BigInt(1)); assert.equal(hash, '0x7c95c29b184e47fbd32e58e5abd42c6e22e8bd5a7e934ab049d21df545e09c2' + 'e33bb2b89df2e59ee01eb2519b1508284b577f66a76d42546b65a6813e592bb84');