diff --git a/package.json b/package.json index 60f1021..2fee9e7 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "typescript": "^4.6.3" }, "dependencies": { - "@ainblockchain/ain-js": "^1.3.5", + "@ainblockchain/ain-js": "^1.6.3", "axios": "^0.26.1", "express": "^4.18.2", "fast-json-stable-stringify": "^2.1.0", diff --git a/src/ain.ts b/src/ain.ts index c271369..aadf681 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -2,6 +2,8 @@ import Ain from "@ainblockchain/ain-js"; import { getBlockChainEndpoint } from "./constants"; import { TransactionBody } from "@ainblockchain/ain-util"; import { txResult } from "./types/type"; +import { Signer } from "@ainblockchain/ain-js/lib/signer/signer"; +import { DefaultSigner } from "@ainblockchain/ain-js/lib/signer/default-signer" // NOTE(yoojin): Plz suggest a good name. export default class AinModule { @@ -20,12 +22,6 @@ export default class AinModule { this.ain = new Ain(blockchainEndpoint, chainId); } - isDefaultAccountExist(): boolean { - if (this.getDefaultAccount()) - return true; - return false; - } - createAccount() { this.checkAinInitiated(); const newAccount = this.ain!.wallet.create(1)[0]; @@ -39,24 +35,46 @@ export default class AinModule { this.ain!.wallet.addAndSetDefaultAccount(privateKey); } + setSigner(signer: Signer) { + this.checkAinInitiated(); + this.ain!.setSigner(signer); + } + getDefaultAccount() { this.checkAinInitiated(); return this.ain!.wallet.defaultAccount; } + getSigner() { + this.checkAinInitiated(); + return this.ain!.signer + } + + getAddress() { + this.checkAinInitiated(); + try { + return this.getSigner().getAddress(); + } catch (e) { + return null; + } + } + removeDefaultAccount() { this.checkAinInitiated(); this.ain!.wallet.removeDefaultAccount(); } - getAddress() { - this.isDefaultAccountExist(); - return this.ain!.wallet.defaultAccount!.address; + removeSigner() { + this.checkAinInitiated(); + const wallet = this.ain!.wallet; + const provider = this.ain!.provider; + wallet.removeDefaultAccount(); + this.ain!.setSigner(new DefaultSigner(wallet, provider)) } async getBalance() { - this.isDefaultAccountExist(); - return await this.ain!.wallet.getBalance(); + const address = this.getAddress(); + return address ? await this.ain!.wallet.getBalance(address) : null; } async getValue(path: string) { @@ -64,14 +82,14 @@ export default class AinModule { return await this.ain!.db.ref(path).getValue(); } - private async _sendTransaction(data: TransactionBody) { + private async _sendTransaction(txBody: TransactionBody) { this.checkAinInitiated(); - return await this.ain!.sendTransaction(data); + return await this.ain!.signer.sendTransaction(txBody); } private checkAinInitiated(): boolean { if (!this.ain) - throw new Error('Set initAin(chainId) First.'); + throw new Error('Set initAin(chainId) first.'); return true; } diff --git a/src/ainize.ts b/src/ainize.ts index 4bfc00f..4d113d7 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -8,6 +8,7 @@ import { deployConfig } from "./types/type"; import AinModule from "./ain"; import Internal from "./internal"; import { Account } from "@ainblockchain/ain-util"; +import { AinWalletSigner } from "@ainblockchain/ain-js/lib/signer/ain-wallet-signer"; export default class Ainize { private cache: NodeCache; @@ -42,21 +43,43 @@ export default class Ainize { console.log('login success! address:', this.ain.getAddress()); } + /** + * Login to ainize using AIN Wallet Signer. + */ + async loginWithSigner() { + const signer = new AinWalletSigner; + this.ain.setSigner(signer); + console.log('login success! address: ', this.ain.getAddress()); + } + /** * Logout from ainize. */ async logout() { - this.ain.removeDefaultAccount(); + this.ain.removeSigner(); await this.handler.disconnect(); console.log('logout success!'); } + /** + * Throw error if user doesn't log in. + * @returns {} + */ + async checkLoggedIn() { + const address = await this.ain.getAddress(); + if (!address) { + throw new Error('You should login first.'); + } + } + async getAddress(): Promise { - return await this.ain.getAddress(); + await this.checkLoggedIn(); + return await this.ain.getAddress()!; } async getAinBalance(): Promise { - return await this.ain.getBalance(); + await this.checkLoggedIn(); + return await this.ain.getBalance() || 0; } // FIXME(yoojin): add config type and change param type. @@ -67,12 +90,10 @@ export default class Ainize { */ // TODO(yoojin, woojae): Deploy container, advanced. async deploy({serviceName, billingConfig, serviceUrl}: deployConfig): Promise { - if(!this.ain.isDefaultAccountExist()) { - throw new Error('you should login first'); - } + await this.checkLoggedIn(); // TODO(yoojin, woojae): Add container deploy logic. const result = await new Promise(async (resolve, reject) => { - const deployer = this.ain.getAddress(); + const deployer = this.ain.getAddress()!; if (!billingConfig) { billingConfig = { ...DEFAULT_BILLING_CONFIG, diff --git a/src/controllers/serviceController.ts b/src/controllers/serviceController.ts index 7f636b1..7f65b6d 100644 --- a/src/controllers/serviceController.ts +++ b/src/controllers/serviceController.ts @@ -120,13 +120,16 @@ export default class ServiceController { return (await this.ain.getValue(Path.app(serviceName).billingConfig())).depositAddress; } - isLoggedIn(): void { - if(!this.ain.getDefaultAccount()) - throw new Error('You should login First.'); + checkLoggedIn(): void { + try { + !this.ain.getAddress(); + } catch(error) { + throw new Error('You should login first.'); + } } async isAdmin(serviceName: string): Promise { - this.isLoggedIn(); + this.checkLoggedIn(); const adminPath = `/manage_app/${serviceName}/config/admin`; const adminList = await this.ain.getValue(adminPath); if(!adminList[this.ain.getAddress()]) { diff --git a/src/service.ts b/src/service.ts index 5f94786..8e7524c 100644 --- a/src/service.ts +++ b/src/service.ts @@ -42,7 +42,7 @@ export default class Service { * @returns {string} Transaction hash. */ async chargeCredit(amount: number) { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.chargeCredit(this.serviceName, amount); } @@ -52,7 +52,7 @@ export default class Service { * @returns {string} Transaction hash. */ async withdrawCredit(amount: number) { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.withdrawCredit(this.serviceName, amount); } @@ -61,7 +61,7 @@ export default class Service { * @returns {number} Amount of credit balance. */ async getCreditBalance() { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.getCreditBalance(this.serviceName); } @@ -70,7 +70,7 @@ export default class Service { * @returns {creditHistories} Histories of credit deposit and usage. */ async getCreditHistory() { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.getCreditHistory(this.serviceName); } @@ -80,7 +80,7 @@ export default class Service { * @returns {string} Response data from service. */ async request(requestData: any, requestKey?: string) { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.request(this.serviceName, requestData, requestKey); } @@ -113,7 +113,7 @@ export default class Service { return this.serviceController.isAdmin(this.serviceName); } - private isLoggedIn() { - return this.serviceController.isLoggedIn(); + private checkLoggedIn() { + return this.serviceController.checkLoggedIn(); } } diff --git a/yarn.lock b/yarn.lock index 414e102..f3d2629 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,10 @@ # yarn lockfile v1 -"@ainblockchain/ain-js@^1.3.5": - version "1.6.0" - resolved "https://registry.npmjs.org/@ainblockchain/ain-js/-/ain-js-1.6.0.tgz" - integrity sha512-REzTJAf8w2TIsJLH7DhKWJF+kxfgMnCCwzWaeD4rYAv4TeD70PhFmYDrDMuy/qZd5KKMXqMigiU9PLWbiu8a7A== +"@ainblockchain/ain-js@^1.6.3": + version "1.6.3" + resolved "https://registry.yarnpkg.com/@ainblockchain/ain-js/-/ain-js-1.6.3.tgz#56ca744a6bf5e558f2acba75f106e8f88f5426ba" + integrity sha512-rdQfT6jcqcF4VP1twwMQkCijZ6SN1RewTjU1D35rJ7ZnRQjoIxekkodkdcIDVvyUEpR6A6iuT9SSSTz9KUMNbA== dependencies: "@ainblockchain/ain-util" "^1.1.9" "@types/node" "^12.7.3"