Skip to content

Commit 1ad1814

Browse files
authored
Merge pull request #184 from ainblockchain/release/v1.10.0
Upgrade version to 1.10.0
2 parents daa4259 + 4308dc3 commit 1ad1814

11 files changed

+50
-28
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ yarn add @ainblockchain/ain-js
1212
## Examples
1313
```
1414
const Ain = require('./lib/ain').default;
15-
const ain = new Ain('http://node.ainetwork.ai:8080/');
15+
const ain = new Ain('http://localhost:8081/', 'ws://localhost:5100/');
16+
// or const ain = new Ain('https://testnet-api.ainetwork.ai/', 'wss://testnet-event.ainetwork.ai/');
1617
1718
ain.wallet.create(1);
1819

__tests__/ain.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('ain-js', function() {
2727
it('chainId', function() {
2828
expect(ain.chainId).toBe(0);
2929
expect(ain.wallet.chainId).toBe(0);
30-
ain.setProvider(test_node_1, 2);
30+
ain.setProvider(test_node_1, null, 2);
3131
expect(ain.chainId).toBe(2);
3232
expect(ain.wallet.chainId).toBe(2);
3333
});
@@ -273,7 +273,7 @@ describe('ain-js', function() {
273273
ain.wallet.setDefaultAccount(TEST_ADDR);
274274
const sig = ain.wallet.sign(message);
275275
const addr:string = String(ain.wallet.defaultAccount!.address);
276-
expect(Ain.utils.ecVerifySig(message, sig, addr)).toBe(true);
276+
expect(ain.wallet.verifySignature(message, sig, addr)).toBe(true);
277277
});
278278

279279
it('signTransaction', function() {
@@ -289,7 +289,7 @@ describe('ain-js', function() {
289289
}
290290
const sig = ain.wallet.signTransaction(tx);
291291
const addr:string = String(ain.wallet.defaultAccount!.address);
292-
expect(Ain.utils.ecVerifySig(tx, sig, addr)).toBe(true);
292+
expect(ain.wallet.verifySignature(tx, sig, addr)).toBe(true);
293293
});
294294

295295
it('recover', function() {
@@ -401,7 +401,7 @@ describe('ain-js', function() {
401401

402402
it('chainId', function() {
403403
// chainId = 0
404-
ain.setProvider(test_node_2, 0);
404+
ain.setProvider(test_node_2, null, 0);
405405
let tx: TransactionBody = {
406406
nonce: 17,
407407
gas_price: 500,
@@ -415,11 +415,11 @@ describe('ain-js', function() {
415415
let sig = ain.wallet.signTransaction(tx);
416416
let addr:string = String(ain.wallet.defaultAccount!.address);
417417
expect(ain.wallet.verifySignature(tx, sig, addr)).toBe(true);
418-
expect(() => Ain.utils.ecVerifySig(tx, sig, addr, 2)).toThrow('[ain-util] ecRecoverPub: Invalid signature v value');
418+
expect(ain.wallet.verifySignature(tx, sig, addr, 2)).toBe(false);
419419
expect(ain.wallet.recover(sig)).toBe(addr);
420420

421421
// chainId = 2
422-
ain.setProvider(test_node_2, 2);
422+
ain.setProvider(test_node_2, null, 2);
423423
tx = {
424424
nonce: 17,
425425
timestamp: Date.now(),
@@ -432,7 +432,7 @@ describe('ain-js', function() {
432432
sig = ain.wallet.signTransaction(tx);
433433
addr = String(ain.wallet.defaultAccount!.address);
434434
expect(ain.wallet.verifySignature(tx, sig, addr)).toBe(true);
435-
expect(() => Ain.utils.ecVerifySig(tx, sig, addr, 0)).toThrow('[ain-util] ecRecoverPub: Invalid signature v value');
435+
expect(ain.wallet.verifySignature(tx, sig, addr, 0)).toBe(false);
436436
expect(ain.wallet.recover(sig)).toBe(addr);
437437
});
438438
});
@@ -476,7 +476,7 @@ describe('ain-js', function() {
476476
}
477477

478478
beforeAll(async () => {
479-
ain.setProvider(test_node_2, 0);
479+
ain.setProvider(test_node_2, null, 0);
480480
const newAccounts = ain.wallet.create(2);
481481
defaultAddr = ain.wallet.defaultAccount!.address as string;
482482
addr1 = newAccounts[0];

__tests__/ain_raw.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const TEST_ADDR = '0x08Aed7AF9354435c38d52143EE50ac839D20696b';
1515
jest.setTimeout(180000);
1616

1717
describe('ain-js', function() {
18-
const ain = new Ain(test_node_1, 0, { rawResultMode: true });
18+
const ain = new Ain(test_node_1, null, 0, { rawResultMode: true });
1919

2020
beforeAll(() => {
2121
ain.wallet.add(TEST_SK);
@@ -61,7 +61,7 @@ describe('ain-js', function() {
6161
}
6262

6363
beforeAll(async () => {
64-
ain.setProvider(test_node_2, 0);
64+
ain.setProvider(test_node_2, null, 0);
6565
const newAccounts = ain.wallet.create(2);
6666
defaultAddr = ain.wallet.defaultAccount!.address as string;
6767
addr1 = newAccounts[0];

__tests__/event_manager.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import Ain from '../src/ain';
33
import { FAILED_TO_REGISTER_ERROR_CODE } from '../src/constants';
44
import { FilterDeletionReasons, TransactionStates } from '../src/types';
5-
6-
const { test_event_handler_node } = require('./test_data');
5+
const {
6+
test_node_3,
7+
test_event_handler_node,
8+
} = require('./test_data');
79

810
jest.setTimeout(180000);
911

1012
describe('Event Handler', function() {
11-
let ain = new Ain(test_event_handler_node);
13+
let ain = new Ain(test_node_3, test_event_handler_node);
1214
let eventFilterId: string;
1315

1416
beforeAll(async () => {

__tests__/test_data.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export const test_seed = 'receive ocean impact unaware march just dragon easy po
44

55
export const test_node_1 = 'http://localhost:8081';
66
export const test_node_2 = 'http://localhost:8082';
7-
export const test_event_handler_node = 'http://localhost:8083';
7+
export const test_node_3 = 'http://localhost:8083';
8+
export const test_event_handler_node = 'ws://localhost:5100';

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ainblockchain/ain-js",
3-
"version": "1.9.0",
3+
"version": "1.10.0",
44
"description": "",
55
"main": "lib/ain.js",
66
"scripts": {

samples/event_manager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Ain = require('@ainblockchain/ain-js').default;
2-
const ain = new Ain('https://testnet-event.ainetwork.ai/');
2+
const ain = new Ain('https://testnet-api.ainetwork.ai/', 'wss://testnet-event.ainetwork.ai/');
33

44
async function main() {
55
await ain.em.connect(); // NOTE: https://docs.ainetwork.ai/reference/blockchain-sdk/ain-js/ain.em#connect

src/ain.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default class Ain {
2323
public axiosConfig: AxiosRequestConfig | undefined;
2424
/** The chain ID of the blockchain network. */
2525
public chainId: number;
26+
/** The endpoint Url of the event handler websocket server. */
27+
public eventHandlerUrl?: string | null;
2628
/** The network provider object. */
2729
public provider: Provider;
2830
/** The raw result mode option. */
@@ -43,12 +45,14 @@ export default class Ain {
4345
/**
4446
* Creates a new Ain object.
4547
* @param {string} providerUrl The endpoint URL of the network provider.
48+
* @param {string} eventHandlerUrl The endpoint URL of the event handler websocket server.
4649
* @param {number} chainId The chain ID of the blockchain network.
4750
* @param {AinOptions} ainOptions The options of the class.
4851
*/
49-
constructor(providerUrl: string, chainId?: number, ainOptions?: AinOptions) {
52+
constructor(providerUrl: string, eventHandlerUrl?: string, chainId?: number, ainOptions?: AinOptions) {
5053
this.axiosConfig = ainOptions?.axiosConfig;
5154
this.provider = new Provider(this, providerUrl, this.axiosConfig);
55+
this.eventHandlerUrl = eventHandlerUrl;
5256
this.chainId = chainId || 0;
5357
this.rawResultMode = ainOptions?.rawResultMode || false;
5458
this.net = new Network(this.provider);
@@ -61,15 +65,17 @@ export default class Ain {
6165

6266
/**
6367
* Sets a new provider.
64-
* @param {string} providerUrl The endpoint URL of the network provider.
65-
* @param {number} chainId The chain ID of the blockchain network.
68+
* @param {string} providerUrl The endpoint URL of the network provider. e.g. http://localhost:8081, https://testnet-api.ainetwork.ai
69+
* @param {string} eventHandlerUrl The endpoint URL of the event handler websocket server. e.g. ws://localhost:5100, wss://testnet-event.ainetwork.ai
70+
* @param {number} chainId The chain ID of the blockchain network. e.g. 0 for local or testnet, and 1 for mainnet
6671
* @param {AxiosRequestConfig} axiosConfig The axios request config.
6772
*/
68-
setProvider(providerUrl: string, chainId?: number, axiosConfig?: AxiosRequestConfig | undefined) {
73+
setProvider(providerUrl: string, eventHandlerUrl?: string, chainId?: number, axiosConfig?: AxiosRequestConfig | undefined) {
6974
if (axiosConfig) {
7075
this.axiosConfig = axiosConfig;
7176
}
7277
this.provider = new Provider(this, providerUrl, this.axiosConfig);
78+
this.eventHandlerUrl = eventHandlerUrl;
7379
this.chainId = chainId || 0;
7480
this.db = new Database(this, this.provider);
7581
this.net = new Network(this.provider);

src/event-manager/event-channel-client.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export default class EventChannelClient {
2424
private readonly _eventCallbackManager: EventCallbackManager;
2525
/** The web socket client. */
2626
private _ws?: WebSocket | WebSocketBE;
27-
/** The blockchain endpoint URL. */
28-
private _endpointUrl?: string;
2927
/** Whether it's connected or not. */
3028
private _isConnected: boolean;
3129
/** The handshake timeout object. */
@@ -42,7 +40,6 @@ export default class EventChannelClient {
4240
this._ain = ain;
4341
this._eventCallbackManager = eventCallbackManager;
4442
this._ws = undefined;
45-
this._endpointUrl = undefined;
4643
this._isConnected = false;
4744
this._handshakeTimeout = undefined;
4845
this._heartbeatTimeout = undefined;
@@ -63,6 +60,13 @@ export default class EventChannelClient {
6360
reject(new Error(`Can't connect multiple channels`));
6461
return;
6562
}
63+
const url = this._ain.eventHandlerUrl;
64+
if (!url) {
65+
reject(new Error(`eventHandlerUrl is not set properly: ${url}`));
66+
return;
67+
}
68+
// TODO(platfowner): Remove or re-implement without using getEventHandlerNetworkInfo() below.
69+
/*
6670
const eventHandlerNetworkInfo = await this._ain.net.getEventHandlerNetworkInfo();
6771
const url = eventHandlerNetworkInfo.url;
6872
if (!url) {
@@ -86,8 +90,8 @@ export default class EventChannelClient {
8690
reject(new Error(`Exceed event channel limit! (node:${url})`));
8791
return;
8892
}
93+
*/
8994

90-
this._endpointUrl = url;
9195
// NOTE(platfowner): Fix WebSocket module import issue (see https://github.com/ainblockchain/ain-js/issues/177).
9296
this._ws = isBrowser ? new WebSocket(url) : new WebSocketBE(url);
9397
// NOTE(platfowner): A custom handshake timeout (see https://github.com/ainblockchain/ain-js/issues/171).

src/he/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ This is a function developed for testing purposes. It returns the secret key cur
2929

3030
## Usage
3131
```ts
32-
const ain = new Ain('http://node.ainetwork.ai:8080');
32+
const ain = new Ain('http://localhost:8081', 'ws://localhost:5100');
33+
// or const ain = new Ain('https://testnet-api.ainetwork.ai/', 'wss://testnet-event.ainetwork.ai/');
3334
await ain.he.init();
3435
const TEST_DATA = Float64Array.from({ length: ain.he.seal.encoder.slotCount }).map((x, i) => i);
3536
const cipherText = ain.he.encrypt(TEST_DATA);

src/wallet.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,17 @@ export default class Wallet {
367367
* @param {any} data The data used in the signing.
368368
* @param {string} signature The signature to verify.
369369
* @param {string} address The address to verify.
370+
* @param {number} chainId The chain Id for test purposes only.
370371
* @returns {boolean}
371372
*/
372-
verifySignature(data: any, signature: string, address: string): boolean {
373-
return Ain.utils.ecVerifySig(data, signature, address, this.chainId);
373+
verifySignature(data: any, signature: string, address: string, chainId?: number): boolean {
374+
try {
375+
return Ain.utils.ecVerifySig(data, signature, address, chainId !== undefined ? chainId : this.chainId);
376+
} catch (err: unknown) {
377+
let errMsg = err instanceof Error ? err.message : err;
378+
console.log(`Signature verification failed with error: ${errMsg}`);
379+
return false;
380+
}
374381
}
375382

376383
/**

0 commit comments

Comments
 (0)