-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwallet.ts
More file actions
139 lines (116 loc) · 3.81 KB
/
Copy pathwallet.ts
File metadata and controls
139 lines (116 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Session } from '@0xsequence/auth'
import { AwsKmsSigner } from '@0xsequence/aws-kms-signer'
import { GoogleKmsSigner } from '@0xsequence/google-kms-signer'
import { findSupportedNetwork } from '@0xsequence/network'
import type { NetworkConfig } from '@0xsequence/network'
import { ethers } from 'ethers'
import { logger } from './logger'
import { getOrCreateDevKey } from './other'
export const getProvider = async (chainConfig: NetworkConfig) => {
const provider = new ethers.JsonRpcProvider(
chainConfig.rpcUrl,
chainConfig.chainId
)
return provider
}
export const getRelayer = async (chainHandle: string) => {
try {
const chainConfig: NetworkConfig | undefined =
findSupportedNetwork(chainHandle)
if (!chainConfig) {
throw new Error(`Chain config not found for chain handle: ${chainHandle}`)
}
const provider = await getProvider(chainConfig)
const walletEOA = new ethers.Wallet(
process.env.BACKEND_WALLET_PV_KEY || getOrCreateDevKey(),
provider
)
const smartAccount = await Session.singleSigner({
signer: walletEOA,
projectAccessKey: process.env.SEQUENCE_PROJECT_ACCESS_KEY as string
})
return smartAccount.account.relayer(chainConfig.chainId)
} catch (err) {
logger.error(`Error getting local signer: ${err}`)
throw err
}
}
export const getLocalSigner = async (chainHandle: string) => {
try {
const chainConfig: NetworkConfig | undefined =
findSupportedNetwork(chainHandle)
if (!chainConfig) {
throw new Error(`Chain config not found for chain handle: ${chainHandle}`)
}
const provider = await getProvider(chainConfig)
const walletEOA = new ethers.Wallet(
process.env.BACKEND_WALLET_PV_KEY || getOrCreateDevKey(),
provider
)
const smartAccount = await Session.singleSigner({
signer: walletEOA,
projectAccessKey: process.env.SEQUENCE_PROJECT_ACCESS_KEY as string
})
return smartAccount.account.getSigner(chainConfig.chainId)
} catch (err) {
logger.error(`Error getting local signer: ${err}`)
throw err
}
}
export const getGoogleKmsSigner = async (chainHandle: string) => {
try {
const chainConfig: NetworkConfig | undefined =
findSupportedNetwork(chainHandle)
if (!chainConfig) {
throw new Error(`Chain config not found for chain handle: ${chainHandle}`)
}
const googleKmsSigner = new GoogleKmsSigner({
project: process.env.PROJECT as string,
location: process.env.LOCATION as string,
keyRing: process.env.KEY_RING as string,
cryptoKey: process.env.CRYPTO_KEY as string,
cryptoKeyVersion: process.env.CRYPTO_KEY_VERSION as string
})
const smartAccount = await Session.singleSigner({
signer: googleKmsSigner,
projectAccessKey: process.env.SEQUENCE_PROJECT_ACCESS_KEY as string
})
return smartAccount.account.getSigner(chainConfig.chainId)
} catch (err) {
console.error(`ERROR: ${err}`)
throw err
}
}
export const getAwsKmsSigner = async (chainHandle: string) => {
try {
const chainConfig: NetworkConfig | undefined =
findSupportedNetwork(chainHandle)
if (!chainConfig) {
throw new Error(`Chain config not found for chain handle: ${chainHandle}`)
}
const awsKmsSigner = new AwsKmsSigner(
process.env.AWS_REGION as string,
process.env.AWS_KMS_KEY_ID as string
)
const smartAccount = await Session.singleSigner({
signer: awsKmsSigner,
projectAccessKey: process.env.SEQUENCE_PROJECT_ACCESS_KEY as string
})
return smartAccount.account.getSigner(chainConfig.chainId)
} catch (err) {
console.error(`ERROR: ${err}`)
throw err
}
}
export const getSigner = async (chainHandle: string) => {
if (process.env.SIGNER_TYPE === 'local') {
return getLocalSigner(chainHandle)
}
if (process.env.SIGNER_TYPE === 'google_kms') {
return getGoogleKmsSigner(chainHandle)
}
if (process.env.SIGNER_TYPE === 'aws_kms') {
return getAwsKmsSigner(chainHandle)
}
return getLocalSigner(chainHandle)
}