forked from ping-pub/widget
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ledanghuy1811/feat/support-owallet
Feat/support-owallet
- Loading branch information
Showing
3 changed files
with
293 additions
and
82 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,115 @@ | ||
import { Registry } from '@cosmjs/proto-signing' | ||
import { defaultRegistryTypes } from "@cosmjs/stargate"; | ||
import { Transaction } from "../utils/type"; | ||
import { Registry } from '@cosmjs/proto-signing'; | ||
import { defaultRegistryTypes } from '@cosmjs/stargate'; | ||
import { Transaction } from '../utils/type'; | ||
import { KeplerWallet } from './wallets/KeplerWallet'; | ||
import { LedgerWallet } from './wallets/LedgerWallet'; | ||
import { MetamaskWallet } from './wallets/MetamaskWallet'; | ||
import { MetamaskSnapWallet } from './wallets/MetamaskSnapWallet'; | ||
import { LeapWallet } from './wallets/LeapWallet'; | ||
import { Owallet } from './wallets/Owallet'; | ||
|
||
export enum WalletName { | ||
Keplr = "Keplr", | ||
Ledger = "LedgerUSB", | ||
LedgerBLE = "LedgerBLE", | ||
Metamask = "Metamask", | ||
MetamaskSnap = "MetamaskSnap", | ||
Leap = "Leap", | ||
Owallet = 'Owallet', | ||
Keplr = 'Keplr', | ||
Ledger = 'LedgerUSB', | ||
LedgerBLE = 'LedgerBLE', | ||
Metamask = 'Metamask', | ||
MetamaskSnap = 'MetamaskSnap', | ||
Leap = 'Leap', | ||
// None Signning | ||
Address = "Address", | ||
NameService = "Nameservice", | ||
Address = 'Address', | ||
NameService = 'Nameservice', | ||
} | ||
|
||
export interface ConnectedWallet { | ||
wallet: WalletName, | ||
cosmosAddress: string | ||
hdPath?: string | ||
wallet: WalletName; | ||
cosmosAddress: string; | ||
hdPath?: string; | ||
} | ||
|
||
export interface Account { | ||
address: string, | ||
algo: string, | ||
pubkey: Uint8Array, | ||
address: string; | ||
algo: string; | ||
pubkey: Uint8Array; | ||
} | ||
|
||
export interface WalletArgument { | ||
chainId?: string, | ||
hdPath?: string, | ||
address?: string, | ||
name?: string, | ||
transport?: string | ||
prefix?: string, | ||
chainId?: string; | ||
hdPath?: string; | ||
address?: string; | ||
name?: string; | ||
transport?: string; | ||
prefix?: string; | ||
} | ||
|
||
export interface AbstractWallet { | ||
name: WalletName | ||
name: WalletName; | ||
/** | ||
* The the accounts from the wallet (addresses) | ||
*/ | ||
getAccounts(): Promise<Account[]> | ||
supportCoinType(coinType?: string): Promise<boolean> | ||
sign(transaction: Transaction): Promise<any> | ||
getAccounts(): Promise<Account[]>; | ||
supportCoinType(coinType?: string): Promise<boolean>; | ||
sign(transaction: Transaction): Promise<any>; | ||
} | ||
|
||
export const DEFAULT_HDPATH = "m/44'/118/0'/0/0"; | ||
|
||
export function keyType(chainId: string) { | ||
switch (true) { | ||
case chainId.search(/\w+_\d+-\d+/g) > -1: // ethermint like chain: evmos_9002-1 | ||
return "/ethermint.crypto.v1.ethsecp256k1.PubKey" | ||
case chainId.startsWith("injective"): | ||
return "/injective.crypto.v1beta1.ethsecp256k1.PubKey"; | ||
case chainId.search(/\w+_\d+-\d+/g) > -1: // ethermint like chain: evmos_9002-1 | ||
return '/ethermint.crypto.v1.ethsecp256k1.PubKey'; | ||
case chainId.startsWith('injective'): | ||
return '/injective.crypto.v1beta1.ethsecp256k1.PubKey'; | ||
default: | ||
return "/cosmos.crypto.secp256k1.PubKey" | ||
return '/cosmos.crypto.secp256k1.PubKey'; | ||
} | ||
} | ||
|
||
export function readWallet(hdPath?: string) { | ||
return JSON.parse( | ||
localStorage.getItem(hdPath || DEFAULT_HDPATH) || '{}' | ||
) as ConnectedWallet | ||
) as ConnectedWallet; | ||
} | ||
export function writeWallet(connected: ConnectedWallet, hdPath?: string) { | ||
localStorage.setItem(hdPath || DEFAULT_HDPATH, JSON.stringify(connected)) | ||
localStorage.setItem(hdPath || DEFAULT_HDPATH, JSON.stringify(connected)); | ||
} | ||
|
||
export function removeWallet(hdPath?: string) { | ||
localStorage.removeItem(hdPath || DEFAULT_HDPATH); | ||
} | ||
|
||
export function extractChainId(chainId: string) { | ||
const start = chainId.indexOf('_') | ||
const end = chainId.indexOf('-') | ||
const start = chainId.indexOf('_'); | ||
const end = chainId.indexOf('-'); | ||
if (end > start && start > 0) { | ||
return Number(chainId.substring(start + 1, end)) | ||
return Number(chainId.substring(start + 1, end)); | ||
} | ||
return 0 | ||
return 0; | ||
} | ||
|
||
export function createWallet(name: WalletName, arg: WalletArgument, registry?: Registry): AbstractWallet { | ||
const reg = registry || new Registry(defaultRegistryTypes) | ||
export function createWallet( | ||
name: WalletName, | ||
arg: WalletArgument, | ||
registry?: Registry | ||
): AbstractWallet { | ||
const reg = registry || new Registry(defaultRegistryTypes); | ||
switch (name) { | ||
case WalletName.Owallet: | ||
return new Owallet(arg, reg); | ||
case WalletName.Keplr: | ||
return new KeplerWallet(arg, reg) | ||
return new KeplerWallet(arg, reg); | ||
case WalletName.Ledger: | ||
return new LedgerWallet(arg, reg) | ||
return new LedgerWallet(arg, reg); | ||
case WalletName.Leap: | ||
return new LeapWallet(arg, reg) | ||
return new LeapWallet(arg, reg); | ||
case WalletName.MetamaskSnap: | ||
return new MetamaskSnapWallet(arg, reg) | ||
return new MetamaskSnapWallet(arg, reg); | ||
case WalletName.Metamask: | ||
return arg.hdPath && | ||
(arg.hdPath.startsWith('m/44/60') || arg.hdPath.startsWith("m/44'/60")) | ||
? new MetamaskWallet(arg, reg) : new MetamaskSnapWallet(arg, reg) | ||
return arg.hdPath && | ||
(arg.hdPath.startsWith('m/44/60') || | ||
arg.hdPath.startsWith("m/44'/60")) | ||
? new MetamaskWallet(arg, reg) | ||
: new MetamaskSnapWallet(arg, reg); | ||
} | ||
throw new Error("No wallet connected") | ||
throw new Error('No wallet connected'); | ||
} |
Oops, something went wrong.