-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Secp256k1PubKey } from "cosmes/client"; | ||
|
||
import { WalletName } from "../../constants/WalletName"; | ||
import { WalletType } from "../../constants/WalletType"; | ||
import { WalletConnectV1 } from "../../walletconnect/WalletConnectV1"; | ||
import { WalletConnectV2 } from "../../walletconnect/WalletConnectV2"; | ||
import { ConnectedWallet } from "../ConnectedWallet"; | ||
import { ChainInfo, WalletController } from "../WalletController"; | ||
import { NinjiExtension } from "./NinjiExtension"; | ||
|
||
export class NinjiController extends WalletController { | ||
constructor() { | ||
super(WalletName.NINJI); | ||
this.registerAccountChangeHandlers(); | ||
} | ||
|
||
public async isInstalled(type: WalletType) { | ||
return type === WalletType.EXTENSION ? "ninji" in window : false; | ||
} | ||
|
||
protected async connectWalletConnect<T extends string>( | ||
_chains: ChainInfo<T>[] | ||
): Promise<{ | ||
wallets: Map<T, ConnectedWallet>; | ||
wc: WalletConnectV1 | WalletConnectV2; | ||
}> { | ||
// Ninji does not support WC yet | ||
throw new Error("WalletConnect not supported"); | ||
} | ||
|
||
protected async connectExtension<T extends string>(chains: ChainInfo<T>[]) { | ||
const wallets = new Map<T, ConnectedWallet>(); | ||
const ext = window.ninji; | ||
if (!ext) { | ||
throw new Error("Ninji extension is not installed"); | ||
} | ||
await ext.enable(chains.map(({ chainId }) => chainId)); | ||
for (const { chainId, rpc, gasPrice } of Object.values(chains)) { | ||
const { bech32Address, pubKey, isNanoLedger } = await ext.getKey(chainId); | ||
const key = new Secp256k1PubKey({ | ||
key: pubKey, | ||
}); | ||
wallets.set( | ||
chainId, | ||
new NinjiExtension( | ||
this.id, | ||
ext, | ||
chainId, | ||
key, | ||
bech32Address, | ||
rpc, | ||
gasPrice, | ||
isNanoLedger | ||
) | ||
); | ||
} | ||
return wallets; | ||
} | ||
|
||
protected registerAccountChangeHandlers() { | ||
/** | ||
* ! IMPORTANT ! | ||
* | ||
* Since Leap also uses the same event key, this causes issues when a user | ||
* has both leap and ninji wallets connected simultaneously. For example, | ||
* a change in Leap's keystore will trigger Ninji to emit this event as | ||
* well, leading to a race condition when `changeAccount` is called. | ||
* | ||
* The Ninji team has been notified to possibly change this event emitted | ||
* to `accountsChanged` instead. | ||
*/ | ||
|
||
if (typeof window !== "undefined" && window.ninji) { | ||
window.ninji.on("accountsChanged", () => | ||
this.changeAccount(WalletType.EXTENSION) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { LeapExtension } from "../leap/LeapExtension"; | ||
|
||
// Ninji's API is similar to Leap. | ||
export const NinjiExtension = LeapExtension; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Keplr } from "cosmes/registry"; | ||
|
||
// Type is similar to Keplr | ||
export type Ninji = Keplr & { | ||
on: (event: string, callback: () => void) => void; | ||
}; | ||
|
||
export type Window = { | ||
ninji?: Ninji | undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters