Skip to content

Commit

Permalink
fix sdk (#1533)
Browse files Browse the repository at this point in the history
* fix sdk-kit

* fix
  • Loading branch information
wow-sven authored Apr 8, 2024
1 parent e9ccc48 commit 93ea732
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Settings } from 'src/@core/context/settingsContext'
import Typography from '@mui/material/Typography'
import { AllNetwork, Network } from '@roochnetwork/rooch-sdk'
import Menu from '@mui/material/Menu'
import { useRoochClientContext } from '@roochnetwork/rooch-sdk-kit'
import { useRoochContext } from '@roochnetwork/rooch-sdk-kit'
import MenuItem from '@mui/material/MenuItem'
import { Fragment, SyntheticEvent, useState } from 'react'

Expand Down Expand Up @@ -39,7 +39,7 @@ const SwitchNetworkDropdown = (props: Props) => {

// const chain = useWalletStore(state => state.currentChain)
// const setChain = useWalletStore(state => state.setChain)
const { currentNetwork, selectedNetwork } = useRoochClientContext()
const { currentNetwork, switchNetwork } = useRoochContext()

// ** States
const [anchorEl, setAnchorEl] = useState<Element | null>(null)
Expand All @@ -54,7 +54,7 @@ const SwitchNetworkDropdown = (props: Props) => {
const handleDropdownClose = (v?: Network) => {
setAnchorEl(null)
if (v) {
selectedNetwork(v)
switchNetwork(v)
}
}

Expand Down
4 changes: 3 additions & 1 deletion dashboard/src/pages/scan/transaction/detail/[tx_hash].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ const TransactionDetail = () => {
Signature Payload:{' '}
</MUITableCell>
<MUITableCell sx={{ pb: '0 !important' }}>
{formatAddress(data.data[0].transaction.sequence_info.tx_order_signature.payload as any)}
{formatAddress(
data.data[0].transaction.sequence_info.tx_order_signature.payload as any,
)}
</MUITableCell>
</TableRow>
<TableRow>
Expand Down
2 changes: 0 additions & 2 deletions sdk/typescript/rooch-sdk-kit/src/auth/walletAuth.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export function useConnectWallet({
> {
const setWalletConnected = useWalletStore((state) => state.setWalletConnected)
const setConnectionStatus = useWalletStore((state) => state.setConnectionStatus)
// const currentAccount = useWalletStore((state) => state.currentAccount)

return useMutation({
mutationKey: walletMutationKeys.connectWallet(mutationKey),
Expand All @@ -43,18 +42,7 @@ export function useConnectWallet({

const connectAccounts = await wallet.connect()
const selectedAccount = connectAccounts[0]

// use cache date
// if (selectedAccount.address === currentAccount?.address) {
// setWalletConnected(connectAccounts, currentAccount)
// return connectAccounts
// }
//

// let selectedAccountRoochAddress = await client.resoleRoochAddress({
// address: selectedAccount.address,
// multiChainID: chain2MultiChainID(chain),
// })
await selectedAccount.resoleRoochAddress()

setWalletConnected(wallet, connectAccounts, selectedAccount)

Expand Down
4 changes: 1 addition & 3 deletions sdk/typescript/rooch-sdk-kit/src/sessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export function createSessionStore({ storage, storageKey }: ClientConfiguration)
removeSession(session) {
const cache = get().sessions
set(() => ({
sessions: cache.filter(
async (c) => (await c.getRoochAddress()) === (await session.getRoochAddress()),
),
sessions: cache.filter((c) => c.getAddress() === session.getAddress()),
}))
},
}),
Expand Down
23 changes: 9 additions & 14 deletions sdk/typescript/rooch-sdk-kit/src/types/WalletAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ import { SupportChain } from '../feature'
import { chain2MultiChainID } from '../utils/chain2MultiChainID'

export class WalletAccount implements IAccount {
public readonly chain: SupportChain
public readonly client: RoochClient
public readonly address: string
public readonly authorization: IAuthorizer
public readonly publicKey?: string
public readonly compressedPublicKey?: string

private chain: SupportChain
private authorization: IAuthorizer
private client?: RoochClient
private roochAddress?: string

public constructor(
client: RoochClient,
chain: SupportChain,
authorization: IAuthorizer,
address: string,
client?: RoochClient,
authorization: IAuthorizer,
publicKey?: string,
compressedPublicKey?: string,
) {
this.chain = chain
this.client = client
this.authorization = authorization
this.address = address
this.authorization = authorization
this.publicKey = publicKey
this.compressedPublicKey = compressedPublicKey
}
Expand All @@ -41,22 +41,17 @@ export class WalletAccount implements IAccount {
return null
}

getAddress(): string | undefined {
getAddress(): string {
return this.address
}

async getRoochAddress(): Promise<string> {
if (!this.client) {
throw new Error()
}

async resoleRoochAddress(): Promise<string> {
if (!this.roochAddress) {
this.roochAddress = await this.client?.resoleRoochAddress({
this.roochAddress = await this.client.resoleRoochAddress({
address: this.address,
multiChainID: chain2MultiChainID(this.chain),
})
}

return this.roochAddress
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { runtime, bcs } from '@roochnetwork/rooch-sdk'
import { WalletAccount } from '../types/WalletAccount'

export class WalletRoochSessionAccount extends RoochSessionAccount {
private roochAddress?: string

constructor(
client: RoochClient,
account: WalletAccount,
Expand Down Expand Up @@ -43,7 +45,18 @@ export class WalletRoochSessionAccount extends RoochSessionAccount {
return new WalletRoochSessionAccount(client, account, scopes, maxInactiveInterval).build(opts)
}

override async register(txData: runtime.RoochTransactionData): Promise<RoochSessionAccount> {
protected override async build(opts?: SendRawTransactionOpts): Promise<RoochSessionAccount> {
this.roochAddress = await (this.account as WalletAccount).resoleRoochAddress()
return super.build(opts)
}

getAddress(): string {
return this.roochAddress!
}

protected override async register(
txData: runtime.RoochTransactionData,
): Promise<RoochSessionAccount> {
const transactionDataPayload = (() => {
const se = new bcs.BcsSerializer()
txData.serialize(se)
Expand Down
17 changes: 2 additions & 15 deletions sdk/typescript/rooch-sdk-kit/src/types/wellet/baseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import {
} from '@roochnetwork/rooch-sdk'
import { Buffer } from 'buffer'
import { SupportChain } from '../../feature'
import { chain2MultiChainID } from '../../utils/chain2MultiChainID'

export const RoochSignPrefix = 'Rooch tx hash:\n'

export abstract class BaseWallet implements IAuthorizer {
account?: WalletAccount
client: RoochClient
account: WalletAccount | undefined
installed: boolean | undefined
name: string | undefined
client: RoochClient
roochAddress?: string

constructor(client: RoochClient) {
this.client = client
Expand Down Expand Up @@ -173,15 +171,4 @@ export abstract class BaseWallet implements IAuthorizer {
}
return Promise.resolve(this.getTarget() !== undefined)
}

async getRoochAddress(): Promise<string> {
if (!this.roochAddress) {
this.roochAddress = await this.client?.resoleRoochAddress({
address: this.account?.address ?? '',
multiChainID: chain2MultiChainID(this.getChain()),
})
}

return this.roochAddress
}
}
8 changes: 7 additions & 1 deletion sdk/typescript/rooch-sdk-kit/src/types/wellet/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export class Metamask extends ETHWallet {
return accounts
})

return accounts.map((v) => new WalletAccount(this.getChain(), this, v, this.client))
const walletAccounts = accounts.map(
(address) => new WalletAccount(this.client, this.getChain(), address, this),
)

this.account = walletAccounts[0]

return walletAccounts
}
}
22 changes: 12 additions & 10 deletions sdk/typescript/rooch-sdk-kit/src/types/wellet/okx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ export class OkxWallet extends BitcoinWallet {
async connect(): Promise<WalletAccount[]> {
const account = await this.getTarget().connect()

return [
new WalletAccount(
SupportChain.BITCOIN,
this,
this.account?.address!,
this.client,
account.publicKey,
account.compressedPublicKey,
),
]
const walletAccount = new WalletAccount(
this.client,
SupportChain.BITCOIN,
account?.address!,
this,
account.publicKey,
account.compressedPublicKey,
)

this.account = walletAccount

return [walletAccount]
}

switchNetwork(): void {
Expand Down
4 changes: 2 additions & 2 deletions sdk/typescript/rooch-sdk-kit/src/types/wellet/unisat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class UniSatWallet extends BitcoinWallet {
const walletAccounts = accounts.map((address, index) => {
if (index === 0) {
// unisat only supports the current account to get publicKey
return new WalletAccount(this.getChain(), this, address, this.client, publicKey)
return new WalletAccount(this.client, this.getChain(), address, this, publicKey)
} else {
return new WalletAccount(this.getChain(), this, address, this.client)
return new WalletAccount(this.client, this.getChain(), address, this)
}
})

Expand Down
13 changes: 9 additions & 4 deletions sdk/typescript/rooch-sdk/src/account/roochSessionAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const SCOPE_MODULE_ADDRESSS = 0
const SCOPE_MODULE_NAMES = 1
const SCOPE_FUNCTION_NAMES = 2

const requiredScope = '0x3::session_key::remove_session_key_entry'

export class RoochSessionAccount implements IAccount {
protected readonly client: RoochClient
protected readonly scopes: string[]
Expand All @@ -43,6 +45,11 @@ export class RoochSessionAccount implements IAccount {
this.localCreateSessionTime = Date.now() / 1000
this.sessionAccount = new RoochAccount(this.client)
this.authInfo = authInfo

// session must have the right to delete itself
if (!this.scopes.find((item) => item === '0x3::*::*' || item === requiredScope)) {
this.scopes.push(requiredScope)
}
}

public static async CREATE(
Expand Down Expand Up @@ -173,7 +180,7 @@ export class RoochSessionAccount implements IAccount {
}

public async getSessionKey() {
const session = this.client.executeViewFunction({
return this.client.executeViewFunction({
funcId: '0x3::session_key::get_session_key',
tyArgs: [],
args: [
Expand All @@ -187,8 +194,6 @@ export class RoochSessionAccount implements IAccount {
},
],
})

return session
}

public async querySessionKeys(
Expand All @@ -209,7 +214,7 @@ export class RoochSessionAccount implements IAccount {
],
tyArgs: [],
address: this.getAddress(),
authorizer: this.account.getAuthorizer(),
authorizer: this.getAuthorizer(),
opts: opts,
})
}
Expand Down

0 comments on commit 93ea732

Please sign in to comment.