Skip to content

Commit

Permalink
chore: begin msgBroadcaster abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
billyjacoby committed Oct 10, 2024
1 parent f625e05 commit 2f603b1
Show file tree
Hide file tree
Showing 28 changed files with 9,318 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/ts-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './aliases'
export * from './transactions'
export * from './cosmos'
export * from './trade'
export * from './wallet/index'

export interface StreamStatusResponse {
details: string
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './enums'
export * from './strategy'

export type UnwrappedPromise<T> = T extends Promise<infer Return> ? Return : T
226 changes: 226 additions & 0 deletions packages/ts-types/src/wallet/strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import type { DirectSignResponse } from '@cosmjs/proto-signing'
import {
AccountAddress,
ChainId,
CosmosChainId,
EthereumChainId,
} from '../index'
import type { TxRaw, TxResponse } from '@injectivelabs/sdk-ts'
import { AminoSignResponse, StdSignDoc } from '@keplr-wallet/types'
import { Wallet, WalletDeviceType } from './enums'

export type onAccountChangeCallback = (account: string) => void
export type onChainIdChangeCallback = () => void

export interface WalletStrategyEthereumOptions {
ethereumChainId: EthereumChainId
rpcUrl?: string
}

export interface SendTransactionOptions {
address: string
chainId: ChainId
txTimeout?: number
endpoints: {
rest: string
grpc: string
tm?: string
}
}

export interface EthereumWalletStrategyArgs {
chainId: ChainId
ethereumOptions: WalletStrategyEthereumOptions
}

export interface ConcreteCosmosWalletStrategy {
/**
* The accounts from the wallet (addresses)
*/
getAddresses(args?: unknown): Promise<string[]>

/**
* Return the WalletDeviceType connected on the
* wallet provider (extension, mobile, hardware wallet)
*/
getWalletDeviceType(): Promise<WalletDeviceType>

/**
* Get the PubKey from the wallet
* in base64 for Cosmos native wallets
*/
getPubKey(address?: string): Promise<string>

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

/**
* Sends Cosmos transaction. Returns a transaction hash
* @param transaction should implement TransactionConfig
* @param options
*/
sendTransaction(
transaction: DirectSignResponse | TxRaw,
options: SendTransactionOptions,
): Promise<TxResponse>

signTransaction(transaction: {
txRaw: TxRaw
chainId: string
accountNumber: number
address: string
}): Promise<DirectSignResponse>

signAminoTransaction(transaction: {
stdSignDoc: StdSignDoc
address: string
}): Promise<AminoSignResponse>
}

export interface WalletStrategyOptions {
privateKey?: string
metadata?: Record<string, string | Record<string, string>>
}

export interface CosmosWalletStrategyArguments {
chainId: CosmosChainId
wallet?: Wallet
}

export interface WalletStrategyArguments
extends Omit<CosmosWalletStrategyArguments, 'chainId'> {
chainId: ChainId
options?: WalletStrategyOptions
ethereumOptions?: WalletStrategyEthereumOptions
disabledWallets?: Wallet[]
wallet?: Wallet
}

export interface ConcreteWalletStrategy
extends Omit<
ConcreteCosmosWalletStrategy,
| 'sendTransaction'
| 'signTransaction'
| 'isChainIdSupported'
| 'signAminoTransaction'
> {
/**
* Sends Cosmos transaction. Returns a transaction hash
* @param transaction should implement TransactionConfig
* @param options
*/
sendTransaction(
transaction: DirectSignResponse | TxRaw,
options: {
address: string
chainId: ChainId
txTimeout?: number
endpoints?: {
rest: string
grpc: string
tm?: string
}
},
): Promise<TxResponse>

/**
* Confirm the address on the wallet
* @param address address
*/
getSessionOrConfirm(address?: string): Promise<string>

/**
* Sends Ethereum transaction. Returns a transaction hash
* @param transaction should implement TransactionConfig
* @param options
*/
sendEthereumTransaction(
transaction: unknown,
options: { address: string; ethereumChainId: EthereumChainId },
): Promise<string>

/** @deprecated * */
signTransaction(
data:
| string /* EIP712 Typed Data in JSON */
| { txRaw: TxRaw; accountNumber: number; chainId: string },
address: string,
): Promise<string | DirectSignResponse>

/**
* Sign a cosmos transaction using the wallet provider
*
* @param transaction
* @param address - injective address
*/
signCosmosTransaction(transaction: {
txRaw: TxRaw
accountNumber: number
chainId: string
address: string
}): Promise<DirectSignResponse>

/**
* Sign an amino sign doc using the wallet provider
*
* @param transaction
* @param address - injective address
*/
signAminoCosmosTransaction(transaction: {
signDoc: any
accountNumber: number
chainId: string
address: string
}): Promise<string>

/**
* Sign EIP712 TypedData using the wallet provider
* @param eip712TypedData
* @param address - ethereum address
*/
signEip712TypedData(eip712TypedData: string, address: string): Promise<string>

signArbitrary(signer: string, data: string | Uint8Array): Promise<string>

getEthereumChainId(): Promise<string>

getEthereumTransactionReceipt(txHash: string): void

onAccountChange?(callback: onAccountChangeCallback): Promise<void> | void

onChainIdChange?(callback: onChainIdChangeCallback): Promise<void> | void

disconnect?(): Promise<void> | void
}

export interface WalletStrategy {
strategies: Record<Wallet, ConcreteWalletStrategy | undefined>
wallet: Wallet
args: WalletStrategyArguments

getWallet(): Wallet
setWallet(wallet: Wallet): void
setOptions(options?: WalletStrategyOptions): void
getStrategy(): ConcreteWalletStrategy
getAddresses(args?: unknown): Promise<AccountAddress[]>
getWalletDeviceType(): Promise<WalletDeviceType>
getPubKey(address?: string): Promise<string>
enable(args?: unknown): Promise<boolean>
enableAndGetAddresses(args?: unknown): Promise<AccountAddress[]>
getEthereumChainId(): Promise<string>
getEthereumTransactionReceipt(txHash: string): Promise<void>
getSessionOrConfirm(address?: AccountAddress): Promise<string>
sendTransaction(tx: DirectSignResponse | TxRaw, options: SendTransactionOptions): Promise<TxResponse>
sendEthereumTransaction(tx: any, options: { address: AccountAddress; ethereumChainId: EthereumChainId }): Promise<string>
signTransaction(data: string | { txRaw: TxRaw; accountNumber: number; chainId: string }, address: AccountAddress): Promise<string | DirectSignResponse>
signEip712TypedData(eip712TypedData: string, address: AccountAddress): Promise<string>
signAminoCosmosTransaction(transaction: { signDoc: any; accountNumber: number; chainId: string; address: string }): Promise<string>
signCosmosTransaction(transaction: { txRaw: TxRaw; accountNumber: number; chainId: string; address: string }): Promise<DirectSignResponse>
signArbitrary(signer: string, data: string | Uint8Array): Promise<string | void>
onAccountChange(callback: onAccountChangeCallback): Promise<void>
onChainIdChange(callback: onChainIdChangeCallback): Promise<void>
disconnect(): Promise<void>
}
1 change: 0 additions & 1 deletion packages/wallet-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './broadcaster'
export * from './strategies'
export * from './utils'
export * from './types'
4 changes: 2 additions & 2 deletions packages/wallet-ts/src/strategies/types/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
ChainId,
CosmosChainId,
EthereumChainId,
SendTransactionOptions,
} from '@injectivelabs/ts-types'
import type { TxRaw, TxResponse } from '@injectivelabs/sdk-ts'
import { AminoSignResponse, StdSignDoc } from '@keplr-wallet/types'
import { Wallet, WalletDeviceType } from '../../types/enums'
import { SendTransactionOptions } from '../wallet-strategy'
import { Wallet, WalletDeviceType } from '@injectivelabs/ts-types'

export type onAccountChangeCallback = (account: string) => void
export type onChainIdChangeCallback = () => void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import Cosmostation from './strategies/Cosmostation'
import WalletConnect from './strategies/WalletConnect'
import Magic from './strategies/Magic'
import { isEthWallet, isCosmosWallet } from './utils'
import { Wallet, WalletDeviceType } from '../../types/enums'
import { MagicMetadata, SendTransactionOptions } from './types'
import { MagicMetadata } from './types'
import {
WalletStrategyOptions,
ConcreteWalletStrategy,
Expand All @@ -28,6 +27,7 @@ import {
EthereumWalletStrategyArgs,
WalletStrategyEthereumOptions,
} from '../types'
import{ Wallet, WalletDeviceType, SendTransactionOptions, WalletStrategy as IWalletStrategy } from '@injectivelabs/ts-types'

const getInitialWallet = (args: WalletStrategyArguments): Wallet => {
if (args.wallet) {
Expand Down Expand Up @@ -142,7 +142,7 @@ const createStrategies = (
)
}

export default class WalletStrategy {
export class WalletStrategy implements IWalletStrategy {
public strategies: Record<Wallet, ConcreteWalletStrategy | undefined>

public wallet: Wallet
Expand Down Expand Up @@ -339,3 +339,5 @@ export default class WalletStrategy {
}
}
}

export default WalletStrategy;
12 changes: 0 additions & 12 deletions packages/wallet-ts/src/strategies/wallet-strategy/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type HDNode from 'hdkey'
import Eip1993Provider from 'eip1193-provider'
import { ChainId } from '@injectivelabs/ts-types'

export interface BrowserEip1993Provider extends Eip1993Provider {
removeAllListeners(): void
Expand Down Expand Up @@ -30,17 +29,6 @@ export interface TrezorWalletInfo {
derivationPath: string
}

export interface SendTransactionOptions {
address: string
chainId: ChainId
txTimeout?: number
endpoints: {
rest: string
grpc: string
tm?: string
}
}

export type WalletConnectMetadata = Record<
string,
string | Record<string, string> | Record<string, string[]>
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-ts/src/strategies/wallet-strategy/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Wallet } from '../../types/enums'
import { Wallet } from '@injectivelabs/ts-types'

export const isEthWallet = (wallet: Wallet): boolean =>
[
Expand Down
60 changes: 60 additions & 0 deletions packages/wallets/wallet-base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 🌟 Injective Protocol - Ledger Wallet Strategy

<!-- TODO -->

[![downloads](https://img.shields.io/npm/dm/@injectivelabs/wallet-ts.svg)](https://www.npmjs.com/package/@injectivelabs/wallet-ts)
[![npm-version](https://img.shields.io/npm/v/@injectivelabs/wallet-ts.svg)](https://www.npmjs.com/package/@injectivelabs/wallet-ts)
[![license](https://img.shields.io/npm/l/express.svg)]()

_Package to use Ledger Wallets on Injective via the wallet strategy._

---

## 📚 Installation

```bash
yarn add @injectivelabs/wallet-ledger
```

---

## 📖 Documentation

<!-- TODO -->

Read more and find example usages on our [WalletStrategy Docs](https://docs.ts.injective.network/wallet/wallet-wallet-strategy)

---

## 📜 Contribution

**Contribution guides and practices will be available once there is a stable foundation of the whole package set within the `injective-ts` repo.**

---

## ⛑ Support

Reach out to us at one of the following places!

- Website at <a href="https://injective.com" target="_blank">`injective.com`</a>
- Twitter at <a href="https://twitter.com/Injective_" target="_blank">`@Injective`</a>
- Discord at <a href="https://discord.com/invite/NK4qdbv" target="_blank">`Discord`</a>
- Telegram at <a href="https://t.me/joininjective" target="_blank">`Telegram`</a>

---

## 🔓 License

Copyright © 2021 - 2022 Injective Labs Inc. (https://injectivelabs.org/)

<a href="https://iili.io/mNneZN.md.png"><img src="https://iili.io/mNneZN.md.png" style="width: 300px; max-width: 100%; height: auto" />

Originally released by Injective Labs Inc. under: <br />
Apache License <br />
Version 2.0, January 2004 <br />
http://www.apache.org/licenses/

<p>&nbsp;</p>
<div align="center">
<sub><em>Powering the future of decentralized finance.</em></sub>
</div>
Loading

0 comments on commit 2f603b1

Please sign in to comment.