-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Signer login #94
Add Signer login #94
Changes from all commits
eef0e3d
a23bea6
64576fa
dc326dc
d1566ac
569d173
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,39 +35,61 @@ 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) { | ||
this.checkAinInitiated(); | ||
return await this.ain!.db.ref(path).getValue(); | ||
} | ||
|
||
private async _sendTransaction(data: TransactionBody) { | ||
private async _sendTransaction(txBody: TransactionBody) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하는 코드들도 바꾸어주어야 하지 않나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 파라메터 변수 명만 변경되어 호출부에선 변경 필요 없을 것 같습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 맞네요 확인했습니다~ |
||
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; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. login with wallet이 더 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wallet 이란 명명이 web3에서 관념적으로 사용되는 의미나 ain-js에서 사용되는 등 혼동될 가능성이 있을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 |
||
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<string> { | ||
return await this.ain.getAddress(); | ||
await this.checkLoggedIn(); | ||
return await this.ain.getAddress()!; | ||
} | ||
|
||
async getAinBalance(): Promise<number> { | ||
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<Service> { | ||
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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ain 초기화가 되면 signer가 무조건 있나요?
signer가 있다면 무조건 defaultAddress가 있는게 맞나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ain 초기화시 defaultSigner가 반드시 생성됩니다. 이 때 defaultSigner에서 getAddress() 는 defaultWalletAddress 를 반환합니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default account가 세팅되지 않은 경우 해당 부분에서 에러를 던지네요. 기존 getAddress에서는 해당 경우에 null을 반환했기 때문에 try-catch 문으로 null 반환하도록 변경하겠습니다.
추가적으로 defaultAccount 존재 유무를 확인하는 코드들의 수정이 덜 이루어진것을 발견하여 같이 수정하도록 하겠습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 그부분만 수정하고 merge해주세요!