-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(global): adds WalletManager and AssetsManager
- Loading branch information
1 parent
33b2fb3
commit 27441a3
Showing
15 changed files
with
2,486 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
.DS_Store | ||
node_modules | ||
dist | ||
*pk | ||
*pk | ||
.env |
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,51 @@ | ||
import { ethers, JsonRpcProvider } from 'ethers' | ||
import { Wallet } from './WalletManager' | ||
import erc20Abi from './kms/keyring/abi/erc20' | ||
|
||
export type NetworkConfigs = { | ||
[key: string]: { | ||
rpc: string | ||
} | ||
} | ||
|
||
export interface AssetsManagerConfigs { | ||
networkConfigs: NetworkConfigs | ||
} | ||
|
||
const DEFAULT_CONFIGS = { | ||
eth: { | ||
rpc: 'https://eth.llamarpc.com', | ||
}, | ||
arb: { | ||
rpc: 'https://arbitrum.llamarpc.com', | ||
}, | ||
gno: { | ||
rpc: 'https://gnosis.drpc.org', | ||
}, | ||
} | ||
|
||
export class AssetsManager { | ||
networkConfigs: NetworkConfigs = DEFAULT_CONFIGS | ||
|
||
constructor(configs?: AssetsManagerConfigs) { | ||
if (configs) { | ||
this.networkConfigs = { ...DEFAULT_CONFIGS, ...configs.networkConfigs } | ||
} | ||
} | ||
|
||
async transferToken(wallet: Wallet, amount: number, tokenAddress: string, destinationAddressAsEIP3770: string) { | ||
const [chain, recipient] = destinationAddressAsEIP3770.split(':') | ||
|
||
const networkConfig = this.networkConfigs[chain] | ||
if (!networkConfig) throw new Error('network configs not supported. You can add it during the initialization') | ||
const provider = new JsonRpcProvider(networkConfig.rpc) | ||
|
||
const asset = new ethers.Contract(tokenAddress, erc20Abi, provider) | ||
const decimals = (await asset.decimals()) as bigint | ||
const onchainAmount = (BigInt(amount * 10 ** 18) * BigInt(Math.pow(10, Number(decimals)))) / BigInt(10 ** 18) | ||
|
||
const data = await wallet.kms.prepareTransfer(tokenAddress, onchainAmount.toString(), recipient, provider) | ||
const signature = await wallet.sign(data) | ||
await wallet.kms.postSignature(signature, data, provider) | ||
} | ||
} |
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,56 @@ | ||
import { KeyringKms, KeyringKmsConfigs, KeyringSignOptions } from './kms/keyring' | ||
import { IKms } from './kms/types' | ||
|
||
export class WalletManager< | ||
O extends KeyringSignOptions = KeyringSignOptions, | ||
K extends IKms<O> = KeyringKms, | ||
C = KeyringKmsConfigs | ||
> { | ||
private wallets: { [key: string]: Wallet<O, K, C> } = {} | ||
|
||
createNewWallet(label: string, configs?: WalletConfigs<K, C, O>): Wallet<O, K, C> { | ||
if (this.wallets[label]) throw new Error('wallet already existent') | ||
this.wallets[label] = new Wallet(configs) | ||
return this.wallets[label] | ||
} | ||
} | ||
|
||
export interface WalletConfigs<K extends IKms<O>, C, O> { | ||
kms: { | ||
constructor: new (configs?: C) => K | ||
configs?: C | ||
} | ||
} | ||
|
||
export class Wallet< | ||
O extends KeyringSignOptions = KeyringSignOptions, | ||
K extends IKms<O> = KeyringKms, | ||
C = KeyringKmsConfigs | ||
> { | ||
kms: K | ||
private initialized: boolean = false | ||
|
||
constructor(configs?: WalletConfigs<K, C, O>) { | ||
if (configs?.kms) { | ||
this.kms = new configs.kms.constructor(configs.kms.configs) | ||
} else { | ||
this.kms = (new KeyringKms() as unknown) as K | ||
} | ||
} | ||
|
||
async initialize(): Promise<void> { | ||
await this.kms.initialize() | ||
this.initialized = true | ||
} | ||
|
||
async sign(data: Buffer, options?: O): Promise<Buffer> { | ||
await this.checkIfInitialized() | ||
return this.kms.sign(data, options || ({} as O)) | ||
} | ||
|
||
private async checkIfInitialized() { | ||
if (!this.initialized) { | ||
await this.initialize() | ||
} | ||
} | ||
} |
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,39 +1,16 @@ | ||
import { IKms } from './kms/types' | ||
import { KeyringKms, KeyringKmsConfigs, KeyringSignOptions } from './kms/keyring' | ||
import { AssetsManager } from './AssetsManager' | ||
import { KeyringKms } from './kms/keyring' | ||
import { LitKms } from './kms/lit' | ||
import { WalletManager } from './WalletManager' | ||
|
||
export interface HandshakeConfigs<K extends IKms<O>, C, O> { | ||
kms: { | ||
constructor: new (configs?: C) => K | ||
configs?: C | ||
} | ||
} | ||
|
||
export class Handshake< | ||
O extends KeyringSignOptions = KeyringSignOptions, | ||
K extends IKms<O> = KeyringKms, | ||
C = KeyringKmsConfigs | ||
> { | ||
private kms: K | ||
|
||
constructor(configs?: HandshakeConfigs<K, C, O>) { | ||
if (configs?.kms) { | ||
this.kms = new configs.kms.constructor(configs.kms.configs) | ||
} else { | ||
this.kms = (new KeyringKms() as unknown) as K | ||
} | ||
} | ||
|
||
async initialize(): Promise<void> { | ||
return this.kms.initialize() | ||
} | ||
export class Handshake { | ||
walletManager: WalletManager | ||
assetsManager: AssetsManager | ||
|
||
async sign(data: Buffer, options?: O): Promise<Buffer> { | ||
return this.kms.sign(data, options || ({} as O)) | ||
constructor() { | ||
this.walletManager = new WalletManager() | ||
this.assetsManager = new AssetsManager() | ||
} | ||
} | ||
|
||
export { | ||
KeyringKms, | ||
LitKms | ||
} | ||
export { KeyringKms, LitKms, WalletManager, AssetsManager } |
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,16 @@ | ||
import { Provider } from 'ethers' | ||
|
||
export interface KmsConfigs { | ||
name: string | ||
provider?: Provider | ||
} | ||
|
||
export class Kms { | ||
name: string | ||
provider?: Provider | ||
|
||
constructor(configs: KmsConfigs) { | ||
this.name = configs.name | ||
this.provider = configs.provider | ||
} | ||
} |
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
Oops, something went wrong.