Skip to content

Commit

Permalink
Add a BigInt package for browsers that do not support BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 authored and TrustHenry committed Jul 14, 2020
1 parent d863bab commit 487e088
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
28 changes: 22 additions & 6 deletions src/modules/data/Hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,27 +92,42 @@ 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;
lo = lo >> 8;
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;
Expand Down
4 changes: 3 additions & 1 deletion tests/Hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 487e088

Please sign in to comment.