Skip to content

Commit

Permalink
feat: add enable method for validation checks on strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Nov 15, 2023
1 parent 9fce77f commit a84d25b
Show file tree
Hide file tree
Showing 18 changed files with 483 additions and 27 deletions.
1 change: 1 addition & 0 deletions packages/wallet-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@solana/wallet-adapter-wallets": "^0.19.5",
"@toruslabs/torus-embed": "^1.39.0",
"@trezor/connect-web": "^9.0.6",
"@web3auth/torus-wallet-connector-plugin": "^7.1.1",
"alchemy-sdk": "^2.6.3",
"eip1193-provider": "^1.0.1",
"eth-sig-util": "^3.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export default class CosmosWalletStrategy {
return this.getStrategy().getPubKey()
}

public enable(): Promise<void> {
return this.getStrategy().enable()
}

public getAddresses(): Promise<AccountAddress[]> {
return this.getStrategy().getAddresses()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,15 @@ export default class Cosmostation implements ConcreteCosmosWalletStrategy {
return Promise.resolve(WalletDeviceType.Browser)
}

async enable() {
await new CosmostationWallet(this.chainId).checkChainIdSupport()
}

async getAddresses(): Promise<string[]> {
const { chainName } = this
const cosmostationWallet = await this.getCosmostationWallet()
const cosmostationWalletUtil = new CosmostationWallet(this.chainId)

try {
if (!(await cosmostationWalletUtil.checkChainIdSupport())) {
throw new CosmosWalletException(
new Error(`The ${this.chainId} is not supported on Cosmostation.`),
{ type: ErrorType.WalletError },
)
}

const accounts = await cosmostationWallet.requestAccount(chainName)

return [accounts.address]
Expand All @@ -81,6 +77,10 @@ export default class Cosmostation implements ConcreteCosmosWalletStrategy {
)
}

if (e instanceof CosmosWalletException) {
throw e
}

throw new CosmosWalletException(new Error((e as any).message), {
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export default class Keplr implements ConcreteCosmosWalletStrategy {
: Promise.resolve(WalletDeviceType.Browser)
}

async enable() {
await this.getKeplrWallet().checkChainIdSupport()
}

async getAddresses(): Promise<string[]> {
const keplrWallet = this.getKeplrWallet()

Expand All @@ -46,6 +50,10 @@ export default class Keplr implements ConcreteCosmosWalletStrategy {

return accounts.map((account) => account.address)
} catch (e: unknown) {
if (e instanceof CosmosWalletException) {
throw e
}

throw new CosmosWalletException(new Error((e as any).message), {
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ export default class Leap implements ConcreteCosmosWalletStrategy {
return Promise.resolve(WalletDeviceType.Browser)
}

async enable() {
await this.getLeapWallet().checkChainIdSupport()
}

async getAddresses(): Promise<string[]> {
const { chainId } = this
const leapWallet = this.getLeapWallet()

try {
if (!(await leapWallet.checkChainIdSupport())) {
throw new CosmosWalletException(
new Error(`The ${chainId} is not supported on Leap.`),
{ type: ErrorType.WalletError },
)
}

const accounts = await leapWallet.getAccounts()

return accounts.map((account) => account.address)
} catch (e: unknown) {
if (e instanceof CosmosWalletException) {
throw e
}

throw new CosmosWalletException(new Error((e as any).message), {
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@ export default class Ninji implements ConcreteCosmosWalletStrategy {
return Promise.resolve(WalletDeviceType.Browser)
}

async enable() {
await this.getNinjiWallet().checkChainIdSupport()
}

async getAddresses(): Promise<string[]> {
const { chainId } = this
const ninjiWallet = this.getNinjiWallet()

try {
if (!(await ninjiWallet.checkChainIdSupport())) {
throw new CosmosWalletException(
new Error(`The ${chainId} is not supported on Ninji.`),
{ type: ErrorType.WalletError },
)
}
await ninjiWallet.checkChainIdSupport()

const accounts = await ninjiWallet.getAccounts()

return accounts.map((account) => account.address)
} catch (e: unknown) {
if (e instanceof CosmosWalletException) {
throw e
}

throw new CosmosWalletException(new Error((e as any).message), {
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
Expand Down
6 changes: 6 additions & 0 deletions packages/wallet-ts/src/strategies/types/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export interface ConcreteCosmosWalletStrategy {
*/
getPubKey(): Promise<string>

/**
* Perform validations and checks
* for the wallet (if the chain is supported, etc)
*/
enable(): Promise<void>

/**
* Sends Cosmos transaction. Returns a transaction hash
* @param transaction should implement TransactionConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ export default class WalletStrategy {
return this.getStrategy().getPubKey()
}

public enable(): Promise<void> {
return this.getStrategy().enable()
}

public getEthereumChainId(): Promise<string> {
return this.getStrategy().getEthereumChainId()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default class Cosmostation
return Promise.resolve(WalletDeviceType.Browser)
}

async enable(): Promise<void> {
//
}

async getAddresses(): Promise<string[]> {
const cosmostationWallet = await this.getCosmostationWallet()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export default class Keplr
: Promise.resolve(WalletDeviceType.Browser)
}

async enable(): Promise<void> {
//
}

async getAddresses(): Promise<string[]> {
const keplrWallet = this.getKeplrWallet()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default class Leap
return Promise.resolve(WalletDeviceType.Browser)
}

async enable(): Promise<void> {
//
}

async getAddresses(): Promise<string[]> {
const { chainId } = this
const leapWallet = this.getLeapWallet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export default class LedgerBase
return Promise.resolve(WalletDeviceType.Hardware)
}

async enable(): Promise<void> {
//
}

public async getAddresses(): Promise<string[]> {
const { baseDerivationPath, derivationPathType } = this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default class Metamask
return Promise.resolve(WalletDeviceType.Browser)
}

async enable(): Promise<void> {
//
}

async getAddresses(): Promise<string[]> {
const ethereum = await this.getEthereum()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default class Ninji
return Promise.resolve(WalletDeviceType.Browser)
}

async enable(): Promise<void> {
//
}

async getAddresses(): Promise<string[]> {
const { chainId } = this
const ninjiWallet = this.getNinjiWallet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export default class Torus
return Promise.resolve(WalletDeviceType.Browser)
}

async enable(): Promise<void> {
//
}

async connect(): Promise<void> {
const { connected, torus, ethereumChainId } = this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export default class Trezor
return Promise.resolve(WalletDeviceType.Hardware)
}

async enable(): Promise<void> {
//
}

public async getAddresses(): Promise<string[]> {
try {
await this.trezor.connect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default class TrustWallet
return Promise.resolve(WalletDeviceType.Browser)
}

async enable(): Promise<void> {
//
}

async getAddresses(): Promise<string[]> {
const ethereum = await this.getEthereum()

Expand Down
Loading

0 comments on commit a84d25b

Please sign in to comment.