Skip to content

Commit

Permalink
refactor(crypto): remove long dependency (#3502)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Feb 17, 2020
1 parent 3ddb098 commit bf16abd
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ByteBuffer from "bytebuffer";
import { Wallets } from "@arkecosystem/core-state";
import { Handlers } from "@arkecosystem/core-transactions";
import { Constants, Crypto, Enums, Identities, Interfaces, Managers, Transactions, Utils } from "@arkecosystem/crypto";
import Long from "long";
import { Connection } from "../../../packages/core-transaction-pool/src/connection";
import { defaults } from "../../../packages/core-transaction-pool/src/defaults";
import { Memory } from "../../../packages/core-transaction-pool/src/memory";
Expand Down Expand Up @@ -115,7 +114,8 @@ describe("Connection", () => {
const writeUint32 = (txField, value) =>
options[txField] ? options[txField](buffer) : buffer.writeUint32(value);
const writeUint64 = (txField, value) =>
options[txField] ? options[txField](buffer) : buffer.writeUint64(Long.fromString(value.toFixed()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
options[txField] ? options[txField](buffer) : buffer.writeUint64(value.toFixed());
const append = (txField, value, encoding = "utf8") =>
options[txField] ? options[txField](buffer) : buffer.append(value, encoding);

Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-transactions/handler-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Registry, TransactionHandler } from "../../../packages/core-transaction
import { TransactionHandlerConstructor } from "../../../packages/core-transactions/src/handlers/transaction";
import { TransferTransactionHandler } from "../../../packages/core-transactions/src/handlers/transfer";

import Long from "long";
import { DeactivatedTransactionHandlerError } from "../../../packages/core-transactions/src/errors";
import { testnet } from "../../../packages/crypto/src/networks";

Expand Down Expand Up @@ -45,7 +44,8 @@ class TestTransaction extends Transactions.Transaction {
public serialize(): ByteBuffer {
const { data } = this;
const buffer = new ByteBuffer(24, true);
buffer.writeUint64(Long.fromString(data.amount.toFixed()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(data.amount.toFixed());
buffer.writeUint32(data.expiration || 0);
buffer.append(Utils.Base58.decodeCheck(data.recipientId));
buffer.writeInt32(data.asset.test);
Expand Down
9 changes: 5 additions & 4 deletions __tests__/unit/crypto/transactions/deserializer.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "jest-extended";

import ByteBuffer from "bytebuffer";
import Long from "long";
import { Enums, Errors, Utils } from "../../../../packages/crypto/src";
import { Hash } from "../../../../packages/crypto/src/crypto";
import {
Expand All @@ -17,8 +16,8 @@ import { TransactionFactory, Utils as TransactionUtils, Verifier } from "../../.
import { BuilderFactory } from "../../../../packages/crypto/src/transactions/builders";
import { Deserializer } from "../../../../packages/crypto/src/transactions/deserializer";
import { Serializer } from "../../../../packages/crypto/src/transactions/serializer";
import { htlcSecretHashHex, htlcSecretHex } from "../../../utils/fixtures";
import { legacyMultiSignatureRegistration } from "./__fixtures__/transaction";
import { htlcSecretHex, htlcSecretHashHex } from "../../../utils/fixtures";

configManager.setHeight(2); // aip11 (v2 transactions) is true from height 2 on testnet

Expand Down Expand Up @@ -501,9 +500,11 @@ describe("Transaction serializer / Deserializer", () => {
buffer.writeByte(transaction.network);
buffer.writeUint32(Enums.TransactionTypeGroup.Core);
buffer.writeUint16(transaction.type);
buffer.writeUint64(Long.fromString(transaction.nonce.toFixed()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(transaction.nonce.toString());
buffer.append(transaction.senderPublicKey, "hex");
buffer.writeUint64(Long.fromString(Utils.BigNumber.make(transaction.fee).toFixed()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(Utils.BigNumber.make(transaction.fee).toString());
buffer.writeByte(0x00);

return Buffer.from(buffer.flip().toBuffer());
Expand Down
3 changes: 1 addition & 2 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"@arkecosystem/utils": "0.8.3",
"@types/bytebuffer": "^5.0.40",
"ajv": "^6.10.2",
"ajv-keywords": "^3.4.1",
"bcrypto": "^4.1.0",
Expand All @@ -44,15 +45,13 @@
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"lodash.sumby": "^4.6.0",
"long": "~3",
"tiny-glob": "^0.2.6",
"wif": "^2.0.6"
},
"devDependencies": {
"@types/bip32": "^1.0.2",
"@types/bip39": "^2.4.2",
"@types/buffer-xor": "^2.0.0",
"@types/bytebuffer": "^5.0.40",
"@types/lodash.get": "^4.4.6",
"@types/lodash.set": "^4.3.6",
"@types/lodash.sumby": "^4.6.6",
Expand Down
10 changes: 6 additions & 4 deletions packages/crypto/src/blocks/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from "assert";
import ByteBuffer from "bytebuffer";
import Long from "long";
import { PreviousBlockIdFormatError } from "../errors";
import { IBlock, IBlockData, ITransactionData } from "../interfaces";
import { configManager } from "../managers/config";
Expand Down Expand Up @@ -83,9 +82,12 @@ export class Serializer {
buffer.writeUint32(block.height);
buffer.append(block.previousBlockHex, "hex");
buffer.writeUint32(block.numberOfTransactions);
buffer.writeUint64(Long.fromString(block.totalAmount.toString()));
buffer.writeUint64(Long.fromString(block.totalFee.toString()));
buffer.writeUint64(Long.fromString(block.reward.toString()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(block.totalAmount.toString());
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(block.totalFee.toString());
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(block.reward.toString());
buffer.writeUint32(block.payloadLength);
buffer.append(block.payloadHash, "hex");
buffer.append(block.generatorPublicKey, "hex");
Expand Down
13 changes: 8 additions & 5 deletions packages/crypto/src/transactions/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* tslint:disable:no-shadowed-variable */
import ByteBuffer from "bytebuffer";
import Long from "long";
import { Utils } from "..";
import { TransactionType, TransactionTypeGroup } from "../enums";
import { TransactionVersionError } from "../errors";
Expand Down Expand Up @@ -149,8 +148,10 @@ export class Serializer {
}
}

bb.writeInt64(Long.fromString(transaction.amount.toString()));
bb.writeInt64(Long.fromString(transaction.fee.toString()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
bb.writeInt64(transaction.amount.toString());
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
bb.writeInt64(transaction.fee.toString());

if (assetSize > 0) {
for (let i = 0; i < assetSize; i++) {
Expand Down Expand Up @@ -199,11 +200,13 @@ export class Serializer {
} else {
buffer.writeUint32(transaction.typeGroup);
buffer.writeUint16(transaction.type);
buffer.writeUint64(Long.fromString(transaction.nonce.toString()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(transaction.nonce.toString());
}

buffer.append(transaction.senderPublicKey, "hex");
buffer.writeUint64(Long.fromString(transaction.fee.toString()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(transaction.fee.toString());
}

private static serializeVendorField(transaction: ITransaction, buffer: ByteBuffer): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/transactions/types/htlc-lock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ByteBuffer from "bytebuffer";
import Long from "long";
import { TransactionType, TransactionTypeGroup } from "../../enums";
import { Address } from "../../identities";
import { ISerializeOptions } from "../../interfaces";
Expand Down Expand Up @@ -33,7 +32,8 @@ export class HtlcLockTransaction extends Transaction {

const buffer: ByteBuffer = new ByteBuffer(8 + 32 + 1 + 4 + 21, true);

buffer.writeUint64(Long.fromString(data.amount.toString()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(data.amount.toString());
buffer.append(Buffer.from(data.asset.lock.secretHash, "hex"));
buffer.writeUint8(data.asset.lock.expiration.type);
buffer.writeUint32(data.asset.lock.expiration.value);
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/transactions/types/multi-payment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ByteBuffer from "bytebuffer";
import Long from "long";
import { TransactionType, TransactionTypeGroup } from "../../enums";
import { Address } from "../../identities";
import { IMultiPaymentItem, ISerializeOptions } from "../../interfaces";
Expand Down Expand Up @@ -34,7 +33,8 @@ export class MultiPaymentTransaction extends Transaction {
buffer.writeUint16(data.asset.payments.length);

for (const payment of data.asset.payments) {
buffer.writeUint64(Long.fromString(payment.amount.toString()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(payment.amount.toString());

const { addressBuffer, addressError } = Address.toBuffer(payment.recipientId);
options.addressError = addressError || options.addressError;
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/transactions/types/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ByteBuffer from "bytebuffer";
import Long from "long";
import { TransactionType, TransactionTypeGroup } from "../../enums";
import { Address } from "../../identities";
import { ISerializeOptions } from "../../interfaces";
Expand All @@ -25,7 +24,8 @@ export class TransferTransaction extends Transaction {
public serialize(options?: ISerializeOptions): ByteBuffer {
const { data } = this;
const buffer: ByteBuffer = new ByteBuffer(24, true);
buffer.writeUint64(Long.fromString(data.amount.toString()));
// @ts-ignore - The ByteBuffer types say we can't use strings but the code actually handles them.
buffer.writeUint64(data.amount.toString());
buffer.writeUint32(data.expiration || 0);

const { addressBuffer, addressError } = Address.toBuffer(data.recipientId);
Expand Down

0 comments on commit bf16abd

Please sign in to comment.