forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,728 additions
and
59 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
42 changes: 42 additions & 0 deletions
42
packages/extension-base/src/services/account-abstraction-service/providers/klaster.ts
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,42 @@ | ||
// Copyright 2019-2022 @subwallet/extension-base authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { _ChainInfo } from '@subwallet/chain-list/types'; | ||
import { _getEvmChainId } from '@subwallet/extension-base/services/chain-service/utils'; | ||
import { AAProviderConfig } from '@subwallet/extension-base/types'; | ||
import { initKlaster, klasterNodeHost, loadBicoV2Account } from 'klaster-sdk'; | ||
|
||
export class KlasterService { | ||
static chainTestnetMap: Record<number, boolean> = {}; | ||
|
||
static async getSmartAccount (ownerAddress: string, config: AAProviderConfig): Promise<string> { | ||
const accountInitData = (() => { | ||
const { name, version } = config; | ||
|
||
if (version === '2.0.0' && name === 'BICONOMY') { | ||
return loadBicoV2Account({ | ||
owner: ownerAddress as `0x${string}` | ||
}); | ||
} | ||
|
||
throw Error('Unsupported account abstraction provider'); | ||
})(); | ||
|
||
const klasterSdk = await initKlaster({ | ||
accountInitData, | ||
nodeUrl: klasterNodeHost.default | ||
}); | ||
|
||
return klasterSdk.account.getAddress(1) as string; | ||
} | ||
|
||
static updateChainMap (chainInfo: _ChainInfo) { | ||
const chainId = _getEvmChainId(chainInfo) as number; | ||
|
||
if (chainId in KlasterService.chainTestnetMap) { | ||
return; | ||
} | ||
|
||
KlasterService.chainTestnetMap[chainId] = chainInfo.isTestnet; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
packages/extension-base/src/services/account-abstraction-service/providers/particle.ts
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,93 @@ | ||
// Copyright 2019-2022 @subwallet/extension-base authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { AccountContract, SmartAccount, SmartAccountConfig, Transaction, UserOp, UserOpBundle } from '@particle-network/aa'; | ||
import { SmartAccountData } from '@subwallet/extension-base/background/types'; | ||
import { AAProviderConfig } from '@subwallet/extension-base/types'; | ||
import { anyNumberToBN } from '@subwallet/extension-base/utils'; | ||
import { createMockParticleProvider } from '@subwallet/extension-base/utils/mock/provider/particle'; | ||
import { TransactionConfig } from 'web3-core'; | ||
|
||
export const ParticleContract: AccountContract = { | ||
name: 'BICONOMY', | ||
version: '2.0.0' | ||
}; | ||
|
||
const defaultParticleConfig: SmartAccountConfig = { | ||
projectId: '80d48082-7a98-41e0-8eb5-571e2e00cc7f', | ||
clientKey: 'cSuNrTTMf0d2Vp3l9aaAyvGm2UzRjywnNK0duRHN', | ||
appId: 'ca41cf00-9bac-4980-aef5-f521925f3de7', | ||
aaOptions: { | ||
accountContracts: { // 'BICONOMY', 'CYBERCONNECT', 'SIMPLE', 'LIGHT', 'XTERIO' | ||
BICONOMY: [ | ||
{ | ||
version: '1.0.0', | ||
chainIds: [1] | ||
}, | ||
{ | ||
version: '2.0.0', | ||
chainIds: [1, 11155111, 8453, 84532, 42161] | ||
} | ||
], | ||
CYBERCONNECT: [ | ||
{ | ||
version: '1.0.0', | ||
chainIds: [1] | ||
} | ||
], | ||
SIMPLE: [ | ||
{ | ||
version: '1.0.0', | ||
chainIds: [1] | ||
} | ||
] | ||
} | ||
} | ||
}; | ||
|
||
export class ParticleAAHandler { | ||
static getSmartAccount = async (owner: string, config: AAProviderConfig): Promise<string> => { | ||
const provider = createMockParticleProvider(1, owner); | ||
|
||
const smartAccount = new SmartAccount(provider, defaultParticleConfig); | ||
|
||
smartAccount.setSmartAccountContract(config); | ||
|
||
return smartAccount.getAddress(); | ||
}; | ||
|
||
static createUserOperation = async (chainId: number, account: SmartAccountData, _txList: TransactionConfig[]): Promise<UserOpBundle> => { | ||
const provider = createMockParticleProvider(chainId, account.owner); | ||
|
||
const smartAccount = new SmartAccount(provider, defaultParticleConfig); | ||
|
||
smartAccount.setSmartAccountContract(account.provider || ParticleContract); | ||
|
||
const txList: Transaction[] = []; | ||
|
||
for (const _tx of _txList) { | ||
const tx: Transaction = { | ||
data: _tx.data, | ||
value: anyNumberToBN(_tx.value).toString(), | ||
to: _tx.to || '', | ||
gasLimit: _tx.gas | ||
}; | ||
|
||
txList.push(tx); | ||
} | ||
|
||
console.debug('quote', await smartAccount.getFeeQuotes(txList)); | ||
|
||
return await smartAccount.buildUserOperation({ tx: txList }); | ||
}; | ||
|
||
static sendSignedUserOperation = async (chainId: number, account: SmartAccountData, userOp: UserOp): Promise<string> => { | ||
const provider = createMockParticleProvider(chainId, account.owner); | ||
|
||
const smartAccount = new SmartAccount(provider, defaultParticleConfig); | ||
|
||
smartAccount.setSmartAccountContract(account.provider || ParticleContract); | ||
|
||
return await smartAccount.sendSignedUserOperation(userOp); | ||
}; | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/extension-base/src/services/account-abstraction-service/sdk.ts
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,94 @@ | ||
// Copyright 2019-2022 @subwallet/extension-base authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { UserOpBundle } from '@particle-network/aa'; | ||
import { QuoteResponse } from 'klaster-sdk'; | ||
import { Observable, Subscribable } from 'rxjs'; | ||
|
||
enum AAAccountType { | ||
EOA = 'eoa', | ||
CONTRACT = 'contract' | ||
} | ||
|
||
/** | ||
* Chain id | ||
* - If negative, it means the chain id is not supported | ||
* - If positive, it means the chain id is supported | ||
* */ | ||
type ChainId = number; | ||
|
||
interface AAAccount { | ||
address: string; | ||
provider?: string; | ||
chainIds: ChainId[]; | ||
providerId: string; | ||
type: AAAccountType; | ||
owner?: string; | ||
} | ||
|
||
interface AAProxy { | ||
id: string; | ||
name: string; | ||
accounts: AAAccount[]; | ||
} | ||
|
||
interface RawTransactionConfig { | ||
to: string; | ||
gas: number | string; | ||
value?: number | string; | ||
data?: string; | ||
chainId: number; | ||
} | ||
|
||
/** | ||
* Balance info of a token on an address | ||
* @property {string} address - Address | ||
* @property {string} balance - Transferable balance | ||
* @property {string} token.symbol - Token symbol | ||
* @property {string} token.address - Token address | ||
* @property {number} token.decimals - Token decimals | ||
*/ | ||
export interface BalanceItem { | ||
address: string; | ||
balance: string; | ||
token: { | ||
symbol: string; | ||
address: string; | ||
decimals: number; | ||
}; | ||
} | ||
|
||
type AATransactionBundle = UserOpBundle | QuoteResponse; | ||
|
||
export interface AccountAbstractionSDK { | ||
/** Return a subscribable object to subscribe to the AAProxy map */ | ||
aaProxyObservable(): Observable<Record<string, AAProxy>>; | ||
/** Return the AAProxy map */ | ||
getProxy(): Promise<Record<string, AAProxy>>; | ||
/** Generate a new AAProxy from address */ | ||
|
||
generateAccount(address: string): Promise<AAProxy>; | ||
|
||
/** Auto subscribe balance with chain and asset list from SW */ | ||
autoSubscribeBalance(proxyId: string): Subscribable<BalanceItem>; | ||
|
||
/** Subscribe balance | ||
* @param {string} proxyId - The proxy id | ||
* @returns {Subscribable<number>} - The subscribable balance | ||
* */ | ||
subscribeBalance(proxyId: string, ): Subscribable<BalanceItem>; | ||
|
||
/** | ||
* Generate `AATransactionBundle` from a list of `RawTransactionConfig` | ||
* @param {string} sender - The sender address | ||
* @param {RawTransactionConfig[]} txs - The list of `RawTransactionConfig` | ||
* @returns | ||
* bundles: A list of `AATransactionBundle` to sign | ||
* | ||
* - Note: `bundles` is an array because some AA provider only supports batch transaction on the same chain | ||
*/ | ||
createTransaction(sender: string, txs: RawTransactionConfig[]): Promise<{ | ||
bundles: AATransactionBundle[]; | ||
signer: string; | ||
}>; | ||
} |
Oops, something went wrong.