diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b5b9e9f..4affae49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ 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.1](https://github.com/thenewboston-developers/thenewboston-js/compare/v1.1.0-alpha.0...v1.1.0-alpha.1) (2021-04-09) + +### Features + +* feat: Get Methods for Single nodes ([#121](https://github.com/thenewboston-developers/thenewboston-js/issues/121)) ([be17ed9](https://github.com/thenewboston-developers/thenewboston-js/commit/be17ed9d1f50d6a439bbfd4efb1506b324b384e0)) +* feat: added updateBankTrust, updateValidatorTrust ([#118](https://github.com/thenewboston-developers/thenewboston-js/issues/118)) ([32e1e1c](https://github.com/thenewboston-developers/thenewboston-js/commit/32e1e1c2afd549bfde80ca4fccc29da55ab5ee32)) + +### Bug Fixes + +* Updated CV and Pv response types ([#133](https://github.com/thenewboston-developers/thenewboston-js/issues/133)) ([7191322](https://github.com/thenewboston-developers/thenewboston-js/commit/7191322e956bc4e2ac4bf47c45a4a075917e8c0d)) + ## [1.1.0-alpha.0](https://github.com/thenewboston-developers/thenewboston-js/compare/v1.0.3...v1.1.0-alpha.0) (2021-04-01) diff --git a/README.md b/README.md index c96ac5b5..cd1dedd8 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,13 @@ + [ ![Issues](https://img.shields.io/github/issues/thenewboston-developers/thenewboston-js?color=0088ff) ](https://github.com/thenewboston-developers/thenewboston-js/issues) [ ![GitHub pull requests](https://img.shields.io/github/issues-pr/thenewboston-developers/thenewboston-js?color=0088ff) ](https://github.com/thenewboston-developers/thenewboston-js/pulls)  [![thenewboston on npm](https://img.shields.io/npm/v/thenewboston?logo=npm&logoColor=fff&label=NPM+package&color=limegreen)](https://www.npmjs.com/package/thenewboston) [ ![Stars](https://img.shields.io/github/stars/thenewboston-developers/thenewboston-js.svg) ](https://github.com/thenewboston-developers/thenewboston-js/stargazers) [ ![All contributors](https://img.shields.io/github/contributors/thenewboston-developers/thenewboston-js.svg) ](https://github.com/thenewboston-developers/thenewboston-js/graphs/contributors) + + ## Overview JavaScript library for thenewboston. -## Contributing Guide - -Here you can learn about how you can help make thenewboston-js a robust and simple JavaScript library to use! - -### Setup - -Make sure you clone the official repository. Here is the command to run if you want to clone with git: - -```sh -> git clone https://github.com/thenewboston-developers/thenewboston-js.git -``` - -After that, you should see a new folder named `thenewboston-js` in your current working directory. Navigate into that project and you're ready to go! - -### Development - -Before you are able to access any of the npm commands, you must first run `npm install` or `npm i` to get all of the modules loaded into your local project. - -### Building - -If you are ready to test out your changes, run `npm run build`. After that, you should see the CommonJS module located in the `dist` directory. You can create a `dev.js` file with something like the following code if you want to test out your changes: - -```js -const tnb = require("./"); // loads the module from the path in the `package.json` - -async function main() { - /* Code! */ -} - -main(); -``` - ### Contributing -Create your fork / branch from the development branch, All pull requests to be made to development branch for review. +Find out how you can contribute [here](https://github.com/thenewboston-developers/thenewboston-js/blob/master/docs/CONTRIBUTING.md). ### Testing @@ -44,7 +15,7 @@ Some day... ## Documentation -Check out the [documentation here](docs/index.md). +Check out the [documentation here](https://github.com/thenewboston-developers/thenewboston-js/blob/master/docs/index.md). ## Community @@ -70,6 +41,7 @@ All donations will go to thenewboston to help fund the team to continue to devel | ![Bitcoin Logo](https://github.com/thenewboston-developers/Website/raw/development/src/assets/images/bitcoin.png) | 3GZYi3w3BXQfyb868K2phHjrS4i8LooaHh | | ![Ethereum Logo](https://github.com/thenewboston-developers/Website/raw/development/src/assets/images/ethereum.png) | 0x0E38e2a838F0B20872E5Ff55c82c2EE7509e6d4A | + ## License thenewboston is [MIT licensed](http://opensource.org/licenses/MIT). diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md new file mode 100644 index 00000000..eb17787c --- /dev/null +++ b/docs/CONTRIBUTING.md @@ -0,0 +1,35 @@ +## Contributing Guide + +Here you can learn about how you can help make thenewboston-js a robust and simple JavaScript library to use! + +### Setup + +Make sure you clone the official repository. Here is the command to run if you want to clone with git: + +```sh +> git clone https://github.com/thenewboston-developers/thenewboston-js.git +``` + +After that, you should see a new folder named `thenewboston-js` in your current working directory. Navigate into that project and you're ready to go! + +### Development + +Before you are able to access any of the npm commands, you must first run `npm install` or `npm i` to get all of the modules loaded into your local project. + +### Building + +If you are ready to test out your changes, run `npm run build`. After that, you should see the CommonJS module located in the `dist` directory. You can create a `dev.js` file with something like the following code if you want to test out your changes: + +```js +const tnb = require("./"); // loads the module from the path in the `package.json` + +async function main() { + /* Code! */ +} + +main(); +``` + +### Contributing + +Create your fork / branch from the development branch, All pull requests to be made to development branch for review. \ No newline at end of file diff --git a/docs/bank.md b/docs/bank.md index e6452636..229f5fe9 100644 --- a/docs/bank.md +++ b/docs/bank.md @@ -108,11 +108,15 @@ console.log(accounts); We can also update the trust of a specific account. -This is an API call to update the level of trust of a specific account. We use `Bank.updateAccount` for this. +This is an API call to update the level of trust of a specific account. We use `Bank.updateAccountTrust` for this. ```ts const account = new Account("0cdd4ba04456ca169baca3d66eace869520c62fe84421329086e03d91a68acdb", "fakeSigningKeyHex"); -const res = await bank.updateAccount("0cdd4ba04456ca169baca3d66eace869520c62fe84421329086e03d91a68acdb", 32, account); +const res = await bank.updateAccountTrust( + "0cdd4ba04456ca169baca3d66eace869520c62fe84421329086e03d91a68acdb", + 32, + account +); console.log(res); // { // "id": "64426fc5-b3ac-42fb-b75b-d5ccfcdc6872", @@ -219,7 +223,7 @@ console.log(transactions); > Just like with `Bank.getAccounts`, you can also pass in an options object here. -## Getting Banks +## Getting Banks and Updating Trust This is similar to the [previous section](#getting-transactions), we are just making an API call. @@ -261,6 +265,59 @@ console.log(banks); > As expected, the options object can also be used with this. +#### Getting a single Bank + +The `getBank()` method is used to get a specified bank of the bank. + +```ts +const bankNodeId = "// node_identifier: '59af0721c572e6032b835722b5fec22110daad069dc135f1e81794747dbe626f', +"; + +const res = await bank.getBank(bankNodeId); +console.log(res); + +// { +// account_number: '9a275161478536d0a5b88ff05d429b9a9e63d0032a46e7a6a8f088da89c69da5', +// ip_address: '13.57.215.62', +// node_identifier: '59af0721c572e6032b835722b5fec22110daad069dc135f1e81794747dbe626f', +// port: 80, +// protocol: 'http', +// version: 'v1.0', +// default_transaction_fee: 1, +// trust: '50.00' +// } +``` + +#### Updating Bank Trust + +We can also update the trust of a specific bank by using the `updateBankTrust` method. + +```ts +const networkIdKeyPair = new Account("fakeSigningKeyHex"); + +const bankToUpdate = "0cdd4ba04456ca169baca3d66eace869520c62fe84421329086e03d91a68acdb"; + +const res = await bank.updateAccountTrust(bankToUpdate, 50, networkIdKeyPair); + +console.log(res); +// { +// account_number: 'd62165ea6102a74fd484176226cd6ed8f7fc779117138e3ee2d9881ff4fc5a04', +// ip_address: '144.126.219.17', +// node_identifier: '38ce9d9a245b2c5c923b1a6e8bbf5324a8cd57d71c977aef6a457740d7fd6451', +// port: 80, +// protocol: 'http', +// version: 'v1.4', +// default_transaction_fee: 1, +// root_account_file: 'http://144.126.219.17:80/media/root_account_file.json', +// root_account_file_hash: 'ab9b95e5bb1dc66dd57ebf2cb8a8dece41748389d68077f74c916659f4bd2f1b', +// seed_block_identifier: '', +// daily_confirmation_rate: 1, +// trust: '50.00' +// } +``` + +
+ ## Getting and Adding Blocks Getting a list of blocks is quite similar to all the previous API calls. For this, we use `Bank.getBlocks`. @@ -405,7 +462,7 @@ We use `Bank.getConfirmationBlocks` to get the confirmation blocks and `Bank.get > Both of these methods support the options object -## Getting Validators and Validator Confirmation Services +## Getting Validators and Updating Trust The `Bank.getValidators` method is used to get all connected validators of the bank. @@ -450,6 +507,60 @@ console.log(res); // } ``` +#### Getting a single Validator + +The `getValidator()` method is used to get a specified validator of the bank. + +```ts +const validatorNodeId = "01181490ac0fa6f73bd980adb81f1a3e72f81eb6b4ccab4dac8b6db7544e5eb1"; + +const res = await bank.getValidator(validatorNodeId); +console.log(res); + +// { +// account_number: 'd2018eb4a13ea1ed307aa8f73207bc1cbad3fac0070899ff5af158982ae7a804', +// ip_address: '18.218.193.164', +// node_identifier: '01181490ac0fa6f73bd980adb81f1a3e72f81eb6b4ccab4dac8b6db7544e5eb1', +// port: 80, +// protocol: 'http', +// version: 'v1.0', +// default_transaction_fee: 1, +// trust: '100.00' +// } +``` + +#### Updating Validator Trust + +We can also update the trust of a specific validator by using the `updateValidatorTrust` method. + +```ts +const networkIdKeyPair = new Account("validatorNetworkIdSigningKey"); + +const validatorToUpdate = "38ce9d9a245b2c5c923b1a6e8bbf5324a8cd57d71c977aef6a457740d7fd6451"; + +const res = await bank.updateAccountTrust(validatorToUpdate, 12, networkIdKeyPair); + +console.log(res); +// { +// account_number: 'd62165ea6102a74fd484176226cd6ed8f7fc779117138e3ee2d9881ff4fc5a04', +// ip_address: '144.126.219.17', +// node_identifier: '38ce9d9a245b2c5c923b1a6e8bbf5324a8cd57d71c977aef6a457740d7fd6451', +// port: 80, +// protocol: 'http', +// version: 'v1.4', +// default_transaction_fee: 1, +// root_account_file: 'http://144.126.219.17:80/media/root_account_file.json', +// root_account_file_hash: 'ab9b95e5bb1dc66dd57ebf2cb8a8dece41748389d68077f74c916659f4bd2f1b', +// seed_block_identifier: '', +// daily_confirmation_rate: 1, +// trust: '12.00' +// } +``` + +
+ +## Getting Validator Confirmation Services + We use `Bank.getValidatorConfirmationServices` to get all the Validator Confirmation Services of the bank. ```ts diff --git a/package-lock.json b/package-lock.json index 38374614..729f746c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "thenewboston", - "version": "1.1.0-alpha.0", + "version": "1.1.0-alpha.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ab2b7a75..ceb86ad5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "thenewboston", - "version": "1.1.0-alpha.0", + "version": "1.1.0-alpha.1", "description": "JavaScript library for thenewboston.", "author": { "name": "thenewboston-developers", diff --git a/src/bank.ts b/src/bank.ts index b0aa5fb1..9d432a01 100644 --- a/src/bank.ts +++ b/src/bank.ts @@ -26,7 +26,7 @@ export class Bank extends ServerNode { * @param trust the trust of the the server * @param account the account for the server node in which the account number is the node identifier and the signing key is the node identifier signing key */ - async updateAccount(accountNumber: string, trust: number, account: Account) { + async updateAccountTrust(accountNumber: string, trust: number, account: Account) { return await super.patchData(`/accounts/${accountNumber}`, account.createSignedMessage({ trust })); } @@ -38,6 +38,14 @@ export class Bank extends ServerNode { return await super.getPaginatedData("/bank_transactions", options); } + /** + * Gets the bank with the specified node identifier. + * @param nodeIdentifier Node Identifier of a bank. + */ + async getBank(nodeIdentifier: string) { + return await super.getData(`/banks/${nodeIdentifier}`); + } + /** * Gets the connected banks for the given bank. * @param options The optional object for the pagination options. @@ -61,6 +69,21 @@ export class Bank extends ServerNode { ); } + /** + * Updates a given validators's trust. + * @param nodeIdentifier the validator to update's node identifier + * @param trust the new validator's trust + * @param account the current bank's network Id to sign the request + */ + async updateValidatorTrust(nodeIdentifier: string, trust: number, account: Account) { + return await super.patchData( + `/validators/${nodeIdentifier}`, + account.createSignedMessage({ + trust, + }) + ); + } + /** * Gets the blocks for the given bank. * @param options The optional object for the pagination options. @@ -194,6 +217,14 @@ export class Bank extends ServerNode { ); } + /** + * Gets the validator with the specified node identifier. + * @param nodeIdentifier Node Identifier of a validator. + */ + async getValidator(nodeIdentifier: string) { + return await super.getData(`/validators/${nodeIdentifier}`); + } + /** * Gets all of the validators for the current bank. * @param options The optional object for the pagination options. diff --git a/src/models/responses/confirmation-validator/config.ts b/src/models/responses/confirmation-validator/config.ts index 52a0b01f..df00cdd8 100644 --- a/src/models/responses/confirmation-validator/config.ts +++ b/src/models/responses/confirmation-validator/config.ts @@ -22,5 +22,9 @@ export interface ConfirmationValidatorConfigResponse { protocol: Protocol; version: Version; default_transaction_fee: number; + root_account_file: Url; + root_account_file_hash: Hex; + seed_block_identifier: Hex; + daily_confirmation_rate: number; node_type: NodeType; } diff --git a/src/models/responses/primary-validator/config.ts b/src/models/responses/primary-validator/config.ts index 45a79efe..6226aea1 100644 --- a/src/models/responses/primary-validator/config.ts +++ b/src/models/responses/primary-validator/config.ts @@ -1,20 +1,7 @@ import type { Hex, Origin, Port, Protocol, Url, Trust, Version, NodeType } from "../constants"; export interface PrimaryValidatorConfigResponse { - primary_validator: { - account_number: Hex; - ip_address: Origin; - node_identifier: Hex; - port: Port | null; - protocol: Protocol; - version: Version; - default_transaction_fee: number; - root_account_file: Url; - root_account_file_hash: Hex; - seed_block_identifier: Hex; - daily_confirmation_rate: number | null; - trust: Trust; - }; + primary_validator: null; account_number: Hex; ip_address: Origin; node_identifier: Hex; diff --git a/src/validator.ts b/src/validator.ts index 99803466..c4bac38e 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -1,8 +1,10 @@ +import { Account } from "./account"; import { ServerNode } from "./server-node"; import type { AccountBalanceResponse, AccountBalanceLockResponse, PaginatedBankEntry, + PaginatedValidatorEntry, PaginatedTransactionEntry, PaginationOptions, } from "./models"; @@ -13,6 +15,14 @@ import type { * Note: this class is meant to be extended. */ export abstract class Validator extends ServerNode { + /** + * Gets the bank with the specified node identifier. + * @param nodeIdentifier Node Identifier of a bank. + */ + async getBank(nodeIdentifier: string) { + return await super.getData(`/banks/${nodeIdentifier}`); + } + /** Gets all of the banks connected to the current validator. */ async getBanks(options: Partial = {}) { return await super.getPaginatedData("/banks", options); @@ -50,11 +60,48 @@ export abstract class Validator extends ServerNode { return await super.getData(`/confirmation_blocks/${id}/valid`); } + /** + * Gets the validator with the specified node identifier. + * @param nodeIdentifier Node Identifier of a validator. + */ + async getValidator(nodeIdentifier: string) { + return await super.getData(`/validators/${nodeIdentifier}`); + } + /** * Gets all of the connected validators to the current validator. * @param options the pagination options */ async getValidators(options: Partial = {}) { - return await super.getPaginatedData(`/validators`, options); + return await super.getPaginatedData(`/validators`, options); + } + /** + * Updates a given bank's trust. + * @param nodeIdentifier the bank to update's node identifier + * @param trust the new bank's trust + * @param account the current validators's network Id to sign the request + */ + async updateBankTrust(nodeIdentifier: string, trust: number, account: Account) { + return await super.patchData( + `/banks/${nodeIdentifier}`, + account.createSignedMessage({ + trust, + }) + ); + } + + /** + * Updates a given validators's trust. + * @param nodeIdentifier the validator to update's node identifier + * @param trust the new validator's trust + * @param account the current validators's network Id to sign the request + */ + async updateValidatorTrust(nodeIdentifier: string, trust: number, account: Account) { + return await super.patchData( + `/validators/${nodeIdentifier}`, + account.createSignedMessage({ + trust, + }) + ); } } diff --git a/tests/bank.test.js b/tests/bank.test.js index 3de3faed..8533e283 100644 --- a/tests/bank.test.js +++ b/tests/bank.test.js @@ -82,8 +82,8 @@ describe("Bank", () => { const bank = new tnb.Bank("http://13.57.215.62"); - it("updateAccount(accountNumber, trust, account)", async () => { - const res = await bank.updateAccount("fakeAccountNumber", 10, new tnb.Account()); + it("updateAccountTrust(accountNumber, trust, account)", async () => { + const res = await bank.updateAccountTrust("fakeAccountNumber", 10, new tnb.Account()); expect(typeof res).toBe("object"); expect(res).toStrictEqual({ id: "64426fc5-b3ac-42fb-b75b-d5ccfcdc6872",