From 60fd8f11d9e53feb3cbffbc0d7b140228f8e354d Mon Sep 17 00:00:00 2001 From: akaster99 Date: Wed, 20 Sep 2023 15:06:12 +0900 Subject: [PATCH 1/3] fix: handler init --- src/ain.ts | 4 +++- src/handlers/handler.ts | 17 +++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/ain.ts b/src/ain.ts index 1047a6a..1b56323 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -28,7 +28,9 @@ export default class AinModule { createAccount() { this.checkAinInitiated(); const newAccount = this.ain!.wallet.create(1)[0]; - return newAccount; + const wallet = this.ain!.wallet.accounts[newAccount]; + this.ain!.wallet.remove(newAccount); + return wallet; } setDefaultAccount(privateKey: string) { diff --git a/src/handlers/handler.ts b/src/handlers/handler.ts index 9a8c2cd..6ce1967 100644 --- a/src/handlers/handler.ts +++ b/src/handlers/handler.ts @@ -3,11 +3,11 @@ import Ain from "@ainblockchain/ain-js"; import Ainize from "../ainize"; import { Path } from "../constants"; import AinModule from "../ain"; -import EventManager from "@ainblockchain/ain-js/lib/event-manager"; export default class Handler { private static instance: Handler | undefined; - em = AinModule.getInstance().getEventManager(); + ain = AinModule.getInstance(); + static getInstance() { if(!Handler.instance){ Handler.instance = new Handler(); @@ -16,18 +16,15 @@ export default class Handler { } checkEventManager() { - if (!this.em) { - if(!AinModule.getInstance().getEventManager()){ - throw new Error('you should init ain first'); - } - this.em = AinModule.getInstance().getEventManager(); + if(!AinModule.getInstance().getEventManager()){ + throw new Error('you should init ain first'); } return true; } async connect() { this.checkEventManager(); - await this.em!.connect({},this.disconnectedCb); + await this.ain.getEventManager().connect({},this.disconnectedCb); console.log('connected'); }; @@ -39,7 +36,7 @@ export default class Handler { async subscribe(requester:string, recordId:string, appName: string, resolve: any) { this.checkEventManager(); const responsePath = Path.app(appName).response(requester, recordId); - const subscribeId = await this.em!.subscribe( + const subscribeId = await this.ain.getEventManager().subscribe( "VALUE_CHANGED", { path: responsePath, @@ -57,7 +54,7 @@ export default class Handler { async unsubscribe(filterId: string) { this.checkEventManager(); - await this.em!.unsubscribe( + await this.ain.getEventManager().unsubscribe( filterId, (err)=>{ if (err) { From 1feb37744a5c8cf1a18a1e6aaaac0889de4dd05e Mon Sep 17 00:00:00 2001 From: akaster99 Date: Wed, 20 Sep 2023 15:56:51 +0900 Subject: [PATCH 2/3] fix: login --- src/ainize.ts | 11 ++++++++--- src/model.ts | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ainize.ts b/src/ainize.ts index ae8550f..996e6cb 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -15,7 +15,6 @@ export default class Ainize { constructor(chainId: 1 | 0) { this.ain.initAin(chainId); - this.handler.connect(); this.cache = new NodeCache(); this.middleware = new Middleware(this.cache); } @@ -24,14 +23,20 @@ export default class Ainize { return AinModule.getInstance().createAccount(); } - login(privateKey: string) { + async login(privateKey: string) { this.ain.setDefaultAccount(privateKey); + await this.handler.connect(); + console.log('login success! address:', this.ain.getAddress()); } logout() { this.ain.removeDefaultAccount(); } + async getAddress(): Promise { + return await this.ain.getAddress(); + } + async getAinBalance(): Promise { return await this.ain.getBalance(); } @@ -52,7 +57,7 @@ export default class Ainize { } await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig }); - return new Model(modelName); + return this.model(modelName); } model(modelName: string): Model { diff --git a/src/model.ts b/src/model.ts index 6280a6a..6bfd921 100644 --- a/src/model.ts +++ b/src/model.ts @@ -2,7 +2,7 @@ import ModelController from "./controllers/modelController"; export default class Model { modelName: string; - modelController: ModelController; + private modelController: ModelController; constructor(modelName: string) { this.modelName = modelName; From 2b39280f6d07fedf46cc093045df4e30fb8aab88 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Wed, 20 Sep 2023 15:59:18 +0900 Subject: [PATCH 3/3] feat: logout disconnect --- src/ainize.ts | 4 +++- src/handlers/handler.ts | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ainize.ts b/src/ainize.ts index 996e6cb..8e3b7c1 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -29,8 +29,10 @@ export default class Ainize { console.log('login success! address:', this.ain.getAddress()); } - logout() { + async logout() { + await this.handler.disconnect(); this.ain.removeDefaultAccount(); + console.log('logout success!'); } async getAddress(): Promise { diff --git a/src/handlers/handler.ts b/src/handlers/handler.ts index 6ce1967..a28b780 100644 --- a/src/handlers/handler.ts +++ b/src/handlers/handler.ts @@ -27,6 +27,12 @@ export default class Handler { await this.ain.getEventManager().connect({},this.disconnectedCb); console.log('connected'); }; + + async disconnect() { + this.checkEventManager(); + await this.ain.getEventManager().disconnect(); + console.log('disconnected'); + } private async disconnectedCb() { console.log('disconnected. reconnecting...');