Skip to content

Commit

Permalink
v1.1.0-alpha.5
Browse files Browse the repository at this point in the history
  • Loading branch information
zinoadidi authored Jun 30, 2021
2 parents 43e5798 + 881a3bf commit 1436225
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 56 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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.5](https://github.com/thenewboston-developers/thenewboston-js/compare/v1.1.0-alpha.4...v1.1.0-alpha.5) (2021-06-30)


### Features

* Verify Signatures ([#164](https://github.com/thenewboston-developers/thenewboston-js/issues/164)) ([267e89b](https://github.com/thenewboston-developers/thenewboston-js/commit/267e89bae70466c8ff2550fdc0aa5e7005d30389))

## [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)


Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ JavaScript library for thenewboston.

Find out how you can contribute [here](https://github.com/thenewboston-developers/thenewboston-js/blob/master/docs/CONTRIBUTING.md).

### Testing

Some day...

## Documentation

Check out the [documentation here](https://github.com/thenewboston-developers/thenewboston-js/blob/master/docs/index.md).
Check out the [documentation here](https://thenewboston-developers.github.io/thenewboston-js/).

## Community

Expand Down
15 changes: 15 additions & 0 deletions docs/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ From running that code, you can see that the `createSignature` method returned a

> If you re-run that code multiple times, you will then notice that the generated signature is different. That is because the signature depends on two variables: the account signing key and the message. The account signing key also is changing on every run because we are generating a random `Account` to use, thus resulting in different outcomes.
## Verifying Signatures

If you need to verify a signature for a message, then you can easily use the `Account.verifySignature` static method. The method takes in the message first, the signature (signed message) second and then the account number last. Here is example of how one might use this method:

```ts
const account = new Account("SIGNING_KEY");

const message = "Hello, world!" or JSON.stringify({greeting: "Hello World!"});

const signature = account.createSignature(message);

Account.verifySignature(message,signature, account.accountNumberHex);
// returns true only if the account number was used to sign the message and the signed message matches the signature
```

## Verifying Account Keys

If you need to verify that the given signing key and account number are paired together, then you can easily use the `Account.isValidPair` static method. The method takes in the signing key first and the account number second. Here is example of how one might use this method:
Expand Down
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ There are two ways to use the library.

- [Using Block Data and Block Messages](account.md#using-block-data-and-block-messages)

- [Account Payment Handler](account-payment-handler.md)

- [Sending Coins](account-payment-handler.md#sending-coins)

- [Send Bulk Payments](account-payment-handler.md#sending-bulk-payments)

- [Bank](bank.md#bank)

- [Creating Banks](bank.md#creating-banks)
Expand Down
90 changes: 46 additions & 44 deletions 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.4",
"version": "1.1.0-alpha.5",
"description": "JavaScript library for thenewboston.",
"author": {
"name": "thenewboston-developers",
Expand Down
4 changes: 2 additions & 2 deletions src/account-payment-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export class AccountPaymentHandler {
}

async sendCoins(recipient: Account | string, amount: number, memo = "") {
await this.client.sendCoins(new TransferDetails(this.account, recipient, amount, memo));
return await this.client.sendCoins(new TransferDetails(this.account, recipient, amount, memo));
}

async sendBulkTransactions(transactions: Transaction[]) {
await this.client.sendBulkTransactions(this.account, transactions);
return await this.client.sendBulkTransactions(this.account, transactions);
}
}
17 changes: 17 additions & 0 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ export class Account {
}
}

/**
* Checks if the message was signed by a specific account number.
* @param message the message to verify
* @param signature the signed message
* @param accountNumber the account number that signed the message
*/
static verifySignature(message: string, signature: string, accountNumber: string) {
const encodedMessage = new TextEncoder().encode(message);
const encodedSignature = hexToUint8Array(signature);
const encodedAccountNumber = hexToUint8Array(accountNumber);
try {
return sign.detached.verify(encodedMessage, encodedSignature.slice(0, 64), encodedAccountNumber);
} catch {
return false;
}
}

/** The 32 byte account number as a 32 byte hex string. */
get accountNumberHex() {
return uint8arrayToHex(this.accountNumber);
Expand Down
14 changes: 10 additions & 4 deletions src/payment-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export class PaymentHandler {
return tx;
});

if (!this.primaryValidator)
throwError(
"The Payment Handler is not initalized yet.\nTry calling the async '.init()' method on the Payment Handler before sending transactions"
);

const { balance_lock: balanceLock } = await this.primaryValidator!.getAccountBalanceLock(
sender.accountNumberHex
).catch((err) =>
Expand All @@ -72,7 +77,8 @@ export class PaymentHandler {
fee: config.node_type,
recipient: config.account_number,
})),
];
].sort((a, b) => (a.recipient > b.recipient ? 1 : -1));

return { balanceLock, transactions, sender };
}

Expand All @@ -85,7 +91,7 @@ export class PaymentHandler {
transactions: Transaction[];
sender: Account;
}) {
await this.bank.addBlocks(transaction.balanceLock!, transaction.transactions, transaction.sender);
return await this.bank.addBlocks(transaction.balanceLock!, transaction.transactions, transaction.sender);
}

/**
Expand All @@ -95,7 +101,7 @@ export class PaymentHandler {
async sendCoins({ sender, recipient, amount, memo = "" }: TransferDetails) {
const recipientAccount = typeof recipient === "string" ? recipient : recipient.accountNumberHex;
const transaction = await this.createTransaction(sender, [{ amount, memo, recipient: recipientAccount }]);
await this.broadcastTransaction(transaction);
return await this.broadcastTransaction(transaction);
}

/**
Expand All @@ -105,6 +111,6 @@ export class PaymentHandler {
*/
async sendBulkTransactions(sender: Account, txs: Transaction[]) {
const transaction = await this.createTransaction(sender, txs);
await this.broadcastTransaction(transaction);
return await this.broadcastTransaction(transaction);
}
}
26 changes: 26 additions & 0 deletions tests/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ describe("Account", () => {
});
});

it("verifySignature(message: string, signature: string, accountNumber: string)", () => {
const account = createDefaultAccount();
const message = JSON.stringify({
balance_key: null,
txs: [
{
amount: 1,
fee: "PRIMARY_VALIDATOR",
recipient: "4afb3eaad999e4c073be0fbde86b76f9370d53b398b9cab9d760825709a1d6b3",
},
{
amount: 100,
memo: "Deposit",
recipient: "718b3c72e1e520479b9f9a2a582f280e549732c97c44de22c5b2a4e443154342",
},
{ amount: 1, fee: "BANK", recipient: "e3a94381f8db207ddad931391886d611d6f4c060d0db2b0e373738e2f4db96d6" },
],
});

const signature = account.createSignature(message);

expect(Account.verifySignature(message, signature, account.accountNumberHex)).toBeTruthy();
expect(Account.verifySignature("Wrong Message", signature, account.accountNumberHex)).toBeFalsy();
expect(Account.verifySignature(message, "Wrong Signature", account.accountNumberHex)).toBeFalsy();
expect(Account.verifySignature(message, signature, "Wrong Account Number")).toBeFalsy();
});
// TODO: createBlockData

// TODO: createBlockMessage
Expand Down

0 comments on commit 1436225

Please sign in to comment.