Skip to content

Commit 1436225

Browse files
authored
v1.1.0-alpha.5
2 parents 43e5798 + 881a3bf commit 1436225

File tree

10 files changed

+131
-56
lines changed

10 files changed

+131
-56
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
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.
44

5+
## [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)
6+
7+
8+
### Features
9+
10+
* Verify Signatures ([#164](https://github.com/thenewboston-developers/thenewboston-js/issues/164)) ([267e89b](https://github.com/thenewboston-developers/thenewboston-js/commit/267e89bae70466c8ff2550fdc0aa5e7005d30389))
11+
512
## [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)
613

714

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ JavaScript library for thenewboston.
99

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

12-
### Testing
13-
14-
Some day...
15-
1612
## Documentation
1713

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

2016
## Community
2117

docs/account.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ From running that code, you can see that the `createSignature` method returned a
7070

7171
> 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.
7272
73+
## Verifying Signatures
74+
75+
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:
76+
77+
```ts
78+
const account = new Account("SIGNING_KEY");
79+
80+
const message = "Hello, world!" or JSON.stringify({greeting: "Hello World!"});
81+
82+
const signature = account.createSignature(message);
83+
84+
Account.verifySignature(message,signature, account.accountNumberHex);
85+
// returns true only if the account number was used to sign the message and the signed message matches the signature
86+
```
87+
7388
## Verifying Account Keys
7489

7590
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:

docs/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ There are two ways to use the library.
2525

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

28+
- [Account Payment Handler](account-payment-handler.md)
29+
30+
- [Sending Coins](account-payment-handler.md#sending-coins)
31+
32+
- [Send Bulk Payments](account-payment-handler.md#sending-bulk-payments)
33+
2834
- [Bank](bank.md#bank)
2935

3036
- [Creating Banks](bank.md#creating-banks)

package-lock.json

Lines changed: 46 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "thenewboston",
3-
"version": "1.1.0-alpha.4",
3+
"version": "1.1.0-alpha.5",
44
"description": "JavaScript library for thenewboston.",
55
"author": {
66
"name": "thenewboston-developers",

src/account-payment-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export class AccountPaymentHandler {
1717
}
1818

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

2323
async sendBulkTransactions(transactions: Transaction[]) {
24-
await this.client.sendBulkTransactions(this.account, transactions);
24+
return await this.client.sendBulkTransactions(this.account, transactions);
2525
}
2626
}

src/account.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ export class Account {
5959
}
6060
}
6161

62+
/**
63+
* Checks if the message was signed by a specific account number.
64+
* @param message the message to verify
65+
* @param signature the signed message
66+
* @param accountNumber the account number that signed the message
67+
*/
68+
static verifySignature(message: string, signature: string, accountNumber: string) {
69+
const encodedMessage = new TextEncoder().encode(message);
70+
const encodedSignature = hexToUint8Array(signature);
71+
const encodedAccountNumber = hexToUint8Array(accountNumber);
72+
try {
73+
return sign.detached.verify(encodedMessage, encodedSignature.slice(0, 64), encodedAccountNumber);
74+
} catch {
75+
return false;
76+
}
77+
}
78+
6279
/** The 32 byte account number as a 32 byte hex string. */
6380
get accountNumberHex() {
6481
return uint8arrayToHex(this.accountNumber);

src/payment-handler.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export class PaymentHandler {
5959
return tx;
6060
});
6161

62+
if (!this.primaryValidator)
63+
throwError(
64+
"The Payment Handler is not initalized yet.\nTry calling the async '.init()' method on the Payment Handler before sending transactions"
65+
);
66+
6267
const { balance_lock: balanceLock } = await this.primaryValidator!.getAccountBalanceLock(
6368
sender.accountNumberHex
6469
).catch((err) =>
@@ -72,7 +77,8 @@ export class PaymentHandler {
7277
fee: config.node_type,
7378
recipient: config.account_number,
7479
})),
75-
];
80+
].sort((a, b) => (a.recipient > b.recipient ? 1 : -1));
81+
7682
return { balanceLock, transactions, sender };
7783
}
7884

@@ -85,7 +91,7 @@ export class PaymentHandler {
8591
transactions: Transaction[];
8692
sender: Account;
8793
}) {
88-
await this.bank.addBlocks(transaction.balanceLock!, transaction.transactions, transaction.sender);
94+
return await this.bank.addBlocks(transaction.balanceLock!, transaction.transactions, transaction.sender);
8995
}
9096

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

101107
/**
@@ -105,6 +111,6 @@ export class PaymentHandler {
105111
*/
106112
async sendBulkTransactions(sender: Account, txs: Transaction[]) {
107113
const transaction = await this.createTransaction(sender, txs);
108-
await this.broadcastTransaction(transaction);
114+
return await this.broadcastTransaction(transaction);
109115
}
110116
}

tests/account.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ describe("Account", () => {
8080
});
8181
});
8282

83+
it("verifySignature(message: string, signature: string, accountNumber: string)", () => {
84+
const account = createDefaultAccount();
85+
const message = JSON.stringify({
86+
balance_key: null,
87+
txs: [
88+
{
89+
amount: 1,
90+
fee: "PRIMARY_VALIDATOR",
91+
recipient: "4afb3eaad999e4c073be0fbde86b76f9370d53b398b9cab9d760825709a1d6b3",
92+
},
93+
{
94+
amount: 100,
95+
memo: "Deposit",
96+
recipient: "718b3c72e1e520479b9f9a2a582f280e549732c97c44de22c5b2a4e443154342",
97+
},
98+
{ amount: 1, fee: "BANK", recipient: "e3a94381f8db207ddad931391886d611d6f4c060d0db2b0e373738e2f4db96d6" },
99+
],
100+
});
101+
102+
const signature = account.createSignature(message);
103+
104+
expect(Account.verifySignature(message, signature, account.accountNumberHex)).toBeTruthy();
105+
expect(Account.verifySignature("Wrong Message", signature, account.accountNumberHex)).toBeFalsy();
106+
expect(Account.verifySignature(message, "Wrong Signature", account.accountNumberHex)).toBeFalsy();
107+
expect(Account.verifySignature(message, signature, "Wrong Account Number")).toBeFalsy();
108+
});
83109
// TODO: createBlockData
84110

85111
// TODO: createBlockMessage

0 commit comments

Comments
 (0)