Skip to content

Commit

Permalink
v1.1.0-alpha.4
Browse files Browse the repository at this point in the history
  • Loading branch information
zinoadidi authored Apr 29, 2021
2 parents 6f6d35b + 55e5b4f commit 43e5798
Show file tree
Hide file tree
Showing 37 changed files with 882 additions and 52 deletions.
38 changes: 38 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Pull Request
about: ''
title: 'feat: description of fix'
labels: ''
assignees: ''

---


# Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

## Type of change

- [ ] Chore (chore: description-of-task)
- [ ] Fix (fix: description-of-fix)
- [ ] Feature (feat: description-of-fix)
- [ ] Docs (docs: description-of-docs-added-or-modified)
- [ ] Test (test: description-of-tests-performed)
- [ ] Refactor (refactor: description-of-refactor)

Please specify the type of change in your title following the above format


# Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [1.1.0-alpha.4](https://github.com/thenewboston-developers/thenewboston-js/compare/v1.1.0-alpha.3...v1.1.0-alpha.4) (2021-04-29)


### Features

* Replaced type with enum for types with multiple values ([#157](https://github.com/thenewboston-developers/thenewboston-js/issues/157)) ([cab172b](https://github.com/thenewboston-developers/thenewboston-js/commit/cab172be7a8d87713fa88ac601e916f99715c71d))


### Bug Fixes

* `Account.isValidPair` throwing on invalid seeds ([#152](https://github.com/thenewboston-developers/thenewboston-js/issues/152)) ([1a18fad](https://github.com/thenewboston-developers/thenewboston-js/commit/1a18fad5e143658e66b2beb01829fbd1c99780d1)), closes [#151](https://github.com/thenewboston-developers/thenewboston-js/issues/151)

## [1.1.0-alpha.3](https://github.com/thenewboston-developers/thenewboston-js/compare/v1.1.0-alpha.2...v1.1.0-alpha.3) (2021-04-12)


Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ module.exports = {
coverageProvider: "v8",
// The test environment that will be used for testing
testEnvironment: "node",

transformIgnorePatterns: ["node_modules/nock/"],
};
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "thenewboston",
"version": "1.1.0-alpha.3",
"version": "1.1.0-alpha.4",
"description": "JavaScript library for thenewboston.",
"author": {
"name": "thenewboston-developers",
Expand Down
6 changes: 5 additions & 1 deletion src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export class Account {
* @param accountNumber the given account number hex string
*/
static isValidPair(signingKey: string, accountNumber: string) {
return new Account(signingKey).accountNumberHex === accountNumber;
try {
return new Account(signingKey).accountNumberHex === accountNumber;
} catch (_) {
return false;
}
}

/** The 32 byte account number as a 32 byte hex string. */
Expand Down
14 changes: 11 additions & 3 deletions src/confirmation-validator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { Validator } from "./validator";
import type { Account } from "./account";
import type { ConfirmationValidatorConfigResponse, CleanResponse, CrawlResponse, CleanData, CrawlData } from "./models";
import type {
BankConfirmationServices,
ConfirmationValidatorConfigResponse,
CleanResponse,
CrawlResponse,
CleanData,
CrawlData,
PaginationOptions,
} from "./models";

/** Used for connecting with and using confirmation validator server nodes. */
export class ConfirmationValidator extends Validator {
/** Gets the current confirmation confirmation validator's listed services. */
async getBankConfirmationServices() {
return await super.getData("/bank_confirmation_services");
async getBankConfirmationServices(options: Partial<PaginationOptions> = {}) {
return await super.getPaginatedData("/bank_confirmation_services", options);
}

/** Gets the current crawl status */
Expand Down
2 changes: 1 addition & 1 deletion src/models/responses/bank/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export interface BankConfigResponse {
protocol: Protocol;
version: Version;
default_transaction_fee: number;
node_type: NodeType;
node_type: NodeType.bank;
}
2 changes: 1 addition & 1 deletion src/models/responses/confirmation-validator/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export interface ConfirmationValidatorConfigResponse {
root_account_file_hash: Hex;
seed_block_identifier: Hex;
daily_confirmation_rate: number;
node_type: NodeType;
node_type: NodeType.confirmationValidator;
}
6 changes: 5 additions & 1 deletion src/models/responses/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export type NodeType = "BANK" | "CONFIRMATION_VALIDATOR" | "PRIMARY_VALIDATOR";
export enum NodeType {
bank = "BANK",
confirmationValidator = "CONFIRMATION_VALIDATOR",
primaryValidator = "PRIMARY_VALIDATOR",
}
export type Version = "v1.0";
export type Protocol = "http" | "https";
export type Port = number;
Expand Down
8 changes: 7 additions & 1 deletion src/models/responses/generic/clean.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { Origin, Port, Protocol } from "../constants";

export enum CleanStatus {
cleaning = "cleaning",
notCleaning = "not_cleaning",
stopRequested = "stop_requested",
}

/** The response model for a crawl request. */
export interface CleanResponse {
clean_last_completed: string;
clean_status: string;
clean_status: CleanStatus | null;
ip_address: Origin;
port: Port;
protocol: Protocol;
Expand Down
17 changes: 17 additions & 0 deletions src/models/responses/generic/confirmation-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BlockMessage } from "../../block-message";

export interface UpdatedBalance {
account_number: string;
balance: number;
balance_lock?: string;
}

export interface ConfirmationBlock {
message: {
block: BlockMessage;
block_identifier: string;
updated_balances: UpdatedBalance[];
};
node_identifier: string;
signature: string;
}
8 changes: 7 additions & 1 deletion src/models/responses/generic/crawl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { Origin, Port, Protocol } from "../constants";

export enum CrawlStatus {
crawling = "crawling",
notCrawling = "not_crawling",
stopRequested = "stop_requested",
}

/** The response model for a crawl request. */
export interface CrawlResponse {
crawl_last_completed: string;
crawl_status: string;
crawl_status: CrawlStatus | null;
ip_address: Origin;
port: Port;
protocol: Protocol;
Expand Down
1 change: 1 addition & 0 deletions src/models/responses/generic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export type { AccountBalanceLockResponse } from "./account-balance-lock";
export type { AccountBalanceResponse } from "./account-balance";
export type { CleanResponse } from "./clean";
export type { CrawlResponse } from "./crawl";
export type { ConfirmationBlock, UpdatedBalance } from "./confirmation-block";
11 changes: 11 additions & 0 deletions src/models/responses/pagination/entries/confirmation-services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PaginatedEntry } from "../";

export interface ConfirmationServices extends PaginatedEntry {
end: string;
start: string;
bank: string;
validator: string;
}

export type BankConfirmationServices = Omit<ConfirmationServices, "validator">;
export type ValidatorConfirmationServices = Omit<ConfirmationServices, "bank">;
1 change: 1 addition & 0 deletions src/models/responses/pagination/entries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export type { PaginatedBankEntry } from "./bank";
export type { PaginatedBlockEntry } from "./block";
export type { PaginatedValidatorEntry } from "./validator";
export type { PaginatedTransactionEntry } from "./transaction";
export type { BankConfirmationServices, ValidatorConfirmationServices } from "./confirmation-services";
4 changes: 2 additions & 2 deletions src/models/responses/primary-validator/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Hex, Origin, Port, Protocol, Url, Trust, Version, NodeType } from "../constants";
import type { Hex, Origin, Port, Protocol, Url, Version, NodeType } from "../constants";

export interface PrimaryValidatorConfigResponse {
primary_validator: null;
Expand All @@ -13,5 +13,5 @@ export interface PrimaryValidatorConfigResponse {
root_account_file_hash: Hex;
seed_block_identifier: Hex;
daily_confirmation_rate: number;
node_type: NodeType;
node_type: NodeType.primaryValidator;
}
2 changes: 1 addition & 1 deletion src/payment-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class PaymentHandler {
async createTransaction(sender: Account, txs: Transaction[]) {
txs = txs.map((tx) => {
if (tx.memo) {
tx.memo?.trim();
tx.memo = tx.memo.trim();
if (!/^[a-zA-Z0-9_ ]*$/.test(tx.memo))
throwError("Invalid memo", "Memo can only contain alphanumeric characters, spaces, and underscores");
if (tx.memo.length > 64) throwError("Invalid memo", "Memo cannot exceed 64 characters");
Expand Down
25 changes: 13 additions & 12 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
PaginatedValidatorEntry,
PaginatedTransactionEntry,
PaginationOptions,
ConfirmationBlock,
} from "./models";

/**
Expand All @@ -30,34 +31,34 @@ export abstract class Validator extends ServerNode {

/**
* Gets the account balance with the given account number (id).
* @param id the account number
* @param accountNumber the public key of the account
*/
async getAccountBalance(id: string) {
return await super.getData<AccountBalanceResponse>(`/accounts/${id}/balance`);
async getAccountBalance(accountNumber: string) {
return await super.getData<AccountBalanceResponse>(`/accounts/${accountNumber}/balance`);
}

/**
* Gets the balance lock of the given account.
* @param id the id of the account
* @param accountNumber the public key of the account
*/
async getAccountBalanceLock(id: string) {
return await super.getData<AccountBalanceLockResponse>(`/accounts/${id}/balance_lock`);
async getAccountBalanceLock(accountNumber: string) {
return await super.getData<AccountBalanceLockResponse>(`/accounts/${accountNumber}/balance_lock`);
}

/**
* Gets the details of given block identifier's queued transactions.
* @param id the block identifier
* @param blockId the block identifier
*/
async getQueuedConfirmationBlock(id: string) {
return await super.getData(`/confirmation_blocks/${id}/queued`);
async getQueuedConfirmationBlock(blockId: string) {
return await super.getData<ConfirmationBlock>(`/confirmation_blocks/${blockId}/queued`);
}

/**
* Gets the details of given block identifier's valid transactions.
* @param id the block identifier
* @param blockId the block identifier
*/
async getValidConfirmationBlock(id: string) {
return await super.getData(`/confirmation_blocks/${id}/valid`);
async getValidConfirmationBlock(blockId: string) {
return await super.getData<ConfirmationBlock>(`/confirmation_blocks/${blockId}/valid`);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion tests/account.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Account } = require("../");
const { Account } = require("../dist");

describe("Account", () => {
const defaultAccount = {
Expand Down Expand Up @@ -46,6 +46,15 @@ describe("Account", () => {
expect(Account.isValidPair(defaultAccount.accountNumber, defaultAccount.signingKey)).toBeFalsy();
});

it("isValidPair doesn't throw errors", () => {
const results = [
Account.isValidPair("asdf", "asdf"),
Account.isValidPair(defaultAccount.signingKey, "asdf"),
Account.isValidPair("asdf", defaultAccount.accountNumber),
];
expect(results.every((val) => typeof val === "boolean")).toBeTruthy();
});

it("createSignature(message)", () => {
const account = createDefaultAccount();
assertAccountBasics(account);
Expand Down
35 changes: 18 additions & 17 deletions tests/bank.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const tnb = require("../");
const tnb = require("../dist");
const nock = require("nock");
const data = require("./data/bank");

Expand Down Expand Up @@ -97,21 +97,22 @@ describe("Bank", () => {
it("getTransactions()", async () => {
const transactions = await bank.getTransactions();
expect(typeof transactions).toBe("object");
expect(transactions.results[0]).toStrictEqual( {
"id": "443aabd9-d06b-4c4b-af3b-5a21cbee523d",
"block": {
"id": "04f407d2-35fa-4416-99f4-1ea39612a014",
"created_date": "2021-04-12T08:21:32.612926Z",
"modified_date": "2021-04-12T08:21:32.612953Z",
"balance_key": "d2af51bfc15be5af4c4120c488625b7b224f6acb84a4467a4dd8f1647a0ec8e8",
"sender": "22d0f0047b572a6acb6615f7aae646b0b96ddc58bfd54ed2775f885baeba3d6a",
"signature": "9e715ea8e5c173a87369215868c649fbe164444ea138d2fff4e4add80f4ccdb3a5ee6a529964b43a5b9fd611d504b58c52c380792ed359c036763942e003a002"
},
"amount": 1,
"fee": "PRIMARY_VALIDATOR",
"memo": "",
"recipient": "4afb3eaad999e4c073be0fbde86b76f9370d53b398b9cab9d760825709a1d6b3"
});
expect(transactions.results[0]).toStrictEqual({
id: "443aabd9-d06b-4c4b-af3b-5a21cbee523d",
block: {
id: "04f407d2-35fa-4416-99f4-1ea39612a014",
created_date: "2021-04-12T08:21:32.612926Z",
modified_date: "2021-04-12T08:21:32.612953Z",
balance_key: "d2af51bfc15be5af4c4120c488625b7b224f6acb84a4467a4dd8f1647a0ec8e8",
sender: "22d0f0047b572a6acb6615f7aae646b0b96ddc58bfd54ed2775f885baeba3d6a",
signature:
"9e715ea8e5c173a87369215868c649fbe164444ea138d2fff4e4add80f4ccdb3a5ee6a529964b43a5b9fd611d504b58c52c380792ed359c036763942e003a002",
},
amount: 1,
fee: "PRIMARY_VALIDATOR",
memo: "",
recipient: "4afb3eaad999e4c073be0fbde86b76f9370d53b398b9cab9d760825709a1d6b3",
});
});

it("getBanks()", async () => {
Expand Down Expand Up @@ -196,7 +197,7 @@ describe("Bank", () => {
"fakeBalanceLock",
[{ amount: 1, recipient: "fakeAccountNumber" }],
new tnb.Account(),
'Memo'
"Memo"
);
expect(typeof res).toBe("object");
expect(res).toStrictEqual({
Expand Down
Loading

0 comments on commit 43e5798

Please sign in to comment.