Skip to content

Commit fd0160e

Browse files
authored
Merge pull request #77 from virtualeconomy/develop
release: v0.2.2
2 parents 8bdac02 + aeef978 commit fd0160e

File tree

11 files changed

+107
-64
lines changed

11 files changed

+107
-64
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@virtualeconomy/js-vsys",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "The official JavaScript SDK for VSYS APIs",
55
"keywords": [
66
"vsys",

spec/model_spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* modelSpec tests module model.
3+
* @module modelSpec
4+
*/
5+
6+
'use strict';
7+
8+
import * as jv from '../src/index.js';
9+
10+
describe('Test class Seed', function () {
11+
describe('Test method encrypt', function () {
12+
it('should properly encrypt', function () {
13+
const key = 'abc';
14+
const rounds = 5000;
15+
16+
const seed = new jv.Seed(this.SEED);
17+
const enc = seed.encrypt(key, rounds);
18+
19+
const dec = enc.decrypt(key, rounds);
20+
expect(dec.data).toEqual(seed.data);
21+
});
22+
});
23+
});
24+
25+
describe('Test class EncryptedSeed', function () {
26+
describe('Test method decrypt', function () {
27+
it('should properly decrypt', function () {
28+
const key = 'abc';
29+
const rounds = 5000;
30+
31+
const seed = new jv.Seed(this.SEED);
32+
const enc = seed.encrypt(key, rounds);
33+
34+
const dec = enc.decrypt(key, rounds);
35+
expect(dec.data).toEqual(seed.data);
36+
});
37+
});
38+
});

src/account.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ export class Account {
206206
/**
207207
* lease leases the VSYS coins from the action taker to the recipient(a supernode).
208208
* @param {string} supernodeAddr - The supernode address.
209-
* @param {md.VSYS} amount - The lease amount.
210-
* @param {md.Fee} fee - The lease fee. DEFAULT to be md.Fee.DEFAULT.
209+
* @param {number} amount - The lease amount.
210+
* @param {number} fee - The lease fee. DEFAULT to be md.Fee.DEFAULT.
211211
* @returns {object} The response returned by the NodeAPI.
212212
*/
213213
async lease(supernodeAddr, amount, fee = md.LeasingFee.DEFAULT) {
@@ -237,7 +237,8 @@ export class Account {
237237

238238
/**
239239
* leaseCancel sends a leasing cancel transaction request on behalf of the account.
240-
* @param {md.Addr} leasingTxId - The leasing Transaction id.
240+
* @param {string} leasingTxId - The leasing Transaction id.
241+
* @param {number} fee - The leasing cancel fee. DEFAULT to be md.Fee.DEFAULT.
241242
* @returns {object} The response returned by the NodeAPI.
242243
*/
243244
async leaseCancel(leasingTxId, fee = md.Fee.DEFAULT) {

src/contract/pay_chan_ctrt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ export class PayChanCtrt extends ctrt.Ctrt {
566566
const msg = Buffer.concat([
567567
bp.packUInt16(chanIdBytes.length),
568568
chanIdBytes,
569-
bp.packUInt64(bn.toBigInt(rawAmount)),
569+
bp.packUInt64(rawAmount),
570570
]);
571571

572572
return msg;

src/data_entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class Long extends DataEntry {
164164
* @returns {Buffer} The bytes representation of this data entry.
165165
*/
166166
get bytes() {
167-
return bp.packUInt64(this.data.bigInt);
167+
return bp.packUInt64(this.data.data);
168168
}
169169

170170
/**

src/model.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as bn from './utils/big_number.js';
1212
import * as bp from './utils/bytes_packer.js';
1313
import * as hs from './utils/hashes.js';
1414
import * as curve from './utils/curve_25519.js';
15+
import * as crypto from './utils/crypto.js';
1516
import { WORDS_SET } from './words.js';
1617

1718
/** Model is the base class for data models */
@@ -239,7 +240,7 @@ export class Seed extends Str {
239240
throw new Error('Password is required');
240241
}
241242
password = this.constructor.strengthenPassword(password, encryptionRounds);
242-
return new EncryptedSeed(hs.aesEncrypt(this.data, password));
243+
return new EncryptedSeed(crypto.aesEncrypt(this.data, password));
243244
}
244245

245246
/**
@@ -276,7 +277,7 @@ export class EncryptedSeed extends Str {
276277
throw new Error('Password is required');
277278
}
278279
password = Seed.strengthenPassword(password, encryptionRounds);
279-
return new Seed(hs.aesDecrypt(this.data, password));
280+
return new Seed(crypto.aesDecrypt(this.data, password));
280281
}
281282
}
282283

@@ -826,14 +827,6 @@ export class Long extends Model {
826827
super(data);
827828
}
828829

829-
/**
830-
* bigInt returns the containing data in JavaScript built-in BigInt.
831-
* @returns {BigInt}
832-
*/
833-
get bigInt() {
834-
return bn.toBigInt(this.data);
835-
}
836-
837830
/**
838831
* validate validates the instance.
839832
*/

src/tx_req.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ export class PaymentTxReq extends TxReq {
9595
const cls = this.constructor;
9696
return Buffer.concat([
9797
cls.TX_TYPE.serialize(),
98-
bp.packUInt64(this.timestamp.bigInt),
99-
bp.packUInt64(this.amount.bigInt),
100-
bp.packUInt64(this.fee.bigInt),
98+
bp.packUInt64(this.timestamp.data),
99+
bp.packUInt64(this.amount.data),
100+
bp.packUInt64(this.fee.data),
101101
bp.packUInt16(cls.FEE_SCALE),
102102
this.recipient.bytes,
103103
bp.packUInt16(this.attachment.data.length),
@@ -162,9 +162,9 @@ export class RegCtrtTxReq extends TxReq {
162162
dataStack,
163163
bp.packUInt16(this.description.data.length),
164164
this.description.bytes,
165-
bp.packUInt64(this.fee.bigInt),
165+
bp.packUInt64(this.fee.data),
166166
bp.packUInt16(cls.FEE_SCALE),
167-
bp.packUInt64(this.timestamp.bigInt),
167+
bp.packUInt64(this.timestamp.data),
168168
]);
169169
}
170170

@@ -226,9 +226,9 @@ export class ExecCtrtFuncTxReq extends TxReq {
226226
dataStack,
227227
bp.packUInt16(this.attachment.data.length),
228228
this.attachment.bytes,
229-
bp.packUInt64(this.fee.bigInt),
229+
bp.packUInt64(this.fee.data),
230230
bp.packUInt16(cls.FEE_SCALE),
231-
bp.packUInt64(this.timestamp.bigInt),
231+
bp.packUInt64(this.timestamp.data),
232232
]);
233233
}
234234

@@ -281,10 +281,10 @@ export class LeaseTxReq extends TxReq {
281281
return Buffer.concat([
282282
cls.TX_TYPE.serialize(),
283283
this.supernodeAddr.bytes,
284-
bp.packUInt64(this.amount.bigInt),
285-
bp.packUInt64(this.fee.bigInt),
284+
bp.packUInt64(this.amount.data),
285+
bp.packUInt64(this.fee.data),
286286
bp.packUInt16(cls.FEE_SCALE),
287-
bp.packUInt64(this.timestamp.bigInt),
287+
bp.packUInt64(this.timestamp.data),
288288
]);
289289
}
290290

@@ -332,9 +332,9 @@ export class LeaseCancelReq extends TxReq {
332332

333333
return Buffer.concat([
334334
cls.TX_TYPE.serialize(),
335-
bp.packUInt64(this.fee.bigInt),
335+
bp.packUInt64(this.fee.data),
336336
bp.packUInt16(cls.FEE_SCALE),
337-
bp.packUInt64(this.timestamp.bigInt),
337+
bp.packUInt64(this.timestamp.data),
338338
this.leasingTxId.bytes,
339339
]);
340340
}

src/utils/big_number.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,3 @@
77

88
import jsBNPkg from 'bignumber.js';
99
export const { BigNumber } = jsBNPkg;
10-
11-
/**
12-
* toBigInt converts BigNumber to JS built-in BigInt
13-
* @param {BigNumber} bn - The BigNumber instance.
14-
* @returns {BigInt} The BigInt instance.
15-
*/
16-
export function toBigInt(bn) {
17-
return BigInt('0x' + bn.toString(16));
18-
}

src/utils/bytes_packer.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import { Buffer } from 'buffer';
99

10+
import jsBNPkg from 'bignumber.js';
11+
export const { BigNumber } = jsBNPkg;
12+
1013
/**
1114
* packUInt8 packs the given integer into 1 byte
1215
* @param {number} val - The value to pack.
@@ -53,13 +56,21 @@ export function packUInt32(val) {
5356

5457
/**
5558
* packUInt64 packs the given integer into 8 bytes in Big Endian order.
56-
* @param {BigInt} val - The value to pack.
59+
* @param {BigNumber} val - The value to pack.
5760
* @returns {Buffer} The packing result.
5861
*/
5962
export function packUInt64(val) {
60-
const buf = Buffer.alloc(8);
61-
buf.writeBigUInt64BE(val);
62-
return buf;
63+
let s = val.toString(16);
64+
if (s.length < 16) {
65+
let diff = 16 - s.length;
66+
let prefix = '';
67+
while (diff--) {
68+
prefix = prefix + '0';
69+
}
70+
s = prefix + s;
71+
}
72+
73+
return Buffer.from(s, 'hex');
6374
}
6475

6576
/**
@@ -101,10 +112,11 @@ export function unpackUInt32(buf) {
101112
/**
102113
* unpackUInt64 unpacks the given 8-byte Buffer to an integer in Big Endian order.
103114
* @param {Buffer} buf - The 8-byte buffer to unpack.
104-
* @returns {BigInt} The unpacking result.
115+
* @returns {BigNumber} The unpacking result.
105116
*/
106117
export function unpackUInt64(buf) {
107-
return buf.readUInt64BE(0);
118+
const s = buf.toString('hex');
119+
return new BigNumber(s, 16);
108120
}
109121

110122
/**

src/utils/crypto.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* module utils/crypto provides functionalities for cryptograhphy.
3+
* @module utils/crypto
4+
*/
5+
6+
'use strict';
7+
8+
import encUtf8 from "crypto-js/enc-utf8.js"
9+
import AES from "crypto-js/aes.js";
10+
11+
/**
12+
* aesEncrypt encrypts the given data in AES.
13+
* @param {string} data - The data to encrypt.
14+
* @param {string} key - The key used to encrypt data.
15+
* @returns {string} The encryption result
16+
*/
17+
export function aesEncrypt(data, key) {
18+
return AES.encrypt(data, key).toString();
19+
}
20+
21+
/**
22+
* aesDecrypt decrypts the given data encrypted by AES.
23+
* @param {string} data - The data to decrypt.
24+
* @param {string} key - The key used to decrypt data.
25+
* @returns {string} The decryption result.
26+
*/
27+
export function aesDecrypt(data, key) {
28+
return AES.decrypt(data, key).toString(encUtf8);
29+
}

0 commit comments

Comments
 (0)