Skip to content

Commit

Permalink
Merge pull request #175 from MickWang/master
Browse files Browse the repository at this point in the history
 fix I128FromBigInt, I128FromInt
  • Loading branch information
MickWang authored Oct 29, 2019
2 parents bfe583d + 7f9a243 commit f5661cd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<h1 align="center">Ontology TypeScript SDK </h1>
<h4 align="center">Version V1.0.26 </h4>
<h4 align="center">Version V1.1.0 </h4>

- [Overview](#overview)
- [Getting Started](#getting-started)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ontology-ts-sdk",
"version": "1.0.26",
"version": "1.1.0",
"description": "Comprehensive TypeScript library for the Ontology blockchain.",
"main": "./lib/index.js",
"types": "./lib/types/index.d.ts",
Expand Down
14 changes: 8 additions & 6 deletions src/common/bigInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { BigNumber } from 'bignumber.js';
import * as Long from 'long';
import { ERROR_CODE } from './../error';
import { bigIntToBytes, reverseHex } from './../utils';
import { bigIntFromBytes, bigIntToBytes } from './../utils';

const SIZE = 8;
/**
Expand All @@ -31,18 +31,20 @@ export default class BigInt {
* @param hex Byte string value
*/
static fromHexstr(hex: string): BigInt {
hex = reverseHex(hex);
const bi = new BigNumber(hex, 16).toString();
return new BigInt(bi);
// hex = reverseHex(hex);
// const bi = new BigNumber(hex, 16).toString();
// return new BigInt(bi);
const long = bigIntFromBytes(hex);
return new BigInt(long.toString());
}

value: string | number;
ledgerCompatible: boolean;

constructor(value: string | number, ledgerCompatible: boolean = true) {
const bi = new BigNumber(value);
if (!bi.isInteger() || bi.isNegative()) {
throw ERROR_CODE.INVALID_PARAMS;
if (!bi.isInteger()) {
throw new Error(String(ERROR_CODE.INVALID_PARAMS));
}
this.value = value;
this.ledgerCompatible = ledgerCompatible;
Expand Down
66 changes: 37 additions & 29 deletions src/common/int128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,6 @@ export class I128 {
return hex;
}

putInt(val: number) {
const value = new Array(I128_SIZE).fill(0);
value[0] = val & 0xFF;
val = val >> 8;
value[1] = val & 0xFF;
val = val >> 8;
value[2] = val & 0xFF;
val = val >> 8;
value[3] = val & 0xFF;
val = val >> 8;
value[4] = val & 0xFF;
val = val >> 8;
value[5] = val & 0xFF;
val = val >> 8;
value[6] = val & 0xFF;
val = val >> 8;
value[7] = val & 0xFF;
this.value = value;
}

}

// little endian u128
Expand Down Expand Up @@ -149,31 +129,41 @@ export function oneBits128() {
}

export function bigPow(a: number, b: number): BigNumber {
return new BigNumber(Math.pow(a, b).toString());
return new BigNumber(a).pow(b);
}

export const pow128 = bigPow(2, 128);

export const maxBigU128 = bigPow(2, 128).minus(1);

export const maxI128 = bigPow(2, 127).minus(1);

export const minI128 = bigPow(2, 127).negated();

export function I128FromInt(val: number) {
let i128 = new I128();
if (val < 0) {
return oneBits128();
i128 = oneBits128();
}
const i128 = new I128();
i128.putInt(val);
putUint64(i128.value, val);
return i128;
}

export function I128FromBigInt(val: BigInt) {
const valB = new BigNumber(val.value);
if (valB.isGreaterThan(maxI128) || valB.isLessThan(minI128)) {
export function I128FromBigInt(val: string) {
let valBN = new BigNumber(val);
if (valBN.isGreaterThan(maxI128) || valBN.isLessThan(minI128)) {
throw new Error('The value is out of I128 range');
}
const buf = val.toHexstr();
const bufRArray = hexstring2ab(buf);

if (valBN.isLessThan(0)) {
valBN = valBN.plus(pow128);
}
const size = I128_SIZE * 2;
let hexstring = valBN.toString(16);
hexstring = hexstring.length % size === 0 ? hexstring : ('0'.repeat(size) + hexstring).substring(hexstring.length);
hexstring = reverseHex(hexstring);
const bufRArray = hexstring2ab(hexstring);

const i128 = new I128();
const value = new Array(I128_SIZE).fill(0);
for (let i = 0; i < bufRArray.length; i++) {
Expand All @@ -182,3 +172,21 @@ export function I128FromBigInt(val: BigInt) {
i128.value = value;
return i128;
}

export function putUint64(value: number[], val: number) {
value[0] = val & 0xFF;
val = val >> 8;
value[1] = val & 0xFF;
val = val >> 8;
value[2] = val & 0xFF;
val = val >> 8;
value[3] = val & 0xFF;
val = val >> 8;
value[4] = val & 0xFF;
val = val >> 8;
value[5] = val & 0xFF;
val = val >> 8;
value[6] = val & 0xFF;
val = val >> 8;
value[7] = val & 0xFF;
}
4 changes: 2 additions & 2 deletions src/transaction/scriptBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { pushHexString } from './program';
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
Expand Down Expand Up @@ -30,6 +29,7 @@ import {
ab2hexstring, bigIntFromBytes, hexstr2str, isHexString, num2hexstring, num2VarInt, str2hexstr, StringReader
} from '../utils';
import opcode from './opcode';
import { pushHexString } from './program';

export const pushBool = (param: boolean) => {
let result = '';
Expand Down Expand Up @@ -357,7 +357,7 @@ export function buildWasmContractParam(params: Parameter[]): string {
result += I128FromInt(p.value).serialize();
break;
case ParameterType.Long:
result += I128FromBigInt(new BigInt(p.value)).serialize();
result += I128FromBigInt(p.value).serialize();
break;
case ParameterType.ByteArray:
result += writeVarBytes(p.value);
Expand Down
11 changes: 6 additions & 5 deletions test/governanceAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('test governance authorization', () => {
});

test('getAttributes', async () => {
const pk = '03cb9417260995baf9781a58cb63db7e8bab2f8fdac193c27477e8e1b9eadecfae';
const pk = '032f6464df7c42b5a80953680165a23cb98453a1fcb5770f233664909847faf36f';
const url = 'http://dappnode1.ont.io:20334';
const res = await getAttributes(pk, url);
console.log(res);
Expand All @@ -155,16 +155,17 @@ describe('test governance authorization', () => {
}, 10000);

test('getPeerPoolMap', async () => {
const res = await getPeerPoolMap();
const pk = stake1.peerPubkey;
const nodeUrl = 'http://dappnode1.ont.io:20334';
const res = await getPeerPoolMap(nodeUrl);
const pk = '032f6464df7c42b5a80953680165a23cb98453a1fcb5770f233664909847faf36f';
console.log(res[pk]);
}, 10000);

test('getAuthorizeInfo', async () => {
// const pk = stake2.peerPubkey;
const pk = '02e172320fd25ffffc25a3580cd3dc8b3be921ae5b8973530f939acfdd3c250ca3';
const pk = '032f6464df7c42b5a80953680165a23cb98453a1fcb5770f233664909847faf36f';
// const userAddr = new Address(account4.address);
const userAddr = new Address('Ady2XfVZGPp1K2ZuTtx6GJmPADxkChyUDh');
const userAddr = new Address('ALaDrS5ZwMKZgTS3a8okgDDz84k3ttfP4x');
const nodeUrl = 'http://dappnode1.ont.io:20334';
const res = await getAuthorizeInfo(pk, userAddr, nodeUrl);
console.log(res);
Expand Down
45 changes: 40 additions & 5 deletions test/wasm/wasmContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../../src/transaction/transactionBuilder';
import { hexstr2str } from '../../src/utils';
import { Account } from './../../src/account';
import { I128, I128FromBigInt, I128FromInt } from './../../src/common/int128';
import { I128, I128FromBigInt, I128FromInt, maxBigU128, maxI128, minI128 } from './../../src/common/int128';
import { Address } from './../../src/crypto/address';
import { WebsocketClient } from './../../src/network/websocket/websocketClient';
import { Parameter, ParameterType } from './../../src/smartcontract/abi/parameter';
Expand All @@ -22,8 +22,8 @@ describe('test deploy contract', () => {
console.log(account.address.toBase58());

const contractCode = fs.readFileSync(__dirname + '/helloworld.wasm').toString('hex');
const restClient = new RestClient('http://127.0.0.1:20334');
const wsClient = new WebsocketClient('http://127.0.0.1:20335');
const restClient = new RestClient();
const wsClient = new WebsocketClient();

test('deployWasmContract', async () => {

Expand Down Expand Up @@ -52,7 +52,9 @@ describe('test deploy contract', () => {
test('invokeAdd', async () => {
const contractAddress = new Address('5daf0ec53b21abfab6459c7ba7f760c376e18ebf');
const params = [
new Parameter('param1', ParameterType.Integer, 1),
// new Parameter('param1', ParameterType.Long, '-1'),
// new Parameter('param2', ParameterType.Long, '2')
new Parameter('param1', ParameterType.Integer, -1),
new Parameter('param2', ParameterType.Integer, 2)
];
const tx = makeWasmVmInvokeTransaction('add', params, contractAddress, '500', '20000', account.address);
Expand Down Expand Up @@ -174,8 +176,41 @@ describe('test deploy contract', () => {
expect(res.Result).toBeTruthy();
});
test('i128', async () => {
const i128 = I128FromBigInt(new BigInt('9007199254740993'));
const i128 = I128FromBigInt('9007199254740993');
console.log(i128.serialize());
expect(i128.serialize()).toEqual('01000000000020000000000000000000');
});

test('max128', () => {
console.log(maxI128.toString(16));
});

test('transformNumber', () => {
const data = {
'minI128': '00000000000000000000000000000080',
'maxI128': 'ffffffffffffffffffffffffffffff7f',
'-2': 'feffffffffffffffffffffffffffffff',
'-1': 'ffffffffffffffffffffffffffffffff',
'0': '00000000000000000000000000000000',
'1': '01000000000000000000000000000000',
'2': '02000000000000000000000000000000'
};
const bmin = minI128.toString();
const bminI128 = I128FromBigInt(bmin).serialize();
expect(bminI128).toEqual(data.minI128);
const bmax = maxI128.toString();
const bmaxI128 = I128FromBigInt(bmax).serialize();
expect(bmaxI128).toEqual(data.maxI128);
expect(I128FromInt(-2).serialize()).toEqual(data['-2']);
expect(I128FromInt(-1).serialize()).toEqual(data['-1']);
expect(I128FromInt(0).serialize()).toEqual(data['0']);
expect(I128FromInt(1).serialize()).toEqual(data['1']);
expect(I128FromInt(2).serialize()).toEqual(data['2']);
console.log(I128FromInt(-2).serialize());
console.log(I128FromInt(-1).serialize());
console.log(I128FromInt(0).serialize());
console.log(I128FromInt(1).serialize());
console.log(I128FromInt(2).serialize());

});
});

0 comments on commit f5661cd

Please sign in to comment.