Skip to content

Commit 7210bc9

Browse files
committed
feat: add turnkey wallet
1 parent adf15fd commit 7210bc9

File tree

30 files changed

+2162
-204
lines changed

30 files changed

+2162
-204
lines changed

packages/exceptions/src/exceptions/types/codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,4 +817,5 @@ export type ErrorContextCode =
817817
| ChainStakingErrorCodes
818818
| ChainWasmErrorCodes
819819
| ErrorCode
820+
| number
820821
| typeof UnspecifiedErrorCode

packages/sdk-ts/src/utils/address.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
getEthereumAddress,
33
getInjectiveAddress,
44
getDefaultSubaccountId,
5+
getChecksumAddress,
56
} from '../../src/utils/index.js'
67

78
describe('address helper functions', () => {
@@ -31,4 +32,13 @@ describe('address helper functions', () => {
3132
'0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000',
3233
)
3334
})
35+
36+
it('getChecksumAddress returns correct value', () => {
37+
const ethereumAddress =
38+
'0xaf79152ac5df276d9a8e1e2e22822f9713474902'.toLowerCase()
39+
40+
expect(getChecksumAddress(ethereumAddress)).toMatch(
41+
'0xAF79152AC5dF276D9A8e1E2E22822f9713474902',
42+
)
43+
})
3444
})

packages/wallets/wallet-base/src/base.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { ChainId, CosmosChainId, EthereumChainId } from '@injectivelabs/ts-types'
21
import {
2+
ChainId,
3+
CosmosChainId,
4+
EthereumChainId,
5+
} from '@injectivelabs/ts-types'
6+
import {
7+
WalletMetadata,
38
WalletEventListener,
49
ConcreteWalletStrategyArgs,
510
ConcreteCosmosWalletStrategyArgs,
@@ -13,10 +18,19 @@ export default abstract class BaseConcreteStrategy {
1318

1419
protected listeners: Partial<Record<WalletEventListener, any>> = {}
1520

16-
protected constructor(args: ConcreteWalletStrategyArgs | ConcreteEthereumWalletStrategyArgs | ConcreteCosmosWalletStrategyArgs) {
17-
this.ethereumChainId = 'ethereumOptions' in args && args.ethereumOptions
18-
? args.ethereumOptions.ethereumChainId
19-
: undefined
21+
public metadata?: WalletMetadata
22+
23+
public constructor(
24+
args:
25+
| ConcreteWalletStrategyArgs
26+
| ConcreteEthereumWalletStrategyArgs
27+
| ConcreteCosmosWalletStrategyArgs,
28+
) {
29+
this.ethereumChainId =
30+
'ethereumOptions' in args && args.ethereumOptions
31+
? args.ethereumOptions.ethereumChainId
32+
: undefined
2033
this.chainId = args.chainId
34+
this.metadata = args.metadata
2135
}
2236
}

packages/wallets/wallet-base/src/types/enums.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export enum Wallet {
1111
Phantom = 'phantom',
1212
Metamask = 'metamask',
1313
OkxWallet = 'okx-wallet',
14+
TurnkeyOtp = 'turnkey-otp',
15+
TurnkeyOauth = 'turnkey-oauth',
1416
TrustWallet = 'trust-wallet',
1517
PrivateKey = 'private-key',
1618
TrezorBip32 = 'trezor-bip32',

packages/wallets/wallet-base/src/types/strategy.ts

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
ChainId,
3-
CosmosChainId,
43
AccountAddress,
54
EthereumChainId,
65
} from '@injectivelabs/ts-types'
@@ -30,10 +29,13 @@ export type MagicMetadata = {
3029
rpcEndpoint?: string
3130
}
3231

33-
export type WalletConnectMetadata = Record<
34-
string,
35-
string | Record<string, string> | Record<string, string[]>
36-
>
32+
export type PrivateKeyMetadata = {
33+
privateKey: string
34+
}
35+
36+
export type WalletConnectMetadata = {
37+
projectId?: string
38+
}
3739

3840
export interface WalletStrategyEthereumOptions {
3941
ethereumChainId: EthereumChainId
@@ -51,20 +53,55 @@ export interface SendTransactionOptions {
5153
}
5254
}
5355

54-
export interface ConcreteWalletStrategyOptions {
55-
privateKey?: string
56-
metadata?: Record<string, string | Record<string, string>>
56+
export enum TurnkeyProvider {
57+
Email = 'email',
58+
Google = 'google',
59+
Apple = 'apple',
60+
}
61+
62+
export type TurnkeySession = {
63+
sessionType: any
64+
userId: string
65+
organizationId: string
66+
expiry: number
67+
token: string
68+
}
69+
70+
export interface TurnkeyMetadata {
71+
defaultOrganizationId: string
72+
apiBaseUrl: string
73+
apiServerEndpoint: string
74+
iframeUrl?: string
75+
email?: string
76+
session?: TurnkeySession
77+
otpId?: string
78+
otpCode?: string
79+
oidcToken?: string
80+
iframeElementId?: string
81+
iframeContainerId: string
82+
credentialBundle?: string
83+
organizationId?: string
84+
provider?: TurnkeyProvider
85+
otpInitPath?: string
86+
otpVerifyPath?: string
87+
oauthLoginPath?: string
88+
}
89+
90+
export interface WalletMetadata {
91+
magic?: MagicMetadata
92+
turnkey?: TurnkeyMetadata
93+
walletConnect?: WalletConnectMetadata
94+
privateKey?: PrivateKeyMetadata
5795
}
5896

5997
export interface ConcreteWalletStrategyArgs {
6098
chainId: ChainId
61-
options?: ConcreteWalletStrategyOptions
99+
metadata?: WalletMetadata
62100
}
63101

64-
export interface ConcreteCosmosWalletStrategyArgs {
65-
chainId: CosmosChainId | ChainId
102+
export interface ConcreteCosmosWalletStrategyArgs
103+
extends ConcreteWalletStrategyArgs {
66104
wallet?: Wallet
67-
options?: ConcreteWalletStrategyOptions
68105
}
69106

70107
export interface ConcreteEthereumWalletStrategyArgs
@@ -73,6 +110,9 @@ export interface ConcreteEthereumWalletStrategyArgs
73110
}
74111

75112
export interface ConcreteCosmosWalletStrategy {
113+
metadata?: WalletMetadata
114+
115+
setMetadata?(metadata?: WalletMetadata): void
76116
/**
77117
* The accounts from the wallet (addresses)
78118
*/
@@ -125,7 +165,7 @@ export type ConcreteStrategiesArg = {
125165

126166
export interface WalletStrategyArguments {
127167
chainId: ChainId
128-
options?: ConcreteWalletStrategyOptions
168+
metadata?: WalletMetadata
129169
ethereumOptions?: WalletStrategyEthereumOptions
130170
disabledWallets?: Wallet[]
131171
wallet?: Wallet
@@ -135,9 +175,7 @@ export interface WalletStrategyArguments {
135175
export interface ConcreteWalletStrategy
136176
extends Omit<
137177
ConcreteCosmosWalletStrategy,
138-
| 'sendTransaction'
139-
| 'isChainIdSupported'
140-
| 'signAminoTransaction'
178+
'sendTransaction' | 'isChainIdSupported' | 'signAminoTransaction'
141179
> {
142180
/**
143181
* Sends Cosmos transaction. Returns a transaction hash
@@ -224,10 +262,11 @@ export interface WalletStrategy {
224262
strategies: ConcreteStrategiesArg
225263
wallet: Wallet
226264
args: WalletStrategyArguments
265+
metadata?: WalletMetadata
227266

228267
getWallet(): Wallet
229268
setWallet(wallet: Wallet): void
230-
setOptions(options?: ConcreteWalletStrategyOptions): void
269+
setMetadata(metadata?: WalletMetadata): void
231270
getStrategy(): ConcreteWalletStrategy
232271
getAddresses(args?: unknown): Promise<AccountAddress[]>
233272
getWalletDeviceType(): Promise<WalletDeviceType>
@@ -269,4 +308,4 @@ export interface WalletStrategy {
269308
getCosmosWallet?(chainId: ChainId): CosmosWalletAbstraction
270309
}
271310

272-
export { StdSignDoc}
311+
export { StdSignDoc }

packages/wallets/wallet-base/src/utils/wallet.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ export const isEvmWallet = (wallet: Wallet): boolean =>
55
Wallet.Magic,
66
Wallet.BitGet,
77
Wallet.Ledger,
8-
Wallet.TrezorBip32,
9-
Wallet.TrezorBip44,
108
Wallet.Phantom,
119
Wallet.Metamask,
1210
Wallet.OkxWallet,
1311
Wallet.PrivateKey,
12+
Wallet.TrezorBip32,
13+
Wallet.TurnkeyOtp,
14+
Wallet.TurnkeyOauth,
15+
Wallet.TrezorBip44,
1416
Wallet.TrustWallet,
1517
Wallet.LedgerLegacy,
1618
Wallet.WalletConnect,
@@ -19,14 +21,16 @@ export const isEvmWallet = (wallet: Wallet): boolean =>
1921

2022
export const isCosmosWallet = (wallet: Wallet): boolean => !isEvmWallet(wallet)
2123

22-
export const isEvmBrowserWallet = (wallet: Wallet) => [
23-
Wallet.BitGet,
24-
Wallet.Phantom,
25-
Wallet.Metamask,
26-
Wallet.OkxWallet,
27-
Wallet.TrustWallet,
28-
].includes(wallet)
29-
24+
export const isEvmBrowserWallet = (wallet: Wallet) =>
25+
[
26+
Wallet.BitGet,
27+
Wallet.Phantom,
28+
Wallet.Metamask,
29+
Wallet.OkxWallet,
30+
Wallet.TrustWallet,
31+
Wallet.TurnkeyOtp,
32+
Wallet.TurnkeyOauth,
33+
].includes(wallet)
3034

3135
export const isCosmosBrowserWallet = (wallet: Wallet): boolean =>
3236
[

packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import {
1818
ConcreteStrategiesArg,
1919
SendTransactionOptions,
2020
ConcreteWalletStrategy,
21+
type WalletMetadata,
2122
onAccountChangeCallback,
2223
onChainIdChangeCallback,
2324
WalletStrategyArguments,
2425
CosmosWalletAbstraction,
25-
ConcreteWalletStrategyOptions,
2626
WalletStrategy as WalletStrategyInterface,
2727
} from '@injectivelabs/wallet-base'
2828
import { StdSignDoc } from '@keplr-wallet/types'
@@ -58,12 +58,15 @@ export default class BaseWalletStrategy implements WalletStrategyInterface {
5858

5959
public args: WalletStrategyArguments
6060

61+
public metadata?: WalletMetadata
62+
6163
public wallets?: Wallet[]
6264

6365
constructor(args: WalletStrategyArguments) {
6466
this.args = args
6567
this.strategies = args.strategies
6668
this.wallet = getInitialWallet(args)
69+
this.metadata = args.metadata
6770
}
6871

6972
public getWallet(): Wallet {
@@ -74,8 +77,13 @@ export default class BaseWalletStrategy implements WalletStrategyInterface {
7477
this.wallet = wallet
7578
}
7679

77-
public setOptions(_options?: ConcreteWalletStrategyOptions) {
78-
//
80+
/**
81+
* When we use setMetadata, we are usually updating the metadata of the
82+
* existing strategy.
83+
*/
84+
public setMetadata(metadata?: WalletMetadata) {
85+
this.metadata = metadata
86+
this.getStrategy().setMetadata?.(metadata)
7987
}
8088

8189
public getStrategy(): ConcreteWalletStrategy {

packages/wallets/wallet-cosmos/src/strategy/strategy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class CosmosWalletStrategy
4141
implements ConcreteWalletStrategy
4242
{
4343
public wallet: Wallet
44+
4445
private cosmosWallet: CosmosWallet
4546

4647
constructor(
@@ -49,7 +50,7 @@ export class CosmosWalletStrategy
4950
endpoints?: { rest: string; rpc: string }
5051
} & { wallet: Wallet },
5152
) {
52-
super(args)
53+
super({ ...args, chainId: args.chainId as ChainId })
5354

5455
if (!cosmosWallets.includes(args.wallet)) {
5556
throw new CosmosWalletException(
@@ -60,7 +61,7 @@ export class CosmosWalletStrategy
6061
}
6162

6263
this.wallet = args.wallet
63-
this.chainId = args.chainId || CosmosChainId.Injective
64+
this.chainId = args.chainId as ChainId
6465
this.cosmosWallet = new CosmosWallet({
6566
wallet: args.wallet,
6667
chainId: args.chainId,

0 commit comments

Comments
 (0)